acme.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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="Custom ACME Server">Custom ACME Server</div>
  110. <!-- <div class="item" data-value="Google">Google</div> -->
  111. </div>
  112. </div>
  113. </div>
  114. <div class="field" id="caInput" style="display:none;">
  115. <label>ACME Server URL</label>
  116. <input id="caURL" type="text" placeholder="https://example.com/acme/dictionary">
  117. </div>
  118. <div class="field" id="skipTLS" style="display:none;">
  119. <div class="ui checkbox">
  120. <input type="checkbox" id="skipTLSCheckbox">
  121. <label>Ignore TLS/SSL Verification Error<br><small>E.g. self-signed, expired certificate (Not Recommended)</small></label>
  122. </div>
  123. </div>
  124. <button id="obtainButton" class="ui basic button" type="submit"><i class="yellow refresh icon"></i> Get Certificate</button>
  125. </div>
  126. <div class="ui divider"></div>
  127. <small>First time setting up HTTPS?<br>Try out our <a href="../tools/https.html" target="_blank">wizard</a></small>
  128. <button class="ui basic button" style="float: right;" onclick="parent.hideSideWrapper();"><i class="remove icon"></i> Cancel</button>
  129. <br><br><br><br>
  130. </div>
  131. <script>
  132. let expiredDomains = [];
  133. let enableTrigerOnChangeEvent = true;
  134. $(".accordion").accordion();
  135. $(".dropdown").dropdown();
  136. $(".checkbox").checkbox();
  137. function setAutoRenewIfCASupportMode(useAutoMode = true){
  138. if (useAutoMode){
  139. $("#domainCertFileTable").addClass("disabled");
  140. $("#renewNowBtn").removeClass("disabled");
  141. $("#renewSelectedButton").addClass("disabled");
  142. }else{
  143. $("#domainCertFileTable").removeClass("disabled");
  144. $("#renewNowBtn").addClass("disabled");
  145. $("#renewSelectedButton").removeClass("disabled");
  146. }
  147. }
  148. function initRenewerConfigFromFile(){
  149. //Set the renew switch state
  150. $.get("/api/acme/autoRenew/enable", function(data){
  151. if (data == true){
  152. $("#enableCertAutoRenew").parent().checkbox("set checked");
  153. }
  154. $("#enableCertAutoRenew").on("change", function(){
  155. if (!enableTrigerOnChangeEvent){
  156. return;
  157. }
  158. toggleAutoRenew();
  159. })
  160. });
  161. //Load the email from server side
  162. $.get("/api/acme/autoRenew/email", function(data){
  163. if (data != "" && data != undefined && data != null){
  164. $("#caRegisterEmail").val(data);
  165. }
  166. });
  167. //Load the domain selection options
  168. $.get("/api/acme/autoRenew/renewPolicy", function(data){
  169. if (data == true){
  170. $("#renewAllSupported").parent().checkbox("set checked");
  171. }else{
  172. $("#renewAllSupported").parent().checkbox("set unchecked");
  173. }
  174. });
  175. }
  176. initRenewerConfigFromFile();
  177. function saveEmailToConfig(btn){
  178. $.ajax({
  179. url: "/api/acme/autoRenew/email",
  180. data: {set: $("#caRegisterEmail").val()},
  181. success: function(data){
  182. if (data.error != undefined){
  183. parent.msgbox(data.error, false, 5000);
  184. }else{
  185. parent.msgbox("Email updated");
  186. $(btn).html(`<i class="green check icon"></i>`);
  187. $(btn).addClass("disabled");
  188. setTimeout(function(){
  189. $(btn).html(`<i class="blue save icon"></i>`);
  190. $(btn).removeClass("disabled");
  191. }, 3000);
  192. }
  193. }
  194. });
  195. }
  196. function toggleAutoRenew(){
  197. var enabled = $("#enableCertAutoRenew").parent().checkbox("is checked");
  198. $.post("/api/acme/autoRenew/enable?enable=" + enabled, function(data){
  199. if (data.error){
  200. parent.msgbox(data.error, false, 5000);
  201. if (enabled){
  202. enableTrigerOnChangeEvent = false;
  203. $("#enableCertAutoRenew").parent().checkbox("set unchecked");
  204. enableTrigerOnChangeEvent = true;
  205. }
  206. if (parent && parent.setACMEEnableStates){
  207. parent.setACMEEnableStates(!enabled);
  208. }
  209. }else{
  210. $("#enableToggleSucc").stop().finish().fadeIn("fast").delay(3000).fadeOut("fast");
  211. if (parent && parent.setACMEEnableStates){
  212. parent.setACMEEnableStates(enabled);
  213. }
  214. }
  215. });
  216. }
  217. //Render the domains table that exists in this zoraxy host
  218. function renderDomainTable(domainFileList) {
  219. // Get the table body element
  220. var tableBody = $('#domainTableBody');
  221. // Clear the table body
  222. tableBody.empty();
  223. // Iterate over the domain names
  224. var counter = 0;
  225. for (const [srcfile, domains] of Object.entries(domainFileList)) {
  226. // Create a table row
  227. var row = $('<tr>');
  228. // Create the domain name cell
  229. var domainClass = "validDomain";
  230. for (var i = 0; i < domains.length; i++){
  231. let thisDomain = domains[i];
  232. if (expiredDomains.includes(thisDomain)){
  233. domainClass = "expiredDomain";
  234. }
  235. }
  236. var domainCell = $('<td class="' + domainClass +'">').html(domains.join("<br>"));
  237. row.append(domainCell);
  238. var srcFileCell = $('<td>').text(srcfile);
  239. row.append(srcFileCell);
  240. // Create the auto-renew checkbox cell
  241. let domainsEncoded = encodeURIComponent(JSON.stringify(domains));
  242. var checkboxCell = $(`<td domain="${domainsEncoded}" srcfile="${srcfile}">`);
  243. var checkbox = $(`<input name="${srcfile}">`).attr('type', 'checkbox');
  244. checkboxCell.append(checkbox);
  245. row.append(checkboxCell);
  246. // Add the row to the table body
  247. tableBody.append(row);
  248. counter++;
  249. }
  250. if (Object.keys(domainFileList).length == 0){
  251. //No certificate in this system
  252. tableBody.append(`<tr>
  253. <td colspan="3"><i class="ui green circle check icon"></i> No certificate in use</td>
  254. </tr>`);
  255. }
  256. }
  257. //Initiate domain table. If you needs to update the expired domain as well
  258. //call from initDomainFileList() instead
  259. function initDomainTable(){
  260. $.get("/api/cert/listdomains?compact=true", function(data){
  261. if (data.error != undefined){
  262. parent.msgbox(data.error, false);
  263. }else{
  264. renderDomainTable(data);
  265. }
  266. initAutoRenewPolicy();
  267. })
  268. }
  269. function initDomainFileList() {
  270. $.ajax({
  271. url: "/api/acme/listExpiredDomains",
  272. method: "GET",
  273. success: function(response) {
  274. // Render domain table
  275. expiredDomains = response.domain;
  276. initDomainTable();
  277. //renderDomainTable(response.domain);
  278. },
  279. error: function(error) {
  280. console.log("Failed to fetch expired domains:", error);
  281. }
  282. });
  283. }
  284. initDomainFileList();
  285. // Button click event handler for obtaining certificate
  286. $("#obtainButton").click(function() {
  287. $("#obtainButton").addClass("loading").addClass("disabled");
  288. obtainCertificate();
  289. });
  290. $("input[name=ca]").on('change', function() {
  291. if(this.value == "Custom ACME Server") {
  292. $("#caInput").show();
  293. $("#skipTLS").show();
  294. } else {
  295. $("#caInput").hide();
  296. $("#skipTLS").hide();
  297. }
  298. })
  299. // Obtain certificate from API
  300. function obtainCertificate() {
  301. var domains = $("#domainsInput").val();
  302. var filename = $("#filenameInput").val();
  303. var email = $("#caRegisterEmail").val();
  304. if (email == ""){
  305. parent.msgbox("ACME renew email is not set", false)
  306. $("#obtainButton").removeClass("loading").removeClass("disabled");
  307. return;
  308. }
  309. if (filename.trim() == "" && !domains.includes(",")){
  310. //Zoraxy filename are the matching name for domains.
  311. //Use the same as domains
  312. filename = domains;
  313. }else if (filename != "" && !domains.includes(",")){
  314. //Invalid settings. Force the filename to be same as domain
  315. //if there are only 1 domain
  316. filename = domains;
  317. }else if (filename == "" && domains.includes(",")){
  318. parent.msgbox("Filename cannot be empty for certs containing multiple domains.", false, 5000);
  319. $("#obtainButton").removeClass("loading").removeClass("disabled");
  320. return;
  321. }
  322. var ca = $("#ca").dropdown("get value");
  323. var caURL = "";
  324. if (ca == "Custom ACME Server") {
  325. ca = "custom";
  326. caURL = $("#caURL").val();
  327. }
  328. var skipTLSValue = $("#skipTLSCheckbox")[0].checked;
  329. $.ajax({
  330. url: "/api/acme/obtainCert",
  331. method: "GET",
  332. data: {
  333. domains: domains,
  334. filename: filename,
  335. email: email,
  336. ca: ca,
  337. caURL: caURL,
  338. skipTLS: skipTLSValue,
  339. },
  340. success: function(response) {
  341. $("#obtainButton").removeClass("loading").removeClass("disabled");
  342. if (response.error) {
  343. console.log("Error:", response.error);
  344. // Show error message
  345. parent.msgbox(response.error, false, 12000);
  346. } else {
  347. console.log("Certificate renewed successfully");
  348. // Show success message
  349. parent.msgbox("Certificate renewed successfully");
  350. // Renew the parent certificate list
  351. parent.initManagedDomainCertificateList();
  352. }
  353. },
  354. error: function(error) {
  355. $("#obtainButton").removeClass("loading").removeClass("disabled");
  356. console.log("Failed to renewed certificate:", error);
  357. }
  358. });
  359. }
  360. function checkIfInputDomainIsMultiple(){
  361. var inputDomains = $("#domainsInput").val();
  362. if (inputDomains.includes(",")){
  363. $(".multiDomainOnly").show();
  364. }else{
  365. $(".multiDomainOnly").hide();
  366. }
  367. }
  368. //Grab the longest common suffix of all domains
  369. //not that smart technically
  370. function autoDetectMatchingRules(){
  371. var domainsString = $("#domainsInput").val();
  372. if (!domainsString.includes(",")){
  373. return domainsString;
  374. }
  375. let domains = domainsString.split(",");
  376. //Clean out any spacing between commas
  377. for (var i = 0; i < domains.length; i++){
  378. domains[i] = domains[i].trim();
  379. }
  380. function getLongestCommonSuffix(strings) {
  381. if (strings.length === 0) {
  382. return ''; // Return an empty string if the array is empty
  383. }
  384. var sortedStrings = strings.slice().sort(); // Create a sorted copy of the array
  385. var firstString = sortedStrings[0];
  386. var lastString = sortedStrings[sortedStrings.length - 1];
  387. var suffix = '';
  388. var minLength = Math.min(firstString.length, lastString.length);
  389. for (var i = 0; i < minLength; i++) {
  390. if (firstString[firstString.length - 1 - i] !== lastString[lastString.length - 1 - i]) {
  391. break; // Stop iterating if characters don't match
  392. }
  393. suffix = firstString[firstString.length - 1 - i] + suffix;
  394. }
  395. return suffix;
  396. }
  397. let longestSuffix = getLongestCommonSuffix(domains);
  398. //Check if the suffix is a valid domain
  399. if (longestSuffix.substr(0,1) == "."){
  400. //Trim off the first dot
  401. longestSuffix = longestSuffix.substr(1);
  402. }
  403. if (!longestSuffix.includes(".")){
  404. parent.msgbox("Auto Detect failed: Multiple Domains", false, 5000);
  405. return;
  406. }
  407. $("#filenameInput").val(longestSuffix);
  408. }
  409. //Handle the renew now btn click
  410. function renewNow(){
  411. $.get("/api/acme/autoRenew/renewNow", function(data){
  412. if (data.error != undefined){
  413. parent.msgbox(data.error, false, 6000);
  414. }else{
  415. parent.msgbox(data)
  416. }
  417. })
  418. }
  419. function initAutoRenewPolicy(){
  420. $.get("/api/acme/autoRenew/listDomains", function(data){
  421. if (data.error != undefined){
  422. parent.msgbox(data.error, false)
  423. }else{
  424. if (data[0] == "*"){
  425. //Auto select and renew is enabled
  426. $("#renewAllSupported").parent().checkbox("set checked");
  427. }else{
  428. //This is a list of domain files
  429. data.forEach(function(name) {
  430. $('#domainTableBody input[type="checkbox"][name="' + name + '"]').prop('checked', true);
  431. });
  432. $("#domainCertFileTable").removeClass("disabled");
  433. $("#renewNowBtn").addClass("disabled");
  434. $("#renewSelectedButton").removeClass("disabled");
  435. }
  436. }
  437. })
  438. }
  439. function saveAutoRenewPolicy(){
  440. let autoRenewAll = $("#renewAllSupported").parent().checkbox("is checked");
  441. if (autoRenewAll == true){
  442. $.ajax({
  443. url: "/api/acme/autoRenew/setDomains",
  444. data: {opr: "setAuto"},
  445. success: function(data){
  446. parent.msgbox("Renew policy rule updated")
  447. }
  448. });
  449. }else{
  450. let checkedNames = [];
  451. $('#domainTableBody input[type="checkbox"]:checked').each(function() {
  452. checkedNames.push($(this).attr('name'));
  453. });
  454. $.ajax({
  455. url: "/api/acme/autoRenew/setDomains",
  456. data: {opr: "setSelected", domains: JSON.stringify(checkedNames)},
  457. success: function(data){
  458. parent.msgbox("Renew policy rule updated")
  459. }
  460. });
  461. }
  462. }
  463. //Clear up the input field when page load
  464. $("#filenameInput").val("");
  465. </script>
  466. </body>
  467. </html>