rules.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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="bypassGlobalTLS">
  53. <label>Bypass Global TLS<br><small>Allow this subdomain / vdir to be connected without TLS</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. //Generic functions for delete rp endpoints
  207. function deleteEndpoint(ptype, epoint){
  208. if (confirm("Confirm remove proxy for :" + epoint + " (type: " + ptype + ")?")){
  209. $.ajax({
  210. url: "/api/proxy/del",
  211. data: {ep: epoint, ptype: ptype},
  212. success: function(){
  213. listVdirs();
  214. listSubd();
  215. }
  216. })
  217. }
  218. }
  219. function autoCheckTls(targetDomain){
  220. $.ajax({
  221. url: "/api/proxy/tlscheck",
  222. data: {url: targetDomain},
  223. success: function(data){
  224. if (data.error != undefined){
  225. }else if (data == "https"){
  226. $("#reqTls").parent().checkbox("set checked");
  227. }else if (data == "http"){
  228. $("#reqTls").parent().checkbox("set unchecked");
  229. }
  230. }
  231. })
  232. }
  233. function toggleBasicAuth() {
  234. var basicAuthDiv = document.getElementById('basicAuthOnly');
  235. if ($("#requireBasicAuth").parent().checkbox("is checked")) {
  236. $("#basicAuthCredentials").removeClass("disabled");
  237. } else {
  238. $("#basicAuthCredentials").addClass("disabled");
  239. }
  240. }
  241. $("#requireBasicAuth").on('change', toggleBasicAuth);
  242. toggleBasicAuth();
  243. /*
  244. Credential Managements
  245. */
  246. let credentials = []; // Global variable to store credentials
  247. function addCredentials() {
  248. // Retrieve the username and password input values
  249. var username = $('#basicAuthCredUsername').val();
  250. var password = $('#basicAuthCredPassword').val();
  251. if(username == "" || password == ""){
  252. msgbox("Username or password cannot be empty", false, 5000);
  253. return;
  254. }
  255. // Create a new credential object
  256. var credential = {
  257. username: username,
  258. password: password
  259. };
  260. // Add the credential to the global credentials array
  261. credentials.push(credential);
  262. // Clear the input fields
  263. $('#basicAuthCredUsername').val('');
  264. $('#basicAuthCredPassword').val('');
  265. // Update the table body with the credentials
  266. updateTable();
  267. }
  268. function updateTable() {
  269. var tableBody = $('#basicAuthCredentialTable');
  270. tableBody.empty();
  271. if (credentials.length === 0) {
  272. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  273. } else {
  274. for (var i = 0; i < credentials.length; i++) {
  275. var credential = credentials[i];
  276. var username = credential.username;
  277. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  278. var row = '<tr>' +
  279. '<td>' + username + '</td>' +
  280. '<td>' + password + '</td>' +
  281. '<td><button class="ui basic button" onclick="removeCredential(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  282. '</tr>';
  283. tableBody.append(row);
  284. }
  285. }
  286. }
  287. function removeCredential(index) {
  288. // Remove the credential from the credentials array
  289. credentials.splice(index, 1);
  290. // Update the table body
  291. updateTable();
  292. }
  293. //Check if a string is a valid subdomain
  294. function isSubdomainDomain(str) {
  295. const regex = /^(localhost|[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,}|[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,}\.)$/i;
  296. return regex.test(str);
  297. }
  298. /*
  299. Inline editor for subd.html and vdir.html
  300. */
  301. function editEndpoint(endpointType, uuid) {
  302. var row = $('tr[eptuuid="' + uuid + '"]');
  303. var columns = row.find('td[data-label]');
  304. var payload = $(row).attr("payload");
  305. payload = JSON.parse(decodeURIComponent(payload));
  306. //console.log(payload);
  307. columns.each(function(index) {
  308. var column = $(this);
  309. var oldValue = column.text().trim();
  310. if ($(this).attr("editable") == "false"){
  311. //This col do not allow edit. Skip
  312. return;
  313. }
  314. // Create an input element based on the column content
  315. var input;
  316. var datatype = $(this).attr("datatype");
  317. if (datatype == "domain"){
  318. let domain = payload.Domain;
  319. let tls = payload.RequireTLS;
  320. if (tls){
  321. tls = "checked";
  322. }else{
  323. tls = "";
  324. }
  325. input = `
  326. <div class="ui mini fluid input">
  327. <input type="text" class="Domain" value="${domain}">
  328. </div>
  329. <div class="ui checkbox" style="margin-top: 0.4em;">
  330. <input type="checkbox" class="RequireTLS" ${tls}>
  331. <label>Require TLS</label>
  332. </div>
  333. `;
  334. column.empty().append(input);
  335. }else if (datatype == "skipver"){
  336. let skipTLSValidation = payload.SkipCertValidations;
  337. let checkstate = "";
  338. if (skipTLSValidation){
  339. checkstate = "checked";
  340. }
  341. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  342. <input type="checkbox" class="SkipCertValidations" ${checkstate}>
  343. <label>Skip Verification</label>
  344. <small>Check this if you are using self signed certificates</small>
  345. </div>`);
  346. }else if (datatype == "basicauth"){
  347. let requireBasicAuth = payload.RequireBasicAuth;
  348. let checkstate = "";
  349. if (requireBasicAuth){
  350. checkstate = "checked";
  351. }
  352. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  353. <input type="checkbox" class="RequireBasicAuth" ${checkstate}>
  354. <label>Require Basic Auth</label>
  355. </div>
  356. <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>`);
  357. }else if (datatype == 'action'){
  358. column.empty().append(`
  359. <button title="Cancel" onclick="exitProxyInlineEdit('${endpointType}');" class="ui basic small circular icon button"><i class="ui remove icon"></i></button>
  360. <button title="Save" onclick="saveProxyInlineEdit('${uuid}');" class="ui basic small circular icon button"><i class="ui green save icon"></i></button>
  361. `);
  362. }else{
  363. //Unknown field. Leave it untouched
  364. }
  365. });
  366. $("#" + endpointType).find(".editBtn").addClass("disabled");
  367. }
  368. function exitProxyInlineEdit(){
  369. listSubd();
  370. listVdirs();
  371. $("#" + endpointType).find(".editBtn").removeClass("disabled");
  372. }
  373. function saveProxyInlineEdit(uuid){
  374. var row = $('tr[eptuuid="' + uuid + '"]');
  375. if (row.length == 0){
  376. return;
  377. }
  378. var epttype = $(row).attr("class");
  379. if (epttype == "subdEntry"){
  380. epttype = "subd";
  381. }else if (epttype == "vdirEntry"){
  382. epttype = "vdir";
  383. }
  384. let newDomain = $(row).find(".Domain").val();
  385. let requireTLS = $(row).find(".RequireTLS")[0].checked;
  386. let skipCertValidations = $(row).find(".SkipCertValidations")[0].checked;
  387. let requireBasicAuth = $(row).find(".RequireBasicAuth")[0].checked;
  388. console.log(newDomain, requireTLS, skipCertValidations, requireBasicAuth)
  389. $.ajax({
  390. url: "/api/proxy/edit",
  391. method: "POST",
  392. data: {
  393. "type": epttype,
  394. "rootname": uuid,
  395. "ep":newDomain,
  396. "tls" :requireTLS,
  397. "tlsval": skipCertValidations,
  398. "bauth" :requireBasicAuth,
  399. },
  400. success: function(data){
  401. if (data.error !== undefined){
  402. msgbox(data.error, false, 6000);
  403. }else{
  404. msgbox("Proxy endpoint updated");
  405. if (epttype == "subd"){
  406. listSubd();
  407. }else if (epttype == "vdir"){
  408. listVdirs();
  409. }
  410. }
  411. }
  412. })
  413. }
  414. function editBasicAuthCredentials(endpointType, uuid){
  415. let payload = encodeURIComponent(JSON.stringify({
  416. ept: endpointType,
  417. ep: uuid
  418. }));
  419. showSideWrapper("snippet/basicAuthEditor.html?t=" + Date.now() + "#" + payload);
  420. }
  421. /*
  422. Obtain Certificate via ACME
  423. */
  424. //Load the ACME email from server side
  425. let acmeEmail = "";
  426. $.get("/api/acme/autoRenew/email", function(data){
  427. if (data != "" && data != undefined && data != null){
  428. acmeEmail = data;
  429. }
  430. });
  431. // Obtain certificate from API, only support one domain
  432. function obtainCertificate(domains, usingCa = "Let's Encrypt") {
  433. let filename = "";
  434. let email = acmeEmail;
  435. if (acmeEmail == ""){
  436. let rootDomain = domains.split(".").pop();
  437. email = "admin@" + rootDomain;
  438. }
  439. if (filename.trim() == "" && !domains.includes(",")){
  440. //Zoraxy filename are the matching name for domains.
  441. //Use the same as domains
  442. filename = domains;
  443. }else if (filename != "" && !domains.includes(",")){
  444. //Invalid settings. Force the filename to be same as domain
  445. //if there are only 1 domain
  446. filename = domains;
  447. }else{
  448. parent.msgbox("Filename cannot be empty for certs containing multiple domains.")
  449. return;
  450. }
  451. $.ajax({
  452. url: "/api/acme/obtainCert",
  453. method: "GET",
  454. data: {
  455. domains: domains,
  456. filename: filename,
  457. email: email,
  458. ca: usingCa,
  459. },
  460. success: function(response) {
  461. if (response.error) {
  462. console.log("Error:", response.error);
  463. // Show error message
  464. msgbox(response.error, false, 12000);
  465. } else {
  466. console.log("Certificate installed successfully");
  467. // Show success message
  468. msgbox("Certificate installed successfully");
  469. // Renew the parent certificate list
  470. initManagedDomainCertificateList();
  471. }
  472. },
  473. error: function(error) {
  474. console.log("Failed to install certificate:", error);
  475. }
  476. });
  477. }
  478. </script>