rules.html 23 KB

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