簡體   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