rules.html 16 KB

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