用 Cloudflare Warp 彻底解决 Google IP 定位中国的问题

2021-09-08 11:19:11 +08:00
 dndx

注:本文只是提供一种思路,不是 step by step 的 guide,建议读者对自己使用的 Linux 发行版的常用网络命令有所了解后做为实际配置时的参考使用。

本文讨论一种可以让落地机 Google 流量走 CF Warp VPN,而非 Google 流量不走的思路,可以彻底解决 IP 被定位到中国带来的不便。此方法适用于在海外落地机上自动分流 Google 流量。

原理:通过 Google 提供的 IP 段列表,自动生成指向 Warp 接口的静态路由表,然后用 BIRD 注入到内核中。

建立 Warp VPN

通过 https://github.com/ViRb3/wgcf 创建一个 WireGuard 接口配置,假设名为 wg0

修改 wg0.conf,将自动路由表禁用,因为我们后面要手动添加静态路由。这一步是防止所有流量都走 Warp 的关键:

[Interface]
PrivateKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Address = 172.16.0.2/32
Address = fd01:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx/128
Table = off

[Peer]
PublicKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AllowedIPs = 0.0.0.0/0
AllowedIPs = ::/0
Endpoint = engage.cloudflareclient.com:2408

注意上面添加的 Table = off 选项,此项避免 WireGuard 创建默认路由。

重要:如果客户端使用的也是三层 VPN,需要开启 wg0 接口的 NAT Masquerade 。对于 ss,V2 这种四层代理应该不需要。请自行 Google 如何开启并且确认重启后也会生效。对于使用 nftables 的用户可以参考:

https://wiki.nftables.org/wiki-nftables/index.php/Performing_Network_Address_Translation_(NAT)

安装 BIRD,编辑 /etc/bird/bird.conf,添加:

protocol static google {
        include "routes4-google.conf";
}

同理,编辑 /etc/bird/bird6.conf,添加:

protocol static google {
        include "routes6-google.conf";
}

并且开启 birdbird6 自动启动。

通过以下脚本生成 Google 服务 IPv4 和 IPv6 路由表(如果 WireGuard 接口名字不是 wg0 则相应修改,需要安装 jq):

#!/bin/bash

curl -O https://www.gstatic.com/ipranges/goog.json

jq '.prefixes[].ipv4Prefix | select(.!=null)' < goog.json | sed 's/"//g' | sed 's/^/route /g' | sed 's/$/ via "wg0";/g' > /etc/bird/routes4-google.conf
jq '.prefixes[].ipv6Prefix | select(.!=null)' < goog.json | sed 's/"//g' | sed 's/^/route /g' | sed 's/$/ via "wg0";/g' > /etc/bird/routes6-google.conf

rm goog.json

sudo /usr/sbin/birdc configure
sudo /usr/sbin/birdc6 configure

sudo 执行以上脚本,之后可以用 ip rip -6 r 确认 BIRD 成功的将 Google 的 IP 路由插入 Kernel 。

可选:创建一个 crontab,每周自动执行一次上面的脚本,更新 Google IP 段和 reload BIRD:

$ sudo crontab -l

@weekly /path/to/update_google.sh

效果

Google IP 走 Warp:

$ traceroute www.google.com
traceroute to www.google.com (172.217.175.4), 30 hops max, 60 byte packets
 1  172.16.0.1 (172.16.0.1)  1.291 ms  1.869 ms  1.834 ms
 2  * * *
 3  103.22.201.37 (103.22.201.37)  2.844 ms  2.845 ms  3.012 ms
 4  142.250.160.170 (142.250.160.170)  2.223 ms  2.182 ms  2.147 ms
 5  * * *
 6  108.170.233.82 (108.170.233.82)  2.347 ms 142.250.226.58 (142.250.226.58)  1.833 ms 108.170.242.129 (108.170.242.129)  2.669 ms
 7  108.170.242.112 (108.170.242.112)  2.373 ms 108.170.227.93 (108.170.227.93)  3.287 ms 108.170.242.112 (108.170.242.112)  2.353 ms
 8  209.85.246.213 (209.85.246.213)  2.765 ms 209.85.244.35 (209.85.244.35)  3.104 ms  3.079 ms
 9  209.85.241.106 (209.85.241.106)  3.112 ms 209.85.244.3 (209.85.244.3)  2.505 ms 209.85.244.63 (209.85.244.63)  2.539 ms
