httprp.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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;">
  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:100px;">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. function listProxyEndpoints(){
  31. $.get("/api/proxy/list?type=host", function(data){
  32. $("#httpProxyList").html(``);
  33. if (data.error !== undefined){
  34. $("#httpProxyList").append(`<tr>
  35. <td data-label="" colspan="5"><i class="remove icon"></i> ${data.error}</td>
  36. </tr>`);
  37. }else if (data.length == 0){
  38. $("#httpProxyList").append(`<tr>
  39. <td data-label="" colspan="5"><i class="green check circle icon"></i> No HTTP Proxy Record</td>
  40. </tr>`);
  41. }else{
  42. data.forEach(subd => {
  43. let tlsIcon = "";
  44. let subdData = encodeURIComponent(JSON.stringify(subd));
  45. if (subd.RequireTLS){
  46. tlsIcon = `<i class="green lock icon" title="TLS Mode"></i>`;
  47. if (subd.SkipCertValidations){
  48. tlsIcon = `<i class="yellow lock icon" title="TLS/SSL mode without verification"></i>`
  49. }
  50. }
  51. let inboundTlsIcon = "";
  52. if ($("#tls").checkbox("is checked")){
  53. inboundTlsIcon = `<i class="green lock icon" title="TLS Mode"></i>`;
  54. if (subd.BypassGlobalTLS){
  55. inboundTlsIcon = `<i class="grey lock icon" title="TLS Bypass Enabled"></i>`;
  56. }
  57. }else{
  58. inboundTlsIcon = `<i class="yellow lock open icon" title="Plain Text Mode"></i>`;
  59. }
  60. //Build the virtual directory list
  61. var vdList = `<div class="ui list">`;
  62. subd.VirtualDirectories.forEach(vdir => {
  63. vdList += `<div class="item">${vdir.MatchingPath} <i class="green angle double right icon"></i> ${vdir.Domain}</div>`;
  64. });
  65. vdList += `</div>`;
  66. if (subd.VirtualDirectories.length == 0){
  67. vdList = `<small style="opacity: 0.3; pointer-events: none; user-select: none;"><i class="check icon"></i> No Virtual Directory</small>`;
  68. }
  69. var enableChecked = "checked";
  70. if (subd.Disabled){
  71. enableChecked = "";
  72. }
  73. $("#httpProxyList").append(`<tr eptuuid="${subd.RootOrMatchingDomain}" payload="${subdData}" class="subdEntry">
  74. <td data-label="" editable="true" datatype="inbound"><a href="//${subd.RootOrMatchingDomain}" target="_blank">${subd.RootOrMatchingDomain}</a> ${inboundTlsIcon}</td>
  75. <td data-label="" editable="true" datatype="domain">${subd.Domain} ${tlsIcon}</td>
  76. <td data-label="" editable="true" datatype="vdir">${vdList}</td>
  77. <td data-label="" editable="true" datatype="basicauth">${subd.RequireBasicAuth?`<i class="ui green check icon"></i>`:`<i class="ui grey remove icon"></i>`}</td>
  78. <td class="center aligned" editable="true" datatype="action" data-label="">
  79. <div class="ui toggle tiny fitted checkbox" style="margin-bottom: -0.5em; margin-right: 0.4em;" title="Enable / Disable Rule">
  80. <input type="checkbox" class="enableToggle" name="active" ${enableChecked} eptuuid="${subd.RootOrMatchingDomain}" onchange="handleProxyRuleToggle(this);">
  81. <label></label>
  82. </div>
  83. <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>
  84. <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>
  85. </td>
  86. </tr>`);
  87. });
  88. }
  89. });
  90. }
  91. /*
  92. Inline editor for httprp.html
  93. */
  94. function editEndpoint(uuid) {
  95. uuid = uuid.hexDecode();
  96. var row = $('tr[eptuuid="' + uuid + '"]');
  97. var columns = row.find('td[data-label]');
  98. var payload = $(row).attr("payload");
  99. payload = JSON.parse(decodeURIComponent(payload));
  100. console.log(payload);
  101. //console.log(payload);
  102. columns.each(function(index) {
  103. var column = $(this);
  104. var oldValue = column.text().trim();
  105. if ($(this).attr("editable") == "false"){
  106. //This col do not allow edit. Skip
  107. return;
  108. }
  109. // Create an input element based on the column content
  110. var input;
  111. var datatype = $(this).attr("datatype");
  112. if (datatype == "domain"){
  113. let domain = payload.Domain;
  114. //Target require TLS for proxying
  115. let tls = payload.RequireTLS;
  116. if (tls){
  117. tls = "checked";
  118. }else{
  119. tls = "";
  120. }
  121. //Require TLS validation
  122. let skipTLSValidation = payload.SkipCertValidations;
  123. let checkstate = "";
  124. if (skipTLSValidation){
  125. checkstate = "checked";
  126. }
  127. input = `
  128. <div class="ui mini fluid input">
  129. <input type="text" class="Domain" value="${domain}">
  130. </div>
  131. <div class="ui checkbox" style="margin-top: 0.4em;">
  132. <input type="checkbox" class="RequireTLS" ${tls}>
  133. <label>Require TLS<br>
  134. <small>Proxy target require HTTPS connection</small></label>
  135. </div><br>
  136. <div class="ui checkbox" style="margin-top: 0.4em;">
  137. <input type="checkbox" class="SkipCertValidations" ${checkstate}>
  138. <label>Skip Verification<br>
  139. <small>Check this if proxy target is using self signed certificates</small></label>
  140. </div>
  141. `;
  142. column.empty().append(input);
  143. }else if (datatype == "vdir"){
  144. //Append a quick access button for vdir page
  145. column.append(`<button class="ui basic tiny button" style="margin-left: 0.4em; margin-top: 0.4em;" onclick="quickEditVdir('${uuid}');">
  146. <i class="ui yellow folder icon"></i> Edit Virtual Directories
  147. </button>`);
  148. }else if (datatype == "basicauth"){
  149. let requireBasicAuth = payload.RequireBasicAuth;
  150. let checkstate = "";
  151. if (requireBasicAuth){
  152. checkstate = "checked";
  153. }
  154. let skipWebSocketOriginCheck = payload.SkipWebSocketOriginCheck;
  155. let wsCheckstate = "";
  156. if (skipWebSocketOriginCheck){
  157. wsCheckstate = "checked";
  158. }
  159. column.empty().append(`<div class="ui checkbox" style="margin-top: 0.4em;">
  160. <input type="checkbox" class="RequireBasicAuth" ${checkstate}>
  161. <label>Require Basic Auth</label>
  162. </div>
  163. <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>
  164. <div class="ui basic advance segment" style="padding: 0.4em !important; border-radius: 0.4em;">
  165. <div class="ui endpointAdvanceConfig accordion" style="padding-right: 0.6em;">
  166. <div class="title">
  167. <i class="dropdown icon"></i>
  168. Advance Configs
  169. </div>
  170. <div class="content">
  171. <div class="ui checkbox" style="margin-top: 0.4em;">
  172. <input type="checkbox" class="SkipWebSocketOriginCheck" ${wsCheckstate}>
  173. <label>Skip WebSocket Origin Check<br>
  174. <small>Check this to allow cross-origin websocket requests</small></label>
  175. </div>
  176. <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>
  177. <!-- <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> -->
  178. </div>
  179. </div>
  180. <div>
  181. `);
  182. }else if (datatype == 'action'){
  183. column.empty().append(`
  184. <button title="Save" onclick="saveProxyInlineEdit('${uuid.hexEncode()}');" class="ui basic small icon circular button inlineEditActionBtn"><i class="ui green save icon"></i></button>
  185. <button title="Cancel" onclick="exitProxyInlineEdit();" class="ui basic small icon circular button inlineEditActionBtn"><i class="ui remove icon"></i></button>
  186. `);
  187. }else if (datatype == "inbound"){
  188. let originalContent = $(column).html();
  189. column.empty().append(`${originalContent}
  190. <div class="ui divider"></div>
  191. <div class="ui checkbox" style="margin-top: 0.4em;">
  192. <input type="checkbox" class="BypassGlobalTLS" ${payload.BypassGlobalTLS?"checked":""}>
  193. <label>Allow plain HTTP access<br>
  194. <small>Allow inbound connections without TLS/SSL</small></label>
  195. </div><br>
  196. `);
  197. }else{
  198. //Unknown field. Leave it untouched
  199. }
  200. });
  201. $(".endpointAdvanceConfig").accordion();
  202. $("#httpProxyList").find(".editBtn").addClass("disabled");
  203. }
  204. function exitProxyInlineEdit(){
  205. listProxyEndpoints();
  206. $("#httpProxyList").find(".editBtn").removeClass("disabled");
  207. }
  208. function saveProxyInlineEdit(uuid){
  209. uuid = uuid.hexDecode();
  210. var row = $('tr[eptuuid="' + uuid + '"]');
  211. if (row.length == 0){
  212. return;
  213. }
  214. var epttype = "host";
  215. let newDomain = $(row).find(".Domain").val();
  216. let requireTLS = $(row).find(".RequireTLS")[0].checked;
  217. let skipCertValidations = $(row).find(".SkipCertValidations")[0].checked;
  218. let requireBasicAuth = $(row).find(".RequireBasicAuth")[0].checked;
  219. let bypassGlobalTLS = $(row).find(".BypassGlobalTLS")[0].checked;
  220. let bypassWebsocketOrigin = $(row).find(".SkipWebSocketOriginCheck")[0].checked;
  221. console.log(newDomain, requireTLS, skipCertValidations, requireBasicAuth)
  222. $.ajax({
  223. url: "/api/proxy/edit",
  224. method: "POST",
  225. data: {
  226. "type": epttype,
  227. "rootname": uuid,
  228. "ep":newDomain,
  229. "bpgtls": bypassGlobalTLS,
  230. "tls" :requireTLS,
  231. "tlsval": skipCertValidations,
  232. "bpwsorg" : bypassWebsocketOrigin,
  233. "bauth" :requireBasicAuth,
  234. },
  235. success: function(data){
  236. if (data.error !== undefined){
  237. msgbox(data.error, false, 6000);
  238. }else{
  239. msgbox("Proxy endpoint updated");
  240. listProxyEndpoints();
  241. }
  242. }
  243. })
  244. }
  245. /* button events */
  246. function editBasicAuthCredentials(uuid){
  247. let payload = encodeURIComponent(JSON.stringify({
  248. ept: "host",
  249. ep: uuid
  250. }));
  251. showSideWrapper("snippet/basicAuthEditor.html?t=" + Date.now() + "#" + payload);
  252. }
  253. function quickEditVdir(uuid){
  254. openTabById("vdir");
  255. $("#vdirBaseRoutingRule").parent().dropdown("set selected", uuid);
  256. }
  257. function editCustomHeaders(uuid){
  258. let payload = encodeURIComponent(JSON.stringify({
  259. ept: "host",
  260. ep: uuid
  261. }));
  262. showSideWrapper("snippet/customHeaders.html?t=" + Date.now() + "#" + payload);
  263. }
  264. function handleProxyRuleToggle(object){
  265. let endpointUUID = $(object).attr("eptuuid");
  266. let isChecked = object.checked;
  267. $.ajax({
  268. url: "/api/proxy/toggle",
  269. data: {
  270. "ep": endpointUUID,
  271. "enable": isChecked
  272. },
  273. success: function(data){
  274. if (data.error != undefined){
  275. msgbox(data.error, false);
  276. }else{
  277. if (isChecked){
  278. msgbox("Proxy Rule Enabled");
  279. }else{
  280. msgbox("Proxy Rule Disabled");
  281. }
  282. }
  283. }
  284. })
  285. }
  286. //Bind on tab switch events
  287. tabSwitchEventBind["httprp"] = function(){
  288. listProxyEndpoints();
  289. }
  290. </script>