rules.html 24 KB

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