1
0

rules.html 21 KB

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