简体   繁体   中英

Creating A HTTP proxy server with https support and use another proxy server to serve the response using nodejs

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

在此处输入图像描述

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.

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?

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM