V2Ray

从软件上 V2Ray 不区分服务器版和客户端版,也就是说在服务器和客户端运行的 V2Ray 是同一个软件,区别只是配置文件的不同。

部署

应用场景

问题:外网无法访问部署在内网的 MySQL,导致 SpringBoot 项目启动时连不上数据库,接口无法调试,开发效率低。

解决:参考以下方式部署 v2ray,然后配置 JVM 参数 -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=18998

部署方案一

在服务器部署 V2Ray 客户端。配置文件如下(更多配置见官网 v2fly):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"inbounds": [
{
"listen": "0.0.0.0", // 服务器监听IP
"port": 8997, // 服务器监听端口
"protocol": "socks", // 协议名称
"settings": { // 详见每个协议中的InboundConfigurationObject
"auth": "noauth", // "noauth" | "password"。password模式下IDEA代理MySQL连接,提示socks验证失败
"accounts": [ // 支持多个用户账号。此选项仅当auth为password时有效。
{
"user": "zhaolq",
"pass": "123456789"
}
],
"udp": true // 是否开启UDP协议的支持
}
}
],
"outbounds": [
{
"protocol": "freedom"
}
]
}

执行 chmod ./v2ray +x./v2ray run 启动 V2Ray 客户端,可以使用 SwitchyOmega 插件测试 socks 代理是否生效。

注意:

Navicat 不支持 socks 代理,但可以使用 HTTP 隧道。Nginx 配置见 Nginx教程 章节的 常用配置。具体参考 https://blog.csdn.net/baikunlong/article/details/132596536

部署方案二(推荐加密协议)

服务端

在服务器部署 V2Ray 服务端,本地部署客户端。

1
2
cd /opt/local/
unzip v2ray-linux-64.zip -d ./v2ray-linux-64

服务端配置(更多配置见官网 v2fly):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
// 请提前创建log目录。请使用绝对路径。
"log" : {
"access": "/opt/local/v2ray-linux-64/log/access.log",
"error": "/opt/local/v2ray-linux-64/log/error.log",
"loglevel": "warning"
},
"inbounds": [
{
"port": 8998, // 服务器监听端口
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "755bc63b-e26d-47cd-85a1-b6ebbecbbabc"
}
]
}
}
],
"outbounds": [
{
"protocol": "freedom"
}
]
}

Linux 启动前需授予可执行权限 chmod ./v2ray +x,然后参考下文 开机自启

客户端

客户端配置(更多配置见官网 v2fly):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
/*
// 请提前创建log目录。请使用绝对路径。
"log" : {
"access": "X:\Program\v2ray-windows-64\log\access.log",
"error": "X:\Program\v2ray-windows-64\log\error.log",
"loglevel": "warning"
},
// 配置Routing路由。不建议配置,因为这样才能打印访问日志。
"routing": {
"domainStrategy": "IPOnDemand",
"rules": [
{
"type": "field",
"ip": [
"geoip:private"
],
"outboundTag": "direct" // 当一个规则生效时,即将这个连接转发至它所指定的 outboundTag。当没有匹配到任何规则时,流量默认被转发至第一个 outbound。
}
]
},
*/
"inbounds": [
{
"port": 18998, // SOCKS 代理端口,在浏览器中需配置代理并指向这个端口
"listen": "127.0.0.1",
"protocol": "socks",
"settings": {
"udp": true
}
}
],
// 一个数组,每个元素是一个出站连接配置。列表中的第一个元素作为主出站协议。当路由匹配不存在或没有匹配成功时,流量由主出站协议发出。
"outbounds": [
{
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "192.168.0.7", // 服务器地址,请修改为你自己的服务器 ip 或域名
"port": 8998, // 服务器端口
"users": [
{
"id": "755bc63b-e26d-47cd-85a1-b6ebbecbbabc"
}
]
}
]
}
},
{
"protocol": "freedom",
"tag": "direct" // 出站连接的标识,用于在其它的配置中定位此连接。当其值不为空时,必须在所有 tag 中唯一。配置Routing路由时可以指定使用哪个出站连接
}
]
}

windows启动脚本

1
2
3
:: 新建文件 v2ray-run.cmd
v2ray.exe run
pause

开机自启

v2ray-linux-64.zip 解压文件中包含 systemd/system/v2ray.service,需要的选项包括 User、ExecStart 等。

vim /etc/systemd/system/v2ray.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# v2ray.service
[Unit]
Description=V2Ray Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target

[Service]
User=root
Group=root
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/opt/local/v2ray-linux-64/v2ray run -config /opt/local/v2ray-linux-64/config.json
Restart=on-failure
RestartPreventExitStatus=23

[Install]
WantedBy=multi-user.target

重新加载配置文件:systemctl daemon-reload