rules.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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" tourstep="matchingkeyword">
  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" tourstep="targetdomain">
  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" tourstep="requireTLS">
  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 advanceoptions">
  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" tourstep="skipTLSValidation">
  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. <div tourstep="newProxyRule" style="display: inline-block;">
  154. <button class="ui basic button" onclick="newProxyEndpoint();"><i class="green add icon"></i> Create Endpoint</button>
  155. </div>
  156. <br><br>
  157. </div>
  158. </div>
  159. </div>
  160. <div class="six wide column">
  161. <div class="ui basic segment rulesInstructions">
  162. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Domain</span><br>
  163. Example of domain matching keyword:<br>
  164. <code>aroz.org</code> <br>Any acess requesting aroz.org will be proxy to the IP address below<br>
  165. <div class="ui divider"></div>
  166. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Subdomain</span><br>
  167. Example of subdomain matching keyword:<br>
  168. <code>s1.aroz.org</code> <br>Any request starting with s1.aroz.org will be proxy to the IP address below<br>
  169. <div class="ui divider"></div>
  170. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Wildcard</span><br>
  171. Example of wildcard matching keyword:<br>
  172. <code>*.aroz.org</code> <br>Any request with a host name matching *.aroz.org will be proxy to the IP address below. Here are some examples.<br>
  173. <div class="ui list">
  174. <div class="item"><code>www.aroz.org</code></div>
  175. <div class="item"><code>foo.bar.aroz.org</code></div>
  176. </div>
  177. <br>
  178. </div>
  179. </div>
  180. </div>
  181. </div>
  182. <script>
  183. //New Proxy Endpoint
  184. function newProxyEndpoint(){
  185. let rootname = $("#rootname").val();
  186. let proxyDomain = $("#proxyDomain").val();
  187. let useTLS = $("#reqTls")[0].checked;
  188. let skipTLSValidation = $("#skipTLSValidation")[0].checked;
  189. let bypassGlobalTLS = $("#bypassGlobalTLS")[0].checked;
  190. let requireBasicAuth = $("#requireBasicAuth")[0].checked;
  191. let proxyRateLimit = $("#proxyRateLimit").val();
  192. let requireRateLimit = $("#requireRateLimit")[0].checked;
  193. let skipWebSocketOriginCheck = $("#skipWebsocketOriginCheck")[0].checked;
  194. let accessRuleToUse = $("#newProxyRuleAccessFilter").val();
  195. let useStickySessionLB = $("#useStickySessionLB")[0].checked;
  196. if (rootname.trim() == ""){
  197. $("#rootname").parent().addClass("error");
  198. return
  199. }else{
  200. $("#rootname").parent().removeClass("error");
  201. }
  202. if (proxyDomain.trim() == ""){
  203. $("#proxyDomain").parent().addClass("error");
  204. return
  205. }else{
  206. $("#proxyDomain").parent().removeClass("error");
  207. }
  208. //Create the endpoint by calling add
  209. $.cjax({
  210. url: "/api/proxy/add",
  211. method: "POST",
  212. data: {
  213. type: "host",
  214. rootname: rootname,
  215. tls: useTLS,
  216. ep: proxyDomain,
  217. tlsval: skipTLSValidation,
  218. bpwsorg: skipWebSocketOriginCheck,
  219. bypassGlobalTLS: bypassGlobalTLS,
  220. bauth: requireBasicAuth,
  221. rate: requireRateLimit,
  222. ratenum: proxyRateLimit,
  223. cred: JSON.stringify(credentials),
  224. access: accessRuleToUse,
  225. stickysess: useStickySessionLB,
  226. },
  227. success: function(data){
  228. if (data.error != undefined){
  229. msgbox(data.error, false, 5000);
  230. }else{
  231. //Clear old data
  232. $("#rootname").val("");
  233. $("#proxyDomain").val("");
  234. credentials = [];
  235. updateTable();
  236. reloadUptimeList();
  237. //Check if it is a new subdomain and TLS enabled
  238. if ($("#tls").checkbox("is checked")){
  239. confirmBox("Request new SSL Cert for this subdomain?", function(choice){
  240. if (choice == true){
  241. //Load the prefer CA from TLS page
  242. let defaultCA = $("#defaultCA").dropdown("get value");
  243. if (defaultCA.trim() == ""){
  244. defaultCA = "Let's Encrypt";
  245. }
  246. //Get a new cert using ACME
  247. msgbox("Requesting certificate via " + defaultCA +"...");
  248. console.log("Trying to get a new certificate via ACME");
  249. //Request ACME for certificate, see cert.html component
  250. obtainCertificate(rootname, defaultCA.trim(), function(){
  251. // Renew the parent certificate list
  252. initManagedDomainCertificateList();
  253. });
  254. }else{
  255. msgbox("Proxy Endpoint Added");
  256. }
  257. });
  258. }else{
  259. msgbox("Proxy Endpoint Added");
  260. }
  261. }
  262. }
  263. });
  264. }
  265. //Clearn the proxy target value, make sure user do not enter http:// or https://
  266. //and auto select TLS checkbox if https:// exists
  267. function autoFillTargetTLS(input){
  268. let targetDomain = $(input).val().trim();
  269. if (targetDomain.startsWith("http://")){
  270. targetDomain = targetDomain.substr(7);
  271. $(input).val(targetDomain);
  272. $("#reqTls").parent().checkbox("set unchecked");
  273. }else if (targetDomain.startsWith("https://")){
  274. targetDomain = targetDomain.substr(8);
  275. $(input).val(targetDomain);
  276. $("#reqTls").parent().checkbox("set checked");
  277. }else{
  278. //No http or https was given. Sniff it
  279. autoCheckTls(targetDomain);
  280. }
  281. }
  282. //Automatic check if the site require TLS and check the checkbox if needed
  283. function autoCheckTls(targetDomain){
  284. $.cjax({
  285. url: "/api/proxy/tlscheck",
  286. data: {url: targetDomain},
  287. success: function(data){
  288. if (data.error != undefined){
  289. msgbox(data.error, false);
  290. }else if (data == "https"){
  291. $("#reqTls").parent().checkbox("set checked");
  292. }else if (data == "http"){
  293. $("#reqTls").parent().checkbox("set unchecked");
  294. }
  295. }
  296. })
  297. }
  298. function toggleBasicAuth() {
  299. var basicAuthDiv = document.getElementById('basicAuthOnly');
  300. if ($("#requireBasicAuth").parent().checkbox("is checked")) {
  301. $("#basicAuthCredentials").removeClass("disabled");
  302. } else {
  303. $("#basicAuthCredentials").addClass("disabled");
  304. }
  305. }
  306. $("#requireBasicAuth").on('change', toggleBasicAuth);
  307. toggleBasicAuth();
  308. function toggleRateLimit() {
  309. if ($("#requireRateLimit").parent().checkbox("is checked")) {
  310. $("#proxyRateLimit").parent().parent().removeClass("disabled");
  311. } else {
  312. $("#proxyRateLimit").parent().parent().addClass("disabled");
  313. }
  314. }
  315. $("#requireRateLimit").on('change', toggleRateLimit);
  316. toggleRateLimit();
  317. /*
  318. Credential Managements
  319. */
  320. let credentials = []; // Global variable to store credentials
  321. function addCredentials() {
  322. // Retrieve the username and password input values
  323. var username = $('#basicAuthCredUsername').val();
  324. var password = $('#basicAuthCredPassword').val();
  325. if(username == "" || password == ""){
  326. msgbox("Username or password cannot be empty", false, 5000);
  327. return;
  328. }
  329. // Create a new credential object
  330. var credential = {
  331. username: username,
  332. password: password
  333. };
  334. // Add the credential to the global credentials array
  335. credentials.push(credential);
  336. // Clear the input fields
  337. $('#basicAuthCredUsername').val('');
  338. $('#basicAuthCredPassword').val('');
  339. // Update the table body with the credentials
  340. updateTable();
  341. }
  342. function updateTable() {
  343. var tableBody = $('#basicAuthCredentialTable');
  344. tableBody.empty();
  345. if (credentials.length === 0) {
  346. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  347. } else {
  348. for (var i = 0; i < credentials.length; i++) {
  349. var credential = credentials[i];
  350. var username = credential.username;
  351. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  352. var row = '<tr>' +
  353. '<td>' + username + '</td>' +
  354. '<td>' + password + '</td>' +
  355. '<td><button class="ui basic button" onclick="removeCredential(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  356. '</tr>';
  357. tableBody.append(row);
  358. }
  359. }
  360. }
  361. function removeCredential(index) {
  362. // Remove the credential from the credentials array
  363. credentials.splice(index, 1);
  364. // Update the table body
  365. updateTable();
  366. }
  367. //Update v3.0.0
  368. //Since some proxy rules now contains wildcard characters
  369. //all uuid are converted to hex code before use in DOM selector
  370. String.prototype.hexEncode = function(){
  371. var hex, i;
  372. var result = "";
  373. for (i=0; i<this.length; i++) {
  374. hex = this.charCodeAt(i).toString(16);
  375. result += ("000"+hex).slice(-4);
  376. }
  377. return result
  378. }
  379. String.prototype.hexDecode = function(){
  380. var j;
  381. var hexes = this.match(/.{1,4}/g) || [];
  382. var back = "";
  383. for(j = 0; j<hexes.length; j++) {
  384. back += String.fromCharCode(parseInt(hexes[j], 16));
  385. }
  386. return back;
  387. }
  388. /*
  389. Access Rule dropdown Initialization
  390. */
  391. function initNewProxyRuleAccessDropdownList(callback=undefined){
  392. $.get("/api/access/list", function(data){
  393. if (data.error == undefined){
  394. $("#newProxyRuleAccessList").html("");
  395. data.forEach(function(rule){
  396. let icon = `<i class="ui grey filter icon"></i>`;
  397. if (rule.ID == "default"){
  398. icon = `<i class="ui yellow star icon"></i>`;
  399. }else if (rule.BlacklistEnabled && !rule.WhitelistEnabled){
  400. //This is a blacklist filter
  401. icon = `<i class="ui red filter icon"></i>`;
  402. }else if (rule.WhitelistEnabled && !rule.BlacklistEnabled){
  403. //This is a whitelist filter
  404. icon = `<i class="ui green filter icon"></i>`;
  405. }
  406. $("#newProxyRuleAccessList").append(`<div class="item" data-value="${rule.ID}">${icon} ${rule.Name}</div>`);
  407. });
  408. $("#newProxyRuleAccessFilter").parent().dropdown();
  409. if (callback != undefined){
  410. callback();
  411. }
  412. }else{
  413. msgbox("Access rule load failed: " + data.error, false);
  414. }
  415. })
  416. }
  417. initNewProxyRuleAccessDropdownList();
  418. //Bind on tab switch events
  419. tabSwitchEventBind["rules"] = function(){
  420. //Update the access rule list
  421. initNewProxyRuleAccessDropdownList();
  422. }
  423. /* Docker Optimizations */
  424. function initDockerUXOptimizations(){
  425. $.get("/api/docker/available", function(dockerAvailable){
  426. if (dockerAvailable){
  427. $(".dockerOptimizations").show();
  428. }else{
  429. $(".dockerOptimizations").hide();
  430. }
  431. });
  432. }
  433. initDockerUXOptimizations();
  434. function openDockerContainersList(){
  435. showSideWrapper('snippet/dockerContainersList.html');
  436. }
  437. function addContainerItem(item) {
  438. $('#rootname').val(item.name);
  439. $('#proxyDomain').val(`${item.ip}:${item.port}`)
  440. hideSideWrapper(true);
  441. }
  442. /* UI Element Initialization */
  443. function initAdvanceSettingsAccordion(){
  444. function hasClickEvent(element) {
  445. var events = $._data(element, "events");
  446. return events && events.click && events.click.length > 0;
  447. }
  448. if (!hasClickEvent($("#advanceProxyRules"))){
  449. // Not sure why sometime the accordion events are not binding
  450. // to the DOM element. This makes sure the element is binded
  451. // correctly by checking it again after 300ms
  452. $("#advanceProxyRules").accordion();
  453. $("#newProxyRuleAccessFilter").parent().dropdown();
  454. setTimeout(function(){
  455. initAdvanceSettingsAccordion();
  456. }, 300);
  457. }
  458. }
  459. initAdvanceSettingsAccordion();
  460. </script>