rules.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <div class="ui stackable grid">
  2. <div class="ten wide column">
  3. <div class="standardContainer">
  4. <div class="ui basic segment" style="margin-top: 1em;">
  5. <h2>New Proxy Rule</h2>
  6. <p>You can create a proxy endpoing by subdomain or virtual directories</p>
  7. <div class="ui form">
  8. <div class="field">
  9. <label>Proxy Type</label>
  10. <div class="ui selection dropdown">
  11. <input type="hidden" id="ptype" value="subd">
  12. <i class="dropdown icon"></i>
  13. <div class="default text">Proxy Type</div>
  14. <div class="menu">
  15. <div class="item" data-value="subd">Sub-domain</div>
  16. <div class="item" data-value="vdir">Virtual Directory</div>
  17. </div>
  18. </div>
  19. </div>
  20. <div class="field">
  21. <label>Subdomain Matching Keyword / Virtual Directory Name</label>
  22. <input type="text" id="rootname" placeholder="s1.mydomain.com">
  23. </div>
  24. <div class="field">
  25. <label>IP Address or Domain Name with port</label>
  26. <input type="text" id="proxyDomain" onchange="autoCheckTls(this.value);">
  27. <small>E.g. 192.168.0.101:8000 or example.com</small>
  28. </div>
  29. <div class="field">
  30. <div class="ui checkbox">
  31. <input type="checkbox" id="reqTls">
  32. <label>Proxy Target require TLS Connection <br><small>(i.e. Your proxy target starts with https://)</small></label>
  33. </div>
  34. </div>
  35. <!-- Advance configs -->
  36. <div class="ui basic segment" style="background-color: #f7f7f7; border-radius: 1em;">
  37. <div id="advanceProxyRules" class="ui fluid accordion">
  38. <div class="title">
  39. <i class="dropdown icon"></i>
  40. Advance Settings
  41. </div>
  42. <div class="content">
  43. <p></p>
  44. <div class="field">
  45. <div class="ui checkbox">
  46. <input type="checkbox" id="skipTLSValidation">
  47. <label>Ignore TLS/SSL Verification Error<br><small>E.g. self-signed, expired certificate (Not Recommended)</small></label>
  48. </div>
  49. </div>
  50. <div class="field">
  51. <div class="ui checkbox">
  52. <input type="checkbox" id="requireBasicAuth">
  53. <label>Require Basic Auth<br><small>Require client to login in order to view the page</small></label>
  54. </div>
  55. </div>
  56. <div id="basicAuthCredentials" class="field">
  57. <p>Enter the username and password for allowing them to access this proxy endpoint</p>
  58. <table class="ui very basic celled table">
  59. <thead>
  60. <tr>
  61. <th>Username</th>
  62. <th>Password</th>
  63. <th>Remove</th>
  64. </tr></thead>
  65. <tbody id="basicAuthCredentialTable">
  66. <tr>
  67. <td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td>
  68. </tr>
  69. </tbody>
  70. </table>
  71. <div class="three small fields credentialEntry">
  72. <div class="field">
  73. <input id="basicAuthCredUsername" type="text" placeholder="Username" autocomplete="off">
  74. </div>
  75. <div class="field">
  76. <input id="basicAuthCredPassword" type="password" placeholder="Password" autocomplete="off">
  77. </div>
  78. <div class="field">
  79. <button class="ui basic button" onclick="addCredentials();"><i class="blue add icon"></i> Add Credential</button>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </div>
  86. <br>
  87. <button class="ui basic button" onclick="newProxyEndpoint();"><i class="blue add icon"></i> Create Endpoint</button>
  88. <br><br>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. <div class="six wide column">
  94. <div class="ui basic segment" style="height: 100%; background-color: var(--theme_grey); color: var(--theme_lgrey);">
  95. <br>
  96. <span style="font-size: 1.2em; font-weight: 300;">Subdomain</span><br>
  97. Example of subdomain matching keyword:<br>
  98. <code>s1.arozos.com</code> <br>(Any access starting with s1.arozos.com will be proxy to the IP address below)<br>
  99. <div class="ui divider"></div>
  100. <span style="font-size: 1.2em; font-weight: 300;">Virtual Directory</span><br>
  101. Example of virtual directory name: <br>
  102. <code>/s1/home/</code> <br>(Any access to {this_server}/s1/home/ will be proxy to the IP address below)<br>
  103. You can also ignore the tailing slash for wildcard like usage.<br>
  104. <code>/s1/room-</code> <br>Any access to {this_server}/s1/classroom_* will be proxied, for example: <br>
  105. <div class="ui list">
  106. <div class="item"><code>/s1/room-101</code></div>
  107. <div class="item"><code>/s1/room-102/</code></div>
  108. <div class="item"><code>/s1/room-103/map.txt</code></div>
  109. </div><br>
  110. <br>
  111. </div>
  112. </div>
  113. </div>
  114. </div>
  115. <script>
  116. $("#advanceProxyRules").accordion();
  117. //New Proxy Endpoint
  118. function newProxyEndpoint(){
  119. var type = $("#ptype").val();
  120. var rootname = $("#rootname").val();
  121. var proxyDomain = $("#proxyDomain").val();
  122. var useTLS = $("#reqTls")[0].checked;
  123. var skipTLSValidation = $("#skipTLSValidation")[0].checked;
  124. var requireBasicAuth = $("#requireBasicAuth")[0].checked;
  125. if (type === "vdir") {
  126. if (!rootname.startsWith("/")) {
  127. rootname = "/" + rootname
  128. $("#rootname").val(rootname);
  129. }
  130. }else{
  131. if (!isSubdomainDomain(rootname)){
  132. //This doesn't seems like a subdomain
  133. if (!confirm(rootname + " does not looks like a subdomain. Continue anyway?")){
  134. return;
  135. }
  136. }
  137. }
  138. if (rootname.trim() == ""){
  139. $("#rootname").parent().addClass("error");
  140. return
  141. }else{
  142. $("#rootname").parent().removeClass("error");
  143. }
  144. if (proxyDomain.trim() == ""){
  145. $("#proxyDomain").parent().addClass("error");
  146. return
  147. }else{
  148. $("#proxyDomain").parent().removeClass("error");
  149. }
  150. //Create the endpoint by calling add
  151. $.ajax({
  152. url: "/api/proxy/add",
  153. data: {
  154. type: type,
  155. rootname: rootname,
  156. tls: useTLS,
  157. ep: proxyDomain,
  158. tlsval: skipTLSValidation,
  159. bauth: requireBasicAuth,
  160. cred: JSON.stringify(credentials),
  161. },
  162. success: function(data){
  163. if (data.error != undefined){
  164. msgbox(data.error, false, 5000);
  165. }else{
  166. //OK
  167. listVdirs();
  168. listSubd();
  169. msgbox("Proxy Endpoint Added");
  170. //Clear old data
  171. $("#rootname").val("");
  172. $("#proxyDomain").val("");
  173. credentials = [];
  174. updateTable();
  175. //Check if it is a new subdomain and TLS enabled
  176. if (type == "subd" && $("#tls").checkbox("is checked")){
  177. confirmBox("Request new SSL Cert for this subdomain?", function(choice){
  178. if (choice == true){
  179. //Get a new cert using ACME
  180. console.log("Trying to get a new certificate via ACME");
  181. obtainCertificate(rootname);
  182. }
  183. });
  184. }
  185. }
  186. }
  187. });
  188. }
  189. //Generic functions for delete rp endpoints
  190. function deleteEndpoint(ptype, epoint){
  191. if (confirm("Confirm remove proxy for :" + epoint + " (type: " + ptype + ")?")){
  192. $.ajax({
  193. url: "/api/proxy/del",
  194. data: {ep: epoint, ptype: ptype},
  195. success: function(){
  196. listVdirs();
  197. listSubd();
  198. }
  199. })
  200. }
  201. }
  202. function autoCheckTls(targetDomain){
  203. $.ajax({
  204. url: "/api/proxy/tlscheck",
  205. data: {url: targetDomain},
  206. success: function(data){
  207. if (data.error != undefined){
  208. }else if (data == "https"){
  209. $("#reqTls").parent().checkbox("set checked");
  210. }else if (data == "http"){
  211. $("#reqTls").parent().checkbox("set unchecked");
  212. }
  213. }
  214. })
  215. }
  216. function toggleBasicAuth() {
  217. var basicAuthDiv = document.getElementById('basicAuthOnly');
  218. if ($("#requireBasicAuth").parent().checkbox("is checked")) {
  219. $("#basicAuthCredentials").removeClass("disabled");
  220. } else {
  221. $("#basicAuthCredentials").addClass("disabled");
  222. }
  223. }
  224. $("#requireBasicAuth").on('change', toggleBasicAuth);
  225. toggleBasicAuth();
  226. /*
  227. Credential Managements
  228. */
  229. let credentials = []; // Global variable to store credentials
  230. function addCredentials() {
  231. // Retrieve the username and password input values
  232. var username = $('#basicAuthCredUsername').val();
  233. var password = $('#basicAuthCredPassword').val();
  234. if(username == "" || password == ""){
  235. msgbox("Username or password cannot be empty", false, 5000);
  236. return;
  237. }
  238. // Create a new credential object
  239. var credential = {
  240. username: username,
  241. password: password
  242. };
  243. // Add the credential to the global credentials array
  244. credentials.push(credential);
  245. // Clear the input fields
  246. $('#basicAuthCredUsername').val('');
  247. $('#basicAuthCredPassword').val('');
  248. // Update the table body with the credentials
  249. updateTable();
  250. }
  251. function updateTable() {
  252. var tableBody = $('#basicAuthCredentialTable');
  253. tableBody.empty();
  254. if (credentials.length === 0) {
  255. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  256. } else {
  257. for (var i = 0; i < credentials.length; i++) {
  258. var credential = credentials[i];
  259. var username = credential.username;
  260. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  261. var row = '<tr>' +
  262. '<td>' + username + '</td>' +
  263. '<td>' + password + '</td>' +
  264. '<td><button class="ui basic button" onclick="removeCredential(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  265. '</tr>';
  266. tableBody.append(row);
  267. }
  268. }
  269. }
  270. function removeCredential(index) {
  271. // Remove the credential from the credentials array
  272. credentials.splice(index, 1);
  273. // Update the table body
  274. updateTable();
  275. }
  276. //Check if a string is a valid subdomain
  277. function isSubdomainDomain(str) {
  278. const regex = /^(localhost|[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,}|[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,}\.)$/i;
  279. return regex.test(str);
  280. }
  281. /*
  282. Inline editor for subd.html and vdir.html
  283. */
  284. function editEndpoint(endpointType, uuid) {
  285. var row = $('tr[eptuuid="' + uuid + '"]');
  286. var columns = row.find('td[data-label]');
  287. var payload = $(row).attr("payload");
  288. payload = JSON.parse(decodeURIComponent(payload));
  289. //console.log(payload);
  290. columns.each(function(index) {
  291. var column = $(this);
  292. var oldValue = column.text().trim();
  293. if ($(this).attr("editable") == "false"){
  294. //This col do not allow edit. Skip
  295. return;
  296. }
  297. // Create an input element based on the column content
  298. var input;
  299. var datatype = $(this).attr("datatype");
  300. if (datatype == "domain"){
  301. let domain = payload.Domain;
  302. let tls = payload.RequireTLS;
  303. if (tls){
  304. tls = "checked";
  305. }else{
  306. tls = "";
  307. }
  308. input = `
  309. <div class="ui mini fluid input">
  310. <input type="text" class="Domain" value="${domain}">
  311. </div>
  312. <div class="ui checkbox" style="margin-top: 0.4em;">
  313. <input type="checkbox" class="RequireTLS" ${tls}>
  314. <label>Require TLS</label>
  315. </div>
  316. `;
  317. column.empty().append(input);
  318. }else if (datatype == "skipver"){
  319. let skipTLSValidation = payload.SkipCertValidations;
  320. let checkstate = "";
  321. if (skipTLSValidation){
  322. checkstate = "checked";
  323. }
  324. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  325. <input type="checkbox" class="SkipCertValidations" ${checkstate}>
  326. <label>Skip Verification</label>
  327. <small>Check this if you are using self signed certificates</small>
  328. </div>`);
  329. }else if (datatype == "basicauth"){
  330. let requireBasicAuth = payload.RequireBasicAuth;
  331. let checkstate = "";
  332. if (requireBasicAuth){
  333. checkstate = "checked";
  334. }
  335. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  336. <input type="checkbox" class="RequireBasicAuth" ${checkstate}>
  337. <label>Require Basic Auth</label>
  338. </div> <button class="ui basic tiny button" style="margin-left: 0.4em;" onclick="editBasicAuthCredentials('${endpointType}','${uuid}');"><i class="ui blue lock icon"></i> Edit Credentials</button>`);
  339. }else if (datatype == 'action'){
  340. column.empty().append(`
  341. <button title="Cancel" onclick="exitProxyInlineEdit('${endpointType}');" class="ui basic small circular icon button"><i class="ui remove icon"></i></button>
  342. <button title="Save" onclick="saveProxyInlineEdit('${uuid}');" class="ui basic small circular icon button"><i class="ui green save icon"></i></button>
  343. `);
  344. }else{
  345. //Unknown field. Leave it untouched
  346. }
  347. });
  348. $("#" + endpointType).find(".editBtn").addClass("disabled");
  349. }
  350. function exitProxyInlineEdit(){
  351. listSubd();
  352. listVdirs();
  353. $("#" + endpointType).find(".editBtn").removeClass("disabled");
  354. }
  355. function saveProxyInlineEdit(uuid){
  356. var row = $('tr[eptuuid="' + uuid + '"]');
  357. if (row.length == 0){
  358. return;
  359. }
  360. var epttype = $(row).attr("class");
  361. if (epttype == "subdEntry"){
  362. epttype = "subd";
  363. }else if (epttype == "vdirEntry"){
  364. epttype = "vdir";
  365. }
  366. let newDomain = $(row).find(".Domain").val();
  367. let requireTLS = $(row).find(".RequireTLS")[0].checked;
  368. let skipCertValidations = $(row).find(".SkipCertValidations")[0].checked;
  369. let requireBasicAuth = $(row).find(".RequireBasicAuth")[0].checked;
  370. console.log(newDomain, requireTLS, skipCertValidations, requireBasicAuth)
  371. $.ajax({
  372. url: "/api/proxy/edit",
  373. method: "POST",
  374. data: {
  375. "type": epttype,
  376. "rootname": uuid,
  377. "ep":newDomain,
  378. "tls" :requireTLS,
  379. "tlsval": skipCertValidations,
  380. "bauth" :requireBasicAuth,
  381. },
  382. success: function(data){
  383. if (data.error !== undefined){
  384. msgbox(data.error, false, 6000);
  385. }else{
  386. msgbox("Proxy endpoint updated");
  387. if (epttype == "subd"){
  388. listSubd();
  389. }else if (epttype == "vdir"){
  390. listVdirs();
  391. }
  392. }
  393. }
  394. })
  395. }
  396. function editBasicAuthCredentials(endpointType, uuid){
  397. let payload = encodeURIComponent(JSON.stringify({
  398. ept: endpointType,
  399. ep: uuid
  400. }));
  401. showSideWrapper("snippet/basicAuthEditor.html?t=" + Date.now() + "#" + payload);
  402. }
  403. /*
  404. Obtain Certificate via ACME
  405. */
  406. //Load the ACME email from server side
  407. let acmeEmail = "";
  408. $.get("/api/acme/autoRenew/email", function(data){
  409. if (data != "" && data != undefined && data != null){
  410. acmeEmail = data;
  411. }
  412. });
  413. // Obtain certificate from API, only support one domain
  414. function obtainCertificate(domains) {
  415. let filename = "";
  416. let email = acmeEmail;
  417. if (acmeEmail == ""){
  418. let rootDomain = domains.split(".").pop();
  419. email = "admin@" + rootDomain;
  420. }
  421. if (filename.trim() == "" && !domains.includes(",")){
  422. //Zoraxy filename are the matching name for domains.
  423. //Use the same as domains
  424. filename = domains;
  425. }else if (filename != "" && !domains.includes(",")){
  426. //Invalid settings. Force the filename to be same as domain
  427. //if there are only 1 domain
  428. filename = domains;
  429. }else{
  430. parent.msgbox("Filename cannot be empty for certs containing multiple domains.")
  431. return;
  432. }
  433. $.ajax({
  434. url: "/api/acme/obtainCert",
  435. method: "GET",
  436. data: {
  437. domains: domains,
  438. filename: filename,
  439. email: email,
  440. ca: "Let's Encrypt",
  441. },
  442. success: function(response) {
  443. if (response.error) {
  444. console.log("Error:", response.error);
  445. // Show error message
  446. msgbox(response.error, false, 12000);
  447. } else {
  448. console.log("Certificate renewed successfully");
  449. // Show success message
  450. msgbox("Certificate renewed successfully");
  451. // Renew the parent certificate list
  452. initManagedDomainCertificateList();
  453. }
  454. },
  455. error: function(error) {
  456. console.log("Failed to renewed certificate:", error);
  457. }
  458. });
  459. }
  460. </script>