customHeaders.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Notes: This should be open in its original path-->
  5. <meta charset="utf-8">
  6. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  7. <script src="../script/jquery-3.6.0.min.js"></script>
  8. <script src="../script/semantic/semantic.min.js"></script>
  9. <style>
  10. .ui.tabular.menu .item.narrowpadding{
  11. padding: 0.6em !important;
  12. margin: 0.15em !important;
  13. }
  14. #permissionPolicyEditor.disabled{
  15. opacity: 0.4;
  16. pointer-events: none;
  17. user-select: none;
  18. }
  19. #permissionPolicyEditor .experimental{
  20. background-color: rgb(241, 241, 241);
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <br>
  26. <div class="ui container">
  27. <div class="ui header">
  28. <div class="content">
  29. Custom Headers
  30. <div class="sub header" id="epname"></div>
  31. </div>
  32. </div>
  33. <div class="ui divider"></div>
  34. <div class="ui small pointing secondary menu">
  35. <a class="item active narrowpadding" data-tab="customheaders">Custom Headers</a>
  36. <a class="item narrowpadding" data-tab="security">Security Headers</a>
  37. </div>
  38. <div class="ui tab basic segment active" data-tab="customheaders">
  39. <table class="ui very basic compacted unstackable celled table">
  40. <thead>
  41. <tr>
  42. <th>Key</th>
  43. <th>Value</th>
  44. <th>Remove</th>
  45. </tr></thead>
  46. <tbody id="headerTable">
  47. <tr>
  48. <td colspan="3"><i class="ui green circle check icon"></i> No Additonal Header</td>
  49. </tr>
  50. </tbody>
  51. </table>
  52. <p>
  53. <i class="angle double right blue icon"></i> Add or remove headers before sending to origin server <br>
  54. <i class="angle double left orange icon"></i> Modify headers from origin server responses before sending to client
  55. </p>
  56. <div class="ui divider"></div>
  57. <h4>Edit Custom Header</h4>
  58. <p>Add or remove custom header(s) over this proxy target</p>
  59. <div class="scrolling content ui form">
  60. <div class="five small fields credentialEntry">
  61. <div class="field" align="center">
  62. <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>
  63. <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>
  64. </div>
  65. <div class="field" align="center">
  66. <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>
  67. <button id="headerModeRemove" style="margin-top: 0.6em;" class="ui circular basic button"><i class="ui red circle times icon"></i> Remove Header</button>
  68. </div>
  69. <div class="field">
  70. <label>Header Key</label>
  71. <input id="headerName" type="text" placeholder="X-Custom-Header" autocomplete="off">
  72. <small>The header key is <b>NOT</b> case sensitive</small>
  73. </div>
  74. <div class="field">
  75. <label>Header Value</label>
  76. <input id="headerValue" type="text" placeholder="value1,value2,value3" autocomplete="off">
  77. </div>
  78. <div class="field" >
  79. <button class="ui basic button" onclick="addCustomHeader();"><i class="green add icon"></i> Add Header Rewrite Rule</button>
  80. </div>
  81. <div class="ui divider"></div>
  82. </div>
  83. </div>
  84. </div>
  85. <div class="ui tab basic segment" data-tab="security">
  86. <h4>HTTP Strict Transport Security</h4>
  87. <p>Force future attempts to access this site to only use HTTPS</p>
  88. <div class="ui toggle checkbox">
  89. <input type="checkbox" id="enableHSTS" name="enableHSTS">
  90. <label>Enable HSTS<br>
  91. <small>HSTS header will be automatically ignored if the site is accessed using HTTP</small></label>
  92. </div>
  93. <div class="ui divider"></div>
  94. <h4>Permission Policy</h4>
  95. <p>Explicitly declare what functionality can and cannot be used on this website. </p>
  96. <div class="ui toggle checkbox" style="margin-top: 0.6em;">
  97. <input type="checkbox" id="enablePP" name="enablePP">
  98. <label>Enable Permission Policy<br>
  99. <small>Enable Permission-Policy header with all allowed state.</small></label>
  100. </div>
  101. <div style="margin-top: 1em;" id="permissionPolicyEditor">
  102. <table class="ui celled unstackable very compact table">
  103. <thead>
  104. <tr><th>Feature</th>
  105. <th>Enabled</th>
  106. <th>Allow All (*)</th>
  107. <th>Self Only (self)</th>
  108. </tr></thead>
  109. <tbody id="permissionPolicyEditTable">
  110. <tr>
  111. <td colspan="4"><i class="ui loading spinner icon"></i> Generating</td>
  112. </tr>
  113. </tbody>
  114. </table>
  115. </div>
  116. <small><i class="ui yellow exclamation triangle icon"></i> Grey out fields are non-standard permission policies</small>
  117. <br><br>
  118. <button class="ui basic button" onclick="savePermissionPolicy();"><i class="green save icon"></i> Save</button>
  119. </div>
  120. <div class="field" >
  121. <button class="ui basic button" style="float: right;" onclick="closeThisWrapper();">Close</button>
  122. </div>
  123. </div>
  124. <br><br><br><br>
  125. <script>
  126. $('.menu .item').tab();
  127. let permissionPolicyKeys = [];
  128. let editingEndpoint = {};
  129. if (window.location.hash.length > 1){
  130. let payloadHash = window.location.hash.substr(1);
  131. try{
  132. payloadHash = JSON.parse(decodeURIComponent(payloadHash));
  133. $("#epname").text(payloadHash.ep);
  134. editingEndpoint = payloadHash;
  135. }catch(ex){
  136. console.log("Unable to load endpoint data from hash")
  137. }
  138. }
  139. function closeThisWrapper(){
  140. parent.hideSideWrapper(true);
  141. }
  142. //Bind events to header mod mode
  143. $("#headerModeAdd").on("click", function(){
  144. $("#headerModeAdd").addClass("active");
  145. $("#headerModeRemove").removeClass("active");
  146. $("#headerValue").parent().show();
  147. });
  148. $("#headerModeRemove").on("click", function(){
  149. $("#headerModeAdd").removeClass("active");
  150. $("#headerModeRemove").addClass("active");
  151. $("#headerValue").parent().hide();
  152. $("#headerValue").val("");
  153. });
  154. //Bind events to header directions option
  155. $("#toOriginButton").on("click", function(){
  156. $("#toOriginButton").addClass("active");
  157. $("#toClientButton").removeClass("active");
  158. });
  159. $("#toClientButton").on("click", function(){
  160. $("#toOriginButton").removeClass("active");
  161. $("#toClientButton").addClass("active");
  162. });
  163. //Return "add" or "remove" depending on mode user selected
  164. function getHeaderEditMode(){
  165. if ($("#headerModeAdd").hasClass("active")){
  166. return "add";
  167. }
  168. return "remove";
  169. }
  170. //Return "toOrigin" or "toClient"
  171. function getHeaderDirection(){
  172. if ($("#toOriginButton").hasClass("active")){
  173. return "toOrigin";
  174. }
  175. return "toClient";
  176. }
  177. //$("#debug").text(JSON.stringify(editingEndpoint));
  178. function addCustomHeader(){
  179. let name = $("#headerName").val().trim();
  180. let value = $("#headerValue").val().trim();
  181. if (name == ""){
  182. $("#headerName").parent().addClass("error");
  183. return
  184. }else{
  185. $("#headerName").parent().removeClass("error");
  186. }
  187. if (getHeaderEditMode() == "add"){
  188. if (value == ""){
  189. $("#headerValue").parent().addClass("error");
  190. return
  191. }else{
  192. $("#headerValue").parent().removeClass("error");
  193. }
  194. }
  195. $.ajax({
  196. url: "/api/proxy/header/add",
  197. data: {
  198. "type": getHeaderEditMode(),
  199. "domain": editingEndpoint.ep,
  200. "direction":getHeaderDirection(),
  201. "name": name,
  202. "value": value
  203. },
  204. success: function(data){
  205. if (data.error != undefined){
  206. if (parent != undefined && parent.msgbox != undefined){
  207. parent.msgbox(data.error,false);
  208. }else{
  209. alert(data.error);
  210. }
  211. }else{
  212. listCustomHeaders();
  213. if (parent != undefined && parent.msgbox != undefined){
  214. parent.msgbox("Custom header added",true);
  215. }
  216. //Clear the form
  217. $("#headerName").val("");
  218. $("#headerValue").val("");
  219. }
  220. }
  221. });
  222. }
  223. function deleteCustomHeader(name){
  224. $.ajax({
  225. url: "/api/proxy/header/remove",
  226. data: {
  227. //"type": editingEndpoint.ept,
  228. "domain": editingEndpoint.ep,
  229. "name": name,
  230. },
  231. success: function(data){
  232. listCustomHeaders();
  233. if (parent != undefined && parent.msgbox != undefined){
  234. parent.msgbox("Custom header removed",true);
  235. }
  236. }
  237. });
  238. }
  239. function listCustomHeaders(){
  240. $("#headerTable").html(`<tr><td colspan="3"><i class="ui loading spinner icon"></i> Loading</td></tr>`);
  241. $.ajax({
  242. url: "/api/proxy/header/list",
  243. data: {
  244. "type": editingEndpoint.ept,
  245. "domain": editingEndpoint.ep,
  246. },
  247. success: function(data){
  248. if (data.error != undefined){
  249. alert(data.error);
  250. }else{
  251. $("#headerTable").html("");
  252. data.forEach(header => {
  253. let editModeIcon = header.IsRemove?`<i class="ui red times circle icon"></i>`:`<i class="ui green add circle icon"></i>`;
  254. let direction = (header.Direction==0)?`<i class="angle double right blue icon"></i>`:`<i class="angle double left orange icon"></i>`;
  255. let valueField = header.Value;
  256. if (header.IsRemove){
  257. valueField = "<small style='color: grey;'>(Field Removed)</small>";
  258. }
  259. $("#headerTable").append(`
  260. <tr>
  261. <td>${direction} ${header.Key}</td>
  262. <td>${editModeIcon} ${valueField}</td>
  263. <td><button class="ui basic circular mini red icon button" onclick="deleteCustomHeader('${header.Key}');"><i class="ui trash icon"></i></button></td>
  264. </tr>
  265. `);
  266. });
  267. if (data.length == 0){
  268. $("#headerTable").html(`<tr>
  269. <td colspan="3"><i class="ui green circle check icon"></i> No Additonal Header</td>
  270. </tr>`);
  271. }
  272. }
  273. },
  274. });
  275. }
  276. listCustomHeaders();
  277. //Start HSTS state
  278. function initHSTSState(){
  279. $.get("/api/proxy/header/handleHSTS?domain=" + editingEndpoint.ep, function(data){
  280. if (data == 0){
  281. //HSTS disabled
  282. $("#enableHSTS").parent().checkbox("set unchecked");
  283. }else{
  284. //HSTS enabled
  285. $("#enableHSTS").parent().checkbox("set checked");
  286. }
  287. /* Bind events to toggles */
  288. $("#enableHSTS").on("change", function(){
  289. let HSTSEnabled = $("#enableHSTS")[0].checked;
  290. $.ajax({
  291. url: "/api/proxy/header/handleHSTS",
  292. method: "POST",
  293. data: {
  294. "domain": editingEndpoint.ep,
  295. "maxage": 31536000
  296. },
  297. success: function(data){
  298. if (data.error != undefined){
  299. parent.msgbox(data.error, false);
  300. }else{
  301. parent.msgbox(`HSTS ${HSTSEnabled?"Enabled":"Disabled"}`);
  302. }
  303. }
  304. })
  305. });
  306. });
  307. }
  308. initHSTSState();
  309. //Return true if this is an proposed permission policy feature
  310. function isExperimentalFeature(header) {
  311. // List of experimental features
  312. const experimentalFeatures = [
  313. "clipboard-read",
  314. "clipboard-write",
  315. "gamepad",
  316. "speaker-selection",
  317. "conversion-measurement",
  318. "focus-without-user-activation",
  319. "hid",
  320. "idle-detection",
  321. "interest-cohort",
  322. "serial",
  323. "sync-script",
  324. "trust-token-redemption",
  325. "unload",
  326. "window-placement",
  327. "vertical-scroll"
  328. ];
  329. header = header.replaceAll("_","-");
  330. // Check if the header is in the list of experimental features
  331. return experimentalFeatures.includes(header);
  332. }
  333. /* List permission policy header from server */
  334. function initPermissionPolicy(){
  335. $.get("/api/proxy/header/handlePermissionPolicy?domain=" + editingEndpoint.ep, function(data){
  336. if (data.error != undefined){
  337. console.log(data.error);
  338. $("#enablePP").parent().addClass('disabled');
  339. return;
  340. }
  341. //Set checkbox initial state
  342. if (data.PPEnabled){
  343. $("#enablePP").parent().checkbox("set checked");
  344. $("#permissionPolicyEditor").removeClass("disabled");
  345. }else{
  346. $("#enablePP").parent().checkbox("set unchecked");
  347. $("#permissionPolicyEditor").addClass("disabled");
  348. }
  349. //Bind toggle change events
  350. $("#enablePP").on("change", function(evt){
  351. //Set checkbox state
  352. let ppEnabled = $("#enablePP")[0].checked;
  353. if (ppEnabled){
  354. $("#permissionPolicyEditor").removeClass("disabled");
  355. }else{
  356. $("#permissionPolicyEditor").addClass("disabled");
  357. }
  358. $.ajax({
  359. url: "/api/proxy/header/handlePermissionPolicy",
  360. method: "POST",
  361. data: {
  362. enable: ppEnabled,
  363. domain: editingEndpoint.ep
  364. },
  365. success: function(data){
  366. if (data.error != undefined){
  367. parent.msgbox(data.error, false);
  368. }else{
  369. parent.msgbox(`Permission Policy ${ppEnabled?"Enabled":"Disabled"}`)
  370. }
  371. }
  372. })
  373. });
  374. //Render the table to list
  375. $("#permissionPolicyEditTable").html("");
  376. for (const [key, value] of Object.entries(data.CurrentPolicy)) {
  377. let allowall = "";
  378. let allowself = "";
  379. let enabled = "checked";
  380. if (value.length == 1 && value[0] == "*"){
  381. allowall = "checked";
  382. }else if (value.length == 1 && value[0] == "self"){
  383. allowself = "checked";
  384. }
  385. if (value.length == 0){
  386. enabled = ""
  387. allowall = "checked"; //default state
  388. }
  389. let isExperimental = isExperimentalFeature(key);
  390. $("#permissionPolicyEditTable").append(`<tr class="${isExperimental?"experimental":""}">
  391. <td>${key.replaceAll("_","-")}</td>
  392. <td>
  393. <div class="ui checkbox">
  394. <input class="enabled" type="checkbox" name="${key}" ${enabled}>
  395. <label></label>
  396. </div>
  397. </td>
  398. <td>
  399. <div class="ui radio checkbox targetinput ${!enabled?"disabled":""}">
  400. <input type="radio" value="all" name="${key}-target" ${allowall} ${!enabled?"disabled=\"\"":""}>
  401. <label></label>
  402. </div>
  403. </td>
  404. <td>
  405. <div class="ui radio checkbox targetinput ${!enabled?"disabled":""}">
  406. <input type="radio" value="self" name="${key}-target" ${allowself} ${!enabled?"disabled=\"\"":""}>
  407. <label></label>
  408. </div>
  409. </td>
  410. </tr>`);
  411. permissionPolicyKeys.push(key);
  412. }
  413. $("#permissionPolicyEditTable .enabled").on("change", function(){
  414. console.log($(this)[0].checked);
  415. let fieldGroup = $(this).parent().parent().parent();
  416. if ($(this)[0].checked){
  417. fieldGroup.find(".targetinput").removeClass("disabled");
  418. fieldGroup.find("input[type=radio]").prop('disabled', false);
  419. }else{
  420. fieldGroup.find(".targetinput").addClass("disabled");
  421. fieldGroup.find("input[type=radio]").prop('disabled', true);
  422. }
  423. })
  424. });
  425. }
  426. initPermissionPolicy();
  427. //Generate the permission policy object for sending to backend
  428. function generatePermissionPolicyObject(){
  429. function getStructuredFieldValueFromDOM(fieldKey){
  430. var policyTarget = $(`#permissionPolicyEditTable input[name="${fieldKey}-target"]:checked`).val();
  431. var isPolicyEnabled = $(`#permissionPolicyEditTable input[name="${fieldKey}"]`).is(':checked');
  432. if (!isPolicyEnabled){
  433. return [];
  434. }
  435. if (policyTarget == "all"){
  436. //Rewrite all to correct syntax
  437. policyTarget = "*";
  438. }
  439. return [policyTarget];
  440. }
  441. let newPermissionPolicyKeyValuePair = {};
  442. permissionPolicyKeys.forEach(policyKey => {
  443. newPermissionPolicyKeyValuePair[policyKey] = getStructuredFieldValueFromDOM(policyKey);
  444. });
  445. console.log(newPermissionPolicyKeyValuePair);
  446. return newPermissionPolicyKeyValuePair;
  447. }
  448. //Handle saving of permission policy
  449. function savePermissionPolicy(){
  450. let permissionPolicy = generatePermissionPolicyObject();
  451. let domain = editingEndpoint.ep;
  452. $.ajax({
  453. url: "/api/proxy/header/handlePermissionPolicy",
  454. method: "PUT",
  455. data: {
  456. "domain": domain,
  457. "pp": JSON.stringify(permissionPolicy),
  458. },
  459. success: function(data){
  460. if (data.error != undefined){
  461. parent.msgbox(data.error, false);
  462. }else{
  463. parent.msgbox("Permission Policy Updated");
  464. }
  465. }
  466. })
  467. }
  468. </script>
  469. </body>
  470. </html>