customHeaders.html 30 KB

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