I want to set up an HTTPS proxy server using node.js. It needs to pick up all the HTTPS requests from the browser window. I have a mac book and I have configured the proxy setting from the preferences for HTTPS. Below is the sample code for capturing any browser requests, is this code correct? I am generating the keys using the following commands.
openssl genrsa -out privatekey.pem 1024
openssl req -new -key privatekey.pem -out certrequest.csr
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
var options = {
https: {
key: fs.readFileSync('privatekey.pem', 'utf8'),
cert: fs.readFileSync('certificate.pem', 'utf8')
},
target: {
https: true
}
};
https.createServer(options,function(request, response) {
console.log(request);
handleRequest(request, response);
}).listen(8877);
So the above code does not work, any suggestions how i can solve this problem, thanks in advance
You might want to check out https://github.com/nodejitsu/node-http-proxy which is an http proxy for node.js. With it, all you would have to do is
var httpProxy = require('http-proxy');
var options = {
https: {
key: fs.readFileSync('privatekey.pem', 'utf8'),
cert: fs.readFileSync('certificate.pem', 'utf8')
}
};
var port = SOME_PORT;
var host = 'SOME_HOST';
httpProxy.createServer(port, host, options).listen(8877);
Now that I know that you vary the host
, I think you should rather build an http server that performs the proxying using request. Something like this (based on the request
documentation):
var https = require('https'),
request = require('request');
var options = {
https: {
key: fs.readFileSync('privatekey.pem', 'utf8'),
cert: fs.readFileSync('certificate.pem', 'utf8')
},
target: {
https: true
}
};
https.createServer(options,function(req, resp) {
var otherhost = req.some_method_to_get_host;
console.log(req);
req.pipe(request(otherhost)).pipe(resp)
}).listen(8877);