1
0

cert.html 11 KB

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