(感谢 Koutian Wu 师弟完整的调试部署,并在评论中指出原版文章中的几处技术问题,文中已经修正)

随着 Cursor、Claude Code 等工具在中国的访问受限,传统的 HTTP/SOCKS 代理已经无法满足日常需求。这些工具不仅在服务端进行了地区限制,还可能采用多层次的技术手段来检测用户的真实地理位置(当前仅采用一部分,未来可能升级):

  1. 基础 IP 数据库匹配:传统的 GeoIP 数据库查询
  2. 时区一致性检测:通过 JavaScript 获取客户端时区,与 IP 地理位置进行交叉验证
  3. DNS 解析检查:利用 Geo DNS 解析结果检查真实位置
  4. WebRTC IP 泄露检测:通过 WebRTC 获取用户真实 IP 地址
  5. CloudFlare 源地址获取:通过 CloudFlare 的 HTTP 头信息获取真实源地址

目前大多数 HTTP/SOCKS 代理只能应对基础检测方式,而对于更复杂的多维度检测往往无能为力。三层隧道由于工作在网络层,能够更彻底地隐藏用户的真实网络环境。

除了绕过地理位置限制,三层隧道还适用于以下场景:

  1. 服务器访问控制:避免把公司服务器的 SSH 访问端口暴露在公共互联网上
  2. 开发测试环境:避免把公司的测试服务器、内部 API 等暴露在公共互联网上
  3. 安全网络环境:在不受信任的公共 WiFi 环境下,确保通信安全

虽然 WireGuard、OpenVPN 等方案稳定高效,但需要安装专用客户端,在多设备使用场景下略显繁琐。

IKEv2 作为现代化的 VPN 标准,不仅具备出色的性能和稳定性,更重要的是,它已经原生集成在 macOS、Windows、iOS、Android 等主流操作系统中,无需安装任何第三方客户端。

本文将基于《巧用香港中转,搭建丝滑稳定的中美三层隧道》的架构思路,构建一个 国内 -> 香港 -> 美国 的 IKEv2 隧道三级跳方案。

架构设计

为了绕过复杂的地理位置检测并保证连接的稳定高速,我们设计的不是一条简单的点对点隧道,而是一个 “三级跳” 架构。

我们将搭建如下的数据链路:

你的设备 (IKEv2)国内服务器 (Xray)香港服务器 (Nginx + Xray)美国服务器 (strongSwan)互联网

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
+--------------------------+
| Your Device |
| (Native IKEv2 Client) |
+--------------------------+
|
IKEv2 (UDP 500/4500)
|
v
+--------------------------+
| China Server |
| (bj.01.me) |
|--------------------------|
| Xray: |
| In: dokodemo-door |
| Out: VLESS+WS+TLS |
+--------------------------+
|
VLESS over WS+TLS (TCP 443)
|
v
+--------------------------+
| Hong Kong Server |
| (hk.01.me) |
|--------------------------|
| Nginx: TLS Termination |
| Xray: |
| In: VLESS/WS |
| Out: freedom (UDP) |
+--------------------------+
|
Raw IKEv2 (UDP 500/4500)
|
v
+--------------------------+
| US Server |
| (us-west.01.me) |
|--------------------------|
| strongSwan: |
| IKEv2 VPN Server |
+--------------------------+
|
v
<-- Internet -->

