文章
技术

【转载】构建并裁剪v2ray

https://starts.sh/posts/go_v2ray.html

菜单
  1. Misaki KBBL
    Misaki   习帝,习帝,我要Diss你!

    备份:

    新版 v2ray 可以直接读取 config.json 配置文件,不像以前需要依赖 v2ctl 转换 json 文件为 Protobuf 格式,才能让 v2ray 读取。也就是新版只要一个 v2ray 二进制文件就可以单独运行,不需要 v2ctl 和一些 .dat 等依赖文件,下面就讲讲如何构建 v2ray,然后在服务器上配置运行。顺带也讲讲怎么裁剪 v2ray,以便在路由上运行。

    配置go环境

    可以在本地或者服务器上构建,建议在服务器上,因为众所周知的原因,本地需要配置好代理环境。

    下载go并解压到/usr/local路径下

    wget https://golang.org/dl/go1.15.7.linux-amd64.tar.gz -O - | tar -xz -C /usr/local/

    设置go环境变量,也可以写入到profile中

    vi ~/.profile 添加下面内容:

    export http_proxy=socks5://127.0.0.1:1080  
    export https_proxy=socks5://127.0.0.1:1080  
    #上面两行是配置本地代理,因go get被墙了,服务器上可删除上面两行  
      
    export PATH=$PATH:/usr/local/go/bin  
    export PATH=$PATH:$HOME/.cargo/bin  
    export GOROOT=/usr/local/go  
    export GOBIN=$GOROOT/bin  
    export PATH=$PATH:$GOBIN  
    

    保存后, source ~/.profile

    上面go环境就配置好了,检查一下: go version

    构建v2ray

    拉取 v2ray 源代码和依赖:

    git clone https://github.com/v2fly/v2ray-core.git  
    cd v2ray-core && go mod download  
    

    构建 v2ray 和 v2ctl

    #构建v2ray  
    CGO_ENABLED=0 go build -o $HOME/v2ray -trimpath -ldflags "-s -w -buildid=" ./main  
      
    #构建v2ctl,运行时已不需要v2ctl,这一步其实不需要  
    CGO_ENABLED=0 go build -o $HOME/v2ctl -trimpath -ldflags "-s -w -buildid=" -tags confonly ./infra/control/main  
    

    构建 ARM 版的 v2ray 以便路由上使用,添加 GOOS 和 GOARCH 两个环境变量就可以了,当然你也可以添加 GOARM 变量设置运行时 CPU 浮点协处理器的版本。

    查看所有支持的系统与架构:

    go tool dist list

    比如构建 Linux 版 arm64 的 v2ray,命令如下:

    CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $HOME/v2ray -trimpath -ldflags "-s -w -buildid=" ./main

    上面构建完成后的二进制文件在 $HOME 目录。

    配置运行v2ray

    移动文件到运行目录:

    mv v2ray /usr/bin/

    新建 v2ray 文件夹:

    mkdir /etc/v2ray

    配置 v2ray 配置文件:

    vi /etc/v2ray/config.json

    添加下面内容,这是最基本 tcp 配置

    {  
     "inbounds": [  
     {  
     "port": 10240,  
     "protocol": "vmess",  
     "settings": {  
     "clients": [  
     {  
     "id": "241b8c56-1707-4b62-958c-ae8b4c0d90af"  
     }  
     ]  
     }  
     }  
     ],  
     "outbounds": [  
     {  
     "protocol": "freedom",  
     "settings": {}  
     }  
     ]  
    }  
    

    上面配置好后,你可以直接下面命令前台运行:

    v2ray -config /etc/v2ray/config.json

    配置后台 system 服务如下:

    vi /etc/systemd/system/v2ray.service

    添加下面内容:

    [Unit]  
    Description=V2Ray Service  
    Documentation=https://www.v2fly.org/  
    After=network.target nss-lookup.target  
      
    [Service]  
    User=nobody  
    CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE  
    AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE  
    NoNewPrivileges=true  
    ExecStart=/usr/bin/v2ray -config /etc/v2ray/config.json  
    Restart=on-failure  
    RestartPreventExitStatus=23  
      
    [Install]  
    WantedBy=multi-user.target  
    

    重载system服务: systemctl daemon-reload

    运行v2ray: systemctl start v2ray

    开机运行v2ray: systemctl enable v2ray

    裁剪v2ray

    因为 v2ray 是模块化代理软件,因此每一个功能都可以简单地添加或移除。比如有些用户想在路由器里运行 v2ray,因路由器的 ROM 通常较小,且不会用到 v2ray 所有功能,所以俺们可以拿掉 v2ray 里一些不必要的功能,减小其体积后,塞进路由器。

    v2ray 中所有的模块列表在下面文件里:

    vi v2ray-core/main/distro/all/all.go

    全部列表如下:

    package all  
      
    import (  
    	// The following are necessary as they register handlers in their init functions.  
      
    	// Required features. Can't remove unless there is replacements.  
    	_ "v2ray.com/core/app/dispatcher"  
    	_ "v2ray.com/core/app/proxyman/inbound"  
    	_ "v2ray.com/core/app/proxyman/outbound"  
      
    	// Default commander and all its services. This is an optional feature.  
    	_ "v2ray.com/core/app/commander"  
    	_ "v2ray.com/core/app/log/command"  
    	_ "v2ray.com/core/app/proxyman/command"  
    	_ "v2ray.com/core/app/stats/command"  
      
    	// Other optional features.  
    	_ "v2ray.com/core/app/dns"  
    	_ "v2ray.com/core/app/log"  
    	_ "v2ray.com/core/app/policy"  
    	_ "v2ray.com/core/app/reverse"  
    	_ "v2ray.com/core/app/router"  
    	_ "v2ray.com/core/app/stats"  
      
    	// Inbound and outbound proxies.  
    	_ "v2ray.com/core/proxy/blackhole"  
    	_ "v2ray.com/core/proxy/dns"  
    	_ "v2ray.com/core/proxy/dokodemo"  
    	_ "v2ray.com/core/proxy/freedom"  
    	_ "v2ray.com/core/proxy/http"  
    	_ "v2ray.com/core/proxy/mtproto"  
    	_ "v2ray.com/core/proxy/shadowsocks"  
    	_ "v2ray.com/core/proxy/socks"  
    	_ "v2ray.com/core/proxy/trojan"  
    	_ "v2ray.com/core/proxy/vless/inbound"  
    	_ "v2ray.com/core/proxy/vless/outbound"  
    	_ "v2ray.com/core/proxy/vmess/inbound"  
    	_ "v2ray.com/core/proxy/vmess/outbound"  
      
    	// Transports  
    	_ "v2ray.com/core/transport/internet/domainsocket"  
    	_ "v2ray.com/core/transport/internet/http"  
    	_ "v2ray.com/core/transport/internet/kcp"  
    	_ "v2ray.com/core/transport/internet/quic"  
    	_ "v2ray.com/core/transport/internet/tcp"  
    	_ "v2ray.com/core/transport/internet/tls"  
    	_ "v2ray.com/core/transport/internet/udp"  
    	_ "v2ray.com/core/transport/internet/websocket"  
      
    	// Transport headers  
    	_ "v2ray.com/core/transport/internet/headers/http"  
    	_ "v2ray.com/core/transport/internet/headers/noop"  
    	_ "v2ray.com/core/transport/internet/headers/srtp"  
    	_ "v2ray.com/core/transport/internet/headers/tls"  
    	_ "v2ray.com/core/transport/internet/headers/utp"  
    	_ "v2ray.com/core/transport/internet/headers/wechat"  
    	_ "v2ray.com/core/transport/internet/headers/wireguard"  
      
    	// JSON config support. Choose only one from the two below.  
    	// The following line loads JSON from v2ctl  
    	// _ "v2ray.com/core/main/json"  
    	// The following line loads JSON internally  
    	_ "v2ray.com/core/main/jsonem"  
      
    	// Load config from file or http(s)  
    	_ "v2ray.com/core/main/confloader/external"  
    )  
    

    具体每个模块这里就不细说了,从注释可以大致猜出是做什么的。根据使用场景,俺们可以注释掉下面模块:

    • commander 模块
    • Transport headers 模块
    • shadowsocks,trojan,mtproto 等等代理模块
    • 只留下 tls,tcp,udp 三个传输模块

    精简后如下,注意,JSON 配置支持模块,一定要注释掉,要不体积会不变。但是注释了 _ "v2ray.com/core/main/jsonem" 这一行后,v2ray 就不能直接读取 config.json 配置了,需要转换为 Protobuf 格式才能读取。这个后面,再详细说明。

    package all  
      
    import (  
     // The following are necessary as they register handlers in their init functions.  
      
     // Required features. Can't remove unless there is replacements.  
     _ "v2ray.com/core/app/dispatcher"  
     _ "v2ray.com/core/app/proxyman/inbound"  
     _ "v2ray.com/core/app/proxyman/outbound"  
      
     // Default commander and all its services. This is an optional feature.  
     //_ "v2ray.com/core/app/commander"  
     //_ "v2ray.com/core/app/log/command"  
     //_ "v2ray.com/core/app/proxyman/command"  
     //_ "v2ray.com/core/app/stats/command"  
      
     // Other optional features.  
     //_ "v2ray.com/core/app/dns"  
     _ "v2ray.com/core/app/log"  
     _ "v2ray.com/core/app/policy"  
     //_ "v2ray.com/core/app/reverse"  
     _ "v2ray.com/core/app/router"  
     //_ "v2ray.com/core/app/stats"  
      
     // Inbound and outbound proxies.  
     //_ "v2ray.com/core/proxy/blackhole"  
     //_ "v2ray.com/core/proxy/dns"  
     _ "v2ray.com/core/proxy/dokodemo"  
     _ "v2ray.com/core/proxy/freedom"  
     //_ "v2ray.com/core/proxy/http"  
     //_ "v2ray.com/core/proxy/mtproto"  
     //_ "v2ray.com/core/proxy/shadowsocks"  
     //_ "v2ray.com/core/proxy/socks"  
     //_ "v2ray.com/core/proxy/trojan"  
     _ "v2ray.com/core/proxy/vless/inbound"  
     _ "v2ray.com/core/proxy/vless/outbound"  
     _ "v2ray.com/core/proxy/vmess/inbound"  
     _ "v2ray.com/core/proxy/vmess/outbound"  
      
     // Transports  
     //_ "v2ray.com/core/transport/internet/domainsocket"  
     //_ "v2ray.com/core/transport/internet/http"  
     //_ "v2ray.com/core/transport/internet/kcp"  
     //_ "v2ray.com/core/transport/internet/quic"  
     _ "v2ray.com/core/transport/internet/tcp"  
     _ "v2ray.com/core/transport/internet/tls"  
     _ "v2ray.com/core/transport/internet/udp"  
     //_ "v2ray.com/core/transport/internet/websocket"  
      
     // Transport headers  
     //_ "v2ray.com/core/transport/internet/headers/http"  
     //_ "v2ray.com/core/transport/internet/headers/noop"  
     //_ "v2ray.com/core/transport/internet/headers/srtp"  
     //_ "v2ray.com/core/transport/internet/headers/tls"  
     //_ "v2ray.com/core/transport/internet/headers/utp"  
     //_ "v2ray.com/core/transport/internet/headers/wechat"  
     //_ "v2ray.com/core/transport/internet/headers/wireguard"  
      
     // JSON config support. Choose only one from the two below.  
     // The following line loads JSON from v2ctl  
     // _ "v2ray.com/core/main/json"  
     // The following line loads JSON internally  
     //_ "v2ray.com/core/main/jsonem"  
      
     // Load config from file or http(s)  
     _ "v2ray.com/core/main/confloader/external"  
    )  
    

    修改完后,再重新编译一下,体积应该会减半。上面说到,裁剪后的 v2ray 已经不能直接读取 config.json 文件了,需要转换格式以供 v2ray 读取。

    转化配置文件

    1、首先准备好你的配置文件 config.json

    2、编译或下载 v2ctl 文件,编译方法上面有写

    3、在 PC 客户端或者服务器上运行 v2ctl 转换配置文件为 Protobuf 格式,命令如下:

    ./v2ctl config /etc/v2ray/config.json > config.pb

    上面转化的 config.pb 文件,是不依赖 v2ctl 以及其它数据文件的,只需要 v2ray 一个文件即可运行。当然,这只是针对这个裁剪后的 v2ray ,不裁剪的 v2ray 是可以直接读取的,不需要转换。

    把上面的 config.pb 文件上传到服务端 /etc/v2ray/ 目录下,然后下面命令运行即可:

    v2ray -config=/etc/v2ray/config.pb