首页 > 解决方案 > 创建一个支持 https 的 HTTP 代理服务器,并使用另一个代理服务器使用 nodejs 提供响应

问题描述

我需要帮助使用 node 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 但问题是我们无法指定任何外部代理服务器来发送连接。

标签: node.js

解决方案


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

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

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

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


推荐阅读