Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forwarded proto #245

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules/
.nyc_output/
.vscode/
examples/*/package-lock.json
.tap
2 changes: 1 addition & 1 deletion examples/blacklist/blacklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

module.exports = function ({ blockedDomains, message }) {
function isRequestBlocked(data) {
const { hostname } = URL.parse(data.url);

Check warning on line 7 in examples/blacklist/blacklist.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 7 in examples/blacklist/blacklist.js

View workflow job for this annotation

GitHub Actions / build (latest)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
return blockedDomains.some(
(blockedDomain) =>
hostname === blockedDomain || hostname.endsWith(`.${blockedDomain}`)
hostname === blockedDomain || hostname.endsWith(`.${blockedDomain}`),
);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/blacklist/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const unblocker = Unblocker({
app.use(unblocker);

app.get("/", (req, res) =>
res.redirect("/proxy/https://en.wikipedia.org/wiki/Main_Page")
res.redirect("/proxy/https://en.wikipedia.org/wiki/Main_Page"),
);

app.listen(8080).on("upgrade", unblocker.onUpgrade);
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-user-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"unblocker": "file:..\\.."
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"start": "node server.js"
Expand Down
6 changes: 3 additions & 3 deletions examples/custom-user-agent/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ app.use(unblocker);

app.get("/", (req, res) =>
res.end(
"Use the format http://thissite.com/proxy/http://site-i-want.com/ to access the proxy."
)
"Use the format http://thissite.com/proxy/http://site-i-want.com/ to access the proxy.",
),
);

app.listen(8080).on("upgrade", unblocker.onUpgrade);

console.log(
"app listening on port 8080. Test at http://localhost:8080/proxy/https://duckduckgo.com/?q=what%27s+my+user+agent&atb=v130-1ei&ia=answer"
"app listening on port 8080. Test at http://localhost:8080/proxy/https://duckduckgo.com/?q=what%27s+my+user+agent&atb=v130-1ei&ia=answer",
);
2 changes: 1 addition & 1 deletion examples/express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const unblocker = Unblocker({
app.use(unblocker);

app.get("/", (req, res) =>
res.redirect("/proxy/https://en.wikipedia.org/wiki/Main_Page")
res.redirect("/proxy/https://en.wikipedia.org/wiki/Main_Page"),
);

// start the server and allow unblocker to proxy websockets:
Expand Down
2 changes: 1 addition & 1 deletion examples/replace_snippet/replace_snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function (config) {
this.push(updated, "utf8");
next();
},
})
}),
);
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/replace_snippet/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ app.use(
responseMiddleware: [
replaceSnippet({
processContentTypes: ["text/html"],
searchFor: /<script type="text\/javascript">\s*BrowserCheck.testForCookies\(\);\s*<\/script>/i,
searchFor:
/<script type="text\/javascript">\s*BrowserCheck.testForCookies\(\);\s*<\/script>/i,
replaceWith: "",
}),
],
})
}),
);

app.get("/", (req, res) =>
res.redirect("/proxy/https://en.wikipedia.org/wiki/Main_Page")
res.redirect("/proxy/https://en.wikipedia.org/wiki/Main_Page"),
);

app.listen(8080);
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var server = http
if (req.url == "/") {
res.writeHead(200, headers);
return res.end(
"Use the format http://thissite.com/proxy/http://site-i-want.com/ to access the proxy."
"Use the format http://thissite.com/proxy/http://site-i-want.com/ to access the proxy.",
);
} else {
res.writeHead(404, headers);
Expand Down
2 changes: 1 addition & 1 deletion examples/whitelist/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const unblocker = Unblocker({
app.use(unblocker);

app.get("/", (req, res) =>
res.redirect("/proxy/https://en.wikipedia.org/wiki/Main_Page")
res.redirect("/proxy/https://en.wikipedia.org/wiki/Main_Page"),
);

app.listen(8080).on("upgrade", unblocker.onUpgrade);
Expand Down
2 changes: 1 addition & 1 deletion examples/whitelist/whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

module.exports = function ({ allowedDomains, message }) {
function isRequestAllowed(data) {
const { hostname } = URL.parse(data.url);

Check warning on line 7 in examples/whitelist/whitelist.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 7 in examples/whitelist/whitelist.js

View workflow job for this annotation

GitHub Actions / build (latest)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
return allowedDomains.some(
(allowedDomain) =>
hostname === allowedDomain || hostname.endsWith(`.${allowedDomain}`)
hostname === allowedDomain || hostname.endsWith(`.${allowedDomain}`),
);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/youtube/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var server = http
if (req.url == "/") {
res.writeHead(200, headers);
return res.end(
'Visit a link such as <a href="/proxy/https://www.youtube.com/watch?v=dQw4w9WgXcQ"><script>document.write(window.location)</script>proxy/https://www.youtube.com/watch?v=dQw4w9WgXcQ</a> to see the magic.'
'Visit a link such as <a href="/proxy/https://www.youtube.com/watch?v=dQw4w9WgXcQ"><script>document.write(window.location)</script>proxy/https://www.youtube.com/watch?v=dQw4w9WgXcQ</a> to see the magic.',
);
} else {
res.writeHead(404, headers);
Expand Down
4 changes: 2 additions & 2 deletions examples/youtube/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ ${formats
(format) =>
` <source type="${format.mimeType
.split(";")
.shift()}" src="/proxy/${format.url.replace(/&/g, "&amp;")}">`
.shift()}" src="/proxy/${format.url.replace(/&/g, "&amp;")}">`,
)
.join("\n")}
</video>
<p>${info.videoDetails.description.replace(/[\n]/g, "\n<br>")}</p>
</body>
</html>
`
`,
);
})
.catch((err) => {
Expand Down
20 changes: 11 additions & 9 deletions lib/charsets.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function charsets(config) {
data.stream = data.stream.pipe(
new PassThrough({
encoding: "utf8",
})
}),
);
}

Expand All @@ -53,7 +53,8 @@ module.exports = charsets;

// based on https://github.com/ashtuchkin/iconv-lite/blob/master/lib/streams.js

var re_charset_finder = /<\?xml[^>]+encoding="([^">]+)"|<meta [^>]*charset=['"]?([^ '">]+)['"]/i; // warning: making this global causes it to not include the matched value in the results :/
var re_charset_finder =
/<\?xml[^>]+encoding="([^">]+)"|<meta [^>]*charset=['"]?([^ '">]+)['"]/i; // warning: making this global causes it to not include the matched value in the results :/

// == Decoder stream =======================================================
function IconvHtmlStream(options) {
Expand All @@ -77,7 +78,7 @@ IconvHtmlStream.prototype = Object.create(Transform.prototype, {
IconvHtmlStream.prototype._transform = function (chunk, encoding, done) {
if (!Buffer.isBuffer(chunk))
return done(
new Error("delayed decoding stream needs buffers as its input.")
new Error("delayed decoding stream needs buffers as its input."),
);

if (this.isBuffering) {
Expand Down Expand Up @@ -123,7 +124,7 @@ IconvHtmlStream.prototype.startStreaming = function (charset, encoding, done) {
} else {
console.error(
"unrecognized charset %s, decoding as utf8",
this.inputEncoding
this.inputEncoding,
);
}
this.emit("charset", this.inputEncoding);
Expand All @@ -150,7 +151,8 @@ IconvHtmlStream.prototype._flush = function (done) {
}
};

var re_charset_replacer = /<\?xml[^>]+encoding="([^">]+)"|<meta [^>]*charset=['"]?([^ '">]+)['"]/gi; // similar to the charset_finder, except global
var re_charset_replacer =
/<\?xml[^>]+encoding="([^">]+)"|<meta [^>]*charset=['"]?([^ '">]+)['"]/gi; // similar to the charset_finder, except global

function MetaCharsetReplacerStream(options) {
options = options || {};
Expand All @@ -168,7 +170,7 @@ MetaCharsetReplacerStream.prototype = Object.create(Transform.prototype, {
MetaCharsetReplacerStream.prototype._transform = function (
chunk,
encoding,
done
done,
) {
done(
null,
Expand All @@ -182,10 +184,10 @@ MetaCharsetReplacerStream.prototype._transform = function (
debug(
"rewriting charset meta tag from %s to %s",
subChunk,
newSubChunk
newSubChunk,
);
return newSubChunk;
}
)
},
),
);
};
6 changes: 3 additions & 3 deletions lib/client-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function ({ prefix }) {
const clientScriptPathFs = path.join(
__dirname,
clientDir,
"unblocker-client.js"
"unblocker-client.js",
);
const isProduction = process.env.NODE_ENV === "production";
const sendOpts = {
Expand Down Expand Up @@ -44,12 +44,12 @@ module.exports = function ({ prefix }) {
prefix,
url: data.url,
})}, window);</script>
`
`,
);
this.push(updated, "utf8");
next();
},
})
}),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/client/unblocker-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
wsSecure +
"://" +
wsHost +
wsPath
wsPath // eslint-disable-line
);
}
// fallback in case the regex failed
Expand Down
2 changes: 1 addition & 1 deletion lib/content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (/*config*/) {
// if any of the middleware is possibly changing the body, remove the content-length header
if (data.stream != data.remoteResponse) {
debug(
"deleting content-length header due to possible content changes by other middleware"
"deleting content-length header due to possible content changes by other middleware",
);
delete data.headers["content-length"];
}
Expand Down
14 changes: 7 additions & 7 deletions lib/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
// will rewrite the links to start with the old protocol & domain (so that we get sent the cookies), and then it
// will copy the old cookies to the new path
function redirectCookiesWith(data) {
var uri = URL.parse(data.url, true); // true = parseQueryString

Check warning on line 29 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 29 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (latest)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
if (uri.query[REDIRECT_QUERY_PARAM]) {
var nextUri = URL.parse(uri.query[REDIRECT_QUERY_PARAM]);

Check warning on line 31 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 31 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (latest)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
debug(
"copying cookies from %s to %s",
data.url,
uri.query[REDIRECT_QUERY_PARAM]
uri.query[REDIRECT_QUERY_PARAM],
);
var cookies = libCookie.parse(data.headers.cookie || "");
var setCookieHeaders = Object.keys(cookies).map(function (name) {
Expand All @@ -56,12 +56,12 @@
}

function rewriteCookiesAndLinks(data) {
var uri = URL.parse(data.url);

Check warning on line 59 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 59 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (latest)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
var nextUri;

// this is set by the redirect middleware in the case of a 3xx redirect
if (data.redirectUrl) {
nextUri = URL.parse(data.redirectUrl);

Check warning on line 64 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 64 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (latest)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
}

// first update any set-cookie headers to ensure the path is prefixed with the site
Expand Down Expand Up @@ -97,7 +97,7 @@

// get all of the old cookies (from the request) indexed by name, and create set-cookie headers for each one
var oldCookies = libCookie.parse(
data.clientRequest.headers.cookie || ""
data.clientRequest.headers.cookie || "",
);
var oldSetCookieHeaders = _.mapValues(
oldCookies,
Expand All @@ -106,7 +106,7 @@
path:
config.prefix + nextUri.protocol + "//" + nextUri.host + "/",
});
}
},
);

// but, if we have a new cookie with the same name as an old one, delete the old one
Expand All @@ -116,14 +116,14 @@

// finally, append the remaining old cookie headers to any existing set-cookie headers in the response
data.headers["set-cookie"] = (data.headers["set-cookie"] || []).concat(
_.values(oldSetCookieHeaders)
_.values(oldSetCookieHeaders),
);
}
}

