使用 Cloudflare WARP 作为 V2Ray/Shadowsocks 出站(落地)连接

2023-12-24 21:08:54

前言

OpenAI is not available in your country

ChatGPT 最近很火,但不幸地是 OpenAI 屏蔽了中国地区和常见数据中心 IP 来源。这意味着,如果你在国内,想要使用 ChatGPT,除了需要一张外币信用卡/借记卡,还需要一个可以访问 OpenAI 的代理 IP。WARP 是 Cloudflare 提供的免费 VPN 服务,基于 WireGuard 协议,VPN 对端为启用了 anycast 的 Cloudflare CDN。WARP 的 IP 不在一些服务商的黑名单内,所以可以借助 WARP 来正常访问 ChatGPT 和 OpenAI 相关接口,以及其他流媒体服务(以解锁本地内容的版权限制),比如 Netflix、Hulu、Disney+。

配置

一般来说我们无法在中国境内使用 WARP,但可以使用自有的境外 VPS 将代理流量路由至 WARP。基本思路是:将 WireGuard 的 Cloudflare WARP “转换”为 Socks5 协议,作为 V2Ray/Shadowsocks 的出站连接。

WARP 提供了官方的 Linux Client,内置了 proxy mode,可以将 WARP 转换为 Socks5 代理。但这个 Socks5 代理只能 bind 在 localhost,对于跑在 docker 里的 V2Ray/Shadowsocks 来说,无法直接使用。我们可以使用 socatrinetd 建立一条本地的端口转发,具体过程略。

一些基于旧版内核的系统无法安装 warp 官方 Linux 客户端,例如 Oracle Linux 或者 RHEL。 Github - Mon-ius/Docker-Warp-Socks 是基于 ubuntu:22.04 base image 构建的一个内置 WireGuard 和 warp-cli 的容器,可以直接下载使用,不会弄脏宿主机。

香港地区是无法使用 warp-cli 创建的 WireGuard 配置连接的,必须使用官方的 Linux 客户端。

1
2
3
4
5
6
7
8
9
10
# 注册设备
warp-cli register
# 替换为自己的 license key
warp-cli set-license <LICENSE>
# 切换到 proxy mode
warp-cli set-mode proxy
# 修改 socks5 端口,默认 40000
warp-cli set-proxy-port <PORT>
# 启动
warp-cli connect

Shadowsocks 并不能像 V2Ray 那样 chaining,而 V2Ray 本身支持 Shadowsocks 协议,所以只需将 Shadowsocks 配置为 inbound object 即可,无需单独运行一个实例。

入站部分配置如下:

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
"inbounds": [
// Shadowsocks inbound
{
"protocol": "shadowsocks",
"port": 8388,
"settings": {
"method":"aes-256-gcm",
"password":"password",
"network": "tcp,udp"
}
},
// V2Ray inbound
{
"port": 8389,
"protocol": "vmess",
"settings": {
"clients": []
},
"tag": "in-0",
"streamSettings": {
"network": "tcp",
"security": "auto",
"tcpSettings": {}
}
}
]

V2Ray 默认使用第一条 outbound 作为出站路由,因此无需打 tag 也可正常使用。出站部分配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 "outbounds": [
{
"protocol": "socks",
"settings": {
"servers": [
{
// 建议修改为固定的网卡 IP
"address": "<宿主机 IP>",
"port": 40000
}
]
}
},
{
"tag": "direct",
"protocol": "freedom",
"settings": {}
},
{
"tag": "blocked",
"protocol": "blackhole",
"settings": {}
}
],

至此配置完成,拓扑如下:

1
Client <-- SS/V2Ray --> VPS <-- WARP --> Cloudflare Edge <--> Internet

优化

docker 网络默认是 bridge mode,将所有容器接在 docker0 的 L2 bridge 下,通过宿主机 NAT 访问外部网络。可以将 v2ray 容器运行在 host mode,这样 outbound address 可以直接使用 127.0.0.1,无需配置额外的端口转发。

检测 WARP 是否成功启用

访问任意托管在 Cloudflare CDN 下的网站的 /cdn-cgi/trace 路径,例如 https://1.1.1.1/cdn-cgi/trace,可以获得如下格式信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fl=Cloudflare WebServer Instance
h=WebServer Hostname
ip=IP Address of client
ts=Epoch Time in seconds.millis (Similar to `date +%s.%3N` in bash)
visit_scheme=https or http
uag=User Agent
colo=IATA location identifier
sliver=Whether the request is splitted
http=HTTP Version
loc=Country Code
tls=TLS or SSL Version
sni=Whether SNI encrypted or plaintext
warp=Whether client over Cloudflares Wireguard VPN
gateway=Whether client over Cloudflare Gateway
rbi=Whether client over Cloudflares Remote Browser Isolation
kex=Key exchange method for TLS

如果 WARP 字段数值为 on,则说明 WARP 已经成功启用。如果为 plus,则说明当前使用的是 WARP+ 或者 Zero Trust 团队版账号。