proxy-utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. var url = require("url");
  2. module.exports.rewriteLinks = function (userServer) {
  3. var host = userServer.hostname;
  4. var string = host;
  5. var port = userServer.port;
  6. if (host && port) {
  7. if (parseInt(port, 10) !== 80) {
  8. string = host + ":" + port;
  9. }
  10. }
  11. var reg = new RegExp(
  12. // a simple, but exact match
  13. "https?:\\\\/\\\\/" +
  14. string +
  15. "|" +
  16. // following ['"] + exact
  17. "('|\")\\/\\/" +
  18. string +
  19. "|" +
  20. // exact match with optional trailing slash
  21. "https?://" +
  22. string +
  23. "(?!:)(/)?" +
  24. "|" +
  25. // following ['"] + exact + possible multiple (imr srcset etc)
  26. "('|\")(https?://|/|\\.)?" +
  27. string +
  28. "(?!:)(/)?(.*?)(?=[ ,'\"\\s])", "g");
  29. return {
  30. match: reg,
  31. //match: new RegExp("https?:\\\\/\\\\/" + string + "|https?://" + string + "(\/)?|('|\")(https?://|/|\\.)?" + string + "(\/)?(.*?)(?=[ ,'\"\\s])", "g"),
  32. //match: new RegExp("https?:\\\\?/\\\\?/" + string + "(\/)?|('|\")(https?://|\\\\?/|\\.)?" + string + "(\/)?(.*?)(?=[ ,'\"\\s])", "g"),
  33. //match: new RegExp('https?://' + string + '(\/)?|(\'|")(https?://|/|\\.)?' + string + '(\/)?(.*?)(?=[ ,\'"\\s])', 'g'),
  34. //match: new RegExp("https?:\\\\/\\\\/" + string, "g"),
  35. fn: function (req, res, match) {
  36. var proxyUrl = req.headers["host"];
  37. /**
  38. * Reject subdomains
  39. */
  40. if (match[0] === ".") {
  41. return match;
  42. }
  43. var captured = match[0] === "'" || match[0] === '"' ? match[0] : "";
  44. /**
  45. * allow http https
  46. * @type {string}
  47. */
  48. var pre = "//";
  49. if (match[0] === "'" || match[0] === '"') {
  50. match = match.slice(1);
  51. }
  52. /**
  53. * parse the url
  54. * @type {number|*}
  55. */
  56. var out = url.parse(match);
  57. /**
  58. * If host not set, just do a simple replace
  59. */
  60. if (!out.host) {
  61. string = string.replace(/^(\/)/, "");
  62. return captured + match.replace(string, proxyUrl);
  63. }
  64. /**
  65. * Only add trailing slash if one was
  66. * present in the original match
  67. */
  68. if (out.path === "/") {
  69. if (match.slice(-1) === "/") {
  70. out.path = "/";
  71. }
  72. else {
  73. out.path = "";
  74. }
  75. }
  76. /**
  77. * Finally append all of parsed url
  78. */
  79. return [
  80. captured,
  81. pre,
  82. proxyUrl,
  83. out.path || "",
  84. out.hash || ""
  85. ].join("");
  86. }
  87. };
  88. };
  89. /**
  90. * Remove 'domain' from any cookies
  91. * @param {Object} res
  92. */
  93. module.exports.checkCookies = function checkCookies(res) {
  94. if (typeof res.headers["set-cookie"] !== "undefined") {
  95. res.headers["set-cookie"] = res.headers["set-cookie"].map(function (item) {
  96. return rewriteCookies(item);
  97. });
  98. }
  99. };
  100. /**
  101. * Remove the domain from any cookies.
  102. * @param rawCookie
  103. * @returns {string}
  104. */
  105. function rewriteCookies(rawCookie) {
  106. var objCookie = (function () {
  107. // simple parse function (does not remove quotes)
  108. var obj = {};
  109. var pairs = rawCookie.split(/; */);
  110. pairs.forEach(function (pair) {
  111. var eqIndex = pair.indexOf("=");
  112. // skip things that don't look like key=value
  113. if (eqIndex < 0) {
  114. return;
  115. }
  116. var key = pair.substr(0, eqIndex).trim();
  117. obj[key] = pair.substr(eqIndex + 1, pair.length).trim();
  118. });
  119. return obj;
  120. })();
  121. var pairs = Object.keys(objCookie)
  122. .filter(function (item) {
  123. return item.toLowerCase() !== "domain";
  124. })
  125. .map(function (key) {
  126. return key + "=" + objCookie[key];
  127. });
  128. if (rawCookie.match(/httponly/i)) {
  129. pairs.push("HttpOnly");
  130. }
  131. return pairs.join("; ");
  132. }
  133. module.exports.rewriteCookies = rewriteCookies;
  134. //# sourceMappingURL=proxy-utils.js.map