How to properly create an HTTPS transparent proxy server with node?

Viewed 1718

I am using the npm package http-proxy that claims to help with that, however I am totally incapable to make it work. So far I have only success by making an transparent HTTP proxy server, however when it goes to create a transparent HTTPS proxy server then nothing happens.

I am using an Android device configured to use a proxy with the port where the proxy is expecting to be configured, but nothing is triggered in the nodejs side. Only if I have configured an HTTP proxy server then things seems to be working.

This is the code I have for the HTTPS:

var https = require('https');
var fs = require('fs');
var httpProxy = require('http-proxy');

var options = {
    key: fs.readFileSync('./client-key.pem', 'utf8'),
    cert: fs.readFileSync('./client-cert.pem', 'utf8')
};

var proxy = httpProxy.createProxyServer({
    ssl: options
});

https.createServer(options, function (req, res) {
    console.log("new", req.url);
    proxy.web(req, res, {
        target: req.url
    });
}).listen(8000);

If I use the createServer from the http package then it works for http calls (as in that the callback is being fired), however it does not for https with these instruction. Anybody knows what am I doing wrong?

PS: I do not care if I have to use a different npm package.

1 Answers

you can use https://www.npmjs.com/package/transparent-proxy

It is built extending native net.createServer. It acts like a REAL transparent http proxy and It allows you, for example, to easy upstream requests to other proxies and make some requests-chaining through multiple proxies... etc...

Install

npm i transparent-proxy

Use

const ProxyServer = require('transparent-proxy');

//init ProxyServer
const server = new ProxyServer();

//starting server on port 8080
server.listen(8080, '0.0.0.0', function () {
    console.log('TCP-Proxy-Server started!', server.address());
});

It works on Termux too :)

Related