技术选型逻辑

  1. 为什么是三级跳?

    • 解决稳定性问题:直接从国内连接美国服务器,会跨越拥堵且不稳定的国际公共互联网,导致高延迟和频繁丢包。我们将这条不稳定的长途链路拆分成了两条高质量的链路:
      • 国内 -> 香港:利用国内云服务商(如阿里云、腾讯云)到香港的优化线路,这条链路通常是专线级别,延迟低、极其稳定。
      • 香港 -> 美国:香港作为亚洲的顶级网络枢纽,拥有充裕的国际出口带宽和到美国西海岸的高质量海底光缆。
    • 规避地理检测:最终出口在美国,可以完美解决 IP 地理位置的限制。
  2. 为什么使用 IKEv2?

    • 免安装客户端,跨平台:这是 IKEv2 最大的优势。macOS, Windows, iOS, Android 系统均原生支持,无需安装任何第三方软件,配置简单,对非技术用户极其友好。
    • 标准化与易管理:使用标准的用户名/密码认证,添加、删除、管理用户非常方便,远比管理密钥文件要简单。
  3. 每一跳的技术细节(为什么这么搭)?

    • 你的设备 -> 国内服务器:你的设备使用系统自带的 IKEv2 客户端,发出标准的 UDP 流量到国内服务器的 5004500 端口。
    • 国内服务器 -> 香港服务器:这是实现 “流量封装” 的关键。
      • Xray (dokodemo-door) 接收到原始的 IKEv2 UDP 包。
      • Xray (VLESS+WS+TLS) 将这些 UDP 包封装进 VLESS 协议,再通过 WebSocket 传输,并最终用 TLS 加密。这样,从国内服务器发出的所有流量,都会封装成访问香港服务器域名的标准 HTTPS 流量(TCP 443 端口)。
    • 香港服务器:这是一个 “中转和解包” 的二传手。
      • Nginx 作为入口,负责处理加密的 HTTPS 流量,进行 TLS 解密,然后将流量反向代理到后端的 Xray 服务。
      • Xray 收到 VLESS over WebSocket 的数据流后,将其解包,还原成最原始的 IKEv2 UDP 包,然后通过 freedom 协议直接转发给美国服务器。
    • 美国服务器:作为隧道的终点,配置极其纯净。
      • strongSwan 只需处理由香港服务器转发来的、干净无加密的 IKEv2 UDP 包,完成最终的 VPN 认证和数据转发,让你的流量从美国 IP 流向互联网。

通过这一系列精巧的设计,我们用一些额外的转发延迟,换来了较高的连接稳定性,同时享受到了免安装客户端的便利。

准备工作

1. 服务器要求

  • 国内服务器(本文以 bj.01.me 为例)
  • 香港服务器(本文以 hk.01.me 为例)
  • 美国服务器(本文以 us-west.01.me 为例)

2. 防火墙配置

美国服务器

  • 允许来自香港服务器 IP 的 UDP 500/4500 端口访问

香港服务器

  • 允许来自国内服务器 IP 的 TCP 443 端口访问

国内服务器

  • 允许来自你的本地网络的 UDP 500/4500 端口访问(如果本地网络 IP 不固定,则需允许所有 UDP 500/4500 端口访问,但这样做存在被 DoS 的风险)

3. 域名

需要为香港和美国服务器准备域名,并设置正确的 DNS 解析,后续我们将使用这些域名申请 TLS 证书:

  • hk.01.me → 香港服务器IP
  • us-west.01.me → 美国服务器IP

4. 生成 UUID

为国内到香港的 VLESS 连接生成 UUID:

1
2
export UUID_BJ_HK=$(uuidgen)
echo "北京 -> 香港 的 VLESS UUID: ${UUID_BJ_HK}"

第一步:配置美国终点服务器

1.1 安装 strongSwan

1
2
3
4
5
6
7
8
9
10
11
12
# 适用于 Debian/Ubuntu
sudo apt update
sudo apt install -y strongswan strongswan-pki libstrongswan-extra-plugins

# 开启 BBR 和 IP 转发
sudo tee -a /etc/sysctl.conf > /dev/null << EOF
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
EOF
sudo sysctl -p

1.2 申请 SSL 证书

为什么需要 SSL 证书?

在 IKEv2 VPN 配置中,我们使用域名 (us-west.01.me) 作为服务器的身份标识 (leftid)。为了防止中间人攻击,客户端在连接时必须验证服务器的身份。SSL 证书就是服务器向客户端出示的“数字身份证”,证明它就是其所声称的 us-west.01.me。客户端会通过其内置的受信任根证书颁发机构(CA)列表来验证该证书的合法性。没有合法有效的证书,客户端将拒绝连接。

1
2
3
4
5
6
7
8
9
10
# 安装 certbot
sudo apt install -y certbot

# 申请证书(确保域名已解析到此服务器)
sudo certbot certonly --standalone -d us-west.01.me --non-interactive --agree-tos -m YOUR_EMAIL@example.com

# 重要:必须复制证书到 strongSwan 目录(这一步很容易被遗漏!)
sudo cp /etc/letsencrypt/live/us-west.01.me/fullchain.pem /etc/ipsec.d/certs/us-west-cert.pem
sudo cp /etc/letsencrypt/live/us-west.01.me/privkey.pem /etc/ipsec.d/private/us-west-key.pem
sudo chmod 600 /etc/ipsec.d/private/us-west-key.pem

注意:请将 YOUR_EMAIL@example.com 替换为你的邮箱,用于接收证书到期提醒。

