cert.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <style>
  2. .expired.certdate{
  3. font-weight: bolder;
  4. color: #bd001c;
  5. }
  6. .valid.certdate{
  7. color: #31c071;
  8. }
  9. </style>
  10. <div class="standardContainer">
  11. <div class="ui basic segment">
  12. <h2>TLS / SSL Certificates</h2>
  13. <p>Setup TLS cert for different domains of your reverse proxy server names</p>
  14. </div>
  15. <div class="ui divider"></div>
  16. <h4>Default Certificates</h4>
  17. <p>When there are no matching certificate for the requested server name, reverse proxy router will always fallback to this one.<br>Note that you need both of them uploaded for it to fallback properly</p>
  18. <table class="ui very basic unstackable celled table">
  19. <thead>
  20. <tr><th class="no-sort">Key Type</th>
  21. <th class="no-sort">Exists</th>
  22. </tr></thead>
  23. <tbody>
  24. <tr>
  25. <td><i class="globe icon"></i> Default Public Key</td>
  26. <td id="pubkeyExists"></td>
  27. </tr>
  28. <tr>
  29. <td><i class="lock icon"></i> Default Private Key</td>
  30. <td id="prikeyExists"></td>
  31. </tr>
  32. </tbody>
  33. </table>
  34. <p style="margin-bottom: 0.4em;"><i class="ui upload icon"></i> Upload Default Keypairs</p>
  35. <div class="ui buttons">
  36. <button class="ui basic grey button" onclick="uploadPublicKey();"><i class="globe icon"></i> Public Key</button>
  37. <button class="ui basic black button" onclick="uploadPrivateKey();"><i class="black lock icon"></i> Private Key</button>
  38. </div>
  39. <div class="ui divider"></div>
  40. <h4>Sub-domain Certificates</h4>
  41. <p>Provide certificates for multiple domains reverse proxy</p>
  42. <div class="ui fluid form">
  43. <div class="three fields">
  44. <div class="field">
  45. <label>Server Name (Domain)</label>
  46. <input type="text" id="certdomain" placeholder="example.com / blog.example.com">
  47. </div>
  48. <div class="field">
  49. <label>Public Key (.pem)</label>
  50. <input type="file" id="pubkeySelector" onchange="handleFileSelect(event, 'pub')">
  51. </div>
  52. <div class="field">
  53. <label>Private Key (.key)</label>
  54. <input type="file" id="prikeySelector" onchange="handleFileSelect(event, 'pri')">
  55. </div>
  56. </div>
  57. <button class="ui basic button" onclick="handleDomainUploadByKeypress();"><i class="ui teal upload icon"></i> Upload</button><br>
  58. <small>You have intermediate certificate? <a style="cursor:pointer;" onclick="showSideWrapper('snippet/intermediateCertConv.html');">Open Conversion Tool</a></small>
  59. </div>
  60. <div id="certUploadSuccMsg" class="ui green message" style="display:none;">
  61. <i class="ui checkmark icon"></i> Certificate for domain <span id="certUploadingDomain"></span> uploaded.
  62. </div>
  63. <br>
  64. <div>
  65. <div style="width: 100%; overflow-x: auto; margin-bottom: 1em;">
  66. <table class="ui sortable unstackable celled table">
  67. <thead>
  68. <tr><th>Domain</th>
  69. <th>Last Update</th>
  70. <th>Expire At</th>
  71. <th class="no-sort">Remove</th>
  72. </tr></thead>
  73. <tbody id="certifiedDomainList">
  74. </tbody>
  75. </table>
  76. </div>
  77. <button class="ui basic button" onclick="initManagedDomainCertificateList();"><i class="green refresh icon"></i> Refresh List</button>
  78. </div>
  79. <div class="ui message">
  80. <h4><i class="info circle icon"></i> Sub-domain Certificates</h4>
  81. If you have 3rd or even 4th level subdomains like <code>blog.example.com</code> or <code>en.blog.example.com</code> ,
  82. depending on your certificates coverage, you might need to setup them one by one (i.e. having two seperate certificate for <code>a.example.com</code> and <code>b.example.com</code>).<br>
  83. If you have a wildcard certificate that covers <code>*.example.com</code>, you can just enter <code>example.com</code> as server name in the form below to add a certificate.
  84. </div>
  85. <div class="ui divider"></div>
  86. <h4>Certificate Authority (CA) and Auto Renew (ACME)</h4>
  87. <p>Management features regarding CA and ACME</p>
  88. <p>The default CA to use when create a new subdomain proxy endpoint with TLS certificate</p>
  89. <div class="ui fluid form">
  90. <div class="field">
  91. <label>Preferred CA</label>
  92. <div class="ui selection dropdown" id="defaultCA">
  93. <input type="hidden" name="defaultCA">
  94. <i class="dropdown icon"></i>
  95. <div class="default text">Let's Encrypt</div>
  96. <div class="menu">
  97. <div class="item" data-value="Let's Encrypt">Let's Encrypt</div>
  98. <div class="item" data-value="Buypass">Buypass</div>
  99. <div class="item" data-value="ZeroSSL">ZeroSSL</div>
  100. </div>
  101. </div>
  102. </div>
  103. <div class="field">
  104. <label>ACME Email</label>
  105. <input id="prefACMEEmail" type="text" placeholder="ACME Email">
  106. </div>
  107. <button class="ui basic icon button" onclick="saveDefaultCA();"><i class="ui blue save icon"></i> Save Settings</button>
  108. </div><br>
  109. <h5>Certificate Renew / Generation (ACME) Settings</h5>
  110. <div class="ui basic segment">
  111. <h4 class="ui header" id="acmeAutoRenewer">
  112. <i class="red circle icon"></i>
  113. <div class="content">
  114. <span id="acmeAutoRenewerStatus">Disabled</span>
  115. <div class="sub header">Auto-Renewer Status</div>
  116. </div>
  117. </h4>
  118. </div>
  119. <p>This tool provide you a graphical interface to setup auto certificate renew on your (sub)domains. You can also manually generate a certificate if one of your domain do not have certificate.</p>
  120. <button class="ui basic button" onclick="openACMEManager();"><i class="yellow external icon"></i> Open ACME Tool</button>
  121. </div>
  122. <script>
  123. var uploadPendingPublicKey = undefined;
  124. var uploadPendingPrivateKey = undefined;
  125. $("#defaultCA").dropdown();
  126. //Delete the certificate by its domain
  127. function deleteCertificate(domain){
  128. if (confirm("Confirm delete certificate for " + domain + " ?")){
  129. $.ajax({
  130. url: "/api/cert/delete",
  131. method: "POST",
  132. data: {domain: domain},
  133. success: function(data){
  134. if (data.error != undefined){
  135. msgbox(data.error, false, 5000);
  136. }else{
  137. initManagedDomainCertificateList();
  138. initDefaultKeypairCheck();
  139. }
  140. }
  141. });
  142. }
  143. }
  144. function initAcmeStatus(){
  145. //Initialize the current default CA options
  146. $.get("/api/acme/autoRenew/email", function(data){
  147. $("#prefACMEEmail").val(data);
  148. });
  149. $.get("/api/acme/autoRenew/ca", function(data){
  150. $("#defaultCA").dropdown("set value", data);
  151. });
  152. $.get("/api/acme/autoRenew/enable", function(data){
  153. setACMEEnableStates(data);
  154. })
  155. }
  156. //Set the status of the acme enable icon
  157. function setACMEEnableStates(enabled){
  158. $("#acmeAutoRenewerStatus").text(enabled?"Enabled":"Disabled");
  159. $("#acmeAutoRenewer").find("i").attr("class", enabled?"green circle icon":"red circle icon");
  160. }
  161. initAcmeStatus();
  162. function saveDefaultCA(){
  163. let newDefaultEmail = $("#prefACMEEmail").val().trim();
  164. let newDefaultCA = $("#defaultCA").dropdown("get value");
  165. if (newDefaultEmail == ""){
  166. msgbox("Invalid acme email given", false);
  167. return;
  168. }
  169. $.ajax({
  170. url: "/api/acme/autoRenew/email",
  171. method: "POST",
  172. data: {"set": newDefaultEmail},
  173. success: function(data){
  174. if (data.error != undefined){
  175. msgbox(data.error, false);
  176. }
  177. }
  178. });
  179. $.ajax({
  180. url: "/api/acme/autoRenew/ca",
  181. data: {"set": newDefaultCA},
  182. method: "POST",
  183. success: function(data){
  184. if (data.error != undefined){
  185. msgbox(data.error, false);
  186. }
  187. }
  188. });
  189. msgbox("Settings updated");
  190. }
  191. //List the stored certificates
  192. function initManagedDomainCertificateList(){
  193. $.get("/api/cert/list?date=true", function(data){
  194. if (data.error != undefined){
  195. msgbox(data.error, false, 5000);
  196. }else{
  197. $("#certifiedDomainList").html("");
  198. data.sort((a,b) => {
  199. return a.Domain > b.Domain
  200. });
  201. data.forEach(entry => {
  202. let isExpired = entry.RemainingDays <= 0;
  203. $("#certifiedDomainList").append(`<tr>
  204. <td>${entry.Domain}</td>
  205. <td>${entry.LastModifiedDate}</td>
  206. <td class="${isExpired?"expired":"valid"} certdate">${entry.ExpireDate} (${!isExpired?entry.RemainingDays+" days left":"Expired"})</td>
  207. <td><button title="Delete key-pair" class="ui mini basic red icon button" onclick="deleteCertificate('${entry.Domain}');"><i class="ui red trash icon"></i></button></td>
  208. </tr>`);
  209. });
  210. if (data.length == 0){
  211. $("#certifiedDomainList").append(`<tr>
  212. <td colspan="4"><i class="ui times circle icon"></i> No valid keypairs found</td>
  213. </tr>`);
  214. }
  215. }
  216. })
  217. }
  218. initManagedDomainCertificateList();
  219. function openACMEManager(){
  220. showSideWrapper('snippet/acme.html');
  221. }
  222. function handleDomainUploadByKeypress(){
  223. handleDomainKeysUpload(function(){
  224. $("#certUploadingDomain").text($("#certdomain").val().trim());
  225. //After uploaded, reset the file selector
  226. document.getElementById('pubkeySelector').value = '';
  227. document.getElementById('prikeySelector').value = '';
  228. document.getElementById('certdomain').value = '';
  229. uploadPendingPublicKey = undefined;
  230. uploadPendingPrivateKey = undefined;
  231. //Show succ
  232. $("#certUploadSuccMsg").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  233. initManagedDomainCertificateList();
  234. });
  235. }
  236. //Handle domain keys upload
  237. function handleDomainKeysUpload(callback=undefined){
  238. let domain = $("#certdomain").val();
  239. if (domain.trim() == ""){
  240. msgbox("Missing domain", false, 5000);
  241. return;
  242. }
  243. if (uploadPendingPublicKey && uploadPendingPrivateKey && typeof uploadPendingPublicKey === 'object' && typeof uploadPendingPrivateKey === 'object') {
  244. const publicKeyForm = new FormData();
  245. publicKeyForm.append('file', uploadPendingPublicKey, 'publicKey');
  246. const privateKeyForm = new FormData();
  247. privateKeyForm.append('file', uploadPendingPrivateKey, 'privateKey');
  248. const publicKeyRequest = new XMLHttpRequest();
  249. publicKeyRequest.open('POST', '/api/cert/upload?ktype=pub&domain=' + domain);
  250. publicKeyRequest.onreadystatechange = function() {
  251. if (publicKeyRequest.readyState === XMLHttpRequest.DONE) {
  252. if (publicKeyRequest.status !== 200) {
  253. msgbox('Error uploading public key: ' + publicKeyRequest.statusText, false, 5000);
  254. }
  255. if (callback != undefined){
  256. callback();
  257. }
  258. }
  259. };
  260. publicKeyRequest.send(publicKeyForm);
  261. const privateKeyRequest = new XMLHttpRequest();
  262. privateKeyRequest.open('POST', '/api/cert/upload?ktype=pri&domain=' + domain);
  263. privateKeyRequest.onreadystatechange = function() {
  264. if (privateKeyRequest.readyState === XMLHttpRequest.DONE) {
  265. if (privateKeyRequest.status !== 200) {
  266. msgbox('Error uploading private key: ' + privateKeyRequest.statusText, false, 5000);
  267. }
  268. if (callback != undefined){
  269. callback();
  270. }
  271. }
  272. };
  273. privateKeyRequest.send(privateKeyForm);
  274. } else {
  275. msgbox('One or both of the files is missing or not a file object');
  276. }
  277. }
  278. //Handlers for selecting domain based key pairs
  279. //ktype = {"pub" / "pri"}
  280. function handleFileSelect(event, ktype="pub") {
  281. const file = event.target.files[0];
  282. //const fileNameInput = document.getElementById('selected-file-name');
  283. if (ktype == "pub"){
  284. uploadPendingPublicKey = file;
  285. }else if (ktype == "pri"){
  286. uploadPendingPrivateKey = file;
  287. }
  288. //fileNameInput.value = file.name;
  289. }
  290. //Check if the default keypairs exists
  291. function initDefaultKeypairCheck(){
  292. $.get("/api/cert/checkDefault", function(data){
  293. let tick = `<i class="ui green checkmark icon"></i>`;
  294. let cross = `<i class="ui red times icon"></i>`;
  295. $("#pubkeyExists").html(data.DefaultPubExists?tick:cross);
  296. $("#prikeyExists").html(data.DefaultPriExists?tick:cross);
  297. });
  298. }
  299. initDefaultKeypairCheck();
  300. function uploadPrivateKey(){
  301. // create file input element
  302. const input = document.createElement('input');
  303. input.type = 'file';
  304. // add change listener to file input
  305. input.addEventListener('change', () => {
  306. // create form data object
  307. const formData = new FormData();
  308. // add selected file to form data
  309. formData.append('file', input.files[0]);
  310. // send form data to server
  311. fetch('/api/cert/upload?ktype=pri', {
  312. method: 'POST',
  313. body: formData
  314. })
  315. .then(response => {
  316. initDefaultKeypairCheck();
  317. if (response.ok) {
  318. msgbox('File upload successful!');
  319. } else {
  320. response.text().then(text => {
  321. msgbox(text, false, 5000);
  322. });
  323. //console.log(response.text());
  324. //alert('File upload failed!');
  325. }
  326. })
  327. .catch(error => {
  328. msgbox('An error occurred while uploading the file.', false, 5000);
  329. console.error(error);
  330. });
  331. });
  332. // click file input to open file selector
  333. input.click();
  334. }
  335. function uploadPublicKey() {
  336. // create file input element
  337. const input = document.createElement('input');
  338. input.type = 'file';
  339. // add change listener to file input
  340. input.addEventListener('change', () => {
  341. // create form data object
  342. const formData = new FormData();
  343. // add selected file to form data
  344. formData.append('file', input.files[0]);
  345. // send form data to server
  346. fetch('/api/cert/upload?ktype=pub', {
  347. method: 'POST',
  348. body: formData
  349. })
  350. .then(response => {
  351. if (response.ok) {
  352. msgbox('File upload successful!');
  353. initDefaultKeypairCheck();
  354. } else {
  355. response.text().then(text => {
  356. msgbox(text, false, 5000);
  357. });
  358. //console.log(response.text());
  359. //alert('File upload failed!');
  360. }
  361. })
  362. .catch(error => {
  363. msgbox('An error occurred while uploading the file.', false, 5000);
  364. console.error(error);
  365. });
  366. });
  367. // click file input to open file selector
  368. input.click();
  369. }
  370. </script>