cert.html 13 KB

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