httprp.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>HTTP Proxy</h2>
  4. <p>Proxy HTTP server with HTTP or HTTPS for multiple hosts. If you are only proxying for one host / domain, use Default Site instead.</p>
  5. </div>
  6. <style>
  7. #httpProxyList .ui.toggle.checkbox input:checked ~ label::before{
  8. background-color: #00ca52 !important;
  9. }
  10. .subdEntry td:not(.ignoremw){
  11. min-width: 200px;
  12. }
  13. </style>
  14. <div style="width: 100%; overflow-x: auto; margin-bottom: 1em; min-height: 300px;">
  15. <table class="ui celled sortable unstackable compact table">
  16. <thead>
  17. <tr>
  18. <th>Host</th>
  19. <th>Destination</th>
  20. <th>Virtual Directory</th>
  21. <th style="max-width: 300px;">Advanced Settings</th>
  22. <th class="no-sort" style="min-width:150px;">Actions</th>
  23. </tr>
  24. </thead>
  25. <tbody id="httpProxyList">
  26. </tbody>
  27. </table>
  28. </div>
  29. <button class="ui icon right floated basic button" onclick="listProxyEndpoints();"><i class="green refresh icon"></i> Refresh</button>
  30. <br><br>
  31. </div>
  32. <script>
  33. /* List all proxy endpoints */
  34. function listProxyEndpoints(){
  35. $.get("/api/proxy/list?type=host", function(data){
  36. $("#httpProxyList").html(``);
  37. if (data.error !== undefined){
  38. $("#httpProxyList").append(`<tr>
  39. <td data-label="" colspan="5"><i class="remove icon"></i> ${data.error}</td>
  40. </tr>`);
  41. }else if (data.length == 0){
  42. $("#httpProxyList").append(`<tr>
  43. <td data-label="" colspan="5"><i class="green check circle icon"></i> No HTTP Proxy Record</td>
  44. </tr>`);
  45. }else{
  46. //Sort by RootOrMatchingDomain field
  47. data.sort((a,b) => (a.RootOrMatchingDomain > b.RootOrMatchingDomain) ? 1 : ((b.RootOrMatchingDomain > a.RootOrMatchingDomain) ? -1 : 0))
  48. data.forEach(subd => {
  49. let tlsIcon = "";
  50. let subdData = encodeURIComponent(JSON.stringify(subd));
  51. if (subd.RequireTLS){
  52. tlsIcon = `<i class="green lock icon" title="TLS Mode"></i>`;
  53. if (subd.SkipCertValidations){
  54. tlsIcon = `<i class="yellow lock icon" title="TLS/SSL mode without verification"></i>`
  55. }
  56. }
  57. let inboundTlsIcon = "";
  58. if ($("#tls").checkbox("is checked")){
  59. inboundTlsIcon = `<i class="green lock icon" title="TLS Mode"></i>`;
  60. if (subd.BypassGlobalTLS){
  61. inboundTlsIcon = `<i class="grey lock icon" title="TLS Bypass Enabled"></i>`;
  62. }
  63. }else{
  64. inboundTlsIcon = `<i class="yellow lock open icon" title="Plain Text Mode"></i>`;
  65. }
  66. //Build the virtual directory list
  67. var vdList = `<div class="ui list">`;
  68. subd.VirtualDirectories.forEach(vdir => {
  69. vdList += `<div class="item">${vdir.MatchingPath} <i class="green angle double right icon"></i> ${vdir.Domain}</div>`;
  70. });
  71. vdList += `</div>`;
  72. if (subd.VirtualDirectories.length == 0){
  73. vdList = `<small style="opacity: 0.3; pointer-events: none; user-select: none;">No Virtual Directory</small>`;
  74. }
  75. let enableChecked = "checked";
  76. if (subd.Disabled){
  77. enableChecked = "";
  78. }
  79. let aliasDomains = ``;
  80. if (subd.MatchingDomainAlias != undefined && subd.MatchingDomainAlias.length > 0){
  81. aliasDomains = `<small class="aliasDomains" eptuuid="${subd.RootOrMatchingDomain}" style="color: #636363;">Alias: `;
  82. subd.MatchingDomainAlias.forEach(alias => {
  83. aliasDomains += `<a href="//${alias}" target="_blank">${alias}</a>, `;
  84. });
  85. aliasDomains = aliasDomains.substr(0, aliasDomains.length - 2); //Remove the last tailing seperator
  86. aliasDomains += `</small><br>`;
  87. }
  88. $("#httpProxyList").append(`<tr eptuuid="${subd.RootOrMatchingDomain}" payload="${subdData}" class="subdEntry">
  89. <td data-label="" editable="true" datatype="inbound">
  90. <a href="//${subd.RootOrMatchingDomain}" target="_blank">${subd.RootOrMatchingDomain}</a> ${inboundTlsIcon}<br>
  91. ${aliasDomains}
  92. <small class="accessRuleNameUnderHost" ruleid="${subd.AccessFilterUUID}"></small>
  93. </td>
  94. <td data-label="" editable="true" datatype="domain">${subd.Domain} ${tlsIcon}</td>
  95. <td data-label="" editable="true" datatype="vdir">${vdList}</td>
  96. <td data-label="" editable="true" datatype="advanced" style="width: 350px;">
  97. ${subd.RequireBasicAuth?`<i class="ui green check icon"></i> Basic Auth`:``}
  98. ${subd.RequireBasicAuth && subd.RequireRateLimit?"<br>":""}
  99. ${subd.RequireRateLimit?`<i class="ui green check icon"></i> Rate Limit @ ${subd.RateLimit} req/s`:``}
  100. ${!subd.RequireBasicAuth && !subd.RequireRateLimit?`<small style="opacity: 0.3; pointer-events: none; user-select: none;">No Special Settings</small>`:""}
  101. </td>
  102. <td class="center aligned ignoremw" editable="true" datatype="action" data-label="">
  103. <div class="ui toggle tiny fitted checkbox" style="margin-bottom: -0.5em; margin-right: 0.4em;" title="Enable / Disable Rule">
  104. <input type="checkbox" class="enableToggle" name="active" ${enableChecked} eptuuid="${subd.RootOrMatchingDomain}" onchange="handleProxyRuleToggle(this);">
  105. <label></label>
  106. </div>
  107. <button title="Edit Proxy Rule" class="ui circular mini basic icon button editBtn inlineEditActionBtn" onclick='editEndpoint("${(subd.RootOrMatchingDomain).hexEncode()}")'><i class="edit icon"></i></button>
  108. <button title="Remove Proxy Rule" class="ui circular mini red basic icon button inlineEditActionBtn" onclick='deleteEndpoint("${(subd.RootOrMatchingDomain).hexEncode()}")'><i class="trash icon"></i></button>
  109. </td>
  110. </tr>`);
  111. });
  112. }
  113. resolveAccessRuleNameOnHostRPlist();
  114. });
  115. }
  116. //Perform realtime alias update without refreshing the whole page
  117. function updateAliasListForEndpoint(endpointName, newAliasDomainList){
  118. let targetEle = $(`.aliasDomains[eptuuid='${endpointName}']`);
  119. console.log(targetEle);
  120. if (targetEle.length == 0){
  121. return;
  122. }
  123. let aliasDomains = ``;
  124. if (newAliasDomainList != undefined && newAliasDomainList.length > 0){
  125. aliasDomains = `Alias: `;
  126. newAliasDomainList.forEach(alias => {
  127. aliasDomains += `<a href="//${alias}" target="_blank">${alias}</a>, `;
  128. });
  129. aliasDomains = aliasDomains.substr(0, aliasDomains.length - 2); //Remove the last tailing seperator
  130. $(targetEle).html(aliasDomains);
  131. $(targetEle).show();
  132. }else{
  133. $(targetEle).hide();
  134. }
  135. }
  136. //Resolve & Update all rule names on host PR list
  137. function resolveAccessRuleNameOnHostRPlist(){
  138. //Resolve the access filters
  139. $.get("/api/access/list", function(data){
  140. console.log(data);
  141. if (data.error == undefined){
  142. //Build a map base on the data
  143. let accessRuleMap = {};
  144. for (var i = 0; i < data.length; i++){
  145. accessRuleMap[data[i].ID] = data[i];
  146. }
  147. $(".accessRuleNameUnderHost").each(function(){
  148. let thisAccessRuleID = $(this).attr("ruleid");
  149. if (thisAccessRuleID== ""){
  150. thisAccessRuleID = "default"
  151. }
  152. if (thisAccessRuleID == "default"){
  153. //No need to label default access rules
  154. $(this).html("");
  155. return;
  156. }
  157. let rule = accessRuleMap[thisAccessRuleID];
  158. let icon = `<i class="ui grey filter icon"></i>`;
  159. if (rule.ID == "default"){
  160. icon = `<i class="ui yellow star icon"></i>`;
  161. }else if (rule.BlacklistEnabled && !rule.WhitelistEnabled){
  162. //This is a blacklist filter
  163. icon = `<i class="ui red filter icon"></i>`;
  164. }else if (rule.WhitelistEnabled && !rule.BlacklistEnabled){
  165. //This is a whitelist filter
  166. icon = `<i class="ui green filter icon"></i>`;
  167. }else if (rule.WhitelistEnabled && rule.BlacklistEnabled){
  168. //Whitelist and blacklist filter
  169. icon = `<i class="ui yellow filter icon"></i>`;
  170. }
  171. if (rule != undefined){
  172. $(this).html(`${icon} ${rule.Name}`);
  173. }
  174. });
  175. }
  176. })
  177. }
  178. //Update the access rule name on given epuuid, call by hostAccessEditor.html
  179. function updateAccessRuleNameUnderHost(epuuid, newruleUID){
  180. $(`tr[eptuuid='${epuuid}'].subdEntry`).find(".accessRuleNameUnderHost").attr("ruleid", newruleUID);
  181. resolveAccessRuleNameOnHostRPlist();
  182. }
  183. /*
  184. Inline editor for httprp.html
  185. */
  186. function editEndpoint(uuid) {
  187. uuid = uuid.hexDecode();
  188. var row = $('tr[eptuuid="' + uuid + '"]');
  189. var columns = row.find('td[data-label]');
  190. var payload = $(row).attr("payload");
  191. payload = JSON.parse(decodeURIComponent(payload));
  192. console.log(payload);
  193. //console.log(payload);
  194. columns.each(function(index) {
  195. var column = $(this);
  196. var oldValue = column.text().trim();
  197. if ($(this).attr("editable") == "false"){
  198. //This col do not allow edit. Skip
  199. return;
  200. }
  201. // Create an input element based on the column content
  202. var input;
  203. var datatype = $(this).attr("datatype");
  204. if (datatype == "domain"){
  205. let domain = payload.Domain;
  206. //Target require TLS for proxying
  207. let tls = payload.RequireTLS;
  208. if (tls){
  209. tls = "checked";
  210. }else{
  211. tls = "";
  212. }
  213. //Require TLS validation
  214. let skipTLSValidation = payload.SkipCertValidations;
  215. let checkstate = "";
  216. if (skipTLSValidation){
  217. checkstate = "checked";
  218. }
  219. input = `
  220. <div class="ui mini fluid input">
  221. <input type="text" class="Domain" value="${domain}">
  222. </div>
  223. <div class="ui checkbox" style="margin-top: 0.6em;">
  224. <input type="checkbox" class="RequireTLS" ${tls}>
  225. <label>Require TLS<br>
  226. <small>Proxy target require HTTPS connection</small></label>
  227. </div><br>
  228. <div class="ui checkbox" style="margin-top: 0.4em;">
  229. <input type="checkbox" class="SkipCertValidations" ${checkstate}>
  230. <label>Skip Verification<br>
  231. <small>Check this if proxy target is using self signed certificates</small></label>
  232. </div><br>
  233. <button class="ui basic compact tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="editLoadBalanceOptions('${uuid}');"><i class="purple server icon"></i> Load Balance</button>
  234. `;
  235. column.empty().append(input);
  236. }else if (datatype == "vdir"){
  237. //Append a quick access button for vdir page
  238. column.append(`<button class="ui basic tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="quickEditVdir('${uuid}');">
  239. <i class="ui yellow folder icon"></i> Edit Virtual Directories
  240. </button>`);
  241. }else if (datatype == "advanced"){
  242. let requireBasicAuth = payload.RequireBasicAuth;
  243. let basicAuthCheckstate = "";
  244. if (requireBasicAuth){
  245. basicAuthCheckstate = "checked";
  246. }
  247. let skipWebSocketOriginCheck = payload.SkipWebSocketOriginCheck;
  248. let wsCheckstate = "";
  249. if (skipWebSocketOriginCheck){
  250. wsCheckstate = "checked";
  251. }
  252. let requireRateLimit = payload.RequireRateLimit;
  253. let rateLimitCheckState = "";
  254. if (requireRateLimit){
  255. rateLimitCheckState = "checked";
  256. }
  257. let rateLimit = payload.RateLimit;
  258. if (rateLimit == 0){
  259. //This value is not set. Make it default to 100
  260. rateLimit = 100;
  261. }
  262. let rateLimitDisableState = "";
  263. if (!payload.RequireRateLimit){
  264. rateLimitDisableState = "disabled";
  265. }
  266. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  267. <input type="checkbox" class="RequireBasicAuth" ${basicAuthCheckstate}>
  268. <label>Require Basic Auth</label>
  269. </div>
  270. <br>
  271. <button class="ui basic compact tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="editBasicAuthCredentials('${uuid}');"><i class="ui blue user circle icon"></i> Edit Credentials</button>
  272. <br>
  273. <button class="ui basic compact tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="editCustomHeaders('${uuid}');"><i class="heading icon"></i> Custom Headers</button>
  274. <div class="ui basic advance segment" style="padding: 0.4em !important; border-radius: 0.4em;">
  275. <div class="ui endpointAdvanceConfig accordion" style="padding-right: 0.6em;">
  276. <div class="title">
  277. <i class="dropdown icon"></i>
  278. Security Options
  279. </div>
  280. <div class="content">
  281. <div class="ui checkbox" style="margin-top: 0.4em;">
  282. <input type="checkbox" class="SkipWebSocketOriginCheck" ${wsCheckstate}>
  283. <label>Skip WebSocket Origin Check<br>
  284. <small>Check this to allow cross-origin websocket requests</small></label>
  285. </div>
  286. <br>
  287. <div class="ui checkbox" style="margin-top: 0.4em;">
  288. <input type="checkbox" onchange="handleToggleRateLimitInput();" class="RequireRateLimit" ${rateLimitCheckState}>
  289. <label>Require Rate Limit<br>
  290. <small>Check this to enable rate limit on this inbound hostname</small></label>
  291. </div><br>
  292. <div class="ui mini right labeled fluid input ${rateLimitDisableState}" style="margin-top: 0.4em;">
  293. <input type="number" class="RateLimit" value="${rateLimit}" min="1" >
  294. <label class="ui basic label">
  295. req / sec / IP
  296. </label>
  297. </div>
  298. </div>
  299. </div>
  300. <div>
  301. `);
  302. } else if (datatype == "ratelimit"){
  303. column.empty().append(`
  304. <div class="ui checkbox" style="margin-top: 0.4em;">
  305. <input type="checkbox" class="RequireRateLimit" ${checkstate}>
  306. <label>Require Rate Limit</label>
  307. </div>
  308. <div class="ui mini fluid input">
  309. <input type="number" class="RateLimit" value="${rateLimit}" placeholder="100" min="1" max="1000" >
  310. </div>
  311. `);
  312. }else if (datatype == 'action'){
  313. column.empty().append(`
  314. <button title="Save" onclick="saveProxyInlineEdit('${uuid.hexEncode()}');" class="ui basic small icon circular button inlineEditActionBtn"><i class="ui green save icon"></i></button>
  315. <button title="Cancel" onclick="exitProxyInlineEdit();" class="ui basic small icon circular button inlineEditActionBtn"><i class="ui remove icon"></i></button>
  316. `);
  317. }else if (datatype == "inbound"){
  318. let originalContent = $(column).html();
  319. column.empty().append(`${originalContent}
  320. <div class="ui divider"></div>
  321. <div class="ui checkbox" style="margin-top: 0.4em;">
  322. <input type="checkbox" class="BypassGlobalTLS" ${payload.BypassGlobalTLS?"checked":""}>
  323. <label>Allow plain HTTP access<br>
  324. <small>Allow inbound connections without TLS/SSL</small></label>
  325. </div><br>
  326. <button class="ui basic compact tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="editAliasHostnames('${uuid}');"><i class=" blue at icon"></i> Alias</button>
  327. <button class="ui basic compact tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="editAccessRule('${uuid}');"><i class="ui filter icon"></i> Access Rule</button>
  328. `);
  329. $(".hostAccessRuleSelector").dropdown();
  330. }else{
  331. //Unknown field. Leave it untouched
  332. }
  333. });
  334. $(".endpointAdvanceConfig").accordion();
  335. $("#httpProxyList").find(".editBtn").addClass("disabled");
  336. }
  337. //handleToggleRateLimitInput will get trigger if the "require rate limit" checkbox
  338. // is changed and toggle the disable state of the rate limit input field
  339. function handleToggleRateLimitInput(){
  340. let isRateLimitEnabled = $("#httpProxyList input.RequireRateLimit")[0].checked;
  341. if (isRateLimitEnabled){
  342. $("#httpProxyList input.RateLimit").parent().removeClass("disabled");
  343. }else{
  344. $("#httpProxyList input.RateLimit").parent().addClass("disabled");
  345. }
  346. }
  347. function exitProxyInlineEdit(){
  348. listProxyEndpoints();
  349. $("#httpProxyList").find(".editBtn").removeClass("disabled");
  350. }
  351. function saveProxyInlineEdit(uuid){
  352. uuid = uuid.hexDecode();
  353. var row = $('tr[eptuuid="' + uuid + '"]');
  354. if (row.length == 0){
  355. return;
  356. }
  357. var epttype = "host";
  358. let newDomain = $(row).find(".Domain").val();
  359. let requireTLS = $(row).find(".RequireTLS")[0].checked;
  360. let skipCertValidations = $(row).find(".SkipCertValidations")[0].checked;
  361. let requireBasicAuth = $(row).find(".RequireBasicAuth")[0].checked;
  362. let requireRateLimit = $(row).find(".RequireRateLimit")[0].checked;
  363. let rateLimit = $(row).find(".RateLimit").val();
  364. let bypassGlobalTLS = $(row).find(".BypassGlobalTLS")[0].checked;
  365. let bypassWebsocketOrigin = $(row).find(".SkipWebSocketOriginCheck")[0].checked;
  366. console.log(newDomain, requireTLS, skipCertValidations, requireBasicAuth)
  367. $.ajax({
  368. url: "/api/proxy/edit",
  369. method: "POST",
  370. data: {
  371. "type": epttype,
  372. "rootname": uuid,
  373. "ep":newDomain,
  374. "bpgtls": bypassGlobalTLS,
  375. "tls" :requireTLS,
  376. "tlsval": skipCertValidations,
  377. "bpwsorg" : bypassWebsocketOrigin,
  378. "bauth" :requireBasicAuth,
  379. "rate" :requireRateLimit,
  380. "ratenum" :rateLimit,
  381. },
  382. success: function(data){
  383. if (data.error !== undefined){
  384. msgbox(data.error, false, 6000);
  385. }else{
  386. msgbox("Proxy endpoint updated");
  387. listProxyEndpoints();
  388. }
  389. }
  390. })
  391. }
  392. /* button events */
  393. function editBasicAuthCredentials(uuid){
  394. let payload = encodeURIComponent(JSON.stringify({
  395. ept: "host",
  396. ep: uuid
  397. }));
  398. showSideWrapper("snippet/basicAuthEditor.html?t=" + Date.now() + "#" + payload);
  399. }
  400. function editAccessRule(uuid){
  401. let payload = encodeURIComponent(JSON.stringify({
  402. ept: "host",
  403. ep: uuid
  404. }));
  405. showSideWrapper("snippet/hostAccessEditor.html?t=" + Date.now() + "#" + payload);
  406. }
  407. function editAliasHostnames(uuid){
  408. let payload = encodeURIComponent(JSON.stringify({
  409. ept: "host",
  410. ep: uuid
  411. }));
  412. showSideWrapper("snippet/aliasEditor.html?t=" + Date.now() + "#" + payload);
  413. }
  414. function quickEditVdir(uuid){
  415. openTabById("vdir");
  416. $("#vdirBaseRoutingRule").parent().dropdown("set selected", uuid);
  417. }
  418. //Open the custom header editor
  419. function editCustomHeaders(uuid){
  420. let payload = encodeURIComponent(JSON.stringify({
  421. ept: "host",
  422. ep: uuid
  423. }));
  424. showSideWrapper("snippet/customHeaders.html?t=" + Date.now() + "#" + payload);
  425. }
  426. //Open the load balance option
  427. function editLoadBalanceOptions(uuid){
  428. let payload = encodeURIComponent(JSON.stringify({
  429. ept: "host",
  430. ep: uuid
  431. }));
  432. showSideWrapper("snippet/loadBalancer.html?t=" + Date.now() + "#" + payload);
  433. }
  434. function handleProxyRuleToggle(object){
  435. let endpointUUID = $(object).attr("eptuuid");
  436. let isChecked = object.checked;
  437. $.ajax({
  438. url: "/api/proxy/toggle",
  439. data: {
  440. "ep": endpointUUID,
  441. "enable": isChecked
  442. },
  443. success: function(data){
  444. if (data.error != undefined){
  445. msgbox(data.error, false);
  446. }else{
  447. if (isChecked){
  448. msgbox("Proxy Rule Enabled");
  449. }else{
  450. msgbox("Proxy Rule Disabled");
  451. }
  452. }
  453. }
  454. })
  455. }
  456. //Bind on tab switch events
  457. tabSwitchEventBind["httprp"] = function(){
  458. listProxyEndpoints();
  459. }
  460. </script>