acme.html 17 KB

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