cert.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>TLS / SSL Certificates</h2>
  4. <p>Setup TLS cert for different domains of your reverse proxy server names</p>
  5. </div>
  6. <div class="ui divider"></div>
  7. <h4>Default Certificates</h4>
  8. <small>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</small></p>
  9. <table class="ui very basic unstackable celled table">
  10. <thead>
  11. <tr><th class="no-sort">Key Type</th>
  12. <th class="no-sort">Exists</th>
  13. </tr></thead>
  14. <tbody>
  15. <tr>
  16. <td><i class="globe icon"></i> Default Public Key</td>
  17. <td id="pubkeyExists"></td>
  18. </tr>
  19. <tr>
  20. <td><i class="lock icon"></i> Default Private Key</td>
  21. <td id="prikeyExists"></td>
  22. </tr>
  23. </tbody>
  24. </table>
  25. <p style="margin-bottom: 0.4em;"><i class="ui upload icon"></i> Upload Default Keypairs</p>
  26. <div class="ui buttons">
  27. <button class="ui basic grey button" onclick="uploadPublicKey();"><i class="globe icon"></i> Public Key</button>
  28. <button class="ui basic black button" onclick="uploadPrivateKey();"><i class="black lock icon"></i> Private Key</button>
  29. </div>
  30. <div class="ui divider"></div>
  31. <h4>Sub-domain Certificates</h4>
  32. <p>Provide certificates for multiple domains reverse proxy</p>
  33. <div class="ui fluid form">
  34. <div class="three fields">
  35. <div class="field">
  36. <label>Server Name (Domain)</label>
  37. <input type="text" id="certdomain" placeholder="example.com / blog.example.com">
  38. </div>
  39. <div class="field">
  40. <label>Public Key</label>
  41. <input type="file" id="pubkeySelector" onchange="handleFileSelect(event, 'pub')">
  42. </div>
  43. <div class="field">
  44. <label>Private Key</label>
  45. <input type="file" id="prikeySelector" onchange="handleFileSelect(event, 'pri')">
  46. </div>
  47. </div>
  48. <button class="ui basic button" onclick="handleDomainUploadByKeypress();"><i class="ui teal upload icon"></i> Upload</button>
  49. </div>
  50. <div id="certUploadSuccMsg" class="ui green message" style="display:none;">
  51. <i class="ui checkmark icon"></i> Certificate for domain <span id="certUploadingDomain"></span> uploaded.
  52. </div>
  53. <br>
  54. <div>
  55. <table class="ui sortable unstackable celled table">
  56. <thead>
  57. <tr><th>Domain</th>
  58. <th>Last Update</th>
  59. <th>Expire At</th>
  60. <th class="no-sort">Remove</th>
  61. </tr></thead>
  62. <tbody id="certifiedDomainList">
  63. </tbody>
  64. </table>
  65. <button class="ui basic button" onclick="initManagedDomainCertificateList();"><i class="green refresh icon"></i> Refresh List</button>
  66. </div>
  67. <div class="ui message">
  68. <h4><i class="info circle icon"></i> Sub-domain Certificates</h4>
  69. If you have 3rd or even 4th level subdomains like <code>blog.example.com</code> or <code>en.blog.example.com</code> ,
  70. 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>
  71. 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.
  72. </div>
  73. </div>
  74. <script>
  75. var uploadPendingPublicKey = undefined;
  76. var uploadPendingPrivateKey = undefined;
  77. //Delete the certificate by its domain
  78. function deleteCertificate(domain){
  79. if (confirm("Confirm delete certificate for " + domain + " ?")){
  80. $.ajax({
  81. url: "/api/cert/delete",
  82. method: "POST",
  83. data: {domain: domain},
  84. success: function(data){
  85. if (data.error != undefined){
  86. msgbox(data.error, false, 5000);
  87. }else{
  88. initManagedDomainCertificateList();
  89. initDefaultKeypairCheck();
  90. }
  91. }
  92. });
  93. }
  94. }
  95. //List the stored certificates
  96. function initManagedDomainCertificateList(){
  97. $("#certifiedDomainList").html("");
  98. $.get("/api/cert/list?date=true", function(data){
  99. if (data.error != undefined){
  100. msgbox(data.error, false, 5000);
  101. }else{
  102. data.forEach(entry => {
  103. $("#certifiedDomainList").append(`<tr>
  104. <td>${entry.Domain}</td>
  105. <td>${entry.LastModifiedDate}</td>
  106. <td>${entry.ExpireDate}</td>
  107. <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>
  108. </tr>`);
  109. })
  110. }
  111. })
  112. }
  113. initManagedDomainCertificateList();
  114. function handleDomainUploadByKeypress(){
  115. handleDomainKeysUpload(function(){
  116. $("#certUploadingDomain").text($("#certdomain").val().trim());
  117. //After uploaded, reset the file selector
  118. document.getElementById('pubkeySelector').value = '';
  119. document.getElementById('prikeySelector').value = '';
  120. document.getElementById('certdomain').value = '';
  121. uploadPendingPublicKey = undefined;
  122. uploadPendingPrivateKey = undefined;
  123. //Show succ
  124. $("#certUploadSuccMsg").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  125. initManagedDomainCertificateList();
  126. });
  127. }
  128. //Handle domain keys upload
  129. function handleDomainKeysUpload(callback=undefined){
  130. let domain = $("#certdomain").val();
  131. if (domain.trim() == ""){
  132. msgbox("Missing domain", false, 5000);
  133. return;
  134. }
  135. if (uploadPendingPublicKey && uploadPendingPrivateKey && typeof uploadPendingPublicKey === 'object' && typeof uploadPendingPrivateKey === 'object') {
  136. const publicKeyForm = new FormData();
  137. publicKeyForm.append('file', uploadPendingPublicKey, 'publicKey');
  138. const privateKeyForm = new FormData();
  139. privateKeyForm.append('file', uploadPendingPrivateKey, 'privateKey');
  140. const publicKeyRequest = new XMLHttpRequest();
  141. publicKeyRequest.open('POST', '/api/cert/upload?ktype=pub&domain=' + domain);
  142. publicKeyRequest.onreadystatechange = function() {
  143. if (publicKeyRequest.readyState === XMLHttpRequest.DONE) {
  144. if (publicKeyRequest.status !== 200) {
  145. msgbox('Error uploading public key: ' + publicKeyRequest.statusText, false, 5000);
  146. }
  147. if (callback != undefined){
  148. callback();
  149. }
  150. }
  151. };
  152. publicKeyRequest.send(publicKeyForm);
  153. const privateKeyRequest = new XMLHttpRequest();
  154. privateKeyRequest.open('POST', '/api/cert/upload?ktype=pri&domain=' + domain);
  155. privateKeyRequest.onreadystatechange = function() {
  156. if (privateKeyRequest.readyState === XMLHttpRequest.DONE) {
  157. if (privateKeyRequest.status !== 200) {
  158. msgbox('Error uploading private key: ' + privateKeyRequest.statusText, false, 5000);
  159. }
  160. if (callback != undefined){
  161. callback();
  162. }
  163. }
  164. };
  165. privateKeyRequest.send(privateKeyForm);
  166. } else {
  167. msgbox('One or both of the files is missing or not a file object');
  168. }
  169. }
  170. //Handlers for selecting domain based key pairs
  171. //ktype = {"pub" / "pri"}
  172. function handleFileSelect(event, ktype="pub") {
  173. const file = event.target.files[0];
  174. //const fileNameInput = document.getElementById('selected-file-name');
  175. if (ktype == "pub"){
  176. uploadPendingPublicKey = file;
  177. }else if (ktype == "pri"){
  178. uploadPendingPrivateKey = file;
  179. }
  180. //fileNameInput.value = file.name;
  181. }
  182. //Check if the default keypairs exists
  183. function initDefaultKeypairCheck(){
  184. $.get("/api/cert/checkDefault", function(data){
  185. let tick = `<i class="ui green checkmark icon"></i>`;
  186. let cross = `<i class="ui red times icon"></i>`;
  187. $("#pubkeyExists").html(data.DefaultPubExists?tick:cross);
  188. $("#prikeyExists").html(data.DefaultPriExists?tick:cross);
  189. });
  190. }
  191. initDefaultKeypairCheck();
  192. function uploadPrivateKey(){
  193. // create file input element
  194. const input = document.createElement('input');
  195. input.type = 'file';
  196. // add change listener to file input
  197. input.addEventListener('change', () => {
  198. // create form data object
  199. const formData = new FormData();
  200. // add selected file to form data
  201. formData.append('file', input.files[0]);
  202. // send form data to server
  203. fetch('/api/cert/upload?ktype=pri', {
  204. method: 'POST',
  205. body: formData
  206. })
  207. .then(response => {
  208. initDefaultKeypairCheck();
  209. if (response.ok) {
  210. msgbox('File upload successful!');
  211. } else {
  212. response.text().then(text => {
  213. msgbox(text, false, 5000);
  214. });
  215. //console.log(response.text());
  216. //alert('File upload failed!');
  217. }
  218. })
  219. .catch(error => {
  220. msgbox('An error occurred while uploading the file.', false, 5000);
  221. console.error(error);
  222. });
  223. });
  224. // click file input to open file selector
  225. input.click();
  226. }
  227. function uploadPublicKey() {
  228. // create file input element
  229. const input = document.createElement('input');
  230. input.type = 'file';
  231. // add change listener to file input
  232. input.addEventListener('change', () => {
  233. // create form data object
  234. const formData = new FormData();
  235. // add selected file to form data
  236. formData.append('file', input.files[0]);
  237. // send form data to server
  238. fetch('/api/cert/upload?ktype=pub', {
  239. method: 'POST',
  240. body: formData
  241. })
  242. .then(response => {
  243. if (response.ok) {
  244. msgbox('File upload successful!');
  245. initDefaultKeypairCheck();
  246. } else {
  247. response.text().then(text => {
  248. msgbox(text, false, 5000);
  249. });
  250. //console.log(response.text());
  251. //alert('File upload failed!');
  252. }
  253. })
  254. .catch(error => {
  255. msgbox('An error occurred while uploading the file.', false, 5000);
  256. console.error(error);
  257. });
  258. });
  259. // click file input to open file selector
  260. input.click();
  261. }
  262. </script>