【问题标题】:Creating a URL prefix HTTP proxy gateway to a SOCKS proxy为 SOCKS 代理创建 URL 前缀 HTTP 代理网关
【发布时间】:2022-01-26 10:38:40
【问题描述】:

我正在尝试设置一个系统,该系统允许我为 URL 添加前缀,以便通过 SOCKS 代理路由请求。例如,我希望在127.0.0.1:8888 上运行一个服务,这样如果我请求http://127.0.0.1:8888/https://www.google.com/potato,该服务将通过其配置的SOCKS 代理请求https://www.google.com/potato,然后将结果返回给我。

我这样做的动机是我的 ISP 已经阻止了我所有的 Sci-Hub 镜像。我使用 Zotero,并已将其配置为通过查看 https://sci-hub.tf/<doi> 来搜索 PDF。我可以在本地运行 Tor 服务,但我不想通过 Tor 重定向我的所有 Zotero 流量,因为我是注册用户,我不希望我的流量转移到 Zotero 服务器通过 Tor:我只希望我的 Sci-Hub 请求通过 Tor,因为这是我唯一需要解除阻止的事情。

我一直在尝试使用 nginx 和 Docker Compose 来尝试让它工作,但我碰壁了。我设法使用以下最小的nginx.conf 设置了初始“URL 前缀代理/网关”服务:

events {}

http {
  resolver 127.0.0.11;
  server {
    location / {
      proxy_pass $arg_uri/;
      proxy_redirect ~^(.*) http://localhost:8888/?uri=$1;
    }
  }
}

这允许我访问http://localhost:8888/?uri=http://google.com,它会将 Google 的 301 重定向重写为 https://www.google.com 以保持“在”我的代理中。我还计划使用socks-nginx-module 来设置socks_pass 指令,希望这会路由我的proxy_pass 通过 SOCKS 代理。不幸的是,这些指令似乎相互竞争,我无法让它发挥作用。 :(

我的目标是能够通过连接 URL 将不同的代理链接在一起,这样我就可以选择在每个请求的基础上进行代理。因此,我可以通过请求http://localhost:8888/?uri=https://sci-hub.tf/<doi> 来通过 Tor 请求给定 DOI 的 Sci-Hub 页面。

我还遇到了DeleGate,他的教程表明它可能能够做到这一点,但我无法让它在 Arch 上编译,并且最新的二进制文件很旧,并且不会链接到我当前的SSL 库。 D:

所以在这一点上我有点卡住了。我无法找到任何现有的软件来执行我所描述的这种“URL 前缀”样式代理,而且我不确定如何在 nginx 中构建一个反向代理来实现这一点。

有谁知道我可以用 Tor 拼凑什么软件来创建这种系统?或者有人可以指导我如何配置 nginx 以通过 SOCKS 代理执行动态反向代理?

【问题讨论】:

    标签: http nginx proxy tor socks


    【解决方案1】:

    我设法用一个简单的 Node.js 脚本来解决这个问题:

    const axios = require('axios');
    const express = require('express');
    const SocksProxyAgent = require('socks-proxy-agent');
    
    const httpsAgent = new SocksProxyAgent('socks5://localhost:9050');
    
    const app = express();
    
    app.all(/\/.*/, (req, res) => {
      const target = req.path.substring(1);
      const targetRes = axios.request({
        url: target,
        method: req.method,
        httpAgent: httpsAgent,
        httpsAgent,
      });
      targetRes.then(re => {
        res.status(re.status);
        res.set(re.headers);
        res.send(re.data);
      });
    });
    
    app.listen(8888);
    

    【讨论】:

    • 经过更多测试,这对许多网站来说并不可靠,尤其是对那些有 Cloudflare 保护的网站,但足以让 Sci-Hub 再次工作:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多