acme.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. let thisKey = $(this).attr("key");
  479. if ($(this).hasClass("checkbox")){
  480. //Boolean option
  481. let checked = $(this).find("input")[0].checked;
  482. dnsCredentials += `${thisKey}=${checked?"true":"false"}\n`
  483. }else{
  484. //String or int options
  485. let value = $(this).find("input").val().trim();
  486. dnsCredentials += `${thisKey}=${value}\n`
  487. }
  488. });
  489. dnsCredentials = dnsCredentials.trim();
  490. return dnsCredentials;
  491. }
  492. // Update DNS values for autorenewal
  493. function updateCertificateDNS() {
  494. var dns = $("#useDnsChallenge")[0].checked;
  495. var dnsProvider = "";
  496. var dnsCredentials = "";
  497. if (!dns) {
  498. return;
  499. }
  500. dnsProvider = $("#dnsProvider").dropdown("get value");
  501. //dnsCredentials = $("#dnsCredentials").val();
  502. dnsCredentials = readDnsCredentials();
  503. if(dnsProvider == "") {
  504. parent.msgbox("DNS Provider cannot be empty", false, 5000);
  505. $("#obtainButton").removeClass("loading").removeClass("disabled");
  506. return;
  507. }
  508. if(dnsCredentials == "") {
  509. parent.msgbox("DNS Credentials cannot be empty", false, 5000);
  510. $("#obtainButton").removeClass("loading").removeClass("disabled");
  511. return;
  512. }
  513. var filename = getFilename();
  514. if (filename == '') {
  515. return;
  516. }
  517. $.ajax({
  518. url: "/api/acme/autoRenew/setDNS",
  519. method: "POST",
  520. data: {
  521. filename: filename,
  522. dnsProvider: dnsProvider,
  523. dnsCredentials: dnsCredentials,
  524. },
  525. success: function(response) {
  526. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  527. if (response.error) {
  528. console.log("Error:", response.error);
  529. // Show error message
  530. parent.msgbox(response.error, false, 12000);
  531. } else {
  532. console.log("Certificate DNS Credentials updated successfully");
  533. // Show success message
  534. parent.msgbox("Certificate DNS Credentials updated successfully");
  535. }
  536. },
  537. error: function(error) {
  538. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  539. console.log("Failed to update DNS configuration:", error);
  540. parent.msgbox("Failed to update DNS configuration");
  541. }
  542. });
  543. }
  544. // Obtain certificate from API
  545. function obtainCertificate() {
  546. var domains = $("#domainsInput").val();
  547. var filename = getFilename();
  548. if (filename == '') {
  549. return;
  550. }
  551. var email = $("#caRegisterEmail").val();
  552. if (email == ""){
  553. parent.msgbox("ACME renew email is not set", false)
  554. $("#obtainButton").removeClass("loading").removeClass("disabled");
  555. return;
  556. }
  557. var ca = $("#ca").dropdown("get value");
  558. var caURL = "";
  559. if (ca == "Custom ACME Server") {
  560. ca = "custom";
  561. caURL = $("#caURL").val();
  562. }
  563. var dns = $("#useDnsChallenge")[0].checked;
  564. var skipTLSValue = $("#skipTLSCheckbox")[0].checked;
  565. $.ajax({
  566. url: "/api/acme/obtainCert",
  567. method: "GET",
  568. data: {
  569. domains: domains,
  570. filename: filename,
  571. email: email,
  572. ca: ca,
  573. caURL: caURL,
  574. skipTLS: skipTLSValue,
  575. dns: dns,
  576. },
  577. success: function(response) {
  578. $("#obtainButton").removeClass("loading").removeClass("disabled");
  579. if (response.error) {
  580. console.log("Error:", response.error);
  581. // Show error message
  582. parent.msgbox(response.error, false, 12000);
  583. } else {
  584. console.log("Certificate renewed successfully");
  585. // Show success message
  586. parent.msgbox("Certificate renewed successfully");
  587. // Renew the parent certificate list
  588. parent.initManagedDomainCertificateList();
  589. }
  590. },
  591. error: function(error) {
  592. $("#obtainButton").removeClass("loading").removeClass("disabled");
  593. console.log("Failed to renewed certificate:", error);
  594. }
  595. });
  596. }
  597. function checkIfInputDomainIsMultiple(){
  598. var inputDomains = $("#domainsInput").val();
  599. if (inputDomains.includes(",")){
  600. $(".multiDomainOnly").show();
  601. }else{
  602. $(".multiDomainOnly").hide();
  603. }
  604. }
  605. function toggleDnsChallenge(){
  606. if ( $("#useDnsChallenge")[0].checked){
  607. $(".dnsChallengeOnly").show();
  608. }else{
  609. $(".dnsChallengeOnly").hide();
  610. }
  611. }
  612. //Grab the longest common suffix of all domains
  613. //not that smart technically
  614. function autoDetectMatchingRules(){
  615. var domainsString = $("#domainsInput").val();
  616. if (!domainsString.includes(",")){
  617. return domainsString;
  618. }
  619. let domains = domainsString.split(",");
  620. //Clean out any spacing between commas
  621. for (var i = 0; i < domains.length; i++){
  622. domains[i] = domains[i].trim();
  623. }
  624. function getLongestCommonSuffix(strings) {
  625. if (strings.length === 0) {
  626. return ''; // Return an empty string if the array is empty
  627. }
  628. var sortedStrings = strings.slice().sort(); // Create a sorted copy of the array
  629. var firstString = sortedStrings[0];
  630. var lastString = sortedStrings[sortedStrings.length - 1];
  631. var suffix = '';
  632. var minLength = Math.min(firstString.length, lastString.length);
  633. for (var i = 0; i < minLength; i++) {
  634. if (firstString[firstString.length - 1 - i] !== lastString[lastString.length - 1 - i]) {
  635. break; // Stop iterating if characters don't match
  636. }
  637. suffix = firstString[firstString.length - 1 - i] + suffix;
  638. }
  639. return suffix;
  640. }
  641. let longestSuffix = getLongestCommonSuffix(domains);
  642. //Check if the suffix is a valid domain
  643. if (longestSuffix.substr(0,1) == "."){
  644. //Trim off the first dot
  645. longestSuffix = longestSuffix.substr(1);
  646. }
  647. if (!longestSuffix.includes(".")){
  648. parent.msgbox("Auto Detect failed: Multiple Domains", false, 5000);
  649. return;
  650. }
  651. $("#filenameInput").val(longestSuffix);
  652. }
  653. //Handle the renew now btn click
  654. function renewNow(){
  655. $.get("/api/acme/autoRenew/renewNow", function(data){
  656. if (data.error != undefined){
  657. parent.msgbox(data.error, false, 6000);
  658. }else{
  659. parent.msgbox(data)
  660. }
  661. })
  662. }
  663. function initAutoRenewPolicy(){
  664. $.get("/api/acme/autoRenew/listDomains", function(data){
  665. if (data.error != undefined){
  666. parent.msgbox(data.error, false)
  667. }else{
  668. if (data[0] == "*"){
  669. //Auto select and renew is enabled
  670. $("#renewAllSupported").parent().checkbox("set checked");
  671. }else{
  672. //This is a list of domain files
  673. data.forEach(function(name) {
  674. $('#domainTableBody input[type="checkbox"][name="' + name + '"]').prop('checked', true);
  675. });
  676. $("#domainCertFileTable").removeClass("disabled");
  677. $("#renewNowBtn").addClass("disabled");
  678. $("#renewSelectedButton").removeClass("disabled");
  679. }
  680. }
  681. })
  682. }
  683. function saveAutoRenewPolicy(){
  684. let autoRenewAll = $("#renewAllSupported").parent().checkbox("is checked");
  685. if (autoRenewAll == true){
  686. $.ajax({
  687. url: "/api/acme/autoRenew/setDomains",
  688. data: {opr: "setAuto"},
  689. success: function(data){
  690. parent.msgbox("Renew policy rule updated")
  691. }
  692. });
  693. }else{
  694. let checkedNames = [];
  695. $('#domainTableBody input[type="checkbox"]:checked').each(function() {
  696. checkedNames.push($(this).attr('name'));
  697. });
  698. $.ajax({
  699. url: "/api/acme/autoRenew/setDomains",
  700. data: {opr: "setSelected", domains: JSON.stringify(checkedNames)},
  701. success: function(data){
  702. parent.msgbox("Renew policy rule updated")
  703. }
  704. });
  705. }
  706. }
  707. //Load the json map and create the dropdown for DNS provider names
  708. let dnsProviderNameMap = {};
  709. function initDNSProviderList(){
  710. $.get("dnsnames.json", function(namemap){
  711. dnsProviderNameMap = namemap;
  712. //Load a list of supported DNS provider from backend
  713. $("#dnsProviderList").html("");
  714. $.get("/api/acme/dns/providers", function(providerList){
  715. providerList.sort();
  716. providerList.forEach(providerid => {
  717. let providerName = providerid;
  718. if (dnsProviderNameMap[providerid] != undefined){
  719. providerName = dnsProviderNameMap[providerid];
  720. }
  721. $("#dnsProviderList").append(`<div class="item" data-value="${providerid}">${providerName}</div>`);
  722. });
  723. $("#dnsProvider").dropdown();
  724. setTimeout(function(){
  725. //The dropdown is large, it takes some time to load
  726. $("#dnsProvider").dropdown("set selected", "cloudflare");
  727. }, 300)
  728. });
  729. });
  730. }
  731. initDNSProviderList();
  732. //Clear up the input field when page load
  733. $("#filenameInput").val("");
  734. </script>
  735. </body>
  736. </html>