acme.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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();">
  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);" checked>
  57. <label>Auto renew if CA is supported</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"><i class="blue save icon"></i> Save Changes</button>
  77. <button id="renewSelectedButton" onclick="renewNow();" class="ui basic right floated disabled button"><i class="yellow refresh icon"></i> Renew Selected</button>
  78. <br><br>
  79. </div>
  80. </div>
  81. </div>
  82. <div class="ui divider"></div>
  83. <h3>Manual Renew</h3>
  84. <p>Pick a certificate below to force renew</p>
  85. <div class="ui form">
  86. <div class="field">
  87. <label>Domain(s)</label>
  88. <input id="domainsInput" type="text" placeholder="example.com" onkeyup="checkIfInputDomainIsMultiple();">
  89. <small>If you have more than one domain in a single certificate, enter the domains separated by commas (e.g. s1.dev.example.com,s2.dev.example.com)</small>
  90. </div>
  91. <div class="field multiDomainOnly" style="display:none;">
  92. <label>Matching Rule</label>
  93. <input id="filenameInput" type="text" placeholder="Enter filename (no file extension)">
  94. <small>Matching rule to let Zoraxy pick which certificate to use (Also be used as filename). Usually is the longest common suffix of the entered addresses. (e.g. dev.example.com)</small>
  95. </div>
  96. <div class="field multiDomainOnly" style="display:none;">
  97. <button class="ui basic fluid button" onclick="autoDetectMatchingRules();">Auto Detect Matching Rule</button>
  98. </div>
  99. <div class="field">
  100. <label>Certificate Authority (CA)</label>
  101. <div class="ui selection dropdown" id="ca">
  102. <input type="hidden" name="ca">
  103. <i class="dropdown icon"></i>
  104. <div class="default text">Let's Encrypt</div>
  105. <div class="menu">
  106. <div class="item" data-value="Let's Encrypt">Let's Encrypt</div>
  107. <div class="item" data-value="Buypass">Buypass</div>
  108. <div class="item" data-value="ZeroSSL">ZeroSSL</div>
  109. <!-- <div class="item" data-value="Google">Google</div> -->
  110. </div>
  111. </div>
  112. </div>
  113. <button id="obtainButton" class="ui basic button" type="submit"><i class="yellow refresh icon"></i> Renew Certificate</button>
  114. </div>
  115. <button class="ui basic button" style="float: right;" onclick="parent.hideSideWrapper();"><i class="remove icon"></i> Cancel</button>
  116. <br><br><br><br>
  117. </div>
  118. <script>
  119. let expiredDomains = [];
  120. $(".accordion").accordion();
  121. $(".dropdown").dropdown();
  122. function setAutoRenewIfCASupportMode(useAutoMode = true){
  123. if (useAutoMode){
  124. $("#domainCertFileTable").addClass("disabled");
  125. $("#renewNowBtn").removeClass("disabled");
  126. $("#renewSelectedButton").addClass("disabled");
  127. }else{
  128. $("#domainCertFileTable").removeClass("disabled");
  129. $("#renewNowBtn").addClass("disabled");
  130. $("#renewSelectedButton").removeClass("disabled");
  131. }
  132. }
  133. function initRenewerConfigFromFile(){
  134. $.get("/api/acme/autoRenew/enable", function(data){
  135. if (data == true){
  136. $("#enableCertAutoRenew").parent().checkbox("set checked");
  137. }
  138. $("#enableCertAutoRenew").on("change", function(){
  139. toggleAutoRenew();
  140. })
  141. });
  142. $.get("/api/acme/autoRenew/email", function(data){
  143. if (data != "" && data != undefined && data != null){
  144. $("#caRegisterEmail").val(data);
  145. }
  146. });
  147. }
  148. initRenewerConfigFromFile();
  149. function saveEmailToConfig(){
  150. $.ajax({
  151. url: "/api/acme/autoRenew/email",
  152. data: {set: $("#caRegisterEmail").val()},
  153. success: function(data){
  154. if (data.error != undefined){
  155. parent.msgbox(data.error, false, 5000);
  156. }else{
  157. parent.msgbox("Email updated");
  158. }
  159. }
  160. });
  161. }
  162. function toggleAutoRenew(){
  163. var enabled = $("#enableCertAutoRenew").parent().checkbox("is checked");
  164. $.post("/api/acme/autoRenew/enable?enable=" + enabled, function(data){
  165. if (data.error){
  166. parent.msgbox(data.error, false, 5000);
  167. }else{
  168. $("#enableToggleSucc").stop().finish().fadeIn("fast").delay(3000).fadeOut("fast");
  169. }
  170. });
  171. }
  172. //Render the domains table that exists in this zoraxy host
  173. function renderDomainTable(domainFileList) {
  174. // Get the table body element
  175. var tableBody = $('#domainTableBody');
  176. // Clear the table body
  177. tableBody.empty();
  178. // Iterate over the domain names
  179. var counter = 0;
  180. for (const [srcfile, domains] of Object.entries(domainFileList)) {
  181. // Create a table row
  182. var row = $('<tr>');
  183. // Create the domain name cell
  184. var domainClass = "validDomain";
  185. for (var i = 0; i < domains.length; i++){
  186. let thisDomain = domains[i];
  187. if (expiredDomains.includes(thisDomain)){
  188. domainClass = "expiredDomain";
  189. }
  190. }
  191. var domainCell = $('<td class="' + domainClass +'">').html(domains.join("<br>"));
  192. row.append(domainCell);
  193. var srcFileCell = $('<td>').text(srcfile);
  194. row.append(srcFileCell);
  195. // Create the auto-renew checkbox cell
  196. let domainsEncoded = encodeURIComponent(JSON.stringify(domains));
  197. var checkboxCell = $(`<td domain="${domainsEncoded}" srcfile="${srcfile}">`);
  198. var checkbox = $(`<input name="${srcfile}">`).attr('type', 'checkbox');
  199. checkboxCell.append(checkbox);
  200. row.append(checkboxCell);
  201. // Add the row to the table body
  202. tableBody.append(row);
  203. counter++;
  204. }
  205. }
  206. //Initiate domain table. If you needs to update the expired domain as well
  207. //call from initDomainFileList() instead
  208. function initDomainTable(){
  209. $.get("/api/cert/listdomains?compact=true", function(data){
  210. if (data.error != undefined){
  211. parent.msgbox(data.error, false);
  212. }else{
  213. renderDomainTable(data);
  214. }
  215. initAutoRenewPolicy();
  216. })
  217. }
  218. function initDomainFileList() {
  219. $.ajax({
  220. url: "/api/acme/listExpiredDomains",
  221. method: "GET",
  222. success: function(response) {
  223. // Render domain table
  224. expiredDomains = response.domain;
  225. initDomainTable();
  226. //renderDomainTable(response.domain);
  227. },
  228. error: function(error) {
  229. console.log("Failed to fetch expired domains:", error);
  230. }
  231. });
  232. }
  233. initDomainFileList();
  234. // Button click event handler for obtaining certificate
  235. $("#obtainButton").click(function() {
  236. $("#obtainButton").addClass("loading").addClass("disabled");
  237. obtainCertificate();
  238. });
  239. // Obtain certificate from API
  240. function obtainCertificate() {
  241. var domains = $("#domainsInput").val();
  242. var filename = $("#filenameInput").val();
  243. if (filename.trim() == "" && !domains.includes(",")){
  244. //Zoraxy filename are the matching name for domains.
  245. //Use the same as domains
  246. filename = domains;
  247. }else if (filename != "" && !domains.includes(",")){
  248. //Invalid settings. Force the filename to be same as domain
  249. //if there are only 1 domain
  250. filename = domains;
  251. }else{
  252. parent.msgbox("Filename cannot be empty for certs containing multiple domains.")
  253. return;
  254. }
  255. var ca = $("#ca").dropdown("get value");
  256. $.ajax({
  257. url: "/api/acme/obtainCert",
  258. method: "GET",
  259. data: {
  260. domains: domains,
  261. filename: filename,
  262. ca: ca,
  263. },
  264. success: function(response) {
  265. $("#obtainButton").removeClass("loading").removeClass("disabled");
  266. if (response.error) {
  267. console.log("Error:", response.error);
  268. // Show error message
  269. parent.msgbox(response.error, false, 12000);
  270. } else {
  271. console.log("Certificate renewed successfully");
  272. // Show success message
  273. parent.msgbox("Certificate renewed successfully");
  274. // Renew the parent certificate list
  275. parent.initManagedDomainCertificateList();
  276. }
  277. },
  278. error: function(error) {
  279. $("#obtainButton").removeClass("loading").removeClass("disabled");
  280. console.log("Failed to renewed certificate:", error);
  281. }
  282. });
  283. }
  284. function checkIfInputDomainIsMultiple(){
  285. var inputDomains = $("#domainsInput").val();
  286. if (inputDomains.includes(",")){
  287. $(".multiDomainOnly").show();
  288. }else{
  289. $(".multiDomainOnly").hide();
  290. }
  291. }
  292. //Grab the longest common suffix of all domains
  293. //not that smart technically
  294. function autoDetectMatchingRules(){
  295. var domainsString = $("#domainsInput").val();
  296. if (!domainsString.includes(",")){
  297. return domainsString;
  298. }
  299. let domains = domainsString.split(",");
  300. //Clean out any spacing between commas
  301. for (var i = 0; i < domains.length; i++){
  302. domains[i] = domains[i].trim();
  303. }
  304. function getLongestCommonSuffix(strings) {
  305. if (strings.length === 0) {
  306. return ''; // Return an empty string if the array is empty
  307. }
  308. var sortedStrings = strings.slice().sort(); // Create a sorted copy of the array
  309. var firstString = sortedStrings[0];
  310. var lastString = sortedStrings[sortedStrings.length - 1];
  311. var suffix = '';
  312. var minLength = Math.min(firstString.length, lastString.length);
  313. for (var i = 0; i < minLength; i++) {
  314. if (firstString[firstString.length - 1 - i] !== lastString[lastString.length - 1 - i]) {
  315. break; // Stop iterating if characters don't match
  316. }
  317. suffix = firstString[firstString.length - 1 - i] + suffix;
  318. }
  319. return suffix;
  320. }
  321. let longestSuffix = getLongestCommonSuffix(domains);
  322. //Check if the suffix is a valid domain
  323. if (longestSuffix.substr(0,1) == "."){
  324. //Trim off the first dot
  325. longestSuffix = longestSuffix.substr(1);
  326. }
  327. if (!longestSuffix.includes(".")){
  328. parent.msgbox("Auto Detect failed: Multiple Domains", false, 5000);
  329. return;
  330. }
  331. $("#filenameInput").val(longestSuffix);
  332. }
  333. //Handle the renew now btn click
  334. function renewNow(){
  335. alert("wip");
  336. return
  337. $.get("/api/acme/autoRenew/renewNow", function(data){
  338. alert(data);
  339. })
  340. }
  341. function initAutoRenewPolicy(){
  342. $.get("/api/acme/autoRenew/listDomains", function(data){
  343. if (data.error != undefined){
  344. parent.msgbox(data.error, false)
  345. }else{
  346. if (data[0] == "*"){
  347. //Auto select and renew is enabled
  348. $("#renewAllSupported").parent().checkbox("set checked");
  349. }else{
  350. //This is a list of domain files
  351. }
  352. }
  353. })
  354. }
  355. function saveAutoRenewPolicy(){
  356. let autoRenewAll = $("#renewAllSupported").parent().checkbox("is checked");
  357. if (autoRenewAll == true){
  358. }else{
  359. }
  360. }
  361. //Clear up the input field when page load
  362. $("#filenameInput").val("");
  363. </script>
  364. </body>
  365. </html>