1
0

rules.html 23 KB

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