编译并配置运行 v2ray+tcp 代理
配置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