1
0

rules.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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">
  32. <div class="ui checkbox">
  33. <input type="checkbox" id="reqTls">
  34. <label>Proxy Target require TLS Connection <br><small>(i.e. Your proxy target starts with https://)</small></label>
  35. </div>
  36. </div>
  37. <!-- Advance configs -->
  38. <div class="ui basic segment" style="background-color: #f7f7f7; border-radius: 1em;">
  39. <div id="advanceProxyRules" class="ui fluid accordion">
  40. <div class="title">
  41. <i class="dropdown icon"></i>
  42. Advance Settings
  43. </div>
  44. <div class="content">
  45. <div class="field">
  46. <label>Access Rule</label>
  47. <div class="ui selection dropdown">
  48. <input type="hidden" id="newProxyRuleAccessFilter" value="default">
  49. <i class="dropdown icon"></i>
  50. <div class="default text">Default</div>
  51. <div class="menu" id="newProxyRuleAccessList">
  52. <div class="item" data-value="default"><i class="ui yellow star icon"></i> Default</div>
  53. </div>
  54. </div>
  55. <small>Allow regional access control using blacklist or whitelist. Use "default" for "allow all".</small>
  56. </div>
  57. <div class="field">
  58. <div class="ui checkbox">
  59. <input type="checkbox" id="skipTLSValidation">
  60. <label>Ignore TLS/SSL Verification Error<br><small>For targets that is using self-signed, expired certificate (Not Recommended)</small></label>
  61. </div>
  62. </div>
  63. <div class="field">
  64. <div class="ui checkbox">
  65. <input type="checkbox" id="skipWebsocketOriginCheck" checked>
  66. <label>Skip WebSocket Origin Check<br><small>Allow cross-origin websocket requests (Usually not a security concern)</small></label>
  67. </div>
  68. </div>
  69. <div class="field">
  70. <div class="ui checkbox">
  71. <input type="checkbox" id="bypassGlobalTLS">
  72. <label>Allow plain HTTP access<br><small>Allow this subdomain to be connected without TLS (Require HTTP server enabled on port 80)</small></label>
  73. </div>
  74. </div>
  75. <div class="field">
  76. <div class="ui checkbox">
  77. <input type="checkbox" id="requireBasicAuth">
  78. <label>Require Basic Auth<br><small>Require client to login in order to view the page</small></label>
  79. </div>
  80. </div>
  81. <div id="basicAuthCredentials" class="field">
  82. <p>Enter the username and password for allowing them to access this proxy endpoint</p>
  83. <table class="ui very basic celled table">
  84. <thead>
  85. <tr>
  86. <th>Username</th>
  87. <th>Password</th>
  88. <th>Remove</th>
  89. </tr></thead>
  90. <tbody id="basicAuthCredentialTable">
  91. <tr>
  92. <td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td>
  93. </tr>
  94. </tbody>
  95. </table>
  96. <div class="three small fields credentialEntry">
  97. <div class="field">
  98. <input id="basicAuthCredUsername" type="text" placeholder="Username" autocomplete="off">
  99. </div>
  100. <div class="field">
  101. <input id="basicAuthCredPassword" type="password" placeholder="Password" autocomplete="off">
  102. </div>
  103. <div class="field">
  104. <button class="ui basic button" onclick="addCredentials();"><i class="blue add icon"></i> Add Credential</button>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. </div>
  110. </div>
  111. <br>
  112. <button class="ui basic button" onclick="newProxyEndpoint();"><i class="green add icon"></i> Create Endpoint</button>
  113. <br><br>
  114. </div>
  115. </div>
  116. </div>
  117. <div class="six wide column">
  118. <div class="ui basic segment rulesInstructions">
  119. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Domain</span><br>
  120. Example of domain matching keyword:<br>
  121. <code>arozos.com</code> <br>Any acess requesting arozos.com will be proxy to the IP address below<br>
  122. <div class="ui divider"></div>
  123. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Subdomain</span><br>
  124. Example of subdomain matching keyword:<br>
  125. <code>s1.arozos.com</code> <br>Any request starting with s1.arozos.com will be proxy to the IP address below<br>
  126. <div class="ui divider"></div>
  127. <span style="font-size: 1.2em; font-weight: 300;"><i class="ui yellow star icon"></i> Wildcard</span><br>
  128. Example of wildcard matching keyword:<br>
  129. <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>
  130. <div class="ui list">
  131. <div class="item"><code>www.arozos.com</code></div>
  132. <div class="item"><code>foo.bar.arozos.com</code></div>
  133. </div>
  134. <br>
  135. </div>
  136. </div>
  137. </div>
  138. </div>
  139. <script>
  140. //New Proxy Endpoint
  141. function newProxyEndpoint(){
  142. var rootname = $("#rootname").val();
  143. var proxyDomain = $("#proxyDomain").val();
  144. var useTLS = $("#reqTls")[0].checked;
  145. var skipTLSValidation = $("#skipTLSValidation")[0].checked;
  146. var bypassGlobalTLS = $("#bypassGlobalTLS")[0].checked;
  147. var requireBasicAuth = $("#requireBasicAuth")[0].checked;
  148. var skipWebSocketOriginCheck = $("#skipWebsocketOriginCheck")[0].checked;
  149. var accessRuleToUse = $("#newProxyRuleAccessFilter").val();
  150. if (rootname.trim() == ""){
  151. $("#rootname").parent().addClass("error");
  152. return
  153. }else{
  154. $("#rootname").parent().removeClass("error");
  155. }
  156. if (proxyDomain.trim() == ""){
  157. $("#proxyDomain").parent().addClass("error");
  158. return
  159. }else{
  160. $("#proxyDomain").parent().removeClass("error");
  161. }
  162. //Create the endpoint by calling add
  163. $.ajax({
  164. url: "/api/proxy/add",
  165. data: {
  166. type: "host",
  167. rootname: rootname,
  168. tls: useTLS,
  169. ep: proxyDomain,
  170. tlsval: skipTLSValidation,
  171. bpwsorg: skipWebSocketOriginCheck,
  172. bypassGlobalTLS: bypassGlobalTLS,
  173. bauth: requireBasicAuth,
  174. cred: JSON.stringify(credentials),
  175. access: accessRuleToUse,
  176. },
  177. success: function(data){
  178. if (data.error != undefined){
  179. msgbox(data.error, false, 5000);
  180. }else{
  181. //Clear old data
  182. $("#rootname").val("");
  183. $("#proxyDomain").val("");
  184. credentials = [];
  185. updateTable();
  186. reloadUptimeList();
  187. //Check if it is a new subdomain and TLS enabled
  188. if ($("#tls").checkbox("is checked")){
  189. confirmBox("Request new SSL Cert for this subdomain?", function(choice){
  190. if (choice == true){
  191. //Load the prefer CA from TLS page
  192. let defaultCA = $("#defaultCA").dropdown("get value");
  193. if (defaultCA.trim() == ""){
  194. defaultCA = "Let's Encrypt";
  195. }
  196. //Get a new cert using ACME
  197. msgbox("Requesting certificate via " + defaultCA +"...");
  198. console.log("Trying to get a new certificate via ACME");
  199. //Request ACME for certificate, see cert.html component
  200. obtainCertificate(rootname, defaultCA.trim(), function(){
  201. // Renew the parent certificate list
  202. initManagedDomainCertificateList();
  203. });
  204. }else{
  205. msgbox("Proxy Endpoint Added");
  206. }
  207. });
  208. }else{
  209. msgbox("Proxy Endpoint Added");
  210. }
  211. }
  212. }
  213. });
  214. }
  215. //Generic functions for delete rp endpoints
  216. function deleteEndpoint(epoint){
  217. epoint = decodeURIComponent(epoint).hexDecode();
  218. if (confirm("Confirm remove proxy for :" + epoint + "?")){
  219. $.ajax({
  220. url: "/api/proxy/del",
  221. data: {ep: epoint, },
  222. success: function(){
  223. listProxyEndpoints();
  224. msgbox("Proxy Rule Deleted", true);
  225. reloadUptimeList();
  226. }
  227. })
  228. }
  229. }
  230. function autoCheckTls(targetDomain){
  231. $.ajax({
  232. url: "/api/proxy/tlscheck",
  233. data: {url: targetDomain},
  234. success: function(data){
  235. if (data.error != undefined){
  236. }else if (data == "https"){
  237. $("#reqTls").parent().checkbox("set checked");
  238. }else if (data == "http"){
  239. $("#reqTls").parent().checkbox("set unchecked");
  240. }
  241. }
  242. })
  243. }
  244. function toggleBasicAuth() {
  245. var basicAuthDiv = document.getElementById('basicAuthOnly');
  246. if ($("#requireBasicAuth").parent().checkbox("is checked")) {
  247. $("#basicAuthCredentials").removeClass("disabled");
  248. } else {
  249. $("#basicAuthCredentials").addClass("disabled");
  250. }
  251. }
  252. $("#requireBasicAuth").on('change', toggleBasicAuth);
  253. toggleBasicAuth();
  254. /*
  255. Credential Managements
  256. */
  257. let credentials = []; // Global variable to store credentials
  258. function addCredentials() {
  259. // Retrieve the username and password input values
  260. var username = $('#basicAuthCredUsername').val();
  261. var password = $('#basicAuthCredPassword').val();
  262. if(username == "" || password == ""){
  263. msgbox("Username or password cannot be empty", false, 5000);
  264. return;
  265. }
  266. // Create a new credential object
  267. var credential = {
  268. username: username,
  269. password: password
  270. };
  271. // Add the credential to the global credentials array
  272. credentials.push(credential);
  273. // Clear the input fields
  274. $('#basicAuthCredUsername').val('');
  275. $('#basicAuthCredPassword').val('');
  276. // Update the table body with the credentials
  277. updateTable();
  278. }
  279. function updateTable() {
  280. var tableBody = $('#basicAuthCredentialTable');
  281. tableBody.empty();
  282. if (credentials.length === 0) {
  283. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  284. } else {
  285. for (var i = 0; i < credentials.length; i++) {
  286. var credential = credentials[i];
  287. var username = credential.username;
  288. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  289. var row = '<tr>' +
  290. '<td>' + username + '</td>' +
  291. '<td>' + password + '</td>' +
  292. '<td><button class="ui basic button" onclick="removeCredential(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  293. '</tr>';
  294. tableBody.append(row);
  295. }
  296. }
  297. }
  298. function removeCredential(index) {
  299. // Remove the credential from the credentials array
  300. credentials.splice(index, 1);
  301. // Update the table body
  302. updateTable();
  303. }
  304. //Update v3.0.0
  305. //Since some proxy rules now contains wildcard characters
  306. //all uuid are converted to hex code before use in DOM selector
  307. String.prototype.hexEncode = function(){
  308. var hex, i;
  309. var result = "";
  310. for (i=0; i<this.length; i++) {
  311. hex = this.charCodeAt(i).toString(16);
  312. result += ("000"+hex).slice(-4);
  313. }
  314. return result
  315. }
  316. String.prototype.hexDecode = function(){
  317. var j;
  318. var hexes = this.match(/.{1,4}/g) || [];
  319. var back = "";
  320. for(j = 0; j<hexes.length; j++) {
  321. back += String.fromCharCode(parseInt(hexes[j], 16));
  322. }
  323. return back;
  324. }
  325. /*
  326. Access Rule dropdown Initialization
  327. */
  328. function initNewProxyRuleAccessDropdownList(callback=undefined){
  329. $.get("/api/access/list", function(data){
  330. if (data.error == undefined){
  331. $("#newProxyRuleAccessList").html("");
  332. data.forEach(function(rule){
  333. let icon = `<i class="ui grey filter icon"></i>`;
  334. if (rule.ID == "default"){
  335. icon = `<i class="ui yellow star icon"></i>`;
  336. }else if (rule.BlacklistEnabled && !rule.WhitelistEnabled){
  337. //This is a blacklist filter
  338. icon = `<i class="ui red filter icon"></i>`;
  339. }else if (rule.WhitelistEnabled && !rule.BlacklistEnabled){
  340. //This is a whitelist filter
  341. icon = `<i class="ui green filter icon"></i>`;
  342. }
  343. $("#newProxyRuleAccessList").append(`<div class="item" data-value="${rule.ID}">${icon} ${rule.Name}</div>`);
  344. });
  345. $("#newProxyRuleAccessFilter").parent().dropdown();
  346. if (callback != undefined){
  347. callback();
  348. }
  349. }else{
  350. msgbox("Access rule load failed: " + data.error, false);
  351. }
  352. })
  353. }
  354. initNewProxyRuleAccessDropdownList();
  355. //Bind on tab switch events
  356. tabSwitchEventBind["rules"] = function(){
  357. //Update the access rule list
  358. initNewProxyRuleAccessDropdownList();
  359. }
  360. $(document).ready(function(){
  361. $("#advanceProxyRules").accordion();
  362. $("#newProxyRuleAccessFilter").parent().dropdown();
  363. });
  364. </script>