10  108.170.242.161 (108.170.242.161)  1.958 ms nrt20s18-in-f4.1e100.net (172.217.175.4)  1.877 ms 108.170.242.193 (108.170.242.193)  2.867 ms

$ traceroute6 www.google.com
traceroute to www.google.com (2404:6800:4004:813::2004), 30 hops max, 80 byte packets
 1  fd01:5ca1:ab1e::1 (fd01:5ca1:ab1e::1)  1.684 ms  1.652 ms  1.713 ms
 2  * * *
 3  2400:cb00:22:2::1 (2400:cb00:22:2::1)  3.674 ms  3.647 ms  12.757 ms
 4  2001:4860:1:1::1a48 (2001:4860:1:1::1a48)  2.194 ms  2.170 ms  2.136 ms
 5  * 2404:6800:802a::1 (2404:6800:802a::1)  1.982 ms 2404:6800:805c::1 (2404:6800:805c::1)  2.058 ms
 6  2001:4860:0:1::195e (2001:4860:0:1::195e)  2.977 ms 2001:4860:0:1::20a8 (2001:4860:0:1::20a8)  2.018 ms 2001:4860:0:1::436 (2001:4860:0:1::436)  2.040 ms
 7  2001:4860:0:1::13e3 (2001:4860:0:1::13e3)  2.444 ms 2001:4860:0:1000::10 (2001:4860:0:1000::10)  2.847 ms *
 8  nrt20s17-in-x04.1e100.net (2404:6800:4004:813::2004)  1.813 ms  1.795 ms  1.912 ms

非 Google IP 不走 Warp 直接离开 VPS:

$ traceroute www.facebook.com
traceroute to www.facebook.com (157.240.7.35), 30 hops max, 60 byte packets
 1  139.162.65.3 (139.162.65.3)  0.851 ms 139.162.65.2 (139.162.65.2)  0.887 ms  0.943 ms
 2  if-0-0-0-0.gw1.shg1.jp.linode.com (139.162.64.18)  0.414 ms if-0-1-5-0.gw2.shg1.jp.linode.com (139.162.64.30)  0.541 ms if-11-0-0-0.gw1.shg1.jp.linode.com (139.162.64.20)  0.513 ms
 3  63.218.251.81 (63.218.251.81)  0.837 ms  0.798 ms  0.807 ms
...

经过测试,基本上完美解决了 IP 频繁被定位成中国的不便。用了一段时间后稳定性和速度都很满意。

23106 次点击
所在节点    Google
24 条回复
guanyin9cn
2021-09-08 15:41:14 +08:00
what is Cloudflare Warp? It like TLS wrap?
benjix
2021-09-08 16:17:02 +08:00
试了下,成功!
allenforrest
2021-09-08 16:20:34 +08:00
@guanyin9cn based on WireGuard
nbweb
2021-09-08 17:31:11 +08:00
我这里 google.com.hk 定位 us,google.com 定位本地。
gstqc
2021-09-08 21:43:33 +08:00
发现 ip r 没有额外的路由记录
birdc
show route 是有路由表的
bird.conf 除了 include route-google.conf 外需要修改其它配置吗
guanyin9cn
2021-09-08 21:59:25 +08:00
@allenforrest #2
on centos, bird maybe not work.

I use ip command to add static route
`ip route add 185.184.223.213 via 172.16.0.2 dev wg1`

and test it is work for me.

```
[root@xx-jp ~]# curl -4 ip.nisekoo.com
8.37.43.11
Asia/Tokyo
AS13335/CLOUDFLARENET
iCloud Private Relay
「 ABUSE IS STRICTLY PROHIBITED 」

```
guanyin9cn
2021-09-08 22:09:37 +08:00
@gstqc #5 我也发现了,centos,bird not work,不知道什么原因,直接用 ip route add 添加,虽然粗鲁了一点,但是好用。
guanyin9cn
2021-09-08 22:14:27 +08:00
# add
jq '.prefixes[].ipv4Prefix | select(.!=null)' < goog.json | sed 's/"//g' | sed 's/^/route /g' | sed 's/$/ via 172.16.0.2 dev wg1;/g' | sed 's/route/ip\ route\ add/g' | bash

