rules.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. .ui.form .sub.field{
  9. background-color: var(--theme_advance);
  10. border-radius: 0.6em;
  11. padding: 1em;
  12. }
  13. </style>
  14. <div class="standardContainer">
  15. <div class="ui stackable grid">
  16. <div class="ten wide column">
  17. <div class="ui basic segment" style="border-radius: 1em; padding: 1em !important;">
  18. <h2>New Proxy Rule</h2>
  19. <p>You can add more proxy rules to support more site via domain / subdomains</p>
  20. <div class="ui form">
  21. <div class="field">
  22. <label>Matching Keyword / Domain</label>
  23. <input type="text" id="rootname" placeholder="mydomain.com">
  24. <small>Support subdomain and wildcard, e.g. s1.mydomain.com or *.test.mydomain.com. Use comma (,) for alias hostnames. </small>
  25. </div>
  26. <div class="field">
  27. <label>Target IP Address or Domain Name with port</label>
  28. <input type="text" id="proxyDomain" onchange="autoCheckTls(this.value);">
  29. <small>E.g. 192.168.0.101:8000 or example.com</small>
  30. </div>
  31. <div class="field">
  32. <div class="ui checkbox">
  33. <input type="checkbox" id="reqTls">
  34. <label>Proxy Target require TLS Connection <br><small>(i.e. Your proxy target starts with https://)</small></label>
  35. </div>
  36. </div>
  37. <!-- Advance configs -->
  38. <div class="ui basic segment" style="background-color: #f7f7f7; border-radius: 1em;">
  39. <div id="advanceProxyRules" class="ui fluid accordion">
  40. <div class="title">
  41. <i class="dropdown icon"></i>
  42. Advance Settings
  43. </div>
  44. <div class="content">
  45. <div class="field">
  46. <label>Access Rule</label>
  47. <div class="ui selection dropdown">
  48. <input type="hidden" id="newProxyRuleAccessFilter" value="default">
  49. <i class="dropdown icon"></i>
  50. <div class="default text">Default</div>
  51. <div class="menu" id="newProxyRuleAccessList">
  52. <div class="item" data-value="default"><i class="ui yellow star icon"></i> Default</div>
  53. </div>
  54. </div>
  55. <small>Allow regional access control using blacklist or whitelist. Use "default" for "allow all".</small>
  56. </div>
  57. <div class="field">
  58. <div class="ui checkbox">
  59. <input type="checkbox" id="skipTLSValidation">
  60. <label>Ignore TLS/SSL Verification Error<br><small>For targets that is using self-signed, expired certificate (Not Recommended)</small></label>
  61. </div>
  62. </div>
  63. <div class="field">
  64. <div class="ui checkbox">
  65. <input type="checkbox" id="skipWebsocketOriginCheck" checked>
  66. <label>Skip WebSocket Origin Check<br><small>Allow cross-origin websocket requests (Usually not a security concern)</small></label>
  67. </div>
  68. </div>
  69. <div class="field">
  70. <div class="ui checkbox">
  71. <input type="checkbox" id="bypassGlobalTLS">
  72. <label>Allow plain HTTP access<br><small>Allow this subdomain to be connected without TLS (Require HTTP server enabled on port 80)</small></label>
  73. </div>
  74. </div>
  75. <div class="field">
  76. <div class="ui checkbox">
  77. <input type="checkbox" id="requireRateLimit">
  78. <label>Require Rate Limit<br><small>This proxy endpoint will be rate limited.</small></label>
  79. </div>
  80. </div>
  81. <div class="field">
  82. <label>Rate Limit</label>
  83. <div class="ui fluid right labeled input">
  84. <input type="number" id="proxyRateLimit" placeholder="100" min="1" max="1000" value="100">
  85. <div class="ui basic label">
  86. req / sec / IP
  87. </div>
  88. </div>
  89. <small>Return a 429 error code if request rate exceed the rate limit.</small>
  90. </div>
  91. <div class="field">
  92. <div class="ui checkbox">
  93. <input type="checkbox" id="requireBasicAuth">
  94. <label>Require Basic Auth<br><small>Require client to login in order to view the page</small></label>
  95. </div>
  96. </div>
  97. <div id="basicAuthCredentials" class="field">
  98. <p>Enter the username and password for allowing them to access this proxy endpoint</p>
  99. <table class="ui very basic celled table">
  100. <thead>
  101. <tr>
  102. <th>Username</th>
  103. <th>Password</th>
  104. <th>Remove</th>
  105. </tr></thead>
  106. <tbody id="basicAuthCredentialTable">
  107. <tr>
  108. <td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td>
  109. </tr>
  110. </tbody>
  111. </table>
  112. <div class="three small fields credentialEntry">
  113. <div class="field">
  114. <input id="basicAuthCredUsername" type="text" placeholder="Username" autocomplete="off">
  115. </div>
  116. <div class="field">
  117. <input id="basicAuthCredPassword" type="password" placeholder="Password" autocomplete="off">
  118. </div>
  119. <div class="field">
  120. <button class="ui basic button" onclick="addCredentials();"><i class="blue add icon"></i> Add Credential</button>
  121. </div>
  122. </div>
  123. </div>
  124. </div>
  125. </div>
  126. </div>
  127. <br>
  128. <button class="ui basic button" onclick="newProxyEndpoint();"><i class="green add icon"></i> Create Endpoint</button>
  129. <br><br>
  130. </div>
  131. </div>
  132. </div>
  133. <div class="six wide column">
  134. <div class="ui basic segment rulesInstructions">
  135. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Domain</span><br>
  136. Example of domain matching keyword:<br>
  137. <code>arozos.com</code> <br>Any acess requesting arozos.com will be proxy to the IP address below<br>
  138. <div class="ui divider"></div>
  139. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Subdomain</span><br>
  140. Example of subdomain matching keyword:<br>
  141. <code>s1.arozos.com</code> <br>Any request starting with s1.arozos.com will be proxy to the IP address below<br>
  142. <div class="ui divider"></div>
  143. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Wildcard</span><br>
  144. Example of wildcard matching keyword:<br>
  145. <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>
  146. <div class="ui list">
  147. <div class="item"><code>www.arozos.com</code></div>
  148. <div class="item"><code>foo.bar.arozos.com</code></div>
  149. </div>
  150. <br>
  151. </div>
  152. </div>
  153. </div>
  154. </div>
  155. <script>
  156. //New Proxy Endpoint
  157. function newProxyEndpoint(){
  158. var rootname = $("#rootname").val();
  159. var proxyDomain = $("#proxyDomain").val();
  160. var useTLS = $("#reqTls")[0].checked;
  161. var skipTLSValidation = $("#skipTLSValidation")[0].checked;
  162. var bypassGlobalTLS = $("#bypassGlobalTLS")[0].checked;
  163. var requireBasicAuth = $("#requireBasicAuth")[0].checked;
  164. var proxyRateLimit = $("#proxyRateLimit").val();
  165. var requireRateLimit = $("#requireRateLimit")[0].checked;
  166. var skipWebSocketOriginCheck = $("#skipWebsocketOriginCheck")[0].checked;
  167. var accessRuleToUse = $("#newProxyRuleAccessFilter").val();
  168. if (rootname.trim() == ""){
  169. $("#rootname").parent().addClass("error");
  170. return
  171. }else{
  172. $("#rootname").parent().removeClass("error");
  173. }
  174. if (proxyDomain.trim() == ""){
  175. $("#proxyDomain").parent().addClass("error");
  176. return
  177. }else{
  178. $("#proxyDomain").parent().removeClass("error");
  179. }
  180. //Create the endpoint by calling add
  181. $.ajax({
  182. url: "/api/proxy/add",
  183. data: {
  184. type: "host",
  185. rootname: rootname,
  186. tls: useTLS,
  187. ep: proxyDomain,
  188. tlsval: skipTLSValidation,
  189. bpwsorg: skipWebSocketOriginCheck,
  190. bypassGlobalTLS: bypassGlobalTLS,
  191. bauth: requireBasicAuth,
  192. rate: requireRateLimit,
  193. ratenum: proxyRateLimit,
  194. cred: JSON.stringify(credentials),
  195. access: accessRuleToUse,
  196. },
  197. success: function(data){
  198. if (data.error != undefined){
  199. msgbox(data.error, false, 5000);
  200. }else{
  201. //Clear old data
  202. $("#rootname").val("");
  203. $("#proxyDomain").val("");
  204. credentials = [];
  205. updateTable();
  206. reloadUptimeList();
  207. //Check if it is a new subdomain and TLS enabled
  208. if ($("#tls").checkbox("is checked")){
  209. confirmBox("Request new SSL Cert for this subdomain?", function(choice){
  210. if (choice == true){
  211. //Load the prefer CA from TLS page
  212. let defaultCA = $("#defaultCA").dropdown("get value");
  213. if (defaultCA.trim() == ""){
  214. defaultCA = "Let's Encrypt";
  215. }
  216. //Get a new cert using ACME
  217. msgbox("Requesting certificate via " + defaultCA +"...");
  218. console.log("Trying to get a new certificate via ACME");
  219. //Request ACME for certificate, see cert.html component
  220. obtainCertificate(rootname, defaultCA.trim(), function(){
  221. // Renew the parent certificate list
  222. initManagedDomainCertificateList();
  223. });
  224. }else{
  225. msgbox("Proxy Endpoint Added");
  226. }
  227. });
  228. }else{
  229. msgbox("Proxy Endpoint Added");
  230. }
  231. }
  232. }
  233. });
  234. }
  235. //Generic functions for delete rp endpoints
  236. function deleteEndpoint(epoint){
  237. epoint = decodeURIComponent(epoint).hexDecode();
  238. if (confirm("Confirm remove proxy for :" + epoint + "?")){
  239. $.ajax({
  240. url: "/api/proxy/del",
  241. data: {ep: epoint, },
  242. success: function(){
  243. listProxyEndpoints();
  244. msgbox("Proxy Rule Deleted", true);
  245. reloadUptimeList();
  246. }
  247. })
  248. }
  249. }
  250. function autoCheckTls(targetDomain){
  251. $.ajax({
  252. url: "/api/proxy/tlscheck",
  253. data: {url: targetDomain},
  254. success: function(data){
  255. if (data.error != undefined){
  256. }else if (data == "https"){
  257. $("#reqTls").parent().checkbox("set checked");
  258. }else if (data == "http"){
  259. $("#reqTls").parent().checkbox("set unchecked");
  260. }
  261. }
  262. })
  263. }
  264. function toggleBasicAuth() {
  265. var basicAuthDiv = document.getElementById('basicAuthOnly');
  266. if ($("#requireBasicAuth").parent().checkbox("is checked")) {
  267. $("#basicAuthCredentials").removeClass("disabled");
  268. } else {
  269. $("#basicAuthCredentials").addClass("disabled");
  270. }
  271. }
  272. $("#requireBasicAuth").on('change', toggleBasicAuth);
  273. toggleBasicAuth();
  274. function toggleRateLimit() {
  275. if ($("#requireRateLimit").parent().checkbox("is checked")) {
  276. $("#proxyRateLimit").parent().parent().removeClass("disabled");
  277. } else {
  278. $("#proxyRateLimit").parent().parent().addClass("disabled");
  279. }
  280. }
  281. $("#requireRateLimit").on('change', toggleRateLimit);
  282. toggleRateLimit();
  283. /*
  284. Credential Managements
  285. */
  286. let credentials = []; // Global variable to store credentials
  287. function addCredentials() {
  288. // Retrieve the username and password input values
  289. var username = $('#basicAuthCredUsername').val();
  290. var password = $('#basicAuthCredPassword').val();
  291. if(username == "" || password == ""){
  292. msgbox("Username or password cannot be empty", false, 5000);
  293. return;
  294. }
  295. // Create a new credential object
  296. var credential = {
  297. username: username,
  298. password: password
  299. };
  300. // Add the credential to the global credentials array
  301. credentials.push(credential);
  302. // Clear the input fields
  303. $('#basicAuthCredUsername').val('');
  304. $('#basicAuthCredPassword').val('');
  305. // Update the table body with the credentials
  306. updateTable();
  307. }
  308. function updateTable() {
  309. var tableBody = $('#basicAuthCredentialTable');
  310. tableBody.empty();
  311. if (credentials.length === 0) {
  312. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  313. } else {
  314. for (var i = 0; i < credentials.length; i++) {
  315. var credential = credentials[i];
  316. var username = credential.username;
  317. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  318. var row = '<tr>' +
  319. '<td>' + username + '</td>' +
  320. '<td>' + password + '</td>' +
  321. '<td><button class="ui basic button" onclick="removeCredential(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  322. '</tr>';
  323. tableBody.append(row);
  324. }
  325. }
  326. }
  327. function removeCredential(index) {
  328. // Remove the credential from the credentials array
  329. credentials.splice(index, 1);
  330. // Update the table body
  331. updateTable();
  332. }
  333. //Update v3.0.0
  334. //Since some proxy rules now contains wildcard characters
  335. //all uuid are converted to hex code before use in DOM selector
  336. String.prototype.hexEncode = function(){
  337. var hex, i;
  338. var result = "";
  339. for (i=0; i<this.length; i++) {
  340. hex = this.charCodeAt(i).toString(16);
  341. result += ("000"+hex).slice(-4);
  342. }
  343. return result
  344. }
  345. String.prototype.hexDecode = function(){
  346. var j;
  347. var hexes = this.match(/.{1,4}/g) || [];
  348. var back = "";
  349. for(j = 0; j<hexes.length; j++) {
  350. back += String.fromCharCode(parseInt(hexes[j], 16));
  351. }
  352. return back;
  353. }
  354. /*
  355. Access Rule dropdown Initialization
  356. */
  357. function initNewProxyRuleAccessDropdownList(callback=undefined){
  358. $.get("/api/access/list", function(data){
  359. if (data.error == undefined){
  360. $("#newProxyRuleAccessList").html("");
  361. data.forEach(function(rule){
  362. let icon = `<i class="ui grey filter icon"></i>`;
  363. if (rule.ID == "default"){
  364. icon = `<i class="ui yellow star icon"></i>`;
  365. }else if (rule.BlacklistEnabled && !rule.WhitelistEnabled){
  366. //This is a blacklist filter
  367. icon = `<i class="ui red filter icon"></i>`;
  368. }else if (rule.WhitelistEnabled && !rule.BlacklistEnabled){
  369. //This is a whitelist filter
  370. icon = `<i class="ui green filter icon"></i>`;
  371. }
  372. $("#newProxyRuleAccessList").append(`<div class="item" data-value="${rule.ID}">${icon} ${rule.Name}</div>`);
  373. });
  374. $("#newProxyRuleAccessFilter").parent().dropdown();
  375. if (callback != undefined){
  376. callback();
  377. }
  378. }else{
  379. msgbox("Access rule load failed: " + data.error, false);
  380. }
  381. })
  382. }
  383. initNewProxyRuleAccessDropdownList();
  384. //Bind on tab switch events
  385. tabSwitchEventBind["rules"] = function(){
  386. //Update the access rule list
  387. initNewProxyRuleAccessDropdownList();
  388. }
  389. $("#advanceProxyRules").accordion();
  390. $("#newProxyRuleAccessFilter").parent().dropdown();
  391. </script>