简体   繁体   English

创建具有 https 支持的 HTTP 代理服务器,并使用另一个代理服务器使用 nodejs 提供响应

[英]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.我需要帮助使用节点 js 创建代理服务器以与 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.最终目标是创建一个代理服务器,它将通过另一个代理服务器(HTTP/SOCKS)隧道传输流量并将响应返回给 firefox。 like this像这样

在此处输入图像描述

I wanna keep the original response received from the proxy server and also wanna support https websites as well.我想保留从代理服务器收到的原始响应,也想支持 https 网站。

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.它不支持 https 请求。
  2. It also does not supports SOCKS 4/5 proxies.它也不支持 SOCKS 4/5 代理。

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. https://github.com/http-party/node-http-proxy但问题是我们无法指定任何外部代理服务器来发送连接。

You have to use some middleware like http-proxy module.您必须使用一些中间件,例如 http-proxy 模块。

Documentation here: https://www.npmjs.com/package/http-proxy此处的文档: https://www.npmjs.com/package/http-proxy

Install it using npm install node-http-proxy使用npm install node-http-proxy安装它

This might help too: How to create a simple http proxy in node.js?这也可能有帮助: 如何在 node.js 中创建一个简单的 http 代理?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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