rules.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. <input type="number" id="proxyRateLimit" placeholder="100" min="1" max="1000" value="100">
  84. <small>The Rate Limit is applied to the whole proxy endpoint. If the number of requests exceeds the limit, the proxy will return a 429 error code.</small>
  85. </div>
  86. <div class="field">
  87. <div class="ui checkbox">
  88. <input type="checkbox" id="requireBasicAuth">
  89. <label>Require Basic Auth<br><small>Require client to login in order to view the page</small></label>
  90. </div>
  91. </div>
  92. <div id="basicAuthCredentials" class="field">
  93. <p>Enter the username and password for allowing them to access this proxy endpoint</p>
  94. <table class="ui very basic celled table">
  95. <thead>
  96. <tr>
  97. <th>Username</th>
  98. <th>Password</th>
  99. <th>Remove</th>
  100. </tr></thead>
  101. <tbody id="basicAuthCredentialTable">
  102. <tr>
  103. <td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td>
  104. </tr>
  105. </tbody>
  106. </table>
  107. <div class="three small fields credentialEntry">
  108. <div class="field">
  109. <input id="basicAuthCredUsername" type="text" placeholder="Username" autocomplete="off">
  110. </div>
  111. <div class="field">
  112. <input id="basicAuthCredPassword" type="password" placeholder="Password" autocomplete="off">
  113. </div>
  114. <div class="field">
  115. <button class="ui basic button" onclick="addCredentials();"><i class="blue add icon"></i> Add Credential</button>
  116. </div>
  117. </div>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. <br>
  123. <button class="ui basic button" onclick="newProxyEndpoint();"><i class="green add icon"></i> Create Endpoint</button>
  124. <br><br>
  125. </div>
  126. </div>
  127. </div>
  128. <div class="six wide column">
  129. <div class="ui basic segment rulesInstructions">
  130. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Domain</span><br>
  131. Example of domain matching keyword:<br>
  132. <code>arozos.com</code> <br>Any acess requesting arozos.com will be proxy to the IP address below<br>
  133. <div class="ui divider"></div>
  134. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Subdomain</span><br>
  135. Example of subdomain matching keyword:<br>
  136. <code>s1.arozos.com</code> <br>Any request starting with s1.arozos.com will be proxy to the IP address below<br>
  137. <div class="ui divider"></div>
  138. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Wildcard</span><br>
  139. Example of wildcard matching keyword:<br>
  140. <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>
  141. <div class="ui list">
  142. <div class="item"><code>www.arozos.com</code></div>
  143. <div class="item"><code>foo.bar.arozos.com</code></div>
  144. </div>
  145. <br>
  146. </div>
  147. </div>
  148. </div>
  149. </div>
  150. <script>
  151. //New Proxy Endpoint
  152. function newProxyEndpoint(){
  153. var rootname = $("#rootname").val();
  154. var proxyDomain = $("#proxyDomain").val();
  155. var useTLS = $("#reqTls")[0].checked;
  156. var skipTLSValidation = $("#skipTLSValidation")[0].checked;
  157. var bypassGlobalTLS = $("#bypassGlobalTLS")[0].checked;
  158. var requireBasicAuth = $("#requireBasicAuth")[0].checked;
  159. var proxyRateLimit = $("#proxyRateLimit").val();
  160. var requireRateLimit = $("#requireRateLimit")[0].checked;
  161. var skipWebSocketOriginCheck = $("#skipWebsocketOriginCheck")[0].checked;
  162. var accessRuleToUse = $("#newProxyRuleAccessFilter").val();
  163. if (rootname.trim() == ""){
  164. $("#rootname").parent().addClass("error");
  165. return
  166. }else{
  167. $("#rootname").parent().removeClass("error");
  168. }
  169. if (proxyDomain.trim() == ""){
  170. $("#proxyDomain").parent().addClass("error");
  171. return
  172. }else{
  173. $("#proxyDomain").parent().removeClass("error");
  174. }
  175. //Create the endpoint by calling add
  176. $.ajax({
  177. url: "/api/proxy/add",
  178. data: {
  179. type: "host",
  180. rootname: rootname,
  181. tls: useTLS,
  182. ep: proxyDomain,
  183. tlsval: skipTLSValidation,
  184. bpwsorg: skipWebSocketOriginCheck,
  185. bypassGlobalTLS: bypassGlobalTLS,
  186. bauth: requireBasicAuth,
  187. rate: requireRateLimit,
  188. ratenum: proxyRateLimit,
  189. cred: JSON.stringify(credentials),
  190. access: accessRuleToUse,
  191. },
  192. success: function(data){
  193. if (data.error != undefined){
  194. msgbox(data.error, false, 5000);
  195. }else{
  196. //Clear old data
  197. $("#rootname").val("");
  198. $("#proxyDomain").val("");
  199. credentials = [];
  200. updateTable();
  201. reloadUptimeList();
  202. //Check if it is a new subdomain and TLS enabled
  203. if ($("#tls").checkbox("is checked")){
  204. confirmBox("Request new SSL Cert for this subdomain?", function(choice){
  205. if (choice == true){
  206. //Load the prefer CA from TLS page
  207. let defaultCA = $("#defaultCA").dropdown("get value");
  208. if (defaultCA.trim() == ""){
  209. defaultCA = "Let's Encrypt";
  210. }
  211. //Get a new cert using ACME
  212. msgbox("Requesting certificate via " + defaultCA +"...");
  213. console.log("Trying to get a new certificate via ACME");
  214. //Request ACME for certificate, see cert.html component
  215. obtainCertificate(rootname, defaultCA.trim(), function(){
  216. // Renew the parent certificate list
  217. initManagedDomainCertificateList();
  218. });
  219. }else{
  220. msgbox("Proxy Endpoint Added");
  221. }
  222. });
  223. }else{
  224. msgbox("Proxy Endpoint Added");
  225. }
  226. }
  227. }
  228. });
  229. }
  230. //Generic functions for delete rp endpoints
  231. function deleteEndpoint(epoint){
  232. epoint = decodeURIComponent(epoint).hexDecode();
  233. if (confirm("Confirm remove proxy for :" + epoint + "?")){
  234. $.ajax({
  235. url: "/api/proxy/del",
  236. data: {ep: epoint, },
  237. success: function(){
  238. listProxyEndpoints();
  239. msgbox("Proxy Rule Deleted", true);
  240. reloadUptimeList();
  241. }
  242. })
  243. }
  244. }
  245. function autoCheckTls(targetDomain){
  246. $.ajax({
  247. url: "/api/proxy/tlscheck",
  248. data: {url: targetDomain},
  249. success: function(data){
  250. if (data.error != undefined){
  251. }else if (data == "https"){
  252. $("#reqTls").parent().checkbox("set checked");
  253. }else if (data == "http"){
  254. $("#reqTls").parent().checkbox("set unchecked");
  255. }
  256. }
  257. })
  258. }
  259. function toggleBasicAuth() {
  260. var basicAuthDiv = document.getElementById('basicAuthOnly');
  261. if ($("#requireBasicAuth").parent().checkbox("is checked")) {
  262. $("#basicAuthCredentials").removeClass("disabled");
  263. } else {
  264. $("#basicAuthCredentials").addClass("disabled");
  265. }
  266. }
  267. $("#requireBasicAuth").on('change', toggleBasicAuth);
  268. toggleBasicAuth();
  269. function toggleRateLimit() {
  270. if ($("#requireRateLimit").parent().checkbox("is checked")) {
  271. $("#proxyRateLimit").parent().removeClass("disabled");
  272. } else {
  273. $("#proxyRateLimit").parent().addClass("disabled");
  274. }
  275. }
  276. $("#requireRateLimit").on('change', toggleRateLimit);
  277. toggleRateLimit();
  278. /*
  279. Credential Managements
  280. */
  281. let credentials = []; // Global variable to store credentials
  282. function addCredentials() {
  283. // Retrieve the username and password input values
  284. var username = $('#basicAuthCredUsername').val();
  285. var password = $('#basicAuthCredPassword').val();
  286. if(username == "" || password == ""){
  287. msgbox("Username or password cannot be empty", false, 5000);
  288. return;
  289. }
  290. // Create a new credential object
  291. var credential = {
  292. username: username,
  293. password: password
  294. };
  295. // Add the credential to the global credentials array
  296. credentials.push(credential);
  297. // Clear the input fields
  298. $('#basicAuthCredUsername').val('');
  299. $('#basicAuthCredPassword').val('');
  300. // Update the table body with the credentials
  301. updateTable();
  302. }
  303. function updateTable() {
  304. var tableBody = $('#basicAuthCredentialTable');
  305. tableBody.empty();
  306. if (credentials.length === 0) {
  307. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  308. } else {
  309. for (var i = 0; i < credentials.length; i++) {
  310. var credential = credentials[i];
  311. var username = credential.username;
  312. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  313. var row = '<tr>' +
  314. '<td>' + username + '</td>' +
  315. '<td>' + password + '</td>' +
  316. '<td><button class="ui basic button" onclick="removeCredential(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  317. '</tr>';
  318. tableBody.append(row);
  319. }
  320. }
  321. }
  322. function removeCredential(index) {
  323. // Remove the credential from the credentials array
  324. credentials.splice(index, 1);
  325. // Update the table body
  326. updateTable();
  327. }
  328. //Update v3.0.0
  329. //Since some proxy rules now contains wildcard characters
  330. //all uuid are converted to hex code before use in DOM selector
  331. String.prototype.hexEncode = function(){
  332. var hex, i;
  333. var result = "";
  334. for (i=0; i<this.length; i++) {
  335. hex = this.charCodeAt(i).toString(16);
  336. result += ("000"+hex).slice(-4);
  337. }
  338. return result
  339. }
  340. String.prototype.hexDecode = function(){
  341. var j;
  342. var hexes = this.match(/.{1,4}/g) || [];
  343. var back = "";
  344. for(j = 0; j<hexes.length; j++) {
  345. back += String.fromCharCode(parseInt(hexes[j], 16));
  346. }
  347. return back;
  348. }
  349. /*
  350. Access Rule dropdown Initialization
  351. */
  352. function initNewProxyRuleAccessDropdownList(callback=undefined){
  353. $.get("/api/access/list", function(data){
  354. if (data.error == undefined){
  355. $("#newProxyRuleAccessList").html("");
  356. data.forEach(function(rule){
  357. let icon = `<i class="ui grey filter icon"></i>`;
  358. if (rule.ID == "default"){
  359. icon = `<i class="ui yellow star icon"></i>`;
  360. }else if (rule.BlacklistEnabled && !rule.WhitelistEnabled){
  361. //This is a blacklist filter
  362. icon = `<i class="ui red filter icon"></i>`;
  363. }else if (rule.WhitelistEnabled && !rule.BlacklistEnabled){
  364. //This is a whitelist filter
  365. icon = `<i class="ui green filter icon"></i>`;
  366. }
  367. $("#newProxyRuleAccessList").append(`<div class="item" data-value="${rule.ID}">${icon} ${rule.Name}</div>`);
  368. });
  369. $("#newProxyRuleAccessFilter").parent().dropdown();
  370. if (callback != undefined){
  371. callback();
  372. }
  373. }else{
  374. msgbox("Access rule load failed: " + data.error, false);
  375. }
  376. })
  377. }
  378. initNewProxyRuleAccessDropdownList();
  379. //Bind on tab switch events
  380. tabSwitchEventBind["rules"] = function(){
  381. //Update the access rule list
  382. initNewProxyRuleAccessDropdownList();
  383. }
  384. $(document).ready(function(){
  385. $("#advanceProxyRules").accordion();
  386. $("#newProxyRuleAccessFilter").parent().dropdown();
  387. });
  388. </script>