# del
jq '.prefixes[].ipv4Prefix | select(.!=null)' < goog.json | sed 's/"//g' | sed 's/^/route /g' | sed 's/$/ via 172.16.0.2 dev wg1;/g' | sed 's/route/ip\ route\ del/g' | bash
dndx
2021-09-08 23:14:33 +08:00
@gstqc
@guanyin9cn
我用的 Debian,默认的 `bird.conf` 就行,不知道是不是 CentOS 的默认配置有问题,看一下:

`/etc/bird/bird.conf`,确保里面有:

```
protocol kernel {
scan time 60;
import none;
export all;
}

protocol device {
scan time 60;
}
```

否则不会插入内核转发表中。另外 ps aux | grep bird 确认 BIRD 守护进程跑起来了。


@guanyin9cn 这个方法也可以,就是更新起来还有重启需要重新添加麻烦点。
guanyin9cn
2021-09-08 23:30:23 +08:00
@dndx #9 systemctl status bird 显示 failed. 进程没起来,route -n 查看,表中没有变化。没用过 bird,不知道怎么弄。先不管他了。

我在想,能不能再优化下,googl.json 中 的 ip 。全部 google 请求都走 cloudflare warp,是不是有点粗暴。比如看 yt,会不会影响播放,毕竟是免费的。

google 是通过哪个域名或 ip 来收集用户的 ip 地址信息的?找到这个 ip,写入路由中,让他走 cloudflare warp.
1423
2021-09-08 23:33:25 +08:00
在 vps 部署 clash 之类的,把谷歌的流量转发到那些一次付费终生使用的代理服务商,似乎更简单靠谱一点

依赖 Cloudflare Warp 感觉哪天服务政策改了,到时候又得改
dndx
2021-09-08 23:33:26 +08:00
@guanyin9cn failed 的话应该是配置文件格式有问题,可以 journalctl 看一下错误日志。

Warp 基本上是不限速的,上 Gbps 不好说,几百 Mbps 还是很轻松,再加上 Cloudflare 本来跟 Google 就有 peering,基本上延迟跟直连比起来就多 1 、2ms,基本上可以忽略不计。
guanyin9cn
2021-09-08 23:45:17 +08:00
@dndx 用最笨的方法,一个个试,找到了。
只需要让 172.217.0.0/16 走 Cloudflare warp 。
这个段是用来收集用户 ip 的 。

实测只加下面一行,就可以达到效果。
ip route add 172.217.0.0/16 via 172.16.0.2 dev wg1
gstqc
2021-09-09 00:00:49 +08:00
@dndx 知道原因了
export all;这行默认是注释掉的,去掉注释就行了
Ayahuasec
2021-09-09 00:28:48 +08:00
之前在新加坡服务器上测试过,cf 有的时候会 reroute 到其他地方,比如日本、马来西亚、印度尼西亚之类,这个时候各种服务就开始疯狂地报异常登录
i8i
2021-09-09 02:13:29 +08:00
我没有用 ipv6

直接修改 wg0.conf,只把 216.58.192.0/19,142.250.0.0/15 走 warp 。这样也是很简单。

[Interface]
PrivateKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
Address = 172.16.0.2/32
DNS = 8.8.8.8,8.8.4.4
MTU = 1280
[Peer]
PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=
AllowedIPs = 216.58.192.0/19
AllowedIPs = 142.250.0.0/15
Endpoint = 162.159.192.1:2408
xctcc
2021-09-09 07:01:16 +08:00
warp 还可以解锁流媒体
n1dragon
2021-09-09 07:42:20 +08:00
可以利用 v2ray 出站协议的 sockopt mark 和 wireguard 的 table 来实现按策略走 WARP,这样可以不限于 IP rule 。
guanyin9cn
2021-09-09 08:27:18 +08:00
@i8i 貌似更简单些
mytsing520
2021-09-10 13:09:58 +08:00
engage.cloudflareclient.com 的 IP,国内已空路由

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/800581

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX