2

I need help creating a proxy server using node js to use with firefox.

the end goal is to create a proxy server that will tunnel the traffic through another proxy server (HTTP/SOCKS) and return the response back to firefox. like this

enter image description here

I wanna keep the original response received from the proxy server and also wanna support https websites as well.

Here is the code I came up with.

var http = require('http');
var request = require("request");
http.createServer(function(req, res){
    const resu = request(req.url, {
        // I wanna Fetch the proxy From database and use it here
        proxy: "<Proxy URL>"
    })
    req.pipe(resu);
    resu.pipe(res);
}).listen(8080);

But it has 2 problems.

  1. It does not support https requests.
  2. It also does not supports SOCKS 4/5 proxies.

EDIT: I tried to create a proxy server using this module. https://github.com/http-party/node-http-proxy but the problem is we cannot specify any external proxy server to send connections through.

2 Answers 2

4

I have found a really super simple solution to the problem. We can just forward all packets as it is to the proxy server. and still can handle the server logic with ease.

var net = require('net');
const server = net.createServer()

server.on('connection', function(socket){
    var laddr = socket.remoteAddress;
    console.log(laddr)
    var to = net.createConnection({
        host: "<Proxy IP>",
        port: <Proxy Port>
    });
    socket.pipe(to);
    to.pipe(socket);
});

server.listen(3000, "0.0.0.0");
Sign up to request clarification or add additional context in comments.

1 Comment

Nice way. I will try to implement it in my future projects. Thanks.
2

You have to use some middleware like http-proxy module.

Documentation here: https://www.npmjs.com/package/http-proxy

Install it using npm install node-http-proxy

This might help too: How to create a simple http proxy in node.js?

3 Comments

I have looked into this module. The problem is, it doesn't allow me to use another proxy server for handling the outgoing request.
Have you tried proxyrack.com/…?
No. It it not what I wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.