关于证书续期
Let’s Encrypt 证书的有效期为 90 天,必须定期更新。certbot 包在安装时通常会自动配置一个系统定时任务(cron job 或 systemd timer)来实现自动续期。

你可以通过 sudo certbot renew --dry-run 命令来测试自动续期过程。

重要提示strongSwan 不会在证书更新后自动重新加载它。你需要配置一个 certbot 的续期挂钩(renewal hook)来在证书成功更新后自动重启服务。可以创建一个脚本放在 /etc/letsencrypt/renewal-hooks/deploy/ 目录下,例如 strongswan-reload.sh

1
2
3
4
5
#!/bin/sh
# This script is run by certbot after a successful renewal

# Reload strongswan to apply the new certificate
systemctl reload strongswan

创建文件后,不要忘记给它执行权限:sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/strongswan-reload.sh

1.3 配置 strongSwan

为什么要强制使用 NAT-T 模式 (forceencaps=yes)?

标准的 IKEv2/IPsec VPN 使用 ESP (Encapsulating Security Payload) 协议来传输加密后的数据。ESP 是一个独立的 IP 协议(协议号 50),它不是 TCP 也不是 UDP。我们的三级跳架构中,中间的 Xray 隧道是为转发 TCP 或 UDP 流量而设计的,它无法直接转发 ESP 这种特殊的 IP 协议。

为了解决这个问题,我们利用了 IKEv2 的 NAT 穿透 (NAT Traversal, or NAT-T) 功能。当启用 NAT-T 时,所有的 ESP 数据包都会被封装(Encapsulate)在 UDP 数据包(通常是 4500 端口)内部进行传输。这样一来,整个 VPN 的数据流就都变成了标准的 UDP 流量。

strongSwan 的配置中,forceencaps=yes 这个参数就是用来强制启用 NAT-T 的。无论客户端和服务器之间是否存在 NAT 设备,它都强制所有流量走 UDP 封装,从而完美兼容我们基于 Xray 的 UDP 转发隧道。

UDP 500 和 4500 端口分别是什么作用?

  • UDP 500: 这是 IKE (Internet Key Exchange) 协议的专用端口。它负责 VPN 连接的第一阶段,即建立安全关联。在这个阶段,客户端和服务器会进行身份验证、协商加密算法,并生成用于后续数据传输的密钥。这个过程也被称为 “控制平面”。
  • UDP 4500: IKEv2 使用这个端口传输经过 NAT-T 封装后的 ESP 数据。也就是说,所有实际的用户数据(例如网页浏览、文件下载等)都会通过这个端口进行传输。这个过程可以被看作是 “数据平面”。

关于加密算法兼容性

不同的客户端(Windows、macOS、iOS、Android 以及不同版本)支持的加密算法集合有所不同。为了确保最大的兼容性,我们配置了从强到弱的多种算法组合:

  • 强加密aes256-sha256-ecp256(现代客户端首选)
  • 中等加密aes128-sha256-modp2048(大多数客户端支持)
  • 兼容性加密3des-sha1-modp1024(旧版 Windows 客户端需要)

客户端会自动选择双方都支持的最强算法。

创建 /etc/ipsec.conf

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
sudo tee /etc/ipsec.conf > /dev/null << 'EOF'
config setup
charondebug="all"
uniqueids=yes
strictcrlpolicy=no

conn %default
keyexchange=ikev2
ike=aes256-sha256-ecp256,aes256-sha256-modp2048,aes256-sha1-modp2048,aes128-sha256-modp2048,aes128-sha1-modp1024,3des-sha1-modp1024
esp=aes256-sha256,aes128-sha256,aes128-sha1
dpdaction=clear
dpddelay=300s
rekey=no
forceencaps=yes
fragmentation=yes
mobike=yes

conn ikev2-vpn
auto=add
type=tunnel
left=%any
leftauth=pubkey
leftcert=us-west-cert.pem
leftid=us-west.01.me
leftsubnet=0.0.0.0/0
leftfirewall=yes
rightauth=eap-mschapv2
rightid=%any
rightsourceip=10.42.42.0/24
rightdns=8.8.8.8,8.8.4.4
installpolicy=yes
EOF

创建 /etc/ipsec.secrets

注意密钥类型:certbot 默认生成的是 ECDSA 类型的密钥(椭圆曲线加密),而不是传统的 RSA 密钥。在配置时必须指定正确的密钥类型,否则 strongSwan 无法加载私钥。

