cert.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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="handleDomainKeysUpload();"><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. function initManagedDomainCertificateList(){
  69. $("#certifiedDomainList").html("");
  70. $.get("/cert/list?date=true", function(data){
  71. if (data.error != undefined){
  72. alert(data.error);
  73. }else{
  74. data.forEach(entry => {
  75. $("#certifiedDomainList").append(`<tr>
  76. <td>${entry.Domain}</td>
  77. <td>${entry.LastModifiedDate}</td>
  78. <td><button title="Delete key-pair" class="ui mini basic red icon button"><i class="ui red trash icon"></i></button></td>
  79. </tr>`);
  80. })
  81. }
  82. })
  83. }
  84. initManagedDomainCertificateList();
  85. function handleDomainUploadByKeypress(){
  86. handleDomainKeysUpload(function(){
  87. $("#certUploadingDomain").text($("#certdomain").val().trim());
  88. //After uploaded, reset the file selector
  89. document.getElementById('pubkeySelector').value = '';
  90. document.getElementById('prikeySelector').value = '';
  91. document.getElementById('certdomain').value = '';
  92. uploadPendingPublicKey = undefined;
  93. uploadPendingPrivateKey = undefined;
  94. //Show succ
  95. $("#certUploadSuccMsg").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  96. });
  97. }
  98. //Handle domain keys upload
  99. function handleDomainKeysUpload(callback=undefined){
  100. let domain = $("#certdomain").val();
  101. if (domain.trim() == ""){
  102. alert("Missing domain.");
  103. return;
  104. }
  105. if (uploadPendingPublicKey && uploadPendingPrivateKey && typeof uploadPendingPublicKey === 'object' && typeof uploadPendingPrivateKey === 'object') {
  106. const publicKeyForm = new FormData();
  107. publicKeyForm.append('file', uploadPendingPublicKey, 'publicKey');
  108. const privateKeyForm = new FormData();
  109. privateKeyForm.append('file', uploadPendingPrivateKey, 'privateKey');
  110. const publicKeyRequest = new XMLHttpRequest();
  111. publicKeyRequest.open('POST', '/cert/upload?ktype=pub&domain=' + domain);
  112. publicKeyRequest.onreadystatechange = function() {
  113. if (publicKeyRequest.readyState === XMLHttpRequest.DONE) {
  114. if (publicKeyRequest.status !== 200) {
  115. alert('Error uploading public key: ' + publicKeyRequest.statusText);
  116. }
  117. if (callback != undefined){
  118. callback();
  119. }
  120. }
  121. };
  122. publicKeyRequest.send(publicKeyForm);
  123. const privateKeyRequest = new XMLHttpRequest();
  124. privateKeyRequest.open('POST', '/cert/upload?ktype=pri&domain=' + domain);
  125. privateKeyRequest.onreadystatechange = function() {
  126. if (privateKeyRequest.readyState === XMLHttpRequest.DONE) {
  127. if (privateKeyRequest.status !== 200) {
  128. alert('Error uploading private key: ' + privateKeyRequest.statusText);
  129. }
  130. if (callback != undefined){
  131. callback();
  132. }
  133. }
  134. };
  135. privateKeyRequest.send(privateKeyForm);
  136. } else {
  137. alert('One or both of the files is missing or not a file object');
  138. }
  139. }
  140. //Handlers for selecting domain based key pairs
  141. //ktype = {"pub" / "pri"}
  142. function handleFileSelect(event, ktype="pub") {
  143. const file = event.target.files[0];
  144. //const fileNameInput = document.getElementById('selected-file-name');
  145. if (ktype == "pub"){
  146. uploadPendingPublicKey = file;
  147. }else if (ktype == "pri"){
  148. uploadPendingPrivateKey = file;
  149. }
  150. //fileNameInput.value = file.name;
  151. }
  152. //Check if the default keypairs exists
  153. function initDefaultKeypairCheck(){
  154. $.get("/cert/checkDefault", function(data){
  155. let tick = `<i class="ui green checkmark icon"></i>`;
  156. let cross = `<i class="ui red times icon"></i>`;
  157. $("#pubkeyExists").html(data.DefaultPubExists?tick:cross);
  158. $("#prikeyExists").html(data.DefaultPriExists?tick:cross);
  159. });
  160. }
  161. initDefaultKeypairCheck();
  162. function uploadPrivateKey(){
  163. // create file input element
  164. const input = document.createElement('input');
  165. input.type = 'file';
  166. // add change listener to file input
  167. input.addEventListener('change', () => {
  168. // create form data object
  169. const formData = new FormData();
  170. // add selected file to form data
  171. formData.append('file', input.files[0]);
  172. // send form data to server
  173. fetch('/cert/upload?ktype=pri', {
  174. method: 'POST',
  175. body: formData
  176. })
  177. .then(response => {
  178. initDefaultKeypairCheck();
  179. if (response.ok) {
  180. alert('File upload successful!');
  181. } else {
  182. response.text().then(text => {
  183. alert(text);
  184. });
  185. //console.log(response.text());
  186. //alert('File upload failed!');
  187. }
  188. })
  189. .catch(error => {
  190. alert('An error occurred while uploading the file.');
  191. console.error(error);
  192. });
  193. });
  194. // click file input to open file selector
  195. input.click();
  196. }
  197. function uploadPublicKey() {
  198. // create file input element
  199. const input = document.createElement('input');
  200. input.type = 'file';
  201. // add change listener to file input
  202. input.addEventListener('change', () => {
  203. // create form data object
  204. const formData = new FormData();
  205. // add selected file to form data
  206. formData.append('file', input.files[0]);
  207. // send form data to server
  208. fetch('/cert/upload?ktype=pub', {
  209. method: 'POST',
  210. body: formData
  211. })
  212. .then(response => {
  213. if (response.ok) {
  214. alert('File upload successful!');
  215. initDefaultKeypairCheck();
  216. } else {
  217. response.text().then(text => {
  218. alert(text);
  219. });
  220. //console.log(response.text());
  221. //alert('File upload failed!');
  222. }
  223. })
  224. .catch(error => {
  225. alert('An error occurred while uploading the file.');
  226. console.error(error);
  227. });
  228. });
  229. // click file input to open file selector
  230. input.click();
  231. }
  232. </script>