acme.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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="dnsChallenge">
  115. <div class="ui checkbox">
  116. <input type="checkbox" id="useDnsChallenge" onchange="toggleDnsChallenge()">
  117. <label>Use a DNS Challenge<br>
  118. </div>
  119. </div>
  120. <div class="field dnsChallengeOnly" style="display:none;">
  121. <label>DNS Provider</label>
  122. <div class="ui search selection dropdown" id="dnsProvider">
  123. <input type="hidden" name="dnsProvider" value="">
  124. <i class="dropdown icon"></i>
  125. <div class="default text">Pick a DNS Provider</div>
  126. <div class="menu" id="dnsProviderList">
  127. <!-- Auto populate moved to acmedns module and initDNSProviderList() -->
  128. </div>
  129. </div>
  130. </div>
  131. <div class="field dnsChallengeOnly" style="display:none;">
  132. <div class="ui divider"></div>
  133. <p>Required Configurations</p>
  134. <div id="dnsProviderAPIFields">
  135. </div>
  136. <!--
  137. <label>Credentials File Content</label>
  138. <textarea id="dnsCredentials" placeholder=""></textarea>
  139. <small>For more information on the supported DNS Providers and their attirbutes look <a href="https://go-acme.github.io/lego/dns/" target="_blank">here</a>! </small>
  140. <div class="ui negative message">
  141. <i class="icon exclamation triangle"></i>
  142. These credentials will be stored as plaintext in the database and in environment variables!
  143. </div>
  144. -->
  145. </div>
  146. <div class="field" id="caInput" style="display:none;">
  147. <label>ACME Server URL</label>
  148. <input id="caURL" type="text" placeholder="https://example.com/acme/dictionary">
  149. </div>
  150. <div class="field" id="kidInput" style="display:none;">
  151. <label>EAB Credentials (KID) for current provider</label>
  152. <input id="eab_kid" type="text" placeholder="Leave this field blank to keep the current configuration">
  153. </div>
  154. <div class="field" id="hmacInput" style="display:none;">
  155. <label>EAB HMAC Key for current provider</label>
  156. <input id="eab_hmac" type="text" placeholder="Leave this field blank to keep the current configuration">
  157. </div>
  158. <div class="field" id="skipTLS" style="display:none;">
  159. <div class="ui checkbox">
  160. <input type="checkbox" id="skipTLSCheckbox">
  161. <label>Ignore TLS/SSL Verification Error<br><small>E.g. self-signed, expired certificate (Not Recommended)</small></label>
  162. </div>
  163. </div>
  164. <button id="obtainButton" class="ui basic button" type="submit"><i class="yellow refresh icon"></i> Get Certificate</button>
  165. </div>
  166. <div class="ui divider"></div>
  167. <small>First time setting up HTTPS?<br>Try out our <a href="../tools/https.html" target="_blank">wizard</a></small>
  168. <button class="ui basic button" style="float: right;" onclick="parent.hideSideWrapper();"><i class="remove icon"></i> Cancel</button>
  169. <br><br><br><br>
  170. </div>
  171. <script>
  172. let expiredDomains = [];
  173. let enableTrigerOnChangeEvent = true;
  174. $(".accordion").accordion();
  175. $(".dropdown").dropdown();
  176. $(".checkbox").checkbox();
  177. function setAutoRenewIfCASupportMode(useAutoMode = true){
  178. if (useAutoMode){
  179. $("#domainCertFileTable").addClass("disabled");
  180. $("#renewNowBtn").removeClass("disabled");
  181. $("#renewSelectedButton").addClass("disabled");
  182. }else{
  183. $("#domainCertFileTable").removeClass("disabled");
  184. $("#renewNowBtn").addClass("disabled");
  185. $("#renewSelectedButton").removeClass("disabled");
  186. }
  187. }
  188. function initRenewerConfigFromFile(){
  189. //Set the renew switch state
  190. $.get("/api/acme/autoRenew/enable", function(data){
  191. if (data == true){
  192. $("#enableCertAutoRenew").parent().checkbox("set checked");
  193. }
  194. $("#enableCertAutoRenew").on("change", function(){
  195. if (!enableTrigerOnChangeEvent){
  196. return;
  197. }
  198. toggleAutoRenew();
  199. })
  200. });
  201. //Load the email from server side
  202. $.get("/api/acme/autoRenew/email", function(data){
  203. if (data != "" && data != undefined && data != null){
  204. $("#caRegisterEmail").val(data);
  205. }
  206. });
  207. //Load the domain selection options
  208. $.get("/api/acme/autoRenew/renewPolicy", function(data){
  209. if (data == true){
  210. $("#renewAllSupported").parent().checkbox("set checked");
  211. }else{
  212. $("#renewAllSupported").parent().checkbox("set unchecked");
  213. }
  214. });
  215. }
  216. initRenewerConfigFromFile();
  217. function saveEmailToConfig(btn){
  218. $.ajax({
  219. url: "/api/acme/autoRenew/email",
  220. data: {set: $("#caRegisterEmail").val()},
  221. success: function(data){
  222. if (data.error != undefined){
  223. parent.msgbox(data.error, false, 5000);
  224. }else{
  225. parent.msgbox("Email updated");
  226. $(btn).html(`<i class="green check icon"></i>`);
  227. $(btn).addClass("disabled");
  228. setTimeout(function(){
  229. $(btn).html(`<i class="blue save icon"></i>`);
  230. $(btn).removeClass("disabled");
  231. }, 3000);
  232. }
  233. }
  234. });
  235. }
  236. function toggleAutoRenew(){
  237. var enabled = $("#enableCertAutoRenew").parent().checkbox("is checked");
  238. $.post("/api/acme/autoRenew/enable?enable=" + enabled, function(data){
  239. if (data.error){
  240. parent.msgbox(data.error, false, 5000);
  241. if (enabled){
  242. enableTrigerOnChangeEvent = false;
  243. $("#enableCertAutoRenew").parent().checkbox("set unchecked");
  244. enableTrigerOnChangeEvent = true;
  245. }
  246. if (parent && parent.setACMEEnableStates){
  247. parent.setACMEEnableStates(!enabled);
  248. }
  249. }else{
  250. $("#enableToggleSucc").stop().finish().fadeIn("fast").delay(3000).fadeOut("fast");
  251. if (parent && parent.setACMEEnableStates){
  252. parent.setACMEEnableStates(enabled);
  253. }
  254. }
  255. });
  256. }
  257. //Render the domains table that exists in this zoraxy host
  258. function renderDomainTable(domainFileList) {
  259. // Get the table body element
  260. var tableBody = $('#domainTableBody');
  261. // Clear the table body
  262. tableBody.empty();
  263. // Iterate over the domain names
  264. var counter = 0;
  265. for (const [srcfile, domains] of Object.entries(domainFileList)) {
  266. // Create a table row
  267. var row = $('<tr>');
  268. // Create the domain name cell
  269. var domainClass = "validDomain";
  270. for (var i = 0; i < domains.length; i++){
  271. let thisDomain = domains[i];
  272. if (expiredDomains.includes(thisDomain)){
  273. domainClass = "expiredDomain";
  274. }
  275. }
  276. var domainCell = $('<td class="' + domainClass +'">').html(domains.join("<br>"));
  277. row.append(domainCell);
  278. var srcFileCell = $('<td>').text(srcfile);
  279. row.append(srcFileCell);
  280. // Create the auto-renew checkbox cell
  281. let domainsEncoded = encodeURIComponent(JSON.stringify(domains));
  282. var checkboxCell = $(`<td domain="${domainsEncoded}" srcfile="${srcfile}">`);
  283. var checkbox = $(`<input name="${srcfile}">`).attr('type', 'checkbox');
  284. checkboxCell.append(checkbox);
  285. row.append(checkboxCell);
  286. // Add the row to the table body
  287. tableBody.append(row);
  288. counter++;
  289. }
  290. if (Object.keys(domainFileList).length == 0){
  291. //No certificate in this system
  292. tableBody.append(`<tr>
  293. <td colspan="3"><i class="ui green circle check icon"></i> No certificate in use</td>
  294. </tr>`);
  295. }
  296. }
  297. //Initiate domain table. If you needs to update the expired domain as well
  298. //call from initDomainFileList() instead
  299. function initDomainTable(){
  300. $.get("/api/cert/listdomains?compact=true", function(data){
  301. if (data.error != undefined){
  302. parent.msgbox(data.error, false);
  303. }else{
  304. renderDomainTable(data);
  305. }
  306. initAutoRenewPolicy();
  307. })
  308. }
  309. function initDomainFileList() {
  310. $.ajax({
  311. url: "/api/acme/listExpiredDomains",
  312. method: "GET",
  313. success: function(response) {
  314. // Render domain table
  315. expiredDomains = response.domain;
  316. initDomainTable();
  317. //renderDomainTable(response.domain);
  318. },
  319. error: function(error) {
  320. console.log("Failed to fetch expired domains:", error);
  321. }
  322. });
  323. }
  324. initDomainFileList();
  325. // Button click event handler for obtaining certificate
  326. $("#obtainButton").click(function() {
  327. $("#obtainButton").addClass("loading").addClass("disabled");
  328. updateCertificateEAB();
  329. updateCertificateDNS();
  330. obtainCertificate();
  331. });
  332. //On CA change in dropdown
  333. $("input[name=ca]").on('change', function() {
  334. if(this.value == "Custom ACME Server") {
  335. $("#caInput").show();
  336. $("#kidInput").show();
  337. $("#hmacInput").show();
  338. $("#skipTLS").show();
  339. $("#dnsChallenge").hide();
  340. $(".dnsChallengeOnly").hide();
  341. } else if (this.value == "ZeroSSL") {
  342. $("#kidInput").show();
  343. $("#hmacInput").show();
  344. $("#dnsChallenge").hide();
  345. $(".dnsChallengeOnly").hide();
  346. $("#skipTLS").hide();
  347. } else if (this.value == "Buypass") {
  348. $("#kidInput").show();
  349. $("#hmacInput").show();
  350. $("#dnsChallenge").hide();
  351. $(".dnsChallengeOnly").hide();
  352. $("#skipTLS").hide();
  353. }else {
  354. $("#caInput").hide();
  355. $("#skipTLS").hide();
  356. $("#kidInput").hide();
  357. $("#hmacInput").hide();
  358. $("#dnsChallenge").show();
  359. if ($("#useDnsChallenge")[0].checked){
  360. $(".dnsChallengeOnly").show();
  361. }
  362. }
  363. })
  364. //On DNS provider dropdown change
  365. $("input[name=dnsProvider]").on('change', function() {
  366. let newProviderName = $("#dnsProvider").find("input").val();
  367. $.get("/api/acme/dns/providers?name=" + newProviderName, function(data){
  368. console.log("Loaded required config", data);
  369. $("#dnsProviderAPIFields").html("");
  370. //Generate a form for this config
  371. let booleanFieldsHTML = "";
  372. for (const [key, datatype] of Object.entries(data)) {
  373. if (datatype == "int"){
  374. $("#dnsProviderAPIFields").append(`<div class="ui fluid labeled dnsConfigField input" key="${key}" style="margin-top: 0.2em;">
  375. <div class="ui basic blue label" style="font-weight: 300;">
  376. ${key}
  377. </div>
  378. <input type="number" value="300">
  379. </div>`);
  380. }else if (datatype == "bool"){
  381. booleanFieldsHTML += (`<div class="ui checkbox dnsConfigField" key="${key}" style="margin-top: 1em !important; padding-left: 0.4em;">
  382. <input type="checkbox">
  383. <label>${key}</label>
  384. </div>`);
  385. }else{
  386. //Default to string
  387. $("#dnsProviderAPIFields").append(`<div class="ui fluid labeled input dnsConfigField" key="${key}" style="margin-top: 0.2em;">
  388. <div class="ui basic label" style="font-weight: 300;">
  389. ${key}
  390. </div>
  391. <input type="text">
  392. </div>`);
  393. }
  394. }
  395. //Append the boolean fields at the bottom, if exists
  396. $("#dnsProviderAPIFields").append(booleanFieldsHTML);
  397. if (booleanFieldsHTML != ""){
  398. $(".dnsConfigField.checkbox").checkbox();
  399. }
  400. });
  401. });
  402. // Get filename form domains and input
  403. function getFilename() {
  404. var domains = $("#domainsInput").val();
  405. var filename = $("#filenameInput").val();
  406. if (filename.trim() == "" && !domains.includes(",")){
  407. //Zoraxy filename are the matching name for domains.
  408. //Use the same as domains
  409. filename = domains;
  410. }else if (filename != "" && !domains.includes(",")){
  411. //Invalid settings. Force the filename to be same as domain
  412. //if there are only 1 domain
  413. filename = domains;
  414. }else if (filename == "" && domains.includes(",")){
  415. parent.msgbox("Filename cannot be empty for certs containing multiple domains.", false, 5000);
  416. $("#obtainButton").removeClass("loading").removeClass("disabled");
  417. return;
  418. }
  419. //Filename cannot contain wildcards, and wildcards are possible with DNS challenges
  420. filename = filename.replace("*", "_");
  421. return filename;
  422. }
  423. // Update EAB values for autorenewal
  424. function updateCertificateEAB() {
  425. var ca = $("#ca").dropdown("get value");
  426. var caURL = "";
  427. if (ca == "Custom ACME Server") {
  428. ca = "custom";
  429. caURL = $("#caURL").val();
  430. }else if(ca == "Buypass") {
  431. caURL = "https://api.buypass.com/acme/directory";
  432. }else if(ca == "ZeroSSL") {
  433. caURL = "https://acme.zerossl.com/v2/DV90";
  434. }
  435. if(caURL == "") {
  436. return;
  437. }
  438. var kid = $("#eab_kid").val();
  439. var hmac = $("#eab_hmac").val();
  440. if(kid == "" || hmac == "") {
  441. return;
  442. }
  443. console.log(caURL + " " + kid + " " + hmac);
  444. $.ajax({
  445. url: "/api/acme/autoRenew/setEAB",
  446. method: "GET",
  447. data: {
  448. acmeDirectoryURL: caURL,
  449. kid: kid,
  450. hmacEncoded: hmac,
  451. },
  452. success: function(response) {
  453. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  454. if (response.error) {
  455. console.log("Error:", response.error);
  456. // Show error message
  457. parent.msgbox(response.error, false, 12000);
  458. } else {
  459. console.log("Certificate EAB updated successfully");
  460. // Show success message
  461. parent.msgbox("Certificate EAB updated successfully");
  462. // Renew the parent certificate list
  463. parent.initManagedDomainCertificateList();
  464. }
  465. },
  466. error: function(error) {
  467. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  468. console.log("Failed to update EAB configuration:", error);
  469. parent.msgbox("Failed to update EAB configuration");
  470. }
  471. });
  472. }
  473. //Read DNS credential from form and generate a key value structure that looks like
  474. // the old DNSCredential TextArea input
  475. function readDnsCredentials(){
  476. let dnsCredentials = {};
  477. $(".dnsConfigField").each(function(){
  478. /*
  479. let thisKey = $(this).attr("key");
  480. if ($(this).hasClass("checkbox")){
  481. //Boolean option
  482. let checked = $(this).find("input")[0].checked;
  483. dnsCredentials += `${thisKey}=${checked?"true":"false"}\n`
  484. }else{
  485. //String or int options
  486. let value = $(this).find("input").val().trim();
  487. dnsCredentials += `${thisKey}=${value}\n`
  488. }
  489. */
  490. let thisKey = $(this).attr("key");
  491. let value = "";
  492. if ($(this).hasClass("checkbox")){
  493. //Boolean option
  494. let checked = $(this).find("input")[0].checked;
  495. dnsCredentials[thisKey] = checked;
  496. }else{
  497. //String or int options
  498. let value = $(this).find("input").val().trim();
  499. dnsCredentials[thisKey] = value;
  500. }
  501. });
  502. return dnsCredentials;
  503. }
  504. // Update DNS values for autorenewal
  505. function updateCertificateDNS() {
  506. var dns = $("#useDnsChallenge")[0].checked;
  507. var dnsProvider = "";
  508. var dnsCredentials = "";
  509. if (!dns) {
  510. return;
  511. }
  512. dnsProvider = $("#dnsProvider").dropdown("get value");
  513. //dnsCredentials = $("#dnsCredentials").val();
  514. dnsCredentials = readDnsCredentials();
  515. if(dnsProvider == "") {
  516. parent.msgbox("DNS Provider cannot be empty", false, 5000);
  517. $("#obtainButton").removeClass("loading").removeClass("disabled");
  518. return;
  519. }
  520. var filename = getFilename();
  521. if (filename == '') {
  522. return;
  523. }
  524. $.ajax({
  525. url: "/api/acme/autoRenew/setDNS",
  526. method: "POST",
  527. data: {
  528. filename: filename,
  529. dnsProvider: dnsProvider,
  530. dnsCredentials: JSON.stringify(dnsCredentials),
  531. },
  532. success: function(response) {
  533. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  534. if (response.error) {
  535. console.log("Error:", response.error);
  536. // Show error message
  537. parent.msgbox(response.error, false, 12000);
  538. } else {
  539. console.log("Certificate DNS Credentials updated successfully");
  540. // Show success message
  541. parent.msgbox("Certificate DNS Credentials updated successfully");
  542. }
  543. },
  544. error: function(error) {
  545. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  546. console.log("Failed to update DNS configuration:", error);
  547. parent.msgbox("Failed to update DNS configuration");
  548. }
  549. });
  550. }
  551. // Obtain certificate from API
  552. function obtainCertificate() {
  553. var domains = $("#domainsInput").val();
  554. var filename = getFilename();
  555. if (filename == '') {
  556. return;
  557. }
  558. var email = $("#caRegisterEmail").val();
  559. if (email == ""){
  560. parent.msgbox("ACME renew email is not set", false)
  561. $("#obtainButton").removeClass("loading").removeClass("disabled");
  562. return;
  563. }
  564. var ca = $("#ca").dropdown("get value");
  565. var caURL = "";
  566. if (ca == "Custom ACME Server") {
  567. ca = "custom";
  568. caURL = $("#caURL").val();
  569. }
  570. var dns = $("#useDnsChallenge")[0].checked;
  571. var skipTLSValue = $("#skipTLSCheckbox")[0].checked;
  572. $.ajax({
  573. url: "/api/acme/obtainCert",
  574. method: "GET",
  575. data: {
  576. domains: domains,
  577. filename: filename,
  578. email: email,
  579. ca: ca,
  580. caURL: caURL,
  581. skipTLS: skipTLSValue,
  582. dns: dns,
  583. },
  584. success: function(response) {
  585. $("#obtainButton").removeClass("loading").removeClass("disabled");
  586. if (response.error) {
  587. console.log("Error:", response.error);
  588. // Show error message
  589. parent.msgbox(response.error, false, 12000);
  590. } else {
  591. console.log("Certificate renewed successfully");
  592. // Show success message
  593. parent.msgbox("Certificate renewed successfully");
  594. // Renew the parent certificate list
  595. parent.initManagedDomainCertificateList();
  596. }
  597. },
  598. error: function(error) {
  599. $("#obtainButton").removeClass("loading").removeClass("disabled");
  600. console.log("Failed to renewed certificate:", error);
  601. }
  602. });
  603. }
  604. function checkIfInputDomainIsMultiple(){
  605. var inputDomains = $("#domainsInput").val();
  606. if (inputDomains.includes(",")){
  607. $(".multiDomainOnly").show();
  608. }else{
  609. $(".multiDomainOnly").hide();
  610. }
  611. }
  612. function toggleDnsChallenge(){
  613. if ( $("#useDnsChallenge")[0].checked){
  614. $(".dnsChallengeOnly").show();
  615. }else{
  616. $(".dnsChallengeOnly").hide();
  617. }
  618. }
  619. //Grab the longest common suffix of all domains
  620. //not that smart technically
  621. function autoDetectMatchingRules(){
  622. var domainsString = $("#domainsInput").val();
  623. if (!domainsString.includes(",")){
  624. return domainsString;
  625. }
  626. let domains = domainsString.split(",");
  627. //Clean out any spacing between commas
  628. for (var i = 0; i < domains.length; i++){
  629. domains[i] = domains[i].trim();
  630. }
  631. function getLongestCommonSuffix(strings) {
  632. if (strings.length === 0) {
  633. return ''; // Return an empty string if the array is empty
  634. }
  635. var sortedStrings = strings.slice().sort(); // Create a sorted copy of the array
  636. var firstString = sortedStrings[0];
  637. var lastString = sortedStrings[sortedStrings.length - 1];
  638. var suffix = '';
  639. var minLength = Math.min(firstString.length, lastString.length);
  640. for (var i = 0; i < minLength; i++) {
  641. if (firstString[firstString.length - 1 - i] !== lastString[lastString.length - 1 - i]) {
  642. break; // Stop iterating if characters don't match
  643. }
  644. suffix = firstString[firstString.length - 1 - i] + suffix;
  645. }
  646. return suffix;
  647. }
  648. let longestSuffix = getLongestCommonSuffix(domains);
  649. //Check if the suffix is a valid domain
  650. if (longestSuffix.substr(0,1) == "."){
  651. //Trim off the first dot
  652. longestSuffix = longestSuffix.substr(1);
  653. }
  654. if (!longestSuffix.includes(".")){
  655. parent.msgbox("Auto Detect failed: Multiple Domains", false, 5000);
  656. return;
  657. }
  658. $("#filenameInput").val(longestSuffix);
  659. }
  660. //Handle the renew now btn click
  661. function renewNow(){
  662. $.get("/api/acme/autoRenew/renewNow", function(data){
  663. if (data.error != undefined){
  664. parent.msgbox(data.error, false, 6000);
  665. }else{
  666. parent.msgbox(data)
  667. }
  668. })
  669. }
  670. function initAutoRenewPolicy(){
  671. $.get("/api/acme/autoRenew/listDomains", function(data){
  672. if (data.error != undefined){
  673. parent.msgbox(data.error, false)
  674. }else{
  675. if (data[0] == "*"){
  676. //Auto select and renew is enabled
  677. $("#renewAllSupported").parent().checkbox("set checked");
  678. }else{
  679. //This is a list of domain files
  680. data.forEach(function(name) {
  681. $('#domainTableBody input[type="checkbox"][name="' + name + '"]').prop('checked', true);
  682. });
  683. $("#domainCertFileTable").removeClass("disabled");
  684. $("#renewNowBtn").addClass("disabled");
  685. $("#renewSelectedButton").removeClass("disabled");
  686. }
  687. }
  688. })
  689. }
  690. function saveAutoRenewPolicy(){
  691. let autoRenewAll = $("#renewAllSupported").parent().checkbox("is checked");
  692. if (autoRenewAll == true){
  693. $.ajax({
  694. url: "/api/acme/autoRenew/setDomains",
  695. data: {opr: "setAuto"},
  696. success: function(data){
  697. parent.msgbox("Renew policy rule updated")
  698. }
  699. });
  700. }else{
  701. let checkedNames = [];
  702. $('#domainTableBody input[type="checkbox"]:checked').each(function() {
  703. checkedNames.push($(this).attr('name'));
  704. });
  705. $.ajax({
  706. url: "/api/acme/autoRenew/setDomains",
  707. data: {opr: "setSelected", domains: JSON.stringify(checkedNames)},
  708. success: function(data){
  709. parent.msgbox("Renew policy rule updated")
  710. }
  711. });
  712. }
  713. }
  714. //Load the json map and create the dropdown for DNS provider names
  715. let dnsProviderNameMap = {};
  716. function initDNSProviderList(){
  717. $.get("dnsnames.json", function(namemap){
  718. dnsProviderNameMap = namemap;
  719. //Load a list of supported DNS provider from backend
  720. $("#dnsProviderList").html("");
  721. $.get("/api/acme/dns/providers", function(providerList){
  722. providerList.sort();
  723. providerList.forEach(providerid => {
  724. let providerName = providerid;
  725. if (dnsProviderNameMap[providerid] != undefined){
  726. providerName = dnsProviderNameMap[providerid];
  727. }
  728. $("#dnsProviderList").append(`<div class="item" data-value="${providerid}">${providerName}</div>`);
  729. });
  730. $("#dnsProvider").dropdown();
  731. setTimeout(function(){
  732. //The dropdown is large, it takes some time to load
  733. $("#dnsProvider").dropdown("set selected", "cloudflare");
  734. }, 300)
  735. });
  736. });
  737. }
  738. initDNSProviderList();
  739. //Clear up the input field when page load
  740. $("#filenameInput").val("");
  741. </script>
  742. </body>
  743. </html>