cert.html 12 KB

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