1
2
3
4
5
6
7
8
sudo tee /etc/ipsec.secrets > /dev/null << 'EOF'
# SSL 证书私钥(注意:行首的冒号代表本机)
: ECDSA us-west-key.pem

# 用户账号(用户名:密码)
user1 : EAP "password123"
user2 : EAP "password456"
EOF

1.4 配置防火墙和 NAT

1
2
3
4
5
6
7
8
# 配置 iptables NAT 规则(请根据实际网卡名称修改 eth0)
sudo iptables -t nat -A POSTROUTING -s 10.42.42.0/24 -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -s 10.42.42.0/24 -j ACCEPT
sudo iptables -A FORWARD -d 10.42.42.0/24 -j ACCEPT

# 保存规则
sudo apt install -y iptables-persistent
sudo netfilter-persistent save

1.5 启动服务

1
2
sudo systemctl enable --now strongswan
sudo systemctl status strongswan

第二步:配置香港中转服务器

2.1 安装软件

1
2
3
4
5
6
7
8
9
sudo apt update
sudo apt install -y xray nginx certbot python3-certbot-nginx

# 开启 BBR
sudo tee -a /etc/sysctl.conf > /dev/null << EOF
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
EOF
sudo sysctl -p

2.2 申请 SSL 证书

为什么需要 SSL 证书?

在香港服务器上,SSL 证书的用途与美国服务器不同。它不是用于 IKEv2 认证,而是用于 Nginx 服务。我们的设计将从国内服务器发来的 VLESS 隧道流量封装在标准的 TLS (HTTPS) 连接中。

1
2
3
4
# 申请证书
# 我们使用 certonly --standalone 模式,因为我们将手动配置 Nginx。
# --standalone 会临时启动一个 Web 服务器来验证域名,请确保 80 端口未被占用(可以暂时 `sudo systemctl stop nginx`)。
sudo certbot certonly --standalone -d hk.01.me --non-interactive --agree-tos -m YOUR_EMAIL@example.com

注意:请将 YOUR_EMAIL@example.com 替换为你的邮箱,用于接收证书到期提醒。

关于证书续期
certbot 会自动配置续期任务。由于我们使用了 standalone 模式,Nginx 在证书续期后不会自动重载。你需要配置一个 certbot 的续期挂钩(renewal hook)来在证书成功更新后自动重载 Nginx。可以创建一个脚本放在 /etc/letsencrypt/renewal-hooks/deploy/nginx-reload.sh

1
2
3
4
5
#!/bin/sh
# This script is run by certbot after a successful renewal

# Reload Nginx to apply the new certificate
systemctl reload nginx

创建文件后,不要忘记给它执行权限:sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/nginx-reload.sh

2.3 配置 Nginx

