acme.html 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Notes: This should be open in its original path-->
  5. <meta charset="utf-8">
  6. <meta name="zoraxy.csrf.Token" content="{{.csrfToken}}">
  7. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  8. <script src="../script/jquery-3.6.0.min.js"></script>
  9. <script src="../script/semantic/semantic.min.js"></script>
  10. <script src="../script/utils.js"></script>
  11. <style>
  12. .disabled.table{
  13. opacity: 0.5;
  14. pointer-events: none;
  15. }
  16. .expiredDomain{
  17. color: rgb(238, 31, 31);
  18. }
  19. .validDomain{
  20. color: rgb(49, 192, 113);
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <link rel="stylesheet" href="../darktheme.css">
  26. <script src="../script/darktheme.js"></script>
  27. <br>
  28. <div class="ui container">
  29. <div class="ui header">
  30. <div class="content">
  31. Certificates Auto Renew Settings
  32. <div class="sub header">Fetch and renew your certificates with Automated Certificate Management Environment (ACME) protocol</div>
  33. </div>
  34. </div>
  35. <div class="ui basic segment">
  36. <p style="float: right; color: #21ba45; display:none;" id="enableToggleSucc"><i class="green checkmark icon"></i> Setting Updated</p>
  37. <div class="ui toggle checkbox">
  38. <input type="checkbox" id="enableCertAutoRenew">
  39. <label>Enable Certificate Auto Renew</label>
  40. </div>
  41. <br>
  42. <h3>ACME Email</h3>
  43. <p>Email is required by many CAs for renewing via ACME protocol</p>
  44. <div class="ui fluid action input">
  45. <input id="caRegisterEmail" type="text" placeholder="[email protected]">
  46. <button class="ui icon basic button" onclick="saveEmailToConfig(this);">
  47. <i class="blue save icon"></i>
  48. </button>
  49. </div>
  50. <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>
  51. </div>
  52. <div class="ui basic segment advanceoptions">
  53. <div class="ui accordion advanceSettings">
  54. <div class="title">
  55. <i class="dropdown icon"></i>
  56. Advance Renew Policy
  57. </div>
  58. <div class="content">
  59. <p>Renew all certificates with ACME supported CAs</p>
  60. <div class="ui toggle checkbox">
  61. <input type="checkbox" id="renewAllSupported" onchange="setAutoRenewIfCASupportMode(this.checked);">
  62. <label>Renew All Certs</label>
  63. </div><br>
  64. <button id="renewNowBtn" onclick="renewNow();" class="ui basic right floated button" style="margin-top: -2em;"><i class="yellow refresh icon"></i> Renew Now</button>
  65. <div class="ui horizontal divider"> OR </div>
  66. <p>Select the certificates to automatic renew in the list below</p>
  67. <div style="width: 100%; overflow-x: auto; margin-bottom: 1em;">
  68. <table id="domainCertFileTable" class="ui very compact unstackable basic disabled table">
  69. <thead>
  70. <tr>
  71. <th>Domain Name</th>
  72. <th>Match Rule</th>
  73. <th>Auto-Renew</th>
  74. </tr>
  75. </thead>
  76. <tbody id="domainTableBody"></tbody>
  77. </table>
  78. </div>
  79. <small><i class="ui red info circle icon"></i> Domain in red are expired</small><br>
  80. <div class="ui yellow message">
  81. Certificate Renew only works on the certification authority (CA) supported by Zoraxy. Check Zoraxy wiki for more information on supported list of CAs.
  82. </div>
  83. <button class="ui basic right floated button" onclick="saveAutoRenewPolicy();"><i class="blue save icon"></i> Save Changes</button>
  84. <button id="renewSelectedButton" onclick="renewNow();" class="ui basic right floated disabled button"><i class="yellow refresh icon"></i> Renew Selected</button>
  85. <br><br>
  86. </div>
  87. </div>
  88. </div>
  89. <div class="ui divider"></div>
  90. <h3>Generate New Certificate</h3>
  91. <p>Enter a new / existing domain(s) to request new certificate(s)</p>
  92. <div class="ui form">
  93. <div class="field">
  94. <label>Domain(s)</label>
  95. <input id="domainsInput" type="text" placeholder="example.com" onkeyup="handlePostInputAutomation();">
  96. <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)
  97. <span id="caNoDNSSupportWarning" style="color: #ffaf2e; display:none;"><br> <i class="exclamation triangle icon"></i> Current selected CA do not support DNS challenge</span>
  98. </small>
  99. </div>
  100. <div class="field multiDomainOnly" style="display:none;">
  101. <label>Matching Rule</label>
  102. <input id="filenameInput" type="text" placeholder="Enter filename (no file extension)">
  103. <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>
  104. </div>
  105. <div class="field multiDomainOnly" style="display:none;">
  106. <button class="ui basic fluid button" onclick="autoDetectMatchingRules();">Auto Detect Matching Rule</button>
  107. </div>
  108. <div class="field">
  109. <label>Certificate Authority (CA)</label>
  110. <div class="ui selection dropdown" id="ca">
  111. <input type="hidden" name="ca">
  112. <i class="dropdown icon"></i>
  113. <div class="default text">Let's Encrypt</div>
  114. <div class="menu">
  115. <div class="item" data-value="Let's Encrypt">Let's Encrypt</div>
  116. <div class="item" data-value="Buypass">Buypass</div>
  117. <div class="item" data-value="ZeroSSL">ZeroSSL</div>
  118. <div class="item" data-value="Custom ACME Server">Custom ACME Server</div>
  119. </div>
  120. </div>
  121. </div>
  122. <div class="field" id="dnsChallenge">
  123. <div class="ui checkbox">
  124. <input type="checkbox" id="useDnsChallenge" onchange="toggleDnsChallenge()">
  125. <label>Use a DNS Challenge<br>
  126. </div>
  127. </div>
  128. <div class="field dnsChallengeOnly" style="display:none;">
  129. <label>DNS Provider</label>
  130. <div class="ui search selection dropdown" id="dnsProvider">
  131. <input type="hidden" name="dnsProvider" value="">
  132. <i class="dropdown icon"></i>
  133. <div class="default text">Pick a DNS Provider</div>
  134. <div class="menu" id="dnsProviderList">
  135. <!-- Auto populate moved to acmedns module and initDNSProviderList() -->
  136. </div>
  137. </div>
  138. </div>
  139. <div class="field dnsChallengeOnly" style="display:none;">
  140. <div class="ui divider"></div>
  141. <p>DNS Credentials</p>
  142. <div id="dnsProviderAPIFields">
  143. <p><i class="ui loading circle notch icon"></i> Generating WebForm</p>
  144. </div>
  145. <h4><i class="yellow exclamation triangle icon"></i> Notes & FAQ</h4>
  146. <div class="ui bulleted list">
  147. <div class="item">Domain DNS credentials are stored separately. For each new subdomain, you will need to enter a new DNS credentials.</div>
  148. <div class="item">For some DNS providers like CloudFlare, you do not need to fill in all fields.</div>
  149. <div class="item">If you are not sure what to fill in, check out the documentation from <a href="https://go-acme.github.io/lego/dns/" target="_blank">lego (DNS challenge library)</a></div>
  150. </div>
  151. <!--
  152. <label>Credentials File Content</label>
  153. <textarea id="dnsCredentials" placeholder=""></textarea>
  154. <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>
  155. <div class="ui negative message">
  156. <i class="icon exclamation triangle"></i>
  157. These credentials will be stored as plaintext in the database and in environment variables!
  158. </div>
  159. -->
  160. </div>
  161. <div class="field dnsChallengeOnly" style="display:none;">
  162. <label>Domain Name Server (optional)</label>
  163. <input id="dnsInput" type="text" placeholder="ns.example.com">
  164. <small>If you have more than one DNS server, enter them separated by commas (e.g. ns1.example.com,ns2.example.com)</small>
  165. </div>
  166. <div class="field" id="caInput" style="display:none;">
  167. <label>ACME Server URL</label>
  168. <input id="caURL" type="text" placeholder="https://example.com/acme/dictionary">
  169. </div>
  170. <div class="field" id="kidInput" style="display:none;">
  171. <label>EAB Credentials (KID) for current provider</label>
  172. <input id="eab_kid" type="text" placeholder="Leave this field blank to keep the current configuration">
  173. </div>
  174. <div class="field" id="hmacInput" style="display:none;">
  175. <label>EAB HMAC Key for current provider</label>
  176. <input id="eab_hmac" type="text" placeholder="Leave this field blank to keep the current configuration">
  177. </div>
  178. <div class="field" id="skipTLS" style="display:none;">
  179. <div class="ui checkbox">
  180. <input type="checkbox" id="skipTLSCheckbox">
  181. <label>Ignore TLS/SSL Verification Error<br><small>E.g. self-signed, expired certificate (Not Recommended)</small></label>
  182. </div>
  183. </div>
  184. <button id="obtainButton" class="ui basic button" type="submit"><i class="yellow refresh icon"></i> Get Certificate</button>
  185. </div>
  186. <div class="ui divider"></div>
  187. <small>First time setting up HTTPS?<br>Try out our <a href="../tools/https.html" target="_blank">wizard</a></small>
  188. <button class="ui basic button" style="float: right;" onclick="parent.hideSideWrapper();"><i class="remove icon"></i> Cancel</button>
  189. <br><br><br><br>
  190. </div>
  191. <script>
  192. let expiredDomains = [];
  193. let enableTrigerOnChangeEvent = true;
  194. $(".accordion").accordion();
  195. $(".dropdown").dropdown();
  196. $(".checkbox").checkbox();
  197. function setAutoRenewIfCASupportMode(useAutoMode = true){
  198. if (useAutoMode){
  199. $("#domainCertFileTable").addClass("disabled");
  200. $("#renewNowBtn").removeClass("disabled");
  201. $("#renewSelectedButton").addClass("disabled");
  202. }else{
  203. $("#domainCertFileTable").removeClass("disabled");
  204. $("#renewNowBtn").addClass("disabled");
  205. $("#renewSelectedButton").removeClass("disabled");
  206. }
  207. }
  208. function initRenewerConfigFromFile(){
  209. //Set the renew switch state
  210. $.get("/api/acme/autoRenew/enable", function(data){
  211. if (data == true){
  212. $("#enableCertAutoRenew").parent().checkbox("set checked");
  213. }
  214. $("#enableCertAutoRenew").on("change", function(){
  215. if (!enableTrigerOnChangeEvent){
  216. return;
  217. }
  218. toggleAutoRenew();
  219. })
  220. });
  221. //Load the email from server side
  222. $.get("/api/acme/autoRenew/email", function(data){
  223. if (data != "" && data != undefined && data != null){
  224. $("#caRegisterEmail").val(data);
  225. }
  226. });
  227. //Load the domain selection options
  228. $.get("/api/acme/autoRenew/renewPolicy", function(data){
  229. if (data == true){
  230. $("#renewAllSupported").parent().checkbox("set checked");
  231. }else{
  232. $("#renewAllSupported").parent().checkbox("set unchecked");
  233. }
  234. });
  235. }
  236. initRenewerConfigFromFile();
  237. function saveEmailToConfig(btn){
  238. $.cjax({
  239. url: "/api/acme/autoRenew/email",
  240. method: "POST",
  241. data: {set: $("#caRegisterEmail").val()},
  242. success: function(data){
  243. if (data.error != undefined){
  244. parent.msgbox(data.error, false, 5000);
  245. }else{
  246. parent.msgbox("Email updated");
  247. $(btn).html(`<i class="green check icon"></i>`);
  248. $(btn).addClass("disabled");
  249. setTimeout(function(){
  250. $(btn).html(`<i class="blue save icon"></i>`);
  251. $(btn).removeClass("disabled");
  252. }, 3000);
  253. }
  254. }
  255. });
  256. }
  257. function toggleAutoRenew(){
  258. var enabled = $("#enableCertAutoRenew").parent().checkbox("is checked");
  259. $.cjax({
  260. url: "/api/acme/autoRenew/enable",
  261. method: "POST",
  262. data: {"enable": enabled},
  263. success: function(data){
  264. if (data.error){
  265. parent.msgbox(data.error, false, 5000);
  266. if (enabled){
  267. enableTrigerOnChangeEvent = false;
  268. $("#enableCertAutoRenew").parent().checkbox("set unchecked");
  269. enableTrigerOnChangeEvent = true;
  270. }
  271. if (parent && parent.setACMEEnableStates){
  272. parent.setACMEEnableStates(!enabled);
  273. }
  274. }else{
  275. $("#enableToggleSucc").stop().finish().fadeIn("fast").delay(3000).fadeOut("fast");
  276. if (parent && parent.setACMEEnableStates){
  277. parent.setACMEEnableStates(enabled);
  278. }
  279. }
  280. }
  281. });
  282. }
  283. //Render the domains table that exists in this zoraxy host
  284. function renderDomainTable(domainFileList) {
  285. // Get the table body element
  286. var tableBody = $('#domainTableBody');
  287. // Clear the table body
  288. tableBody.empty();
  289. // Iterate over the domain names
  290. var counter = 0;
  291. for (const [srcfile, domains] of Object.entries(domainFileList)) {
  292. // Create a table row
  293. var row = $('<tr>');
  294. // Create the domain name cell
  295. var domainClass = "validDomain";
  296. for (var i = 0; i < domains.length; i++){
  297. let thisDomain = domains[i];
  298. if (expiredDomains.includes(thisDomain)){
  299. domainClass = "expiredDomain";
  300. }
  301. }
  302. var domainCell = $('<td class="' + domainClass +'">').html(domains.join("<br>"));
  303. row.append(domainCell);
  304. var srcFileCell = $('<td>').text(srcfile);
  305. row.append(srcFileCell);
  306. // Create the auto-renew checkbox cell
  307. let domainsEncoded = encodeURIComponent(JSON.stringify(domains));
  308. var checkboxCell = $(`<td domain="${domainsEncoded}" srcfile="${srcfile}">`);
  309. var checkbox = $(`<input name="${srcfile}">`).attr('type', 'checkbox');
  310. checkboxCell.append(checkbox);
  311. row.append(checkboxCell);
  312. // Add the row to the table body
  313. tableBody.append(row);
  314. counter++;
  315. }
  316. if (Object.keys(domainFileList).length == 0){
  317. //No certificate in this system
  318. tableBody.append(`<tr>
  319. <td colspan="3"><i class="ui green circle check icon"></i> No certificate in use</td>
  320. </tr>`);
  321. }
  322. }
  323. //Initiate domain table. If you needs to update the expired domain as well
  324. //call from initDomainFileList() instead
  325. function initDomainTable(){
  326. $.get("/api/cert/listdomains?compact=true", function(data){
  327. if (data.error != undefined){
  328. parent.msgbox(data.error, false);
  329. }else{
  330. renderDomainTable(data);
  331. }
  332. initAutoRenewPolicy();
  333. })
  334. }
  335. function initDomainFileList() {
  336. $.ajax({
  337. url: "/api/acme/listExpiredDomains",
  338. method: "GET",
  339. success: function(response) {
  340. // Render domain table
  341. expiredDomains = response.domain;
  342. initDomainTable();
  343. //renderDomainTable(response.domain);
  344. },
  345. error: function(error) {
  346. console.log("Failed to fetch expired domains:", error);
  347. }
  348. });
  349. }
  350. initDomainFileList();
  351. // Button click event handler for obtaining certificate
  352. $("#obtainButton").click(function() {
  353. $("#obtainButton").addClass("loading").addClass("disabled");
  354. updateCertificateEAB(function(succ){
  355. if (succ){
  356. //Continue to next step
  357. updateCertificateDNS(function(succ){
  358. if (succ){
  359. obtainCertificate(function(succ){
  360. $("#obtainButton").removeClass("loading").removeClass("disabled");
  361. });
  362. }else{
  363. $("#obtainButton").removeClass("loading").removeClass("disabled");
  364. console.log("update Certificate DNS process halted");
  365. }
  366. });
  367. }else{
  368. console.log("Update Certificate EAB process halted");
  369. $("#obtainButton").removeClass("loading").removeClass("disabled");
  370. }
  371. });
  372. });
  373. //On CA change in dropdown
  374. $("input[name=ca]").on('change', function() {
  375. if(this.value == "Custom ACME Server") {
  376. $("#caInput").show();
  377. $("#kidInput").show();
  378. $("#hmacInput").show();
  379. $("#skipTLS").show();
  380. $("#dnsChallenge").hide();
  381. $(".dnsChallengeOnly").hide();
  382. } else if (this.value == "ZeroSSL") {
  383. $("#kidInput").show();
  384. $("#hmacInput").show();
  385. $("#dnsChallenge").hide();
  386. $(".dnsChallengeOnly").hide();
  387. $("#skipTLS").hide();
  388. } else if (this.value == "Buypass") {
  389. $("#kidInput").show();
  390. $("#hmacInput").show();
  391. $("#dnsChallenge").hide();
  392. $(".dnsChallengeOnly").hide();
  393. $("#skipTLS").hide();
  394. }else {
  395. $("#caInput").hide();
  396. $("#skipTLS").hide();
  397. $("#kidInput").hide();
  398. $("#hmacInput").hide();
  399. $("#dnsChallenge").show();
  400. if ($("#useDnsChallenge")[0].checked){
  401. $(".dnsChallengeOnly").show();
  402. }
  403. }
  404. })
  405. //On DNS provider dropdown change
  406. $("input[name=dnsProvider]").on('change', function() {
  407. let newProviderName = $("#dnsProvider").find("input").val();
  408. $.get("/api/acme/dns/providers?name=" + newProviderName, function(data){
  409. console.log("Loaded required config", data);
  410. $("#dnsProviderAPIFields").html("");
  411. //Generate a form for this config
  412. let booleanFieldsHTML = "";
  413. let optionalFieldsHTML = "";
  414. for (const [key, datatype] of Object.entries(data)) {
  415. if (datatype == "int"){
  416. let defaultValue = 10;
  417. if (key == "HTTPTimeout"){
  418. defaultValue = 300;
  419. }
  420. $("#dnsProviderAPIFields").append(`<div class="ui fluid labeled dnsConfigField input typeint" key="${key}" style="margin-top: 0.2em;">
  421. <div class="ui basic blue label" style="font-weight: 300;">
  422. ${key}
  423. </div>
  424. <input type="number" value="${defaultValue}">
  425. </div>`);
  426. }else if (datatype == "bool"){
  427. booleanFieldsHTML += (`<div class="ui checkbox dnsConfigField" key="${key}" style="margin-top: 1em !important; padding-left: 0.4em;">
  428. <input type="checkbox">
  429. <label>${key}</label>
  430. </div>`);
  431. }else if (datatype == "time.Duration"){
  432. let defaultIntValue = 120;
  433. let defaultMinValue = 30;
  434. if (key == "PollingInterval"){
  435. defaultIntValue = 2;
  436. defaultMinValue = 1;
  437. }else if (key == "PropagationTimeout"){
  438. defaultIntValue = 120;
  439. defaultMinValue = 30;
  440. }
  441. optionalFieldsHTML += (`<div class="ui fluid labeled dnsConfigField small input" key="${key}" style="margin-top: 0.2em;">
  442. <div class="ui basic blue label" style="font-weight: 300;">
  443. ${key}
  444. </div>
  445. <input type="number" min="${defaultMinValue}" value="${defaultIntValue}">
  446. <div class="ui basic label" style="font-weight: 300;">
  447. secs
  448. </div>
  449. </div>`);
  450. }else{
  451. //Default to string
  452. $("#dnsProviderAPIFields").append(`<div class="ui fluid labeled input dnsConfigField" key="${key}" style="margin-top: 0.2em;">
  453. <div class="ui basic label" style="font-weight: 300;">
  454. ${key}
  455. </div>
  456. <input type="text">
  457. </div>`);
  458. }
  459. }
  460. //Append the boolean fields at the bottom, if exists
  461. $("#dnsProviderAPIFields").append(booleanFieldsHTML);
  462. if (booleanFieldsHTML != ""){
  463. $(".dnsConfigField.checkbox").checkbox();
  464. }
  465. //Append the optional fields at the bottom, if exists
  466. $("#dnsProviderAPIFields").append(optionalFieldsHTML);
  467. });
  468. });
  469. // Get filename form domains and input
  470. function getFilename() {
  471. var domains = $("#domainsInput").val();
  472. var filename = $("#filenameInput").val();
  473. if (filename.trim() == "" && !domains.includes(",")){
  474. //Zoraxy filename are the matching name for domains.
  475. //Use the same as domains
  476. filename = domains;
  477. }else if (filename != "" && !domains.includes(",")){
  478. //Invalid settings. Force the filename to be same as domain
  479. //if there are only 1 domain
  480. filename = domains;
  481. }else if (filename == "" && domains.includes(",")){
  482. parent.msgbox("Filename cannot be empty for certs containing multiple domains.", false, 5000);
  483. $("#obtainButton").removeClass("loading").removeClass("disabled");
  484. return;
  485. }
  486. //Filename cannot contain wildcards, and wildcards are possible with DNS challenges
  487. filename = filename.replace("*", "_");
  488. return filename;
  489. }
  490. // Update EAB values for autorenewal
  491. function updateCertificateEAB(callback=undefined) {
  492. var ca = $("#ca").dropdown("get value");
  493. var caURL = "";
  494. if (ca == "Custom ACME Server") {
  495. ca = "custom";
  496. caURL = $("#caURL").val();
  497. }else if(ca == "Buypass") {
  498. caURL = "https://api.buypass.com/acme/directory";
  499. }else if(ca == "ZeroSSL") {
  500. caURL = "https://acme.zerossl.com/v2/DV90";
  501. }
  502. if(caURL == "") {
  503. //Skip update
  504. if (callback != undefined){
  505. callback(true);
  506. }
  507. return;
  508. }
  509. var kid = $("#eab_kid").val();
  510. var hmac = $("#eab_hmac").val();
  511. if(kid == "" || hmac == "") {
  512. //Skip update
  513. if (callback != undefined){
  514. callback(true);
  515. }
  516. return;
  517. }
  518. console.log(caURL + " " + kid + " " + hmac);
  519. $.ajax({
  520. url: "/api/acme/autoRenew/setEAB",
  521. method: "GET",
  522. data: {
  523. acmeDirectoryURL: caURL,
  524. kid: kid,
  525. hmacEncoded: hmac,
  526. },
  527. success: function(response) {
  528. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  529. if (response.error) {
  530. console.log("Error:", response.error);
  531. // Show error message
  532. parent.msgbox(response.error, false, 12000);
  533. if (callback != undefined){
  534. callback(false);
  535. }
  536. } else {
  537. console.log("Certificate EAB updated successfully");
  538. // Show success message
  539. parent.msgbox("Certificate EAB updated successfully");
  540. // Renew the parent certificate list
  541. parent.initManagedDomainCertificateList();
  542. if (callback != undefined){
  543. callback(true);
  544. }
  545. }
  546. },
  547. error: function(error) {
  548. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  549. console.log("Failed to update EAB configuration:", error);
  550. parent.msgbox("Failed to update EAB configuration");
  551. if (callback != undefined){
  552. callback(false);
  553. }
  554. }
  555. });
  556. }
  557. //Read DNS credential from form and generate a key value structure that looks like
  558. // the old DNSCredential TextArea input
  559. function readDnsCredentials(){
  560. let dnsCredentials = {};
  561. $(".dnsConfigField").each(function(){
  562. let thisKey = $(this).attr("key");
  563. let value = "";
  564. if ($(this).hasClass("checkbox")){
  565. //Boolean option
  566. let checked = $(this).find("input")[0].checked;
  567. dnsCredentials[thisKey] = checked;
  568. }else if ($(this).hasClass("typeint")){
  569. //Int options
  570. let value = $(this).find("input").val();
  571. dnsCredentials[thisKey] = parseInt(value);
  572. }else{
  573. //String options
  574. let value = $(this).find("input").val().trim();
  575. dnsCredentials[thisKey] = value;
  576. }
  577. });
  578. return dnsCredentials;
  579. }
  580. // Update DNS values for autorenewal
  581. function updateCertificateDNS(callback=undefined) {
  582. var dns = $("#useDnsChallenge")[0].checked;
  583. var dnsProvider = "";
  584. var dnsCredentials = "";
  585. if (!dns) {
  586. if (callback != undefined){
  587. callback(true);
  588. }
  589. return;
  590. }
  591. //Check if all fields is empty. If yes, do not update the config
  592. let allFieldsEmpty = true;
  593. $(".dnsConfigField").each(function(){
  594. if ($(this).find("input").val().trim() != ""){
  595. allFieldsEmpty = false;
  596. }
  597. });
  598. if (allFieldsEmpty){
  599. //Do not update config on server side
  600. if (callback != undefined){
  601. callback(true);
  602. }
  603. return;
  604. }
  605. dnsProvider = $("#dnsProvider").dropdown("get value");
  606. //dnsCredentials = $("#dnsCredentials").val();
  607. dnsCredentials = readDnsCredentials();
  608. if(dnsProvider == "") {
  609. parent.msgbox("DNS Provider cannot be empty", false, 5000);
  610. $("#obtainButton").removeClass("loading").removeClass("disabled");
  611. if (callback != undefined){
  612. callback(false);
  613. }
  614. return;
  615. }
  616. var filename = getFilename();
  617. if (filename == '') {
  618. parent.msgbox("Domain to renew cannot be empty", false, 5000);
  619. if (callback != undefined){
  620. callback(false);
  621. }
  622. return;
  623. }
  624. $.cjax({
  625. url: "/api/acme/autoRenew/setDNS",
  626. method: "POST",
  627. data: {
  628. filename: filename,
  629. dnsProvider: dnsProvider,
  630. dnsCredentials: JSON.stringify(dnsCredentials),
  631. },
  632. success: function(response) {
  633. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  634. if (response.error) {
  635. console.log("Error:", response.error);
  636. // Show error message
  637. parent.msgbox(response.error, false, 12000);
  638. if (callback != undefined){
  639. callback(false);
  640. }
  641. } else {
  642. console.log("Certificate DNS Credentials updated successfully");
  643. // Show success message
  644. parent.msgbox("Certificate DNS Credentials updated successfully");
  645. if (callback != undefined){
  646. callback(true);
  647. }
  648. }
  649. },
  650. error: function(error) {
  651. //$("#obtainButton").removeClass("loading").removeClass("disabled");
  652. console.log("Failed to update DNS configuration:", error);
  653. parent.msgbox("Failed to update DNS configuration");
  654. if (callback != undefined){
  655. callback(false);
  656. }
  657. }
  658. });
  659. }
  660. // Obtain certificate from API
  661. function obtainCertificate(callback=undefined) {
  662. var domains = $("#domainsInput").val();
  663. var filename = getFilename();
  664. if (filename == '') {
  665. if (callback != undefined){
  666. parent.msgbox("Domain to obtain certificate cannot be empty", false)
  667. callback(false);
  668. }
  669. return;
  670. }
  671. var email = $("#caRegisterEmail").val();
  672. if (email == ""){
  673. parent.msgbox("ACME renew email is not set", false)
  674. if (callback != undefined){callback(false);}
  675. return;
  676. }
  677. var ca = $("#ca").dropdown("get value");
  678. var caURL = "";
  679. if (ca == "Custom ACME Server") {
  680. ca = "custom";
  681. caURL = $("#caURL").val();
  682. }
  683. var dns = $("#useDnsChallenge")[0].checked;
  684. var skipTLSValue = $("#skipTLSCheckbox")[0].checked;
  685. var dnsServers = $("#dnsInput").val(); // Erfassen der DNS-Server
  686. $.ajax({
  687. url: "/api/acme/obtainCert",
  688. method: "GET",
  689. data: {
  690. domains: domains,
  691. filename: filename,
  692. email: email,
  693. ca: ca,
  694. caURL: caURL,
  695. skipTLS: skipTLSValue,
  696. dns: dns,
  697. dnsServers: dnsServers // DNS-Server in die Anfrage einfügen
  698. },
  699. success: function(response) {
  700. $("#obtainButton").removeClass("loading").removeClass("disabled");
  701. if (response.error) {
  702. console.log("Error:", response.error);
  703. // Show error message
  704. parent.msgbox(response.error, false, 12000);
  705. if (callback != undefined){callback(false);}
  706. } else {
  707. console.log("Certificate renewed successfully");
  708. // Show success message
  709. parent.msgbox("Certificate renewed successfully");
  710. // Renew the parent certificate list
  711. parent.initManagedDomainCertificateList();
  712. if (callback != undefined){callback(true);}
  713. }
  714. },
  715. error: function(error) {
  716. $("#obtainButton").removeClass("loading").removeClass("disabled");
  717. console.log("Failed to renewed certificate:", error);
  718. if (callback != undefined){callback(false);}
  719. }
  720. });
  721. }
  722. //Check if the entered domain contains multiple domains
  723. function checkIfInputDomainIsMultiple(){
  724. var inputDomains = $("#domainsInput").val();
  725. if (inputDomains.includes(",")){
  726. $(".multiDomainOnly").show();
  727. }else{
  728. $(".multiDomainOnly").hide();
  729. }
  730. }
  731. //Validate if the current combinations of domain and CA supports DNS challenge
  732. function validateDNSChallengeSupport(){
  733. if ($("#domainsInput").val().includes("*")){
  734. var ca = $("#ca").dropdown("get value");
  735. if (ca == "Let's Encrypt" || ca == ""){
  736. $("#caNoDNSSupportWarning").hide();
  737. }else{
  738. $("#caNoDNSSupportWarning").show();
  739. }
  740. }else{
  741. $("#caNoDNSSupportWarning").hide();
  742. }
  743. }
  744. //call to validateDNSChallengeSupport() on #ca value change
  745. $("#ca").dropdown({
  746. onChange: function(value, text, $selectedItem) {
  747. validateDNSChallengeSupport();
  748. }
  749. });
  750. //Handle the input change event on domain input
  751. function handlePostInputAutomation(){
  752. checkIfInputDomainIsMultiple();
  753. validateDNSChallengeSupport();
  754. }
  755. function toggleDnsChallenge(){
  756. if ( $("#useDnsChallenge")[0].checked){
  757. $(".dnsChallengeOnly").show();
  758. setTimeout(function(){
  759. $("#dnsProvider").dropdown("set text", "Cloudflare");
  760. }, 500);
  761. }else{
  762. $(".dnsChallengeOnly").hide();
  763. }
  764. }
  765. //Grab the longest common suffix of all domains
  766. //not that smart technically
  767. function autoDetectMatchingRules(){
  768. var domainsString = $("#domainsInput").val();
  769. if (!domainsString.includes(",")){
  770. return domainsString;
  771. }
  772. let domains = domainsString.split(",");
  773. //Clean out any spacing between commas
  774. for (var i = 0; i < domains.length; i++){
  775. domains[i] = domains[i].trim();
  776. }
  777. function getLongestCommonSuffix(strings) {
  778. if (strings.length === 0) {
  779. return ''; // Return an empty string if the array is empty
  780. }
  781. var sortedStrings = strings.slice().sort(); // Create a sorted copy of the array
  782. var firstString = sortedStrings[0];
  783. var lastString = sortedStrings[sortedStrings.length - 1];
  784. var suffix = '';
  785. var minLength = Math.min(firstString.length, lastString.length);
  786. for (var i = 0; i < minLength; i++) {
  787. if (firstString[firstString.length - 1 - i] !== lastString[lastString.length - 1 - i]) {
  788. break; // Stop iterating if characters don't match
  789. }
  790. suffix = firstString[firstString.length - 1 - i] + suffix;
  791. }
  792. return suffix;
  793. }
  794. let longestSuffix = getLongestCommonSuffix(domains);
  795. //Check if the suffix is a valid domain
  796. if (longestSuffix.substr(0,1) == "."){
  797. //Trim off the first dot
  798. longestSuffix = longestSuffix.substr(1);
  799. }
  800. if (!longestSuffix.includes(".")){
  801. parent.msgbox("Auto Detect failed: Multiple Domains", false, 5000);
  802. return;
  803. }
  804. $("#filenameInput").val(longestSuffix);
  805. }
  806. //Handle the renew now btn click
  807. function renewNow(){
  808. $.get("/api/acme/autoRenew/renewNow", function(data){
  809. if (data.error != undefined){
  810. parent.msgbox(data.error, false, 6000);
  811. }else{
  812. parent.msgbox(data)
  813. }
  814. })
  815. }
  816. function initAutoRenewPolicy(){
  817. $.get("/api/acme/autoRenew/listDomains", function(data){
  818. if (data.error != undefined){
  819. parent.msgbox(data.error, false)
  820. }else{
  821. if (data[0] == "*"){
  822. //Auto select and renew is enabled
  823. $("#renewAllSupported").parent().checkbox("set checked");
  824. }else{
  825. //This is a list of domain files
  826. data.forEach(function(name) {
  827. $('#domainTableBody input[type="checkbox"][name="' + name + '"]').prop('checked', true);
  828. });
  829. $("#domainCertFileTable").removeClass("disabled");
  830. $("#renewNowBtn").addClass("disabled");
  831. $("#renewSelectedButton").removeClass("disabled");
  832. }
  833. }
  834. })
  835. }
  836. function saveAutoRenewPolicy(){
  837. let autoRenewAll = $("#renewAllSupported").parent().checkbox("is checked");
  838. if (autoRenewAll == true){
  839. $.cjax({
  840. url: "/api/acme/autoRenew/setDomains",
  841. method: "POST",
  842. data: {opr: "setAuto"},
  843. success: function(data){
  844. parent.msgbox("Renew policy rule updated")
  845. }
  846. });
  847. }else{
  848. let checkedNames = [];
  849. $('#domainTableBody input[type="checkbox"]:checked').each(function() {
  850. checkedNames.push($(this).attr('name'));
  851. });
  852. $.cjax({
  853. url: "/api/acme/autoRenew/setDomains",
  854. method: "POST",
  855. data: {opr: "setSelected", domains: JSON.stringify(checkedNames)},
  856. success: function(data){
  857. parent.msgbox("Renew policy rule updated")
  858. }
  859. });
  860. }
  861. }
  862. //Load the json map and create the dropdown for DNS provider names
  863. let dnsProviderNameMap = {};
  864. function initDNSProviderList(){
  865. $.get("dnsnames.json", function(namemap){
  866. dnsProviderNameMap = namemap;
  867. //Load a list of supported DNS provider from backend
  868. $("#dnsProviderList").html("");
  869. $.get("/api/acme/dns/providers", function(providerList){
  870. providerList.sort();
  871. providerList.forEach(providerid => {
  872. let providerName = providerid;
  873. if (dnsProviderNameMap[providerid] != undefined){
  874. providerName = dnsProviderNameMap[providerid];
  875. }
  876. $("#dnsProviderList").append(`<div class="item" data-value="${providerid}">${providerName}</div>`);
  877. });
  878. $("#dnsProvider").dropdown();
  879. setTimeout(function(){
  880. //The dropdown is large, it takes some time to load
  881. $("#dnsProvider").dropdown("set selected", "cloudflare");
  882. }, 300)
  883. });
  884. });
  885. }
  886. initDNSProviderList();
  887. //Clear up the input field when page load
  888. $("#filenameInput").val("");
  889. </script>
  890. </body>
  891. </html>