acme.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Notes: This should be open in its original path-->
  5. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  6. <script src="../script/jquery-3.6.0.min.js"></script>
  7. <script src="../script/semantic/semantic.min.js"></script>
  8. <style>
  9. .disabled.table{
  10. opacity: 0.5;
  11. pointer-events: none;
  12. }
  13. .expiredDomain{
  14. color: rgb(238, 31, 31);
  15. }
  16. .validDomain{
  17. color: rgb(49, 192, 113);
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <br>
  23. <div class="ui container">
  24. <div class="ui header">
  25. <div class="content">
  26. Certificates Auto Renew Settings
  27. <div class="sub header">Fetch and renew your certificates with Automated Certificate Management Environment (ACME) protocol</div>
  28. </div>
  29. </div>
  30. <div class="ui basic segment">
  31. <p style="float: right; color: #21ba45; display:none;" id="enableToggleSucc"><i class="green checkmark icon"></i> Setting Updated</p>
  32. <div class="ui toggle checkbox">
  33. <input type="checkbox" id="enableCertAutoRenew">
  34. <label>Enable Certificate Auto Renew</label>
  35. </div>
  36. <br>
  37. <h3>ACME Email</h3>
  38. <p>Email is required by many CAs for renewing via ACME protocol</p>
  39. <div class="ui fluid action input">
  40. <input id="caRegisterEmail" type="text" placeholder="[email protected]">
  41. <button class="ui icon basic button" onclick="saveEmailToConfig(this);">
  42. <i class="blue save icon"></i>
  43. </button>
  44. </div>
  45. <small>If you don't want to share your private email address, you can also fill in an email address that point to a mailbox not exists on your domain.</small>
  46. </div>
  47. <div class="ui basic segment" style="background-color: #f7f7f7; border-radius: 1em;">
  48. <div class="ui accordion advanceSettings">
  49. <div class="title">
  50. <i class="dropdown icon"></i>
  51. Advance Renew Policy
  52. </div>
  53. <div class="content">
  54. <p>Renew all certificates with ACME supported CAs</p>
  55. <div class="ui toggle checkbox">
  56. <input type="checkbox" id="renewAllSupported" onchange="setAutoRenewIfCASupportMode(this.checked);">
  57. <label>Renew All Certs</label>
  58. </div><br>
  59. <button id="renewNowBtn" onclick="renewNow();" class="ui basic right floated button" style="margin-top: -2em;"><i class="yellow refresh icon"></i> Renew Now</button>
  60. <div class="ui horizontal divider"> OR </div>
  61. <p>Select the certificates to automatic renew in the list below</p>
  62. <table id="domainCertFileTable" class="ui very compact unstackable basic disabled table">
  63. <thead>
  64. <tr>
  65. <th>Domain Name</th>
  66. <th>Match Rule</th>
  67. <th>Auto-Renew</th>
  68. </tr>
  69. </thead>
  70. <tbody id="domainTableBody"></tbody>
  71. </table>
  72. <small><i class="ui red info circle icon"></i> Domain in red are expired</small><br>
  73. <div class="ui yellow message">
  74. Certificate Renew only works on the certification authority (CA) supported by Zoraxy. Check Zoraxy wiki for more information on supported list of CAs.
  75. </div>
  76. <button class="ui basic right floated button" onclick="saveAutoRenewPolicy();"><i class="blue save icon"></i> Save Changes</button>
  77. <button id="renewSelectedButton" onclick="renewNow();" class="ui basic right floated disabled button"><i class="yellow refresh icon"></i> Renew Selected</button>
  78. <br><br>
  79. </div>
  80. </div>
  81. </div>
  82. <div class="ui divider"></div>
  83. <h3>Generate New Certificate</h3>
  84. <p>Enter a new / existing domain(s) to request new certificate(s)</p>
  85. <div class="ui form">
  86. <div class="field">
  87. <label>Domain(s)</label>
  88. <input id="domainsInput" type="text" placeholder="example.com" onkeyup="checkIfInputDomainIsMultiple();">
  89. <small>If you have more than one domain in a single certificate, enter the domains separated by commas (e.g. s1.dev.example.com,s2.dev.example.com)</small>
  90. </div>
  91. <div class="field multiDomainOnly" style="display:none;">
  92. <label>Matching Rule</label>
  93. <input id="filenameInput" type="text" placeholder="Enter filename (no file extension)">
  94. <small>Matching rule to let Zoraxy pick which certificate to use (Also be used as filename). Usually is the longest common suffix of the entered addresses. (e.g. dev.example.com)</small>
  95. </div>
  96. <div class="field multiDomainOnly" style="display:none;">
  97. <button class="ui basic fluid button" onclick="autoDetectMatchingRules();">Auto Detect Matching Rule</button>
  98. </div>
  99. <div class="field">
  100. <label>Certificate Authority (CA)</label>
  101. <div class="ui selection dropdown" id="ca">
  102. <input type="hidden" name="ca">
  103. <i class="dropdown icon"></i>
  104. <div class="default text">Let's Encrypt</div>
  105. <div class="menu">
  106. <div class="item" data-value="Let's Encrypt">Let's Encrypt</div>
  107. <div class="item" data-value="Buypass">Buypass</div>
  108. <div class="item" data-value="ZeroSSL">ZeroSSL</div>
  109. <!-- <div class="item" data-value="Google">Google</div> -->
  110. </div>
  111. </div>
  112. </div>
  113. <button id="obtainButton" class="ui basic button" type="submit"><i class="yellow refresh icon"></i> Renew Certificate</button>
  114. </div>
  115. <div class="ui divider"></div>
  116. <small>First time setting up HTTPS?<br>Try out our <a href="../tools/https.html" target="_blank">wizard</a></small>
  117. <button class="ui basic button" style="float: right;" onclick="parent.hideSideWrapper();"><i class="remove icon"></i> Cancel</button>
  118. <br><br><br><br>
  119. </div>
  120. <script>
  121. let expiredDomains = [];
  122. let enableTrigerOnChangeEvent = true;
  123. $(".accordion").accordion();
  124. $(".dropdown").dropdown();
  125. function setAutoRenewIfCASupportMode(useAutoMode = true){
  126. if (useAutoMode){
  127. $("#domainCertFileTable").addClass("disabled");
  128. $("#renewNowBtn").removeClass("disabled");
  129. $("#renewSelectedButton").addClass("disabled");
  130. }else{
  131. $("#domainCertFileTable").removeClass("disabled");
  132. $("#renewNowBtn").addClass("disabled");
  133. $("#renewSelectedButton").removeClass("disabled");
  134. }
  135. }
  136. function initRenewerConfigFromFile(){
  137. //Set the renew switch state
  138. $.get("/api/acme/autoRenew/enable", function(data){
  139. if (data == true){
  140. $("#enableCertAutoRenew").parent().checkbox("set checked");
  141. }
  142. $("#enableCertAutoRenew").on("change", function(){
  143. if (!enableTrigerOnChangeEvent){
  144. return;
  145. }
  146. toggleAutoRenew();
  147. })
  148. });
  149. //Load the email from server side
  150. $.get("/api/acme/autoRenew/email", function(data){
  151. if (data != "" && data != undefined && data != null){
  152. $("#caRegisterEmail").val(data);
  153. }
  154. });
  155. //Load the domain selection options
  156. $.get("/api/acme/autoRenew/renewPolicy", function(data){
  157. if (data == true){
  158. $("#renewAllSupported").parent().checkbox("set checked");
  159. }else{
  160. $("#renewAllSupported").parent().checkbox("set unchecked");
  161. }
  162. });
  163. }
  164. initRenewerConfigFromFile();
  165. function saveEmailToConfig(btn){
  166. $.ajax({
  167. url: "/api/acme/autoRenew/email",
  168. data: {set: $("#caRegisterEmail").val()},
  169. success: function(data){
  170. if (data.error != undefined){
  171. parent.msgbox(data.error, false, 5000);
  172. }else{
  173. parent.msgbox("Email updated");
  174. $(btn).html(`<i class="green check icon"></i>`);
  175. $(btn).addClass("disabled");
  176. setTimeout(function(){
  177. $(btn).html(`<i class="blue save icon"></i>`);
  178. $(btn).removeClass("disabled");
  179. }, 3000);
  180. }
  181. }
  182. });
  183. }
  184. function toggleAutoRenew(){
  185. var enabled = $("#enableCertAutoRenew").parent().checkbox("is checked");
  186. $.post("/api/acme/autoRenew/enable?enable=" + enabled, function(data){
  187. if (data.error){
  188. parent.msgbox(data.error, false, 5000);
  189. if (enabled){
  190. enableTrigerOnChangeEvent = false;
  191. $("#enableCertAutoRenew").parent().checkbox("set unchecked");
  192. enableTrigerOnChangeEvent = true;
  193. }
  194. }else{
  195. $("#enableToggleSucc").stop().finish().fadeIn("fast").delay(3000).fadeOut("fast");
  196. }
  197. });
  198. }
  199. //Render the domains table that exists in this zoraxy host
  200. function renderDomainTable(domainFileList) {
  201. // Get the table body element
  202. var tableBody = $('#domainTableBody');
  203. // Clear the table body
  204. tableBody.empty();
  205. // Iterate over the domain names
  206. var counter = 0;
  207. for (const [srcfile, domains] of Object.entries(domainFileList)) {
  208. // Create a table row
  209. var row = $('<tr>');
  210. // Create the domain name cell
  211. var domainClass = "validDomain";
  212. for (var i = 0; i < domains.length; i++){
  213. let thisDomain = domains[i];
  214. if (expiredDomains.includes(thisDomain)){
  215. domainClass = "expiredDomain";
  216. }
  217. }
  218. var domainCell = $('<td class="' + domainClass +'">').html(domains.join("<br>"));
  219. row.append(domainCell);
  220. var srcFileCell = $('<td>').text(srcfile);
  221. row.append(srcFileCell);
  222. // Create the auto-renew checkbox cell
  223. let domainsEncoded = encodeURIComponent(JSON.stringify(domains));
  224. var checkboxCell = $(`<td domain="${domainsEncoded}" srcfile="${srcfile}">`);
  225. var checkbox = $(`<input name="${srcfile}">`).attr('type', 'checkbox');
  226. checkboxCell.append(checkbox);
  227. row.append(checkboxCell);
  228. // Add the row to the table body
  229. tableBody.append(row);
  230. counter++;
  231. }
  232. if (Object.keys(domainFileList).length == 0){
  233. //No certificate in this system
  234. tableBody.append(`<tr>
  235. <td colspan="3"><i class="ui green circle check icon"></i> No certificate in use</td>
  236. </tr>`);
  237. }
  238. }
  239. //Initiate domain table. If you needs to update the expired domain as well
  240. //call from initDomainFileList() instead
  241. function initDomainTable(){
  242. $.get("/api/cert/listdomains?compact=true", function(data){
  243. if (data.error != undefined){
  244. parent.msgbox(data.error, false);
  245. }else{
  246. renderDomainTable(data);
  247. }
  248. initAutoRenewPolicy();
  249. })
  250. }
  251. function initDomainFileList() {
  252. $.ajax({
  253. url: "/api/acme/listExpiredDomains",
  254. method: "GET",
  255. success: function(response) {
  256. // Render domain table
  257. expiredDomains = response.domain;
  258. initDomainTable();
  259. //renderDomainTable(response.domain);
  260. },
  261. error: function(error) {
  262. console.log("Failed to fetch expired domains:", error);
  263. }
  264. });
  265. }
  266. initDomainFileList();
  267. // Button click event handler for obtaining certificate
  268. $("#obtainButton").click(function() {
  269. $("#obtainButton").addClass("loading").addClass("disabled");
  270. obtainCertificate();
  271. });
  272. // Obtain certificate from API
  273. function obtainCertificate() {
  274. var domains = $("#domainsInput").val();
  275. var filename = $("#filenameInput").val();
  276. var email = $("#caRegisterEmail").val();
  277. if (email == ""){
  278. parent.msgbox("ACME renew email is not set")
  279. return;
  280. }
  281. if (filename.trim() == "" && !domains.includes(",")){
  282. //Zoraxy filename are the matching name for domains.
  283. //Use the same as domains
  284. filename = domains;
  285. }else if (filename != "" && !domains.includes(",")){
  286. //Invalid settings. Force the filename to be same as domain
  287. //if there are only 1 domain
  288. filename = domains;
  289. }else{
  290. parent.msgbox("Filename cannot be empty for certs containing multiple domains.")
  291. return;
  292. }
  293. var ca = $("#ca").dropdown("get value");
  294. $.ajax({
  295. url: "/api/acme/obtainCert",
  296. method: "GET",
  297. data: {
  298. domains: domains,
  299. filename: filename,
  300. email: email,
  301. ca: ca,
  302. },
  303. success: function(response) {
  304. $("#obtainButton").removeClass("loading").removeClass("disabled");
  305. if (response.error) {
  306. console.log("Error:", response.error);
  307. // Show error message
  308. parent.msgbox(response.error, false, 12000);
  309. } else {
  310. console.log("Certificate renewed successfully");
  311. // Show success message
  312. parent.msgbox("Certificate renewed successfully");
  313. // Renew the parent certificate list
  314. parent.initManagedDomainCertificateList();
  315. }
  316. },
  317. error: function(error) {
  318. $("#obtainButton").removeClass("loading").removeClass("disabled");
  319. console.log("Failed to renewed certificate:", error);
  320. }
  321. });
  322. }
  323. function checkIfInputDomainIsMultiple(){
  324. var inputDomains = $("#domainsInput").val();
  325. if (inputDomains.includes(",")){
  326. $(".multiDomainOnly").show();
  327. }else{
  328. $(".multiDomainOnly").hide();
  329. }
  330. }
  331. //Grab the longest common suffix of all domains
  332. //not that smart technically
  333. function autoDetectMatchingRules(){
  334. var domainsString = $("#domainsInput").val();
  335. if (!domainsString.includes(",")){
  336. return domainsString;
  337. }
  338. let domains = domainsString.split(",");
  339. //Clean out any spacing between commas
  340. for (var i = 0; i < domains.length; i++){
  341. domains[i] = domains[i].trim();
  342. }
  343. function getLongestCommonSuffix(strings) {
  344. if (strings.length === 0) {
  345. return ''; // Return an empty string if the array is empty
  346. }
  347. var sortedStrings = strings.slice().sort(); // Create a sorted copy of the array
  348. var firstString = sortedStrings[0];
  349. var lastString = sortedStrings[sortedStrings.length - 1];
  350. var suffix = '';
  351. var minLength = Math.min(firstString.length, lastString.length);
  352. for (var i = 0; i < minLength; i++) {
  353. if (firstString[firstString.length - 1 - i] !== lastString[lastString.length - 1 - i]) {
  354. break; // Stop iterating if characters don't match
  355. }
  356. suffix = firstString[firstString.length - 1 - i] + suffix;
  357. }
  358. return suffix;
  359. }
  360. let longestSuffix = getLongestCommonSuffix(domains);
  361. //Check if the suffix is a valid domain
  362. if (longestSuffix.substr(0,1) == "."){
  363. //Trim off the first dot
  364. longestSuffix = longestSuffix.substr(1);
  365. }
  366. if (!longestSuffix.includes(".")){
  367. parent.msgbox("Auto Detect failed: Multiple Domains", false, 5000);
  368. return;
  369. }
  370. $("#filenameInput").val(longestSuffix);
  371. }
  372. //Handle the renew now btn click
  373. function renewNow(){
  374. $.get("/api/acme/autoRenew/renewNow", function(data){
  375. if (data.error != undefined){
  376. parent.msgbox(data.error, false, 6000);
  377. }else{
  378. parent.msgbox(data)
  379. }
  380. })
  381. }
  382. function initAutoRenewPolicy(){
  383. $.get("/api/acme/autoRenew/listDomains", function(data){
  384. if (data.error != undefined){
  385. parent.msgbox(data.error, false)
  386. }else{
  387. if (data[0] == "*"){
  388. //Auto select and renew is enabled
  389. $("#renewAllSupported").parent().checkbox("set checked");
  390. }else{
  391. //This is a list of domain files
  392. data.forEach(function(name) {
  393. $('#domainTableBody input[type="checkbox"][name="' + name + '"]').prop('checked', true);
  394. });
  395. $("#domainCertFileTable").removeClass("disabled");
  396. $("#renewNowBtn").addClass("disabled");
  397. $("#renewSelectedButton").removeClass("disabled");
  398. }
  399. }
  400. })
  401. }
  402. function saveAutoRenewPolicy(){
  403. let autoRenewAll = $("#renewAllSupported").parent().checkbox("is checked");
  404. if (autoRenewAll == true){
  405. $.ajax({
  406. url: "/api/acme/autoRenew/setDomains",
  407. data: {opr: "setAuto"},
  408. success: function(data){
  409. parent.msgbox("Renew policy rule updated")
  410. }
  411. });
  412. }else{
  413. let checkedNames = [];
  414. $('#domainTableBody input[type="checkbox"]:checked').each(function() {
  415. checkedNames.push($(this).attr('name'));
  416. });
  417. $.ajax({
  418. url: "/api/acme/autoRenew/setDomains",
  419. data: {opr: "setSelected", domains: JSON.stringify(checkedNames)},
  420. success: function(data){
  421. parent.msgbox("Renew policy rule updated")
  422. }
  423. });
  424. }
  425. }
  426. //Clear up the input field when page load
  427. $("#filenameInput").val("");
  428. </script>
  429. </body>
  430. </html>