acme.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. }else{
  207. $("#enableToggleSucc").stop().finish().fadeIn("fast").delay(3000).fadeOut("fast");
  208. }
  209. });
  210. }
  211. //Render the domains table that exists in this zoraxy host
  212. function renderDomainTable(domainFileList) {
  213. // Get the table body element
  214. var tableBody = $('#domainTableBody');
  215. // Clear the table body
  216. tableBody.empty();
  217. // Iterate over the domain names
  218. var counter = 0;
  219. for (const [srcfile, domains] of Object.entries(domainFileList)) {
  220. // Create a table row
  221. var row = $('<tr>');
  222. // Create the domain name cell
  223. var domainClass = "validDomain";
  224. for (var i = 0; i < domains.length; i++){
  225. let thisDomain = domains[i];
  226. if (expiredDomains.includes(thisDomain)){
  227. domainClass = "expiredDomain";
  228. }
  229. }
  230. var domainCell = $('<td class="' + domainClass +'">').html(domains.join("<br>"));
  231. row.append(domainCell);
  232. var srcFileCell = $('<td>').text(srcfile);
  233. row.append(srcFileCell);
  234. // Create the auto-renew checkbox cell
  235. let domainsEncoded = encodeURIComponent(JSON.stringify(domains));
  236. var checkboxCell = $(`<td domain="${domainsEncoded}" srcfile="${srcfile}">`);
  237. var checkbox = $(`<input name="${srcfile}">`).attr('type', 'checkbox');
  238. checkboxCell.append(checkbox);
  239. row.append(checkboxCell);
  240. // Add the row to the table body
  241. tableBody.append(row);
  242. counter++;
  243. }
  244. if (Object.keys(domainFileList).length == 0){
  245. //No certificate in this system
  246. tableBody.append(`<tr>
  247. <td colspan="3"><i class="ui green circle check icon"></i> No certificate in use</td>
  248. </tr>`);
  249. }
  250. }
  251. //Initiate domain table. If you needs to update the expired domain as well
  252. //call from initDomainFileList() instead
  253. function initDomainTable(){
  254. $.get("/api/cert/listdomains?compact=true", function(data){
  255. if (data.error != undefined){
  256. parent.msgbox(data.error, false);
  257. }else{
  258. renderDomainTable(data);
  259. }
  260. initAutoRenewPolicy();
  261. })
  262. }
  263. function initDomainFileList() {
  264. $.ajax({
  265. url: "/api/acme/listExpiredDomains",
  266. method: "GET",
  267. success: function(response) {
  268. // Render domain table
  269. expiredDomains = response.domain;
  270. initDomainTable();
  271. //renderDomainTable(response.domain);
  272. },
  273. error: function(error) {
  274. console.log("Failed to fetch expired domains:", error);
  275. }
  276. });
  277. }
  278. initDomainFileList();
  279. // Button click event handler for obtaining certificate
  280. $("#obtainButton").click(function() {
  281. $("#obtainButton").addClass("loading").addClass("disabled");
  282. obtainCertificate();
  283. });
  284. $("input[name=ca]").on('change', function() {
  285. if(this.value == "Custom ACME Server") {
  286. $("#caInput").show();
  287. $("#skipTLS").show();
  288. } else {
  289. $("#caInput").hide();
  290. $("#skipTLS").hide();
  291. }
  292. })
  293. // Obtain certificate from API
  294. function obtainCertificate() {
  295. var domains = $("#domainsInput").val();
  296. var filename = $("#filenameInput").val();
  297. var email = $("#caRegisterEmail").val();
  298. if (email == ""){
  299. parent.msgbox("ACME renew email is not set", false)
  300. $("#obtainButton").removeClass("loading").removeClass("disabled");
  301. return;
  302. }
  303. if (filename.trim() == "" && !domains.includes(",")){
  304. //Zoraxy filename are the matching name for domains.
  305. //Use the same as domains
  306. filename = domains;
  307. }else if (filename != "" && !domains.includes(",")){
  308. //Invalid settings. Force the filename to be same as domain
  309. //if there are only 1 domain
  310. filename = domains;
  311. }else{
  312. parent.msgbox("Filename cannot be empty for certs containing multiple domains.")
  313. return;
  314. }
  315. var ca = $("#ca").dropdown("get value");
  316. var caURL = "";
  317. if (ca == "Custom ACME Server") {
  318. ca = "custom";
  319. caURL = $("#caURL").val();
  320. }
  321. var skipTLSValue = $("#skipTLSCheckbox")[0].checked;
  322. $.ajax({
  323. url: "/api/acme/obtainCert",
  324. method: "GET",
  325. data: {
  326. domains: domains,
  327. filename: filename,
  328. email: email,
  329. ca: ca,
  330. caURL: caURL,
  331. skipTLS: skipTLSValue,
  332. },
  333. success: function(response) {
  334. $("#obtainButton").removeClass("loading").removeClass("disabled");
  335. if (response.error) {
  336. console.log("Error:", response.error);
  337. // Show error message
  338. parent.msgbox(response.error, false, 12000);
  339. } else {
  340. console.log("Certificate renewed successfully");
  341. // Show success message
  342. parent.msgbox("Certificate renewed successfully");
  343. // Renew the parent certificate list
  344. parent.initManagedDomainCertificateList();
  345. }
  346. },
  347. error: function(error) {
  348. $("#obtainButton").removeClass("loading").removeClass("disabled");
  349. console.log("Failed to renewed certificate:", error);
  350. }
  351. });
  352. }
  353. function checkIfInputDomainIsMultiple(){
  354. var inputDomains = $("#domainsInput").val();
  355. if (inputDomains.includes(",")){
  356. $(".multiDomainOnly").show();
  357. }else{
  358. $(".multiDomainOnly").hide();
  359. }
  360. }
  361. //Grab the longest common suffix of all domains
  362. //not that smart technically
  363. function autoDetectMatchingRules(){
  364. var domainsString = $("#domainsInput").val();
  365. if (!domainsString.includes(",")){
  366. return domainsString;
  367. }
  368. let domains = domainsString.split(",");
  369. //Clean out any spacing between commas
  370. for (var i = 0; i < domains.length; i++){
  371. domains[i] = domains[i].trim();
  372. }
  373. function getLongestCommonSuffix(strings) {
  374. if (strings.length === 0) {
  375. return ''; // Return an empty string if the array is empty
  376. }
  377. var sortedStrings = strings.slice().sort(); // Create a sorted copy of the array
  378. var firstString = sortedStrings[0];
  379. var lastString = sortedStrings[sortedStrings.length - 1];
  380. var suffix = '';
  381. var minLength = Math.min(firstString.length, lastString.length);
  382. for (var i = 0; i < minLength; i++) {
  383. if (firstString[firstString.length - 1 - i] !== lastString[lastString.length - 1 - i]) {
  384. break; // Stop iterating if characters don't match
  385. }
  386. suffix = firstString[firstString.length - 1 - i] + suffix;
  387. }
  388. return suffix;
  389. }
  390. let longestSuffix = getLongestCommonSuffix(domains);
  391. //Check if the suffix is a valid domain
  392. if (longestSuffix.substr(0,1) == "."){
  393. //Trim off the first dot
  394. longestSuffix = longestSuffix.substr(1);
  395. }
  396. if (!longestSuffix.includes(".")){
  397. parent.msgbox("Auto Detect failed: Multiple Domains", false, 5000);
  398. return;
  399. }
  400. $("#filenameInput").val(longestSuffix);
  401. }
  402. //Handle the renew now btn click
  403. function renewNow(){
  404. $.get("/api/acme/autoRenew/renewNow", function(data){
  405. if (data.error != undefined){
  406. parent.msgbox(data.error, false, 6000);
  407. }else{
  408. parent.msgbox(data)
  409. }
  410. })
  411. }
  412. function initAutoRenewPolicy(){
  413. $.get("/api/acme/autoRenew/listDomains", function(data){
  414. if (data.error != undefined){
  415. parent.msgbox(data.error, false)
  416. }else{
  417. if (data[0] == "*"){
  418. //Auto select and renew is enabled
  419. $("#renewAllSupported").parent().checkbox("set checked");
  420. }else{
  421. //This is a list of domain files
  422. data.forEach(function(name) {
  423. $('#domainTableBody input[type="checkbox"][name="' + name + '"]').prop('checked', true);
  424. });
  425. $("#domainCertFileTable").removeClass("disabled");
  426. $("#renewNowBtn").addClass("disabled");
  427. $("#renewSelectedButton").removeClass("disabled");
  428. }
  429. }
  430. })
  431. }
  432. function saveAutoRenewPolicy(){
  433. let autoRenewAll = $("#renewAllSupported").parent().checkbox("is checked");
  434. if (autoRenewAll == true){
  435. $.ajax({
  436. url: "/api/acme/autoRenew/setDomains",
  437. data: {opr: "setAuto"},
  438. success: function(data){
  439. parent.msgbox("Renew policy rule updated")
  440. }
  441. });
  442. }else{
  443. let checkedNames = [];
  444. $('#domainTableBody input[type="checkbox"]:checked').each(function() {
  445. checkedNames.push($(this).attr('name'));
  446. });
  447. $.ajax({
  448. url: "/api/acme/autoRenew/setDomains",
  449. data: {opr: "setSelected", domains: JSON.stringify(checkedNames)},
  450. success: function(data){
  451. parent.msgbox("Renew policy rule updated")
  452. }
  453. });
  454. }
  455. }
  456. //Clear up the input field when page load
  457. $("#filenameInput").val("");
  458. </script>
  459. </body>
  460. </html>