cert.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>TLS / SSL Certificates</h2>
  4. <p>Setup TLS cert for different domains of your reverse proxy server names</p>
  5. </div>
  6. <div class="ui divider"></div>
  7. <h4>Default Certificates</h4>
  8. <small>When there are no matching certificate for the requested server name, reverse proxy router will always fallback to this one.<br>Note that you need both of them uploaded for it to fallback properly</small></p>
  9. <table class="ui very basic unstackable celled table">
  10. <thead>
  11. <tr><th class="no-sort">Key Type</th>
  12. <th class="no-sort">Exists</th>
  13. </tr></thead>
  14. <tbody>
  15. <tr>
  16. <td><i class="globe icon"></i> Default Public Key</td>
  17. <td id="pubkeyExists"></td>
  18. </tr>
  19. <tr>
  20. <td><i class="lock icon"></i> Default Private Key</td>
  21. <td id="prikeyExists"></td>
  22. </tr>
  23. </tbody>
  24. </table>
  25. <p style="margin-bottom: 0.4em;"><i class="ui upload icon"></i> Upload Default Keypairs</p>
  26. <div class="ui buttons">
  27. <button class="ui basic grey button" onclick="uploadPublicKey();"><i class="globe icon"></i> Public Key</button>
  28. <button class="ui basic black button" onclick="uploadPrivateKey();"><i class="black lock icon"></i> Private Key</button>
  29. </div>
  30. <div class="ui divider"></div>
  31. <h4>Sub-domain Certificates</h4>
  32. <p>Provide certificates for multiple domains reverse proxy</p>
  33. <div class="ui fluid form">
  34. <div class="three fields">
  35. <div class="field">
  36. <label>Server Name (Domain)</label>
  37. <input type="text" id="certdomain" placeholder="example.com / blog.example.com">
  38. </div>
  39. <div class="field">
  40. <label>Public Key (.pem)</label>
  41. <input type="file" id="pubkeySelector" onchange="handleFileSelect(event, 'pub')">
  42. </div>
  43. <div class="field">
  44. <label>Private Key (.key)</label>
  45. <input type="file" id="prikeySelector" onchange="handleFileSelect(event, 'pri')">
  46. </div>
  47. </div>
  48. <button class="ui basic button" onclick="handleDomainUploadByKeypress();"><i class="ui teal upload icon"></i> Upload</button><br>
  49. <small>You have intermediate certificate? <a style="cursor:pointer;" onclick="showSideWrapper('snippet/intermediateCertConv.html');">Open Conversion Tool</a></small>
  50. </div>
  51. <div id="certUploadSuccMsg" class="ui green message" style="display:none;">
  52. <i class="ui checkmark icon"></i> Certificate for domain <span id="certUploadingDomain"></span> uploaded.
  53. </div>
  54. <br>
  55. <div>
  56. <table class="ui sortable unstackable celled table">
  57. <thead>
  58. <tr><th>Domain</th>
  59. <th>Last Update</th>
  60. <th>Expire At</th>
  61. <th class="no-sort">Remove</th>
  62. </tr></thead>
  63. <tbody id="certifiedDomainList">
  64. </tbody>
  65. </table>
  66. <button class="ui basic button" onclick="initManagedDomainCertificateList();"><i class="green refresh icon"></i> Refresh List</button>
  67. <button class="ui basic button" onclick="openACMEManager();"><i class="yellow refresh icon"></i> Auto Renew (ACME) Settings</button>
  68. </div>
  69. <div class="ui message">
  70. <h4><i class="info circle icon"></i> Sub-domain Certificates</h4>
  71. If you have 3rd or even 4th level subdomains like <code>blog.example.com</code> or <code>en.blog.example.com</code> ,
  72. depending on your certificates coverage, you might need to setup them one by one (i.e. having two seperate certificate for <code>a.example.com</code> and <code>b.example.com</code>).<br>
  73. If you have a wildcard certificate that covers <code>*.example.com</code>, you can just enter <code>example.com</code> as server name in the form below to add a certificate.
  74. </div>
  75. </div>
  76. <script>
  77. var uploadPendingPublicKey = undefined;
  78. var uploadPendingPrivateKey = undefined;
  79. //Delete the certificate by its domain
  80. function deleteCertificate(domain){
  81. if (confirm("Confirm delete certificate for " + domain + " ?")){
  82. $.ajax({
  83. url: "/api/cert/delete",
  84. method: "POST",
  85. data: {domain: domain},
  86. success: function(data){
  87. if (data.error != undefined){
  88. msgbox(data.error, false, 5000);
  89. }else{
  90. initManagedDomainCertificateList();
  91. initDefaultKeypairCheck();
  92. }
  93. }
  94. });
  95. }
  96. }
  97. //List the stored certificates
  98. function initManagedDomainCertificateList(){
  99. $.get("/api/cert/list?date=true", function(data){
  100. if (data.error != undefined){
  101. msgbox(data.error, false, 5000);
  102. }else{
  103. $("#certifiedDomainList").html("");
  104. data.sort((a,b) => {
  105. return a.Domain > b.Domain
  106. });
  107. data.forEach(entry => {
  108. $("#certifiedDomainList").append(`<tr>
  109. <td>${entry.Domain}</td>
  110. <td>${entry.LastModifiedDate}</td>
  111. <td>${entry.ExpireDate} (${entry.RemainingDays} days left)</td>
  112. <td><button title="Delete key-pair" class="ui mini basic red icon button" onclick="deleteCertificate('${entry.Domain}');"><i class="ui red trash icon"></i></button></td>
  113. </tr>`);
  114. });
  115. if (data.length == 0){
  116. $("#certifiedDomainList").append(`<tr>
  117. <td colspan="4"><i class="ui times circle icon"></i> No valid keypairs found</td>
  118. </tr>`);
  119. }
  120. }
  121. })
  122. }
  123. initManagedDomainCertificateList();
  124. function openACMEManager(){
  125. showSideWrapper('snippet/acme.html');
  126. }
  127. function handleDomainUploadByKeypress(){
  128. handleDomainKeysUpload(function(){
  129. $("#certUploadingDomain").text($("#certdomain").val().trim());
  130. //After uploaded, reset the file selector
  131. document.getElementById('pubkeySelector').value = '';
  132. document.getElementById('prikeySelector').value = '';
  133. document.getElementById('certdomain').value = '';
  134. uploadPendingPublicKey = undefined;
  135. uploadPendingPrivateKey = undefined;
  136. //Show succ
  137. $("#certUploadSuccMsg").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  138. initManagedDomainCertificateList();
  139. });
  140. }
  141. //Handle domain keys upload
  142. function handleDomainKeysUpload(callback=undefined){
  143. let domain = $("#certdomain").val();
  144. if (domain.trim() == ""){
  145. msgbox("Missing domain", false, 5000);
  146. return;
  147. }
  148. if (uploadPendingPublicKey && uploadPendingPrivateKey && typeof uploadPendingPublicKey === 'object' && typeof uploadPendingPrivateKey === 'object') {
  149. const publicKeyForm = new FormData();
  150. publicKeyForm.append('file', uploadPendingPublicKey, 'publicKey');
  151. const privateKeyForm = new FormData();
  152. privateKeyForm.append('file', uploadPendingPrivateKey, 'privateKey');
  153. const publicKeyRequest = new XMLHttpRequest();
  154. publicKeyRequest.open('POST', '/api/cert/upload?ktype=pub&domain=' + domain);
  155. publicKeyRequest.onreadystatechange = function() {
  156. if (publicKeyRequest.readyState === XMLHttpRequest.DONE) {
  157. if (publicKeyRequest.status !== 200) {
  158. msgbox('Error uploading public key: ' + publicKeyRequest.statusText, false, 5000);
  159. }
  160. if (callback != undefined){
  161. callback();
  162. }
  163. }
  164. };
  165. publicKeyRequest.send(publicKeyForm);
  166. const privateKeyRequest = new XMLHttpRequest();
  167. privateKeyRequest.open('POST', '/api/cert/upload?ktype=pri&domain=' + domain);
  168. privateKeyRequest.onreadystatechange = function() {
  169. if (privateKeyRequest.readyState === XMLHttpRequest.DONE) {
  170. if (privateKeyRequest.status !== 200) {
  171. msgbox('Error uploading private key: ' + privateKeyRequest.statusText, false, 5000);
  172. }
  173. if (callback != undefined){
  174. callback();
  175. }
  176. }
  177. };
  178. privateKeyRequest.send(privateKeyForm);
  179. } else {
  180. msgbox('One or both of the files is missing or not a file object');
  181. }
  182. }
  183. //Handlers for selecting domain based key pairs
  184. //ktype = {"pub" / "pri"}
  185. function handleFileSelect(event, ktype="pub") {
  186. const file = event.target.files[0];
  187. //const fileNameInput = document.getElementById('selected-file-name');
  188. if (ktype == "pub"){
  189. uploadPendingPublicKey = file;
  190. }else if (ktype == "pri"){
  191. uploadPendingPrivateKey = file;
  192. }
  193. //fileNameInput.value = file.name;
  194. }
  195. //Check if the default keypairs exists
  196. function initDefaultKeypairCheck(){
  197. $.get("/api/cert/checkDefault", function(data){
  198. let tick = `<i class="ui green checkmark icon"></i>`;
  199. let cross = `<i class="ui red times icon"></i>`;
  200. $("#pubkeyExists").html(data.DefaultPubExists?tick:cross);
  201. $("#prikeyExists").html(data.DefaultPriExists?tick:cross);
  202. });
  203. }
  204. initDefaultKeypairCheck();
  205. function uploadPrivateKey(){
  206. // create file input element
  207. const input = document.createElement('input');
  208. input.type = 'file';
  209. // add change listener to file input
  210. input.addEventListener('change', () => {
  211. // create form data object
  212. const formData = new FormData();
  213. // add selected file to form data
  214. formData.append('file', input.files[0]);
  215. // send form data to server
  216. fetch('/api/cert/upload?ktype=pri', {
  217. method: 'POST',
  218. body: formData
  219. })
  220. .then(response => {
  221. initDefaultKeypairCheck();
  222. if (response.ok) {
  223. msgbox('File upload successful!');
  224. } else {
  225. response.text().then(text => {
  226. msgbox(text, false, 5000);
  227. });
  228. //console.log(response.text());
  229. //alert('File upload failed!');
  230. }
  231. })
  232. .catch(error => {
  233. msgbox('An error occurred while uploading the file.', false, 5000);
  234. console.error(error);
  235. });
  236. });
  237. // click file input to open file selector
  238. input.click();
  239. }
  240. function uploadPublicKey() {
  241. // create file input element
  242. const input = document.createElement('input');
  243. input.type = 'file';
  244. // add change listener to file input
  245. input.addEventListener('change', () => {
  246. // create form data object
  247. const formData = new FormData();
  248. // add selected file to form data
  249. formData.append('file', input.files[0]);
  250. // send form data to server
  251. fetch('/api/cert/upload?ktype=pub', {
  252. method: 'POST',
  253. body: formData
  254. })
  255. .then(response => {
  256. if (response.ok) {
  257. msgbox('File upload successful!');
  258. initDefaultKeypairCheck();
  259. } else {
  260. response.text().then(text => {
  261. msgbox(text, false, 5000);
  262. });
  263. //console.log(response.text());
  264. //alert('File upload failed!');
  265. }
  266. })
  267. .catch(error => {
  268. msgbox('An error occurred while uploading the file.', false, 5000);
  269. console.error(error);
  270. });
  271. });
  272. // click file input to open file selector
  273. input.click();
  274. }
  275. </script>