1
0

rules.html 21 KB

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