1
0

customHeaders.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Notes: This should be open in its original path-->
  5. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  6. <script src="../script/jquery-3.6.0.min.js"></script>
  7. <script src="../script/semantic/semantic.min.js"></script>
  8. <style>
  9. .ui.tabular.menu .item.narrowpadding{
  10. padding: 0.6em !important;
  11. margin: 0.15em !important;
  12. }
  13. #permissionPolicyEditor.disabled{
  14. opacity: 0.4;
  15. pointer-events: none;
  16. user-select: none;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <br>
  22. <div class="ui container">
  23. <div class="ui header">
  24. <div class="content">
  25. Custom Headers
  26. <div class="sub header" id="epname"></div>
  27. </div>
  28. </div>
  29. <div class="ui divider"></div>
  30. <div class="ui small pointing secondary menu">
  31. <a class="item active narrowpadding" data-tab="customheaders">Custom Headers</a>
  32. <a class="item narrowpadding" data-tab="security">Security Headers</a>
  33. </div>
  34. <div class="ui tab basic segment active" data-tab="customheaders">
  35. <table class="ui very basic compacted unstackable celled table">
  36. <thead>
  37. <tr>
  38. <th>Key</th>
  39. <th>Value</th>
  40. <th>Remove</th>
  41. </tr></thead>
  42. <tbody id="headerTable">
  43. <tr>
  44. <td colspan="3"><i class="ui green circle check icon"></i> No Additonal Header</td>
  45. </tr>
  46. </tbody>
  47. </table>
  48. <p>
  49. <i class="angle double right blue icon"></i> Sent additional custom headers to origin server <br>
  50. <i class="angle double left orange icon"></i> Inject custom headers into origin server responses
  51. </p>
  52. <div class="ui divider"></div>
  53. <h4>Edit Custom Header</h4>
  54. <p>Add or remove custom header(s) over this proxy target</p>
  55. <div class="scrolling content ui form">
  56. <div class="five small fields credentialEntry">
  57. <div class="field" align="center">
  58. <button id="toOriginButton" style="margin-top: 0.6em;" title="Downstream to Upstream" class="ui circular basic active button">Zoraxy <i class="angle double right blue icon" style="margin-right: 0.4em;"></i> Origin</button>
  59. <button id="toClientButton" style="margin-top: 0.6em;" title="Upstream to Downstream" class="ui circular basic button">Client <i class="angle double left orange icon" style="margin-left: 0.4em;"></i> Zoraxy</button>
  60. </div>
  61. <div class="field" align="center">
  62. <button id="headerModeAdd" style="margin-top: 0.6em;" class="ui circular basic active button"><i class="ui green circle add icon"></i> Add Header</button>
  63. <button id="headerModeRemove" style="margin-top: 0.6em;" class="ui circular basic button"><i class="ui red circle times icon"></i> Remove Header</button>
  64. </div>
  65. <div class="field">
  66. <label>Header Key</label>
  67. <input id="headerName" type="text" placeholder="X-Custom-Header" autocomplete="off">
  68. <small>The header key is <b>NOT</b> case sensitive</small>
  69. </div>
  70. <div class="field">
  71. <label>Header Value</label>
  72. <input id="headerValue" type="text" placeholder="value1,value2,value3" autocomplete="off">
  73. </div>
  74. <div class="field" >
  75. <button class="ui basic button" onclick="addCustomHeader();"><i class="green add icon"></i> Add Header Rewrite Rule</button>
  76. </div>
  77. <div class="ui divider"></div>
  78. </div>
  79. </div>
  80. </div>
  81. <div class="ui tab basic segment" data-tab="security">
  82. <h4>HTTP Strict Transport Security</h4>
  83. <p>Force future attempts to access this site to only use HTTPS</p>
  84. <div class="ui toggle checkbox">
  85. <input type="checkbox" id="enableHSTS" name="enableHSTS">
  86. <label>Enable HSTS<br>
  87. <small>HSTS header will be automatically ignored if the site is accessed using HTTP</small></label>
  88. </div>
  89. <div class="ui divider"></div>
  90. <h4>Permission Policy</h4>
  91. <p>Explicitly declare what functionality can and cannot be used on this website. </p>
  92. <div class="ui toggle checkbox" style="margin-top: 0.6em;">
  93. <input type="checkbox" id="enablePP" name="enablePP">
  94. <label>Enable Permission Policy<br>
  95. <small>Enable Permission-Policy header with all allowed state.</small></label>
  96. </div>
  97. <div style="margin-top: 1em;" id="permissionPolicyEditor">
  98. <table class="ui celled unstackable very compact table">
  99. <thead>
  100. <tr><th>Feature</th>
  101. <th>Enabled</th>
  102. <th>Allow All (*)</th>
  103. <th>Self Only (self)</th>
  104. </tr></thead>
  105. <tbody id="permissionPolicyEditTable">
  106. <tr>
  107. <td>James</td>
  108. <td>24</td>
  109. <td>Engineer</td>
  110. <td>Engineer</td>
  111. </tr>
  112. </tbody>
  113. </table>
  114. </div>
  115. <br>
  116. <button class="ui basic button"><i class="green save icon"></i> Save</button>
  117. </div>
  118. <div class="field" >
  119. <button class="ui basic button" style="float: right;" onclick="closeThisWrapper();">Close</button>
  120. </div>
  121. </div>
  122. <br><br><br><br>
  123. <script>
  124. $('.menu .item').tab();
  125. let editingEndpoint = {};
  126. if (window.location.hash.length > 1){
  127. let payloadHash = window.location.hash.substr(1);
  128. try{
  129. payloadHash = JSON.parse(decodeURIComponent(payloadHash));
  130. $("#epname").text(payloadHash.ep);
  131. editingEndpoint = payloadHash;
  132. }catch(ex){
  133. console.log("Unable to load endpoint data from hash")
  134. }
  135. }
  136. function closeThisWrapper(){
  137. parent.hideSideWrapper(true);
  138. }
  139. //Bind events to header mod mode
  140. $("#headerModeAdd").on("click", function(){
  141. $("#headerModeAdd").addClass("active");
  142. $("#headerModeRemove").removeClass("active");
  143. $("#headerValue").parent().show();
  144. });
  145. $("#headerModeRemove").on("click", function(){
  146. $("#headerModeAdd").removeClass("active");
  147. $("#headerModeRemove").addClass("active");
  148. $("#headerValue").parent().hide();
  149. $("#headerValue").val("");
  150. });
  151. //Bind events to header directions option
  152. $("#toOriginButton").on("click", function(){
  153. $("#toOriginButton").addClass("active");
  154. $("#toClientButton").removeClass("active");
  155. });
  156. $("#toClientButton").on("click", function(){
  157. $("#toOriginButton").removeClass("active");
  158. $("#toClientButton").addClass("active");
  159. });
  160. //Return "add" or "remove" depending on mode user selected
  161. function getHeaderEditMode(){
  162. if ($("#headerModeAdd").hasClass("active")){
  163. return "add";
  164. }
  165. return "remove";
  166. }
  167. //Return "toOrigin" or "toClient"
  168. function getHeaderDirection(){
  169. if ($("#toOriginButton").hasClass("active")){
  170. return "toOrigin";
  171. }
  172. return "toClient";
  173. }
  174. //$("#debug").text(JSON.stringify(editingEndpoint));
  175. function addCustomHeader(){
  176. let name = $("#headerName").val().trim();
  177. let value = $("#headerValue").val().trim();
  178. if (name == ""){
  179. $("#headerName").parent().addClass("error");
  180. return
  181. }else{
  182. $("#headerName").parent().removeClass("error");
  183. }
  184. if (getHeaderEditMode() == "add"){
  185. if (value == ""){
  186. $("#headerValue").parent().addClass("error");
  187. return
  188. }else{
  189. $("#headerValue").parent().removeClass("error");
  190. }
  191. }
  192. $.ajax({
  193. url: "/api/proxy/header/add",
  194. data: {
  195. "type": getHeaderEditMode(),
  196. "domain": editingEndpoint.ep,
  197. "direction":getHeaderDirection(),
  198. "name": name,
  199. "value": value
  200. },
  201. success: function(data){
  202. if (data.error != undefined){
  203. if (parent != undefined && parent.msgbox != undefined){
  204. parent.msgbox(data.error,false);
  205. }else{
  206. alert(data.error);
  207. }
  208. }else{
  209. listCustomHeaders();
  210. if (parent != undefined && parent.msgbox != undefined){
  211. parent.msgbox("Custom header added",true);
  212. }
  213. //Clear the form
  214. $("#headerName").val("");
  215. $("#headerValue").val("");
  216. }
  217. }
  218. });
  219. }
  220. function deleteCustomHeader(name){
  221. $.ajax({
  222. url: "/api/proxy/header/remove",
  223. data: {
  224. //"type": editingEndpoint.ept,
  225. "domain": editingEndpoint.ep,
  226. "name": name,
  227. },
  228. success: function(data){
  229. listCustomHeaders();
  230. if (parent != undefined && parent.msgbox != undefined){
  231. parent.msgbox("Custom header removed",true);
  232. }
  233. }
  234. });
  235. }
  236. function listCustomHeaders(){
  237. $("#headerTable").html(`<tr><td colspan="3"><i class="ui loading spinner icon"></i> Loading</td></tr>`);
  238. $.ajax({
  239. url: "/api/proxy/header/list",
  240. data: {
  241. "type": editingEndpoint.ept,
  242. "domain": editingEndpoint.ep,
  243. },
  244. success: function(data){
  245. if (data.error != undefined){
  246. alert(data.error);
  247. }else{
  248. $("#headerTable").html("");
  249. data.forEach(header => {
  250. let editModeIcon = header.IsRemove?`<i class="ui red times circle icon"></i>`:`<i class="ui green add circle icon"></i>`;
  251. let direction = (header.Direction==0)?`<i class="angle double right blue icon"></i>`:`<i class="angle double left orange icon"></i>`;
  252. let valueField = header.Value;
  253. if (header.IsRemove){
  254. valueField = "<small style='color: grey;'>(Field Removed)</small>";
  255. }
  256. $("#headerTable").append(`
  257. <tr>
  258. <td>${direction} ${header.Key}</td>
  259. <td>${editModeIcon} ${valueField}</td>
  260. <td><button class="ui basic circular mini red icon button" onclick="deleteCustomHeader('${header.Key}');"><i class="ui trash icon"></i></button></td>
  261. </tr>
  262. `);
  263. });
  264. if (data.length == 0){
  265. $("#headerTable").html(`<tr>
  266. <td colspan="3"><i class="ui green circle check icon"></i> No Additonal Header</td>
  267. </tr>`);
  268. }
  269. }
  270. },
  271. });
  272. }
  273. listCustomHeaders();
  274. //Start HSTS state
  275. function initHSTSState(){
  276. $.get("/api/proxy/header/handleHSTS?domain=" + editingEndpoint.ep, function(data){
  277. if (data == 0){
  278. //HSTS disabled
  279. $("#enableHSTS").parent().checkbox("set unchecked");
  280. }else{
  281. //HSTS enabled
  282. $("#enableHSTS").parent().checkbox("set checked");
  283. }
  284. /* Bind events to toggles */
  285. $("#enableHSTS").on("change", function(){
  286. let HSTSEnabled = $("#enableHSTS")[0].checked;
  287. $.ajax({
  288. url: "/api/proxy/header/handleHSTS",
  289. method: "POST",
  290. data: {
  291. "domain": editingEndpoint.ep,
  292. "maxage": 31536000
  293. },
  294. success: function(data){
  295. if (data.error != undefined){
  296. parent.msgbox(data.error, false);
  297. }else{
  298. parent.msgbox(`HSTS ${HSTSEnabled?"Enabled":"Disabled"}`);
  299. }
  300. }
  301. })
  302. });
  303. });
  304. }
  305. initHSTSState();
  306. /* List permission policy header from server */
  307. function initPermissionPolicy(){
  308. $.get("/api/proxy/header/handlePermissionPolicy?domain=" + editingEndpoint.ep, function(data){
  309. if (data.error != undefined){
  310. console.log(data.error);
  311. $("#enablePP").parent().addClass('disabled');
  312. return;
  313. }
  314. //Set checkbox state
  315. if (data.PPEnabled){
  316. $("#enablePP").parent().checkbox("set checked");
  317. $("#permissionPolicyEditor").removeClass("disabled");
  318. }else{
  319. $("#enablePP").parent().checkbox("set unchecked");
  320. $("#permissionPolicyEditor").addClass("disabled");
  321. }
  322. //Render the table to list
  323. $("#permissionPolicyEditTable").html("");
  324. for (const [key, value] of Object.entries(data.CurrentPolicy)) {
  325. let allowall = "";
  326. let allowself = "";
  327. let enabled = "checked";
  328. if (value.length == 1 && value[0] == "*"){
  329. allowall = "checked";
  330. }else if (value.length == 1 && value[0] == "self"){
  331. allowself = "checked";
  332. }
  333. if (value.length == 0){
  334. enabled = ""
  335. }
  336. $("#permissionPolicyEditTable").append(`<tr>
  337. <td>${key}</td>
  338. <td>
  339. <div class="ui checkbox">
  340. <input class="enabled" type="checkbox" name="${key}" ${enabled}>
  341. <label></label>
  342. </div>
  343. </td>
  344. <td>
  345. <div class="ui radio checkbox">
  346. <input type="radio" value="all" name="${key}-target" ${allowall}>
  347. <label></label>
  348. </div>
  349. </td>
  350. <td>
  351. <div class="ui radio checkbox">
  352. <input type="radio" value="self" name="${key}-target" ${allowself}>
  353. <label></label>
  354. </div>
  355. </td>
  356. </tr>`);
  357. }
  358. });
  359. }
  360. initPermissionPolicy();
  361. </script>
  362. </body>
  363. </html>