httprp.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. </style>
  11. <div style="width: 100%; overflow-x: auto; margin-bottom: 1em; min-height: 300px;">
  12. <table class="ui celled sortable unstackable compact table">
  13. <thead>
  14. <tr>
  15. <th>Host</th>
  16. <th>Destination</th>
  17. <th>Virtual Directory</th>
  18. <th>Basic Auth</th>
  19. <th class="no-sort" style="min-width:150px;">Actions</th>
  20. </tr>
  21. </thead>
  22. <tbody id="httpProxyList">
  23. </tbody>
  24. </table>
  25. </div>
  26. <button class="ui icon right floated basic button" onclick="listProxyEndpoints();"><i class="green refresh icon"></i> Refresh</button>
  27. <br><br>
  28. </div>
  29. <script>
  30. /* List all proxy endpoints */
  31. function listProxyEndpoints(){
  32. $.get("/api/proxy/list?type=host", function(data){
  33. $("#httpProxyList").html(``);
  34. if (data.error !== undefined){
  35. $("#httpProxyList").append(`<tr>
  36. <td data-label="" colspan="5"><i class="remove icon"></i> ${data.error}</td>
  37. </tr>`);
  38. }else if (data.length == 0){
  39. $("#httpProxyList").append(`<tr>
  40. <td data-label="" colspan="5"><i class="green check circle icon"></i> No HTTP Proxy Record</td>
  41. </tr>`);
  42. }else{
  43. //Sort by RootOrMatchingDomain field
  44. data.sort((a,b) => (a.RootOrMatchingDomain > b.RootOrMatchingDomain) ? 1 : ((b.RootOrMatchingDomain > a.RootOrMatchingDomain) ? -1 : 0))
  45. data.forEach(subd => {
  46. let tlsIcon = "";
  47. let subdData = encodeURIComponent(JSON.stringify(subd));
  48. if (subd.RequireTLS){
  49. tlsIcon = `<i class="green lock icon" title="TLS Mode"></i>`;
  50. if (subd.SkipCertValidations){
  51. tlsIcon = `<i class="yellow lock icon" title="TLS/SSL mode without verification"></i>`
  52. }
  53. }
  54. let inboundTlsIcon = "";
  55. if ($("#tls").checkbox("is checked")){
  56. inboundTlsIcon = `<i class="green lock icon" title="TLS Mode"></i>`;
  57. if (subd.BypassGlobalTLS){
  58. inboundTlsIcon = `<i class="grey lock icon" title="TLS Bypass Enabled"></i>`;
  59. }
  60. }else{
  61. inboundTlsIcon = `<i class="yellow lock open icon" title="Plain Text Mode"></i>`;
  62. }
  63. //Build the virtual directory list
  64. var vdList = `<div class="ui list">`;
  65. subd.VirtualDirectories.forEach(vdir => {
  66. vdList += `<div class="item">${vdir.MatchingPath} <i class="green angle double right icon"></i> ${vdir.Domain}</div>`;
  67. });
  68. vdList += `</div>`;
  69. if (subd.VirtualDirectories.length == 0){
  70. vdList = `<small style="opacity: 0.3; pointer-events: none; user-select: none;"><i class="check icon"></i> No Virtual Directory</small>`;
  71. }
  72. var enableChecked = "checked";
  73. if (subd.Disabled){
  74. enableChecked = "";
  75. }
  76. $("#httpProxyList").append(`<tr eptuuid="${subd.RootOrMatchingDomain}" payload="${subdData}" class="subdEntry">
  77. <td data-label="" editable="true" datatype="inbound">
  78. <a href="//${subd.RootOrMatchingDomain}" target="_blank">${subd.RootOrMatchingDomain}</a> ${inboundTlsIcon}<br>
  79. <small class="accessRuleNameUnderHost" ruleid="${subd.AccessFilterUUID}"></small>
  80. </td>
  81. <td data-label="" editable="true" datatype="domain">${subd.Domain} ${tlsIcon}</td>
  82. <td data-label="" editable="true" datatype="vdir">${vdList}</td>
  83. <td data-label="" editable="true" datatype="basicauth">
  84. ${subd.RequireBasicAuth?`<i class="ui green check icon"></i>`:`<i class="ui grey remove icon"></i>`}
  85. </td>
  86. <td class="center aligned" editable="true" datatype="action" data-label="">
  87. <div class="ui toggle tiny fitted checkbox" style="margin-bottom: -0.5em; margin-right: 0.4em;" title="Enable / Disable Rule">
  88. <input type="checkbox" class="enableToggle" name="active" ${enableChecked} eptuuid="${subd.RootOrMatchingDomain}" onchange="handleProxyRuleToggle(this);">
  89. <label></label>
  90. </div>
  91. <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>
  92. <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>
  93. </td>
  94. </tr>`);
  95. });
  96. }
  97. resolveAccessRuleNameOnHostRPlist();
  98. });
  99. }
  100. //Resolve & Update all rule names on host PR list
  101. function resolveAccessRuleNameOnHostRPlist(){
  102. //Resolve the access filters
  103. $.get("/api/access/list", function(data){
  104. console.log(data);
  105. if (data.error == undefined){
  106. //Build a map base on the data
  107. let accessRuleMap = {};
  108. for (var i = 0; i < data.length; i++){
  109. accessRuleMap[data[i].ID] = data[i];
  110. }
  111. $(".accessRuleNameUnderHost").each(function(){
  112. let thisAccessRuleID = $(this).attr("ruleid");
  113. if (thisAccessRuleID== ""){
  114. thisAccessRuleID = "default"
  115. }
  116. if (thisAccessRuleID == "default"){
  117. //No need to label default access rules
  118. $(this).html("");
  119. return;
  120. }
  121. let rule = accessRuleMap[thisAccessRuleID];
  122. let icon = `<i class="ui grey filter icon"></i>`;
  123. if (rule.ID == "default"){
  124. icon = `<i class="ui yellow star icon"></i>`;
  125. }else if (rule.BlacklistEnabled && !rule.WhitelistEnabled){
  126. //This is a blacklist filter
  127. icon = `<i class="ui red filter icon"></i>`;
  128. }else if (rule.WhitelistEnabled && !rule.BlacklistEnabled){
  129. //This is a whitelist filter
  130. icon = `<i class="ui green filter icon"></i>`;
  131. }else if (rule.WhitelistEnabled && rule.BlacklistEnabled){
  132. //Whitelist and blacklist filter
  133. icon = `<i class="ui yellow filter icon"></i>`;
  134. }
  135. if (rule != undefined){
  136. $(this).html(`${icon} ${rule.Name}`);
  137. }
  138. });
  139. }
  140. })
  141. }
  142. //Update the access rule name on given epuuid, call by hostAccessEditor.html
  143. function updateAccessRuleNameUnderHost(epuuid, newruleUID){
  144. $(`tr[eptuuid='${epuuid}'].subdEntry`).find(".accessRuleNameUnderHost").attr("ruleid", newruleUID);
  145. resolveAccessRuleNameOnHostRPlist();
  146. }
  147. /*
  148. Inline editor for httprp.html
  149. */
  150. function editEndpoint(uuid) {
  151. uuid = uuid.hexDecode();
  152. var row = $('tr[eptuuid="' + uuid + '"]');
  153. var columns = row.find('td[data-label]');
  154. var payload = $(row).attr("payload");
  155. payload = JSON.parse(decodeURIComponent(payload));
  156. console.log(payload);
  157. //console.log(payload);
  158. columns.each(function(index) {
  159. var column = $(this);
  160. var oldValue = column.text().trim();
  161. if ($(this).attr("editable") == "false"){
  162. //This col do not allow edit. Skip
  163. return;
  164. }
  165. // Create an input element based on the column content
  166. var input;
  167. var datatype = $(this).attr("datatype");
  168. if (datatype == "domain"){
  169. let domain = payload.Domain;
  170. //Target require TLS for proxying
  171. let tls = payload.RequireTLS;
  172. if (tls){
  173. tls = "checked";
  174. }else{
  175. tls = "";
  176. }
  177. //Require TLS validation
  178. let skipTLSValidation = payload.SkipCertValidations;
  179. let checkstate = "";
  180. if (skipTLSValidation){
  181. checkstate = "checked";
  182. }
  183. input = `
  184. <div class="ui mini fluid input">
  185. <input type="text" class="Domain" value="${domain}">
  186. </div>
  187. <div class="ui checkbox" style="margin-top: 0.4em;">
  188. <input type="checkbox" class="RequireTLS" ${tls}>
  189. <label>Require TLS<br>
  190. <small>Proxy target require HTTPS connection</small></label>
  191. </div><br>
  192. <div class="ui checkbox" style="margin-top: 0.4em;">
  193. <input type="checkbox" class="SkipCertValidations" ${checkstate}>
  194. <label>Skip Verification<br>
  195. <small>Check this if proxy target is using self signed certificates</small></label>
  196. </div>
  197. `;
  198. column.empty().append(input);
  199. }else if (datatype == "vdir"){
  200. //Append a quick access button for vdir page
  201. column.append(`<button class="ui basic tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="quickEditVdir('${uuid}');">
  202. <i class="ui yellow folder icon"></i> Edit Virtual Directories
  203. </button>`);
  204. }else if (datatype == "basicauth"){
  205. let requireBasicAuth = payload.RequireBasicAuth;
  206. let checkstate = "";
  207. if (requireBasicAuth){
  208. checkstate = "checked";
  209. }
  210. let skipWebSocketOriginCheck = payload.SkipWebSocketOriginCheck;
  211. let wsCheckstate = "";
  212. if (skipWebSocketOriginCheck){
  213. wsCheckstate = "checked";
  214. }
  215. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  216. <input type="checkbox" class="RequireBasicAuth" ${checkstate}>
  217. <label>Require Basic Auth</label>
  218. </div>
  219. <button class="ui basic 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>
  220. <div class="ui basic advance segment" style="padding: 0.4em !important; border-radius: 0.4em;">
  221. <div class="ui endpointAdvanceConfig accordion" style="padding-right: 0.6em;">
  222. <div class="title">
  223. <i class="dropdown icon"></i>
  224. Advance Configs
  225. </div>
  226. <div class="content">
  227. <div class="ui checkbox" style="margin-top: 0.4em;">
  228. <input type="checkbox" class="SkipWebSocketOriginCheck" ${wsCheckstate}>
  229. <label>Skip WebSocket Origin Check<br>
  230. <small>Check this to allow cross-origin websocket requests</small></label>
  231. </div>
  232. <br>
  233. <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>
  234. <!-- <button class="ui basic compact tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="editLoadBalanceOptions('${uuid}');"><i class="blue server icon"></i> Load Balance</button> -->
  235. </div>
  236. </div>
  237. <div>
  238. `);
  239. }else if (datatype == 'action'){
  240. column.empty().append(`
  241. <button title="Save" onclick="saveProxyInlineEdit('${uuid.hexEncode()}');" class="ui basic small icon circular button inlineEditActionBtn"><i class="ui green save icon"></i></button>
  242. <button title="Cancel" onclick="exitProxyInlineEdit();" class="ui basic small icon circular button inlineEditActionBtn"><i class="ui remove icon"></i></button>
  243. `);
  244. }else if (datatype == "inbound"){
  245. let originalContent = $(column).html();
  246. column.empty().append(`${originalContent}
  247. <div class="ui divider"></div>
  248. <div class="ui checkbox" style="margin-top: 0.4em;">
  249. <input type="checkbox" class="BypassGlobalTLS" ${payload.BypassGlobalTLS?"checked":""}>
  250. <label>Allow plain HTTP access<br>
  251. <small>Allow inbound connections without TLS/SSL</small></label>
  252. </div><br>
  253. <button class="ui basic tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="editAccessRule('${uuid}');"><i class="ui filter icon"></i> Edit Access Rule</button>
  254. `);
  255. $(".hostAccessRuleSelector").dropdown();
  256. }else{
  257. //Unknown field. Leave it untouched
  258. }
  259. });
  260. $(".endpointAdvanceConfig").accordion();
  261. $("#httpProxyList").find(".editBtn").addClass("disabled");
  262. }
  263. function exitProxyInlineEdit(){
  264. listProxyEndpoints();
  265. $("#httpProxyList").find(".editBtn").removeClass("disabled");
  266. }
  267. function saveProxyInlineEdit(uuid){
  268. uuid = uuid.hexDecode();
  269. var row = $('tr[eptuuid="' + uuid + '"]');
  270. if (row.length == 0){
  271. return;
  272. }
  273. var epttype = "host";
  274. let newDomain = $(row).find(".Domain").val();
  275. let requireTLS = $(row).find(".RequireTLS")[0].checked;
  276. let skipCertValidations = $(row).find(".SkipCertValidations")[0].checked;
  277. let requireBasicAuth = $(row).find(".RequireBasicAuth")[0].checked;
  278. let bypassGlobalTLS = $(row).find(".BypassGlobalTLS")[0].checked;
  279. let bypassWebsocketOrigin = $(row).find(".SkipWebSocketOriginCheck")[0].checked;
  280. console.log(newDomain, requireTLS, skipCertValidations, requireBasicAuth)
  281. $.ajax({
  282. url: "/api/proxy/edit",
  283. method: "POST",
  284. data: {
  285. "type": epttype,
  286. "rootname": uuid,
  287. "ep":newDomain,
  288. "bpgtls": bypassGlobalTLS,
  289. "tls" :requireTLS,
  290. "tlsval": skipCertValidations,
  291. "bpwsorg" : bypassWebsocketOrigin,
  292. "bauth" :requireBasicAuth,
  293. },
  294. success: function(data){
  295. if (data.error !== undefined){
  296. msgbox(data.error, false, 6000);
  297. }else{
  298. msgbox("Proxy endpoint updated");
  299. listProxyEndpoints();
  300. }
  301. }
  302. })
  303. }
  304. /* button events */
  305. function editBasicAuthCredentials(uuid){
  306. let payload = encodeURIComponent(JSON.stringify({
  307. ept: "host",
  308. ep: uuid
  309. }));
  310. showSideWrapper("snippet/basicAuthEditor.html?t=" + Date.now() + "#" + payload);
  311. }
  312. function editAccessRule(uuid){
  313. let payload = encodeURIComponent(JSON.stringify({
  314. ept: "host",
  315. ep: uuid
  316. }));
  317. showSideWrapper("snippet/hostAccessEditor.html?t=" + Date.now() + "#" + payload);
  318. }
  319. function quickEditVdir(uuid){
  320. openTabById("vdir");
  321. $("#vdirBaseRoutingRule").parent().dropdown("set selected", uuid);
  322. }
  323. function editCustomHeaders(uuid){
  324. let payload = encodeURIComponent(JSON.stringify({
  325. ept: "host",
  326. ep: uuid
  327. }));
  328. showSideWrapper("snippet/customHeaders.html?t=" + Date.now() + "#" + payload);
  329. }
  330. function handleProxyRuleToggle(object){
  331. let endpointUUID = $(object).attr("eptuuid");
  332. let isChecked = object.checked;
  333. $.ajax({
  334. url: "/api/proxy/toggle",
  335. data: {
  336. "ep": endpointUUID,
  337. "enable": isChecked
  338. },
  339. success: function(data){
  340. if (data.error != undefined){
  341. msgbox(data.error, false);
  342. }else{
  343. if (isChecked){
  344. msgbox("Proxy Rule Enabled");
  345. }else{
  346. msgbox("Proxy Rule Disabled");
  347. }
  348. }
  349. }
  350. })
  351. }
  352. /* Access List handling */
  353. //Bind on tab switch events
  354. tabSwitchEventBind["httprp"] = function(){
  355. listProxyEndpoints();
  356. }
  357. </script>