繁体   English   中英

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

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

我需要帮助使用节点 js 创建代理服务器以与 firefox 一起使用。

最终目标是创建一个代理服务器,它将通过另一个代理服务器(HTTP/SOCKS)隧道传输流量并将响应返回给 firefox。 像这样

在此处输入图像描述

我想保留从代理服务器收到的原始响应,也想支持 https 网站。

这是我想出的代码。

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

但它有两个问题。

  1. 它不支持 https 请求。
  2. 它也不支持 SOCKS 4/5 代理。

编辑:我尝试使用此模块创建代理服务器。 https://github.com/http-party/node-http-proxy但问题是我们无法指定任何外部代理服务器来发送连接。

您必须使用一些中间件,例如 http-proxy 模块。

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

使用npm install node-http-proxy安装它

这也可能有帮助: 如何在 node.js 中创建一个简单的 http 代理?

我找到了一个非常简单的解决方案。 我们可以将所有数据包原样转发到代理服务器。 并且仍然可以轻松处理服务器逻辑。

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