1
0

rules.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. //Clear old data
  157. $("#rootname").val("");
  158. $("#proxyDomain").val("");
  159. credentials = [];
  160. updateTable();
  161. //Check if it is a new subdomain and TLS enabled
  162. if ($("#tls").checkbox("is checked")){
  163. confirmBox("Request new SSL Cert for this subdomain?", function(choice){
  164. if (choice == true){
  165. //Load the prefer CA from TLS page
  166. let defaultCA = $("#defaultCA").dropdown("get value");
  167. if (defaultCA.trim() == ""){
  168. defaultCA = "Let's Encrypt";
  169. }
  170. //Get a new cert using ACME
  171. msgbox("Requesting certificate via " + defaultCA +"...");
  172. console.log("Trying to get a new certificate via ACME");
  173. obtainCertificate(rootname, defaultCA.trim());
  174. }else{
  175. msgbox("Proxy Endpoint Added");
  176. }
  177. });
  178. }else{
  179. msgbox("Proxy Endpoint Added");
  180. }
  181. }
  182. }
  183. });
  184. }
  185. //Generic functions for delete rp endpoints
  186. function deleteEndpoint(epoint){
  187. epoint = decodeURIComponent(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. msgbox("Proxy Rule Deleted", true);
  195. }
  196. })
  197. }
  198. }
  199. function autoCheckTls(targetDomain){
  200. $.ajax({
  201. url: "/api/proxy/tlscheck",
  202. data: {url: targetDomain},
  203. success: function(data){
  204. if (data.error != undefined){
  205. }else if (data == "https"){
  206. $("#reqTls").parent().checkbox("set checked");
  207. }else if (data == "http"){
  208. $("#reqTls").parent().checkbox("set unchecked");
  209. }
  210. }
  211. })
  212. }
  213. function toggleBasicAuth() {
  214. var basicAuthDiv = document.getElementById('basicAuthOnly');
  215. if ($("#requireBasicAuth").parent().checkbox("is checked")) {
  216. $("#basicAuthCredentials").removeClass("disabled");
  217. } else {
  218. $("#basicAuthCredentials").addClass("disabled");
  219. }
  220. }
  221. $("#requireBasicAuth").on('change', toggleBasicAuth);
  222. toggleBasicAuth();
  223. /*
  224. Credential Managements
  225. */
  226. let credentials = []; // Global variable to store credentials
  227. function addCredentials() {
  228. // Retrieve the username and password input values
  229. var username = $('#basicAuthCredUsername').val();
  230. var password = $('#basicAuthCredPassword').val();
  231. if(username == "" || password == ""){
  232. msgbox("Username or password cannot be empty", false, 5000);
  233. return;
  234. }
  235. // Create a new credential object
  236. var credential = {
  237. username: username,
  238. password: password
  239. };
  240. // Add the credential to the global credentials array
  241. credentials.push(credential);
  242. // Clear the input fields
  243. $('#basicAuthCredUsername').val('');
  244. $('#basicAuthCredPassword').val('');
  245. // Update the table body with the credentials
  246. updateTable();
  247. }
  248. function updateTable() {
  249. var tableBody = $('#basicAuthCredentialTable');
  250. tableBody.empty();
  251. if (credentials.length === 0) {
  252. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  253. } else {
  254. for (var i = 0; i < credentials.length; i++) {
  255. var credential = credentials[i];
  256. var username = credential.username;
  257. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  258. var row = '<tr>' +
  259. '<td>' + username + '</td>' +
  260. '<td>' + password + '</td>' +
  261. '<td><button class="ui basic button" onclick="removeCredential(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  262. '</tr>';
  263. tableBody.append(row);
  264. }
  265. }
  266. }
  267. function removeCredential(index) {
  268. // Remove the credential from the credentials array
  269. credentials.splice(index, 1);
  270. // Update the table body
  271. updateTable();
  272. }
  273. /*
  274. Obtain Certificate via ACME
  275. */
  276. //Load the ACME email from server side
  277. let acmeEmail = "";
  278. $.get("/api/acme/autoRenew/email", function(data){
  279. if (data != "" && data != undefined && data != null){
  280. acmeEmail = data;
  281. }
  282. });
  283. // Obtain certificate from API, only support one domain
  284. function obtainCertificate(domains, usingCa = "Let's Encrypt") {
  285. let filename = "";
  286. let email = acmeEmail;
  287. if (acmeEmail == ""){
  288. let rootDomain = domains.split(".").pop();
  289. email = "admin@" + rootDomain;
  290. }
  291. if (filename.trim() == "" && !domains.includes(",")){
  292. //Zoraxy filename are the matching name for domains.
  293. //Use the same as domains
  294. filename = domains;
  295. }else if (filename != "" && !domains.includes(",")){
  296. //Invalid settings. Force the filename to be same as domain
  297. //if there are only 1 domain
  298. filename = domains;
  299. }else{
  300. parent.msgbox("Filename cannot be empty for certs containing multiple domains.")
  301. return;
  302. }
  303. $.ajax({
  304. url: "/api/acme/obtainCert",
  305. method: "GET",
  306. data: {
  307. domains: domains,
  308. filename: filename,
  309. email: email,
  310. ca: usingCa,
  311. },
  312. success: function(response) {
  313. if (response.error) {
  314. console.log("Error:", response.error);
  315. // Show error message
  316. msgbox(response.error, false, 12000);
  317. } else {
  318. console.log("Certificate installed successfully");
  319. // Show success message
  320. msgbox("Certificate installed successfully");
  321. // Renew the parent certificate list
  322. initManagedDomainCertificateList();
  323. }
  324. },
  325. error: function(error) {
  326. console.log("Failed to install certificate:", error);
  327. }
  328. });
  329. }
  330. //Update v3.0.0
  331. //Since some proxy rules now contains wildcard characters
  332. //all uuid are converted to hex code before use in DOM selector
  333. String.prototype.hexEncode = function(){
  334. var hex, i;
  335. var result = "";
  336. for (i=0; i<this.length; i++) {
  337. hex = this.charCodeAt(i).toString(16);
  338. result += ("000"+hex).slice(-4);
  339. }
  340. return result
  341. }
  342. String.prototype.hexDecode = function(){
  343. var j;
  344. var hexes = this.match(/.{1,4}/g) || [];
  345. var back = "";
  346. for(j = 0; j<hexes.length; j++) {
  347. back += String.fromCharCode(parseInt(hexes[j], 16));
  348. }
  349. return back;
  350. }
  351. </script>