1
0

acme.html 36 KB

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