I've been working on a personal project lately, and it involves intercepting any HTTP/S requests coming from my PC to external servers (that includes requesting through browsers, curl, etc.) and sometimes redirecting requests to a personal page depending on the requested hostname.
In order to get a proof-of-concept for intercepting and forwarding requests, I built a quick HTTPS proxy server in Node.js. It shouldn't really do anything other than printing some information about each request and forwarding it to where it was supposed to go.
Here's the main part of it:
var https = require('https');
var httpProxy = require('http-proxy');
var fs = require('fs');
var path = require('path');
const options = {
ssl: {
key: fs.readFileSync(path.resolve(__dirname, './mycert.key'), 'utf8'),
cert: fs.readFileSync(path.resolve(__dirname, './mycert.crt'), 'utf8')
},
secure: false,
toProxy: true
}
const proxy = httpProxy.createProxyServer(options);
const httpsServer = https.createServer(options.ssl, (req, res) => {
console.log("WEBSERVER")
console.log(req.headers.host);
proxy.web(req, res, {
target: req.headers.host,
})
})
const httpsListener = httpsServer.listen(8080);
console.log("LISTENING ON 8080")
Problem is, whenever I turn my Windows proxy settings to go to my new server, any request made through a browser just gives me an ERR_EMPTY_RESPONSE, and nothing gets printed out in the console where I run the proxy.
If I do an HTTPS request directly to the proxy it does print what I need out, but obviously doesn't take me anywhere.
Any ideas about what I should try? To be honest, I'm not sure if it is even possible to make a "localhost" proxy server, so I might be completely off track :/.
Thanks!