rules.html 24 KB

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