rules.html 22 KB

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