创建 /etc/nginx/sites-available/hk.01.me

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
sudo tee /etc/nginx/sites-available/hk.01.me > /dev/null << 'EOF'
server {
listen 80;
server_name hk.01.me;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2;
server_name hk.01.me;

# SSL configuration
ssl_certificate /etc/letsencrypt/live/hk.01.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/hk.01.me/privkey.pem;

# WebSocket proxy for IKEv2 UDP 500
location /ws500 {
proxy_pass http://127.0.0.1:10500;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}

# WebSocket proxy for IKEv2 UDP 4500
location /ws4500 {
proxy_pass http://127.0.0.1:14500;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}

# Default location for other requests
location / {
return 404;
}
}
EOF

# 启用站点
sudo ln -s /etc/nginx/sites-available/hk.01.me /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

2.4 配置 Xray

创建 /usr/local/etc/xray/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
# 使用之前生成的 UUID
sudo tee /usr/local/etc/xray/config.json > /dev/null << EOF
{
"log": { "loglevel": "warning" },
"inbounds": [
{
"tag": "ipsec-in-500",
"port": 10500,
"listen": "127.0.0.1",
"protocol": "vless",
"settings": {
"clients": [
{
"id": "${UUID_BJ_HK}",
"encryption": "none"
}
],
"decryption": "none"
},
"streamSettings": {
"network": "ws",
"wsSettings": {
"path": "/ws500"
}
}
},
{
"tag": "ipsec-in-4500",
"port": 14500,
"listen": "127.0.0.1",
"protocol": "vless",
"settings": {
"clients": [
{
"id": "${UUID_BJ_HK}",
"encryption": "none"
}
],
"decryption": "none"
},
"streamSettings": {
"network": "ws",
"wsSettings": {
"path": "/ws4500"
}
}
}
],
"outbounds": [
{
"tag": "ipsec-out-500",
"protocol": "freedom",
"settings": {
"redirect": "<美国服务器IP>:500"
}
},
{
"tag": "ipsec-out-4500",
"protocol": "freedom",
"settings": {
"redirect": "<美国服务器IP>:4500"
}
}
],
"routing": {
"strategy": "rules",
"rules": [
{
"type": "field",
"inboundTag": ["ipsec-in-500"],
"outboundTag": "ipsec-out-500"
},
{
"type": "field",
"inboundTag": ["ipsec-in-4500"],
"outboundTag": "ipsec-out-4500"
}
]
}
}
EOF

注意:将 <美国服务器IP> 替换为实际的美国服务器 IP 地址。

2.5 启动服务

1
2
sudo systemctl enable --now xray
sudo systemctl status xray

第三步:配置国内入口服务器

3.1 安装软件

1
2
3
4
5
6
7
8
9
sudo apt update
sudo apt install -y xray

# 开启 BBR
sudo tee -a /etc/sysctl.conf > /dev/null << EOF
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
EOF
sudo sysctl -p

3.2 配置 Xray

创建 /usr/local/etc/xray/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
sudo tee /usr/local/etc/xray/config.json > /dev/null << EOF
{
"log": { "loglevel": "warning" },
"inbounds": [
{
"listen": "0.0.0.0",
"port": 500,
"protocol": "dokodemo-door",
"settings": {
"address": "127.0.0.1",
"port": 500,
"network": "udp"
},
"tag": "ipsec-in-500"
},
{
"listen": "0.0.0.0",
"port": 4500,
"protocol": "dokodemo-door",
"settings": {
"address": "127.0.0.1",
"port": 4500,
"network": "udp"
},
"tag": "ipsec-in-4500"
}
],
"outbounds": [
{
"tag": "ipsec-out-500",
"protocol": "vless",
"settings": {
"vnext": [
{
"address": "hk.01.me",
"port": 443,
"users": [
{
"id": "${UUID_BJ_HK}",
"encryption": "none"
}
]
}
]
},
"streamSettings": {
"network": "ws",
"security": "tls",
"wsSettings": {
"path": "/ws500"
}
}
},
{
"tag": "ipsec-out-4500",
"protocol": "vless",
"settings": {
"vnext": [
{
"address": "hk.01.me",
"port": 443,
"users": [
{
"id": "${UUID_BJ_HK}",
"encryption": "none"
}
]
}
]
},
"streamSettings": {
"network": "ws",
"security": "tls",
"wsSettings": {
"path": "/ws4500"
}
}
}
],
"routing": {
"strategy": "rules",
"rules": [
{
"type": "field",
"inboundTag": ["ipsec-in-500"],
"outboundTag": "ipsec-out-500"
},
{
"type": "field",
"inboundTag": ["ipsec-in-4500"],
"outboundTag": "ipsec-out-4500"
}
]
}
}
EOF

3.3 启动服务

1
2
sudo systemctl enable --now xray
sudo systemctl status xray

第四步:客户端配置

4.1 macOS 配置

  1. 打开 系统偏好设置网络
  2. 点击左下角的 + 号,选择 VPN
  3. VPN 类型选择 IKEv2
  4. 填写配置信息:
    • 服务器地址bj.01.me
    • 远程 IDus-west.01.me
    • 本地 ID:留空
    • 认证设置:选择 用户名
    • 用户名user1
    • 密码password123

4.2 Windows 配置

  1. 打开 设置网络和 InternetVPN
  2. 点击 添加 VPN 连接
  3. 填写配置信息:
    • VPN 提供程序:Windows(内置)
    • 连接名称IKEv2 VPN
    • 服务器名称或地址bj.01.me
    • VPN 类型:IKEv2
    • 登录信息的类型:用户名和密码
    • 用户名user1
    • 密码password123

4.3 iOS 配置

  1. 打开 设置通用VPN 与设备管理VPN
  2. 点击 添加 VPN 配置
  3. 选择 IKEv2
  4. 填写配置信息:
    • 描述IKEv2 VPN
    • 服务器bj.01.me
    • 远程 IDus-west.01.me
    • 本地 ID:留空
    • 用户鉴定:选择 用户名
    • 用户名user1
    • 密码password123

4.4 Android 配置

  1. 打开 设置网络和互联网VPN
  2. 点击 + 添加 VPN 配置
  3. 类型选择 IKEv2/IPSec MSCHAPv2
  4. 填写配置信息:
    • 名称IKEv2 VPN
    • 服务器地址bj.01.me
    • IPSec 标识符us-west.01.me
    • 用户名user1
    • 密码password123
    • CA 证书:选择 (未指定)
  5. 点击 保存

    注意:我们使用的是 Let’s Encrypt 证书,其根证书通常已内置于现代 Android 系统中,因此 CA 证书选择“未指定”即可。如果连接失败,可以尝试在手机浏览器中访问 https://us-west.01.me,这通常会触发系统安装所需的中间证书。

4.5 Linux 配置

Linux 系统可以通过多种方式配置 IKEv2 VPN 连接,具体取决于你使用的发行版和桌面环境。

方法一:使用 NetworkManager

适用于:大多数 Linux 桌面

  1. 安装必要软件包

    1
    2
    3
    # Ubuntu/Debian
    sudo apt update
    sudo apt install -y network-manager-strongswan strongswan-charon strongswan-libcharon
  2. 重启 NetworkManager

    1
    sudo systemctl restart NetworkManager
  3. GUI配置

    • 打开 设置网络网络连接
    • 点击 VPN 旁边的 +
    • 选择 IPsec/IKEv2 (strongswan)
    • 填写配置信息:
      • 名称IKEv2 VPN
      • 网关bj.01.me
      • 证书:选择 从服务器请求
      • 方法:选择 EAP
      • 用户名user1
      • 密码password123
      • 虚拟 IP:勾选 请求内网 IP
    • 点击 高级 进行详细配置:
      • 服务器标识us-west.01.me
      • 勾选 强制使用 UDP 封装

方法二:使用 strongSwan 命令行配置

适用于:服务器或无 GUI 环境的 Linux 系统,如树莓派

  1. 安装 strongSwan

    1
    2
    3
    # Ubuntu/Debian
    sudo apt update
    sudo apt install -y strongswan strongswan-pki libstrongswan-extra-plugins
  2. 配置 strongSwan

    创建 /etc/ipsec.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    sudo tee /etc/ipsec.conf > /dev/null << 'EOF'
    config setup
    charondebug="all"
    uniqueids=yes

    conn ikev2-vpn
    auto=start
    keyexchange=ikev2
    type=tunnel
    left=%defaultroute
    leftauth=eap-mschapv2
    leftid=user1
    leftsourceip=%config
    right=bj.01.me
    rightauth=pubkey
    rightid=us-west.01.me
    rightsubnet=0.0.0.0/0
    ike=aes256-sha256-ecp256,aes256-sha256-modp2048
    esp=aes256-sha256
    forceencaps=yes
    dpdaction=restart
    dpddelay=30s
    EOF
  3. 配置认证信息

    创建 /etc/ipsec.secrets

    1
    2
    3
    sudo tee /etc/ipsec.secrets > /dev/null << 'EOF'
    user1 : EAP "password123"
    EOF
  4. 启动连接

    1
    2
    sudo systemctl enable --now strongswan
    sudo ipsec up ikev2-vpn
  5. 验证连接

    1
    sudo ipsec statusall

用户管理

添加新用户

在美国服务器上编辑 /etc/ipsec.secrets

1
2
3
4
sudo nano /etc/ipsec.secrets

# 添加新用户
newuser : EAP "newpassword"

重启 strongSwan 服务:

1
sudo systemctl restart strongswan

删除用户

/etc/ipsec.secrets 中删除对应行,然后重启服务。

验证与测试

连接成功后,验证方法:

1
2
3
4
5
6
7
8
# 检查 IP 地址
curl ifconfig.me

# 检查 DNS 解析
nslookup google.com

# 测试连通性
ping 8.8.8.8

在美国服务器上查看连接状态:

1
sudo ipsec statusall

总结

通过 国内 -> 香港 -> 美国 的 IKEv2 VPN 三级跳架构,我们成功构建了一个免安装客户端、跨平台、易管理的三层隧道解决方案。相比其他三层隧道方案,IKEv2 具有以下优势:

  1. 无需安装客户端:所有主流操作系统都原生支持
  2. 用户管理简单:支持用户名密码认证,易于添加/删除用户
  3. 一账号多设备:一个用户可以同时连接多个设备
  4. 稳定可靠:基于成熟的 IPsec 协议,连接稳定

在实际使用中,这种架构不仅解决了 Cursor 等工具的访问问题,还为团队提供了一个统一的网络访问解决方案。通过合理的用户管理和服务器部署,可以满足不同场景下的网络需求。

Comments