Last active
June 4, 2018 15:49
-
-
Save user24/9183567 to your computer and use it in GitHub Desktop.
Proxy connections, opening up Access-control-allow-origin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint node:true*/ | |
// Usage curl localhost:8080/http://example.com | |
// - proxies the request, adding Access-control-allow-origin: * and stripping Origin and Referer headers and fixing Host | |
"use strict"; | |
var request = require("request"), | |
http = require("http"), | |
url = require("url"); | |
http.createServer(function proxy(req, res) { | |
var log = true, | |
setHeader, | |
end, | |
write; | |
// Cut off the leading / | |
req.url = req.url.substring(1); | |
// Always log the URL | |
console.log("proxying " + req.url); | |
// Force universal access | |
res.setHeader("Access-control-allow-origin", "*"); | |
// Overwrite setHeader, remove any other ACAO's | |
setHeader = res.setHeader; | |
res.setHeader = function (name, value) { | |
if (name.toLowerCase() === "access-control-allow-origin") { | |
return; | |
} | |
if (log) { | |
console.log(name + ": " + value); | |
} | |
setHeader.apply(res, arguments); | |
}; | |
if (log) { | |
write = res.write; | |
res.write = function (data) { | |
console.log("write", new Buffer(data).toString()); | |
write.apply(res, arguments); | |
}; | |
end = res.end; | |
res.end = function () { | |
console.log("end", arguments); | |
end.apply(res, arguments); | |
}; | |
} | |
// Remove the Origin & ref headers: | |
delete req.headers.origin; | |
delete req.headers.referer; | |
// Set the correct host: | |
req.headers.host = url.parse(req.url).host; | |
if (log) { | |
console.log("Request headers", req.headers); | |
} | |
// Proxy the request | |
req.pipe(request(req.url)).pipe(res); | |
}).listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment