cert.html 12 KB

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