// takes a link that switches protocol and/or subdomain and makes it first go through the cookie handler on the current protocol/sub and then redirect with the cookies coppied over
function updateLink(proxiedUrl, url /*, subdomain*/) {
var next_uri = URL.parse(url);

Check warning on line 126 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 126 in lib/cookies.js

View workflow job for this annotation

GitHub Actions / build (latest)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
if (next_uri.protocol != uri.protocol || next_uri.host != uri.host) {
// rewrite the url - we want the old proto and domain, but the new path just in case there are any cookies that are limited to that sub-path (although they won't be on the new protodomain...)
var cookieProxiedUrl =
Expand All @@ -139,7 +139,7 @@
debug(
"rewriting link from %s to %s in order to allow cookies to be copied over to new path",
proxiedUrl,
cookieProxiedUrl
cookieProxiedUrl,
);
return cookieProxiedUrl;
} else {
Expand All @@ -153,7 +153,7 @@
var tld = TLD.registered(uri.hostname);
var RE_PROTO_SUBDOMAIN_URL = new RegExp(
config.prefix + "(https?://([a-z0-9.-]+.)?" + tld + "[^'\") \\\\]*)",
"ig"
"ig",
);

data.stream = data.stream.pipe(
Expand All @@ -166,7 +166,7 @@
this.push(updated, "utf8");
next();
},
})
}),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/decompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function (config) {
if (contentTypes.shouldProcess(config, data) && shouldProcess(data)) {
debug(
"decompressing %s encoding and deleting content-encoding header",
data.headers["content-encoding"]
data.headers["content-encoding"],
);

// https://github.com/nfriedly/node-unblocker/pull/105
Expand Down
2 changes: 1 addition & 1 deletion lib/get-real-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// note: the prefix only appears in the regex once because the other will have already been trimmed out.
var RE_DUOBLE_PREFIX = new RegExp(
"^https?:/?/?" + config.prefix + "(https?://)",
"i"
"i",
);
/**
* Takes a /proxy/http://site.com url from a request or a referer and returns the http://site.com/ part
Expand All @@ -18,7 +18,7 @@
* @todo: come up with a better name for this
*/
function getRealUrl(path) {
var uri = URL.parse(path),

Check warning on line 21 in lib/get-real-url.js

View workflow job for this annotation

GitHub Actions / build (lts/*)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 21 in lib/get-real-url.js

View workflow job for this annotation

GitHub Actions / build (latest)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
real_url = uri.path.substr(config.prefix.length);
real_url = real_url.replace(RE_DUOBLE_PREFIX, "$1");
real_url = real_url.replace(RE_UNMERGE_SLASHES, "$1/$2");
Expand Down
2 changes: 1 addition & 1 deletion lib/meta-robots.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function (/* config */) {
.toString()
.replace(
"</head>",
'<meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>\n</head>'
'<meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>\n</head>',
);
this.push(updated, "utf8");
next();
Expand Down
6 changes: 3 additions & 3 deletions lib/middleware-debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getDebugMiddlewareFor(middleware, dir) {
"starting %s middleware stack for %s %s",
dir,
data.contentType || "",
data.url
data.url,
);
data.prevStream = null;
}
Expand All @@ -43,7 +43,7 @@ function getDebugMiddlewareFor(middleware, dir) {
dir,
prevMiddleware || "source",
chunk.length,
hash
hash,
);
if (data.prevHash && hash != data.prevHash) {
debug("chunk modified by " + prevMiddleware);
Expand All @@ -52,7 +52,7 @@ function getDebugMiddlewareFor(middleware, dir) {
this.push(chunk);
next();
},
})
}),
);
}
if (nextName) {
Expand Down
Loading
Loading