使用V2Ray搭建个人VPN

在服务器上部署自己的端口转发服务,V2ray是一个非常好的选择。 目前V2ray支持多种协议,拥有方便的Android与Windows客户端, Linux上的配置文件简单方便,本篇记录V2ray的使用与配置方式。

使用亚马逊搭建个人服务器可参考: 使用免费的亚马逊云服务(AWS)

V2ray的相关仓可以直接从github上进行获取:

Installation

官方一键安装脚本: https://github.com/v2fly/fhs-install-v2ray

Configs

  • Path: /usr/local/etc/v2ray/config.json
  • Manual: https://v2ray.com
  • Sample: https://www.v2ray.com/en/welcome/start.html

Using as server

在服务端的/usr/local/etc/v2ray/config.json 设置接受端口vmess协议并向外直接转发:

/usr/local/etc/v2ray/config.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"inbounds": [{
// Server port, need to add TCP security policy in AWS security group
"port": 11223,
"protocol": "vmess",
"settings": {
"clients": [{ "id": "81498305-0be0-4923-a270-df4e490a086b" }] // Same as client
}
}],
"outbounds": [{
"protocol": "freedom",
"settings": {}
}]
}

Using as client

在客户端的/usr/local/etc/v2ray/config.json同样文件里设置本地端口转发到服务端的 对应对口上:

/usr/local/etc/v2ray/config.json
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
// Log output to var and set log level to error
"log": {
"access": "/var/log/v2ray/log",
"error": "/var/log/v2ray/log",
"loglevel": "error"
},

// Accept local socks request
"inbounds": [{
"listen": "127.0.0.1",
"port": 11112,
"protocol": "socks",
"tag": "socks-inbound",
"settings": {
"auth": "noauth",
"udp": true,
"userlevel": 8,
"ip": "127.0.0.1"
},
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
}
},

// Accept local http request
{
"listen": "127.0.0.1",
"port": 11111,
"protocol": "http",
"tag": "http-inbound",
"settings": {
"userlevel": 8
}
}],

// Transfer to vps server using vmess
"outbounds": [{
"mux": {
"concurrency": -1,
"enabled": false
},
"protocol": "vmess",
"settings": {
"vnext": [{
"address": "server.ip.address",
"port": 11223, // Same as server
"users": [{
"alterId": 0,
"id": "81498305-0be0-4923-a270-df4e490a086b",
"security": "auto",
"level": 8
}]
}]
},
"streamSettings": {
"network": "tcp",
"security":""
},
"tag": "proxy"
},
{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
},
{
"protocol": "freedom",
"settings": {},
"tag": "direct"
}],

"routing": {
"domainStrategy": "IPIfNonMatch",
"rules":[ ]
},

"dns": {
"hosts": {
"domain:github.io": "pages.github.com",
"domain:wikipedia.org": "www.wikimedia.org",
"domain:shadowsocks.org": "electronicsrealm.com",
"domain:googleapis.cn": "googleapis.com"
},
"servers": [
"1.1.1.1"
]
},

"policy": {
"levels": {
"8": {
"connIdle": 300,
"uplinkOnly": 1,
"handshake": 4,
"downlinkOnly": 1
}
},
"system": {
"statsInboundUplink": false,
"statsInboundDownlink": false,
"statsOutboundUplink": false,
"statsOutboundDownlink": false
}
}
}

Running service

客户端和服务端都启动v2ray:

Directly Run:

1
v2ray --config /usr/local/etc/v2ray/config.json &

Start as systemd service

Linux
1
2
3
# Set v2ray as startup service
sudo systemctl enable v2ray
sudo systemctl start v2ray
MacOS
1
sudo brew services start v2ray

之后在客户端使用对应代理就可以了:

linux/unix
1
2
export http_proxy = "127.0.0.1:11111"
export https_proxy = "127.0.0.1:11111"

在Windows上配置浏览器代理即可

Note