Proxy on Server¶
在服务器上使用 v2ray 作为代理工具。
首先导出代理配置,v2ray 使用 JSON 格式的配置文件,Clash 使用 YAML 格式的配置文件。
订阅链接导出为 JSON 格式,需要用到 v2rayN 客户端(Windows),官方仓库为 https://github.com/2dust/v2rayN。在机场网站拿到订阅链接后,先导入到 v2rayN 客户端,然后选择某个节点导出为 JSON 格式,复制到远程服务器上。
v2ray 配置中,inbounds 可能存在 "tag": "socks" 的配置,这是 v2ray 无法识别的,只需修改为 "tag": "socks" 。之后,如果 inbounds 中只有 socks5,可以考虑添加一个 http 代理端口,方便一些不支持 socks5 的软件使用。在 JSON 文件中 "inbounds": [ ... ] 里,添加第二个 inbound:
修改后的 inbounds 示例:
JSON
"inbounds": [
{
"tag": "socks",
"port": 10810,
"listen": "127.0.0.1",
"protocol": "socks",
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls"
],
"routeOnly": false
},
"settings": {
"auth": "noauth",
"udp": true,
"allowTransparent": false
}
},
{
"tag": "http",
"port": 10811,
"listen": "127.0.0.1",
"protocol": "http",
"settings": {}
}
],
在服务器上,为当前用户创建一个 v2ray 进程,且让 v2ray 持续在后台运行、不随 SSH 断开而退出:
Bash
mkdir -p ~/v2ray
mv /path/to/exported/config.json ~/v2ray/config.json
nohup /usr/local/bin/v2ray run -config ~/v2ray/config.json > ~/v2ray/log.txt 2>&1 &
创建自己的 systemd 服务,使 v2ray 开机自动启动,无需每次都在一个新的终端会话中手动开启:
写入如下内容:
INI
[Unit]
Description=User V2Ray Service
After=network-online.target
[Service]
ExecStart=/usr/local/bin/v2ray run -config /home/derrick/v2ray/config.json
Restart=always
RestartSec=5
[Install]
WantedBy=default.target
保存后依次运行:
Bash
systemctl --user daemon-reload
systemctl --user enable v2ray
systemctl --user start v2ray
systemctl --user status v2ray
loginctl enable-linger <user_name>
最后可以在 ~/.bashrc 或 ~/.zshrc 中添加代理环境变量:
Bash
export ALL_PROXY=socks5h://127.0.0.1:10810
export http_proxy=http://127.0.0.1:10811
export https_proxy=http://127.0.0.1:10811
proxy_on() {
export ALL_PROXY=socks5h://127.0.0.1:10810
export http_proxy=http://127.0.0.1:10811
export https_proxy=http://127.0.0.1:10811
echo "Proxy ON → 127.0.0.1:10810"
}
proxy_off() {
unset ALL_PROXY http_proxy https_proxy
echo "Proxy OFF"
}
然后就可以通过 proxy_on 和 proxy_off 来便捷地开启和关闭代理。