rules.html 22 KB

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