cert.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. </div>
  77. <div class="ui message">
  78. <h4><i class="info circle icon"></i> Sub-domain Certificates</h4>
  79. If you have 3rd or even 4th level subdomains like <code>blog.example.com</code> or <code>en.blog.example.com</code> ,
  80. 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>
  81. 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.
  82. </div>
  83. <div class="ui divider"></div>
  84. <h4>Certificate Authority (CA) and Auto Renew (ACME)</h4>
  85. <p>Management features regarding CA and ACME</p>
  86. <p>The default CA to use when create a new subdomain proxy endpoint with TLS certificate</p>
  87. <div class="ui fluid form">
  88. <div class="field">
  89. <label>Preferred CA</label>
  90. <div class="ui selection dropdown" id="defaultCA">
  91. <input type="hidden" name="defaultCA">
  92. <i class="dropdown icon"></i>
  93. <div class="default text">Let's Encrypt</div>
  94. <div class="menu">
  95. <div class="item" data-value="Let's Encrypt">Let's Encrypt</div>
  96. <div class="item" data-value="Buypass">Buypass</div>
  97. <div class="item" data-value="ZeroSSL">ZeroSSL</div>
  98. </div>
  99. </div>
  100. </div>
  101. <div class="field">
  102. <label>ACME Email</label>
  103. <input id="prefACMEEmail" type="text" placeholder="ACME Email">
  104. </div>
  105. <button class="ui basic icon button" onclick="saveDefaultCA();"><i class="ui blue save icon"></i> Save Settings</button>
  106. </div><br>
  107. <h5>Certificate Renew / Generation (ACME) Settings</h5>
  108. <div class="ui basic segment">
  109. <h4 class="ui header" id="acmeAutoRenewer">
  110. <i class="red circle icon"></i>
  111. <div class="content">
  112. <span id="acmeAutoRenewerStatus">Disabled</span>
  113. <div class="sub header">Auto-Renewer Status</div>
  114. </div>
  115. </h4>
  116. </div>
  117. <p>This tool provide you a graphical interface to setup auto certificate renew on your (sub)domains. You can also manually generate a certificate if one of your domain do not have certificate.</p>
  118. <button class="ui basic button" onclick="openACMEManager();"><i class="yellow external icon"></i> Open ACME Tool</button>
  119. </div>
  120. <script>
  121. var uploadPendingPublicKey = undefined;
  122. var uploadPendingPrivateKey = undefined;
  123. $("#defaultCA").dropdown();
  124. //Delete the certificate by its domain
  125. function deleteCertificate(domain){
  126. if (confirm("Confirm delete certificate for " + domain + " ?")){
  127. $.ajax({
  128. url: "/api/cert/delete",
  129. method: "POST",
  130. data: {domain: domain},
  131. success: function(data){
  132. if (data.error != undefined){
  133. msgbox(data.error, false, 5000);
  134. }else{
  135. initManagedDomainCertificateList();
  136. initDefaultKeypairCheck();
  137. }
  138. }
  139. });
  140. }
  141. }
  142. function initAcmeStatus(){
  143. //Initialize the current default CA options
  144. $.get("/api/acme/autoRenew/email", function(data){
  145. $("#prefACMEEmail").val(data);
  146. });
  147. $.get("/api/acme/autoRenew/ca", function(data){
  148. $("#defaultCA").dropdown("set selected", data);
  149. });
  150. $.get("/api/acme/autoRenew/enable", function(data){
  151. setACMEEnableStates(data);
  152. })
  153. }
  154. //Set the status of the acme enable icon
  155. function setACMEEnableStates(enabled){
  156. $("#acmeAutoRenewerStatus").text(enabled?"Enabled":"Disabled");
  157. $("#acmeAutoRenewer").find("i").attr("class", enabled?"green circle icon":"red circle icon");
  158. }
  159. initAcmeStatus();
  160. function saveDefaultCA(){
  161. let newDefaultEmail = $("#prefACMEEmail").val().trim();
  162. let newDefaultCA = $("#defaultCA").dropdown("get value");
  163. if (newDefaultEmail == ""){
  164. msgbox("Invalid acme email given", false);
  165. return;
  166. }
  167. $.post("/api/acme/autoRenew/email?set=" + newDefaultEmail, function(data){
  168. if (data.error != undefined){
  169. msgbox(data.error, false);
  170. }
  171. });
  172. $.post("/api/acme/autoRenew/ca?set=" + newDefaultCA, function(data){
  173. if (data.error != undefined){
  174. msgbox(data.error, false);
  175. }
  176. });
  177. msgbox("Settings updated");
  178. }
  179. //List the stored certificates
  180. function initManagedDomainCertificateList(){
  181. $.get("/api/cert/list?date=true", function(data){
  182. if (data.error != undefined){
  183. msgbox(data.error, false, 5000);
  184. }else{
  185. $("#certifiedDomainList").html("");
  186. data.sort((a,b) => {
  187. return a.Domain > b.Domain
  188. });
  189. data.forEach(entry => {
  190. let isExpired = entry.RemainingDays <= 0;
  191. $("#certifiedDomainList").append(`<tr>
  192. <td>${entry.Domain}</td>
  193. <td>${entry.LastModifiedDate}</td>
  194. <td class="${isExpired?"expired":"valid"} certdate">${entry.ExpireDate} (${!isExpired?entry.RemainingDays+" days left":"Expired"})</td>
  195. <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>
  196. </tr>`);
  197. });
  198. if (data.length == 0){
  199. $("#certifiedDomainList").append(`<tr>
  200. <td colspan="4"><i class="ui times circle icon"></i> No valid keypairs found</td>
  201. </tr>`);
  202. }
  203. }
  204. })
  205. }
  206. initManagedDomainCertificateList();
  207. function openACMEManager(){
  208. showSideWrapper('snippet/acme.html');
  209. }
  210. function handleDomainUploadByKeypress(){
  211. handleDomainKeysUpload(function(){
  212. $("#certUploadingDomain").text($("#certdomain").val().trim());
  213. //After uploaded, reset the file selector
  214. document.getElementById('pubkeySelector').value = '';
  215. document.getElementById('prikeySelector').value = '';
  216. document.getElementById('certdomain').value = '';
  217. uploadPendingPublicKey = undefined;
  218. uploadPendingPrivateKey = undefined;
  219. //Show succ
  220. $("#certUploadSuccMsg").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  221. initManagedDomainCertificateList();
  222. });
  223. }
  224. //Handle domain keys upload
  225. function handleDomainKeysUpload(callback=undefined){
  226. let domain = $("#certdomain").val();
  227. if (domain.trim() == ""){
  228. msgbox("Missing domain", false, 5000);
  229. return;
  230. }
  231. if (uploadPendingPublicKey && uploadPendingPrivateKey && typeof uploadPendingPublicKey === 'object' && typeof uploadPendingPrivateKey === 'object') {
  232. const publicKeyForm = new FormData();
  233. publicKeyForm.append('file', uploadPendingPublicKey, 'publicKey');
  234. const privateKeyForm = new FormData();
  235. privateKeyForm.append('file', uploadPendingPrivateKey, 'privateKey');
  236. const publicKeyRequest = new XMLHttpRequest();
  237. publicKeyRequest.open('POST', '/api/cert/upload?ktype=pub&domain=' + domain);
  238. publicKeyRequest.onreadystatechange = function() {
  239. if (publicKeyRequest.readyState === XMLHttpRequest.DONE) {
  240. if (publicKeyRequest.status !== 200) {
  241. msgbox('Error uploading public key: ' + publicKeyRequest.statusText, false, 5000);
  242. }
  243. if (callback != undefined){
  244. callback();
  245. }
  246. }
  247. };
  248. publicKeyRequest.send(publicKeyForm);
  249. const privateKeyRequest = new XMLHttpRequest();
  250. privateKeyRequest.open('POST', '/api/cert/upload?ktype=pri&domain=' + domain);
  251. privateKeyRequest.onreadystatechange = function() {
  252. if (privateKeyRequest.readyState === XMLHttpRequest.DONE) {
  253. if (privateKeyRequest.status !== 200) {
  254. msgbox('Error uploading private key: ' + privateKeyRequest.statusText, false, 5000);
  255. }
  256. if (callback != undefined){
  257. callback();
  258. }
  259. }
  260. };
  261. privateKeyRequest.send(privateKeyForm);
  262. } else {
  263. msgbox('One or both of the files is missing or not a file object');
  264. }
  265. }
  266. //Handlers for selecting domain based key pairs
  267. //ktype = {"pub" / "pri"}
  268. function handleFileSelect(event, ktype="pub") {
  269. const file = event.target.files[0];
  270. //const fileNameInput = document.getElementById('selected-file-name');
  271. if (ktype == "pub"){
  272. uploadPendingPublicKey = file;
  273. }else if (ktype == "pri"){
  274. uploadPendingPrivateKey = file;
  275. }
  276. //fileNameInput.value = file.name;
  277. }
  278. //Check if the default keypairs exists
  279. function initDefaultKeypairCheck(){
  280. $.get("/api/cert/checkDefault", function(data){
  281. let tick = `<i class="ui green checkmark icon"></i>`;
  282. let cross = `<i class="ui red times icon"></i>`;
  283. $("#pubkeyExists").html(data.DefaultPubExists?tick:cross);
  284. $("#prikeyExists").html(data.DefaultPriExists?tick:cross);
  285. });
  286. }
  287. initDefaultKeypairCheck();
  288. function uploadPrivateKey(){
  289. // create file input element
  290. const input = document.createElement('input');
  291. input.type = 'file';
  292. // add change listener to file input
  293. input.addEventListener('change', () => {
  294. // create form data object
  295. const formData = new FormData();
  296. // add selected file to form data
  297. formData.append('file', input.files[0]);
  298. // send form data to server
  299. fetch('/api/cert/upload?ktype=pri', {
  300. method: 'POST',
  301. body: formData
  302. })
  303. .then(response => {
  304. initDefaultKeypairCheck();
  305. if (response.ok) {
  306. msgbox('File upload successful!');
  307. } else {
  308. response.text().then(text => {
  309. msgbox(text, false, 5000);
  310. });
  311. //console.log(response.text());
  312. //alert('File upload failed!');
  313. }
  314. })
  315. .catch(error => {
  316. msgbox('An error occurred while uploading the file.', false, 5000);
  317. console.error(error);
  318. });
  319. });
  320. // click file input to open file selector
  321. input.click();
  322. }
  323. function uploadPublicKey() {
  324. // create file input element
  325. const input = document.createElement('input');
  326. input.type = 'file';
  327. // add change listener to file input
  328. input.addEventListener('change', () => {
  329. // create form data object
  330. const formData = new FormData();
  331. // add selected file to form data
  332. formData.append('file', input.files[0]);
  333. // send form data to server
  334. fetch('/api/cert/upload?ktype=pub', {
  335. method: 'POST',
  336. body: formData
  337. })
  338. .then(response => {
  339. if (response.ok) {
  340. msgbox('File upload successful!');
  341. initDefaultKeypairCheck();
  342. } else {
  343. response.text().then(text => {
  344. msgbox(text, false, 5000);
  345. });
  346. //console.log(response.text());
  347. //alert('File upload failed!');
  348. }
  349. })
  350. .catch(error => {
  351. msgbox('An error occurred while uploading the file.', false, 5000);
  352. console.error(error);
  353. });
  354. });
  355. // click file input to open file selector
  356. input.click();
  357. }
  358. </script>