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

tun2proxy -SOCKS5 等代理转换为 TUN 口

  •  
  •   Songxwn · 111 天前 · 2319 次点击
    这是一个创建于 111 天前的主题,其中的信息可能已经有所发展或是发生改变。

    博客

    https://songxwn.com/

    RSS: https://songxwn.com/atom.xml

    介绍

    tun2proxy 是一个基于 smoltcp 在 Linux 上 通过 HTTP 和 SOCKS 代理的创建隧道接口的项目,使用 Rust 语言开发。

    功能

    • HTTP 代理支持(无身份验证、基本身份验证和摘要身份验证)
    • SOCKS4 和 SOCKS5 支持(无身份验证、用户名/密码身份验证)
    • SOCKS4a 和 SOCKS5h 支持(通过虚拟 DNS 功能)
    • 用于路由所有流量的自动化配置
    • IPv4 和 IPv6 支持
    • SOCKS5 UDP 支持
    • 本机支持通过 TCP 代理 DNS

    项目地址:https://github.com/blechschmidt/tun2proxy

    安装使用

    手动编译

    git clone https://github.com/blechschmidt/tun2proxy.git
    
    # 克隆项目
    
    cd tun2proxy
    
    cargo build --release
    
    # 编译 (需要提前安装 cargo 工具)
    
    cd ./target/release
    
    # 进入输出目录
    
    

    下载二进制包

    官方发布地址:https://github.com/blechschmidt/tun2proxy/releases

    PS:选好对应 CPU 和系统

    本站编译好的:https://songxwn.com/file/tun2proxy (Linux X86-64)

    使用-手动配置路由和接口

    ip tuntap add name tun163 mode tun user $USER
    
    # 提前创建好 tun 三层虚拟口
    
    sudo ip link set tun163 up
    
    # 配置状态为 UP
    
    
    ./tun2proxy --tun tun163 --proxy "socks5://username:[email protected]:9527"
    
    # 前台运行,将 socks5 转换为三层接口。格式为 proto://[username[:password]@]host:port
    
    ip route add 128.0.0.0/1 dev tun163
    
    # 手动配置路由指向,也可以配置策略路由等操作。
    
    
    

    PS:IP 命令配置的都是临时的,重启失效。

    使用-自动化配置

    ./tun2proxy --setup auto --proxy "socks5://1.2.3.4:1080"
    
    
    # 自动将所有流量路由到指定的代理。
    
    

    命令说明

    Tunnel interface to proxy.
    
    Usage: tun2proxy [OPTIONS] --proxy <URL>
    
    Options:
      -t, --tun <name>         Name of the tun interface [default: tun0]
          --tun-fd <fd>        File descriptor of the tun interface
          --tun-mtu <mtu>      MTU of the tun interface (only with tunnel file descriptor) [default: 1500]
      -p, --proxy <URL>        Proxy URL in the form proto://[username[:password]@]host:port
      -d, --dns <strategy>     DNS handling strategy [default: virtual] [possible values: virtual, over-tcp, direct]
          --dns-addr <IP>      DNS resolver address [default: 8.8.8.8]
      -6, --ipv6-enabled       IPv6 enabled
      -s, --setup <method>     Routing and system setup [default: none] [possible values: none, auto]
      -b, --bypass <IP|CIDR>   IPs and CIDRs used in routing setup which should bypass the tunnel
      -v, --verbosity <level>  Verbosity level [default: info] [possible values: off, error, warn, info, debug, trace]
      -h, --help               Print help
      -V, --version            Print version
    
    

    DNS 解析

    当 DNS 解析由机器上的服务或本地网络中的服务器执行时,DNS 解析将不会通过隧道接口执行,因为指向 localhost 或本地网络的路由比 Tun 通道更细。 在这种情况下,建议更新文件以使用通过隧道接口路由的名称服务器地址。当虚拟 DNS 正常工作时,你会看到通过以下方式解析后的主机名连接日志信息,

    Tun 和 Tap

    Tun 虚拟设备和物理网卡的区别是 Tun 虚拟设备是 IP 层设备,从/dev/net/tun 字符设备上读取的是 IP 数据包,写入的也只能是 IP 数据包,因此不能进行二层操作,如发送 ARP 请求和以太网广播。与之相对的是,Tap 虚拟设备是以太网设备,处理的是二层以太网数据帧,从/dev/net/tun 字符设备上读取的是以太网数据帧,写入的也只能是以太网数据帧。从这点来看,Tap 虚拟设备和真实的物理网卡的能力更接近。

    11 条回复    2024-01-22 08:13:59 +08:00
    kkocdko
        1
    kkocdko  
       111 天前
    终于看到用 smoltcp 做的了 hhhhh ,之前就有这个想法但一直没时间做。

    还是很好玩的,但是如果想拿来用的请谨慎,smoltcp 性能堪忧,比不上 gvisor 的 tcp stack 实现。如果只是代理需求,可以考虑 sing-box 或者 clash-meta 内置的 tun device 功能(源自 gvisor netstack ,如果系统支持也可以直接用内核里的),性能要好很多。
    heiher
        2
    heiher  
       110 天前 via Android
    smoltcp 的性能确实比较一般。在我的 tun2socks 项目中比较过几款不同实现的吞吐、CPU 和内存使用量:

    https://github.com/heiher/hev-socks5-tunnel
    junmoxiao
        3
    junmoxiao  
       110 天前
    程序 C-c 停止时是否会自动恢复 sudo ip 修改的那些设置呢?
    Songxwn
        4
    Songxwn  
    OP
       110 天前
    @heiher 看着不错,也试试
    Songxwn
        5
    Songxwn  
    OP
       110 天前
    @junmoxiao 接口 down 的话,会自动取消
    junmoxiao
        6
    junmoxiao  
       110 天前
    用 --setup auto 时
    Songxwn
        7
    Songxwn  
    OP
       110 天前
    @heiher 很奇怪,把单个路由指向 tun 接口,curl 提示重连。日志报错 socks5 destruct 。请问您的项目支持 socks5 认证吗?
    heiher
        8
    heiher  
       108 天前
    @Songxwn 支持 username/password 认证方式,日志贴出来看看吧。
    Songxwn
        9
    Songxwn  
    OP
       108 天前
    @heiher #8




    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 construct
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client construct
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client tcp construct ip
    [2024-01-08 13:56:47] [I] 0x1dd1f10 socks5 client tcp -> [34.117.186.192]:80
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 session tcp construct
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 session tcp new
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 session run
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client connect [110.255.70.50]:6051
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client connect server
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 session tcp bind
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client connect server fd 10
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client set auth
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client auth prarsGvF1X:51374941
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client handshake
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client write request
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client read response
    [2024-01-08 13:56:47] [E] 0x1dd1f10 socks5 client auth.ver 72
    [2024-01-08 13:56:47] [E] 0x1dd1f10 socks5 session handshake
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 session tcp destruct
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client tcp destruct
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 client destruct
    [2024-01-08 13:56:47] [D] 0x1dd1f10 socks5 destruct
    heiher
        10
    heiher  
       97 天前
    @Songxwn 哈喽,请再试试 2.6.6 版本:

    https://github.com/heiher/hev-socks5-tunnel/releases/tag/2.6.6

    问题的机理很可能是:在 2.6.6 版本之前,hev-socks5-tunnel 的 socks5 客户端采用流水化的 socks5 握手实现方式,该方式会将所有的请求合并发送,以减少建立延迟。但这种方式对 socks5 服务器端的实现要求比较严格,如果遇到处理接收缓冲区不严谨的实现,就会造成后续请求不匹配。2.6.6 版本开始支持流水化握手方式开关,且默认关闭。
    Songxwn
        11
    Songxwn  
    OP
       96 天前
    @heiher #10 好的,我试试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1036 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 23:01 · PVG 07:01 · LAX 16:01 · JFK 19:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.