V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
mahogany
V2EX  ›  Linux

Linux 环境下有什么工具可以不改变应用程序代码的前提下把其中 requests 请求的流量重定向到 sock5 代理

  •  
  •   mahogany · 8 天前 · 2287 次点击

    需求 1 、我想通过 api 访问 openai 服务,开发环境在本地。 2 、我想无痛代理流量,并不想修改代码,所以应该涉及 dns 解析和类似 tun 代理的方式配合。

    先前的实践 1 、使用过 socat 代理到 sock5 ,但这个需要修改 api_base ,后面 ssl 握手总有问题。

    大家有什么工具推荐吗?感觉这个应该是比较普遍的需求?

    第 1 条附言  ·  8 天前
    已解决,谢谢各位。
    32 条回复    2024-06-20 19:32:06 +08:00
    tradzero
        1
    tradzero  
       8 天前
    proxifier
    matianhe
        2
    matianhe  
       8 天前
    proxychains
    mohumohu
        3
    mohumohu  
       8 天前
    sniproxy 代理加 socks5 ,hosts 写 127.0.0.1 ,最无痛的方式。
    thomaspaine
        4
    thomaspaine  
       8 天前   ❤️ 1
    配置 iptables ,设置 tproxy
    zkl2333
        5
    zkl2333  
       8 天前
    我还以为 Linux 配个环境变量就好了
    ysc3839
        6
    ysc3839  
       8 天前 via Android
    proxychains
    zengxs
        7
    zengxs  
       8 天前
    如果你是用的 python 的话,装个 socks 代理库,然后设置环境变量就行了

    requests 安装 pysocks
    httpx (openai 库用的 http client) 安装 socksio

    然后设置环境变量 HTTPS_PROXY=socks5://user:pass@host:port

    参考 https://www.python-httpx.org/advanced/proxies/
    infun
        8
    infun  
       8 天前
    如果你是用的 Python 的话,假设本地已经有 http/socks5://127.0.0.1:7890 代理


    import os
    os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
    os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'
    infun
        10
    infun  
       8 天前
    @infun 看错了。。。。不要修改代码,忽略
    greenskinmonster
        12
    greenskinmonster  
       8 天前
    cgproxy -> 本机 v2ray tproxy -> socks5
    body007
        13
    body007  
       8 天前
    我编写了监听 443 端口的程序,按照 https 解析 tls 的 hello 数据包,提取 SNI 里面的目标域名,然后进行转发。因此我只需要在 host 文件里面添加域名解析到我的服务就行,如下所示,此时系统任何程序访问 https://域名 时都会连接 127.0.0.1:443 我这个服务,我的服务代理转发这些请求,因此不需要修改任何客户端逻辑。



    相关实现参考: https://www.agwa.name/blog/post/writing_an_sni_proxy_in_go

    当然这个方案只针对 tls 连接,其他流量需要自己编写相关代码。
    docxs
        14
    docxs  
       8 天前
    用这个 直接 gg cmd
    https://github.com/mzz2017/gg
    jiucaijiucai
        15
    jiucaijiucai  
       8 天前   ❤️ 1
    用 linux 的网络命名空间功能 + https://github.com/xjasonlyu/tun2socks + https://github.com/netblue30/firejail 可以实现:


    如果你的 socks 代理端口是本机的 7890 (注意必须是本机的),在本机上执行如下脚本 (这个脚本依赖 tun2socks):

    #!/bin/bash

    mkdir -p /etc/netns/proxy/
    echo nameserver 8.8.8.8 > /etc/netns/proxy/resolv.conf

    ip netns add proxy
    ip netns exec proxy ip link set lo up

    ip link add veth0 type veth peer name eth0 netns proxy
    ip addr add 172.16.1.1/24 dev veth0
    ip link set veth0 up

    ip netns exec proxy ip addr add 172.16.1.2/24 dev eth0
    ip netns exec proxy ip link set eth0 up
    ip netns exec proxy ip route add default via 172.16.1.1 dev eth0 metric 10

    ip netns exec proxy ip tuntap add mode tun dev tun0
    ip netns exec proxy ip addr add 198.18.0.1/15 dev tun0
    ip netns exec proxy ip link set tun0 up
    ip netns exec proxy ip route add default via 198.18.0.1 dev tun0 metric 1

    ip netns exec proxy nohup tun2socks -device tun0 -proxy socks5://172.16.1.1:7890 -interface eth0 > /var/log/proxy-tun2socks.log &


    然后使用 firejail 来执行应用程序,以下以 vscode 为例:

    firejail --noprofile --netns=proxy --quiet code

    我平常跑 vscode 和 jetbrains 全家桶都是用这种方式跑的。
    ETiV
        16
    ETiV  
       8 天前 via iPhone
    https://tsocks.sourceforge.net/about.php

    配置文件写好,然后 tsocks python ./main.py 就行
    wuzhewuyou
        17
    wuzhewuyou  
       8 天前
    直接搞个 openwrt 的旁路由吧,网关直接指向旁路由就好
    wktadmin
        18
    wktadmin  
       8 天前
    运行前修改环境变量即可
    export HTTP_PROXY=...
    export HTTPS_PROXY=...
    zltningx
        19
    zltningx  
       8 天前
    这个我做过,利用 LD_PRELOADHook C 函数实现流量劫持到代理
    具体见: https://github.com/Intika-Linux-Proxy/Proxybound
    yolee599
        20
    yolee599  
       8 天前
    vim ~/.bashrc

    然后添加以下内容:
    # set/unset clash proxy
    alias setproxy='export http_proxy=127.0.0.1:7890; export https_proxy=127.0.0.1:7890; export socks_proxy=127.0.0.1:7891'
    alias delproxy='unset http_proxy; unset https_proxy;unset socks_proxy'

    再执行一下:
    source ~/.bashrc

    打开终端默认不使用代理,需要使用的时候就执行一下 setproxy ,不需要使用的时候 delproxy 。方便得很
    license
        21
    license  
       8 天前 via Android
    dae
    jiucaijiucai
        22
    jiucaijiucai  
       8 天前
    @jiucaijiucai #15 如果需要绕过代理访问本地局域网的其他主机,可以在末尾添加如下 4 行:

    echo 1 > /proc/sys/net/ipv4/ip_forward
    iptables -t nat -I POSTROUTING -s 172.16.1.1/255.255.255.0 -j MASQUERADE
    ip netns exec proxy ip route add 192.168.1.0/24 via 172.16.1.1 dev eth0
    ip netns exec proxy ip route add 10.0.0.0/24 via 172.16.1.1 dev eth0

    这将强制 192.168.1.0/24 和 10.0.0.0/24 网段不走代理
    jiucaijiucai
        23
    jiucaijiucai  
       8 天前
    @jiucaijiucai #22 加上这 4 行后就可以使用局域网其他主机的 socks 代理,比如把 socks5://172.16.1.1:7890 替换为 socks5://192.168.1.3:7890:

    ip netns exec proxy nohup tun2socks -device tun0 -proxy socks5://192.168.1.3:7890 -interface eth0 > /var/log/proxy-tun2socks.log &
    kobe718
        24
    kobe718  
       8 天前
    1)proxychain
    2)iptables+tproxy
    3)旁路由
    4)gptapi.us
    任选其一即可
    tofuliang
        25
    tofuliang  
       8 天前
    NikoXu
        26
    NikoXu  
       8 天前
    iptables + redsocks
    tofuliang
        27
    tofuliang  
       8 天前
    mailndk
        28
    mailndk  
       8 天前
    哪有那么麻烦啊,你都明确访问 openai 了,你把 openai route 到 tun 啊,tun 再走 socks 。
    sm1314
        29
    sm1314  
       8 天前
    路由+tun2socks
    SenLief
        30
    SenLief  
       8 天前
    requests 支持系统代理的,直接配置 http_proxy, https_proxy, all_proxy 即可。
    https://docs.python-requests.org/en/latest/user/advanced/#proxies
    mahogany
        31
    mahogany  
    OP
       8 天前
    谢谢各位。

    我最终采用 SNIProxy 的方案。

    我把我的解决方案粘贴一下:
    `
    基于 gost 的 SNIProxy 方案
    时间:2024-06-18 15:28:56

    1 、curl -LO https://github.com/go-gost/gost/releases/download/v3.0.0-rc8/gost_3.0.0-rc8_linux_amd64.tar.gz
    tar zxvf gost*.tar.gz

    2 、启动
    sudo ./gost -L sni://:80 -L sni://:443 -F socks5://127.0.0.1:1081
    curl -H 'Host: www.google.com' 127.0.0.1 成功。

    3 、设置/etc/hosts
    127.0.0.1 generativelanguage.googleapis.com

    4 、清理 dns 缓存
    sudo systemctl restart systemd-resolved
    sudo service nscd restart
    sudo service dnsmasq restart

    5 、重跑应用程序。
    成功。

    总结:
    1 、流量路径:
    应用程序请求 generativelanguage.googleapis.com -> 本地 dns 解析到 127.0.0.1 -> gost sni 代理 -> 本地 v2ray socks5 代理 -> balabala... ->(远程 DNS 解析)
    2 、效果:
    应用程序不用做任何改动,同时能够细粒度地控制需要走 proxy 的域名。

    参考:
    1 、https://gost.run/concepts/proxy/
    2 、https://gost.run/tutorials/protocols/sni/
    3 、https://wener.me/story/sni-proxy#%E8%83%BD%E4%B8%8D%E8%83%BD%E4%B8%8D%E9%99%90%E5%88%B6%E5%9F%9F%E5%90%8D%E6%89%80%E6%9C%89%E9%83%BD%E4%BB%A3%E7%90%86
    `

    =========================================

    gost 这个工具没细究,看起来有点 v2ray 的味道。
    nmap
        32
    nmap  
       6 天前
    http_proxy
    https_proxy
    最简单的办法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1756 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 72ms · UTC 16:32 · PVG 00:32 · LAX 09:32 · JFK 12:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.