rules.html 22 KB

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