用 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 频繁被定位成中国的不便。用了一段时间后稳定性和速度都很满意。

25307 次点击
所在节点    Google
24 条回复
bao3
2021-10-07 22:18:58 +08:00
@gstqc bird.conf "export all" 去掉注释
noyle
2021-11-08 16:40:11 +08:00
抱歉我还没有试过,但有个问题。Cloudflare 说 Warp 会把用户 IP 发给目标站点的,并不会隐藏用户 IP ,只是重新路由并了加密了用户流量,不是传统的 VPN 。这样的话,这种方法真的会有效吗?
dndx
2021-11-08 20:15:27 +08:00
@noyle CF 只把真实 IP 发给使用 CF CDN 的站点,对于 Google 来说因为 Google 没有用 CF 的 CDN 这个信息他们就算想要也无从得知。
titanium98118
2022-02-14 17:34:01 +08:00
参照楼主的方法成功把 google 的 ip 路由到 warp 上了,现在想把中国的 ip 也放进 warp ,
/etc/bird# cat routes4-cn.conf

route 117.128.0.0/10 via "wgcf0";
route 118.0.0.0/11 via "wgcf0";
route 118.64.0.0/10 via "wgcf0";
route 118.128.0.0/9 via "wgcf0";
route 119.0.0.0/9 via "wgcf0";
route 119.128.0.0/10 via "wgcf0";
route 119.224.0.0/11 via "wgcf0";
route 120.0.0.0/10 via "wgcf0";
.......以下省略

然后 reload bird 提示 Invalid prefix ,搞不懂这个 conf 哪里出错了?
protocol static google {
include "routes4-google.conf";
}
protocol static cn {
include "routes4-cn.conf";
}

/etc/bird# birdc configure
BIRD 1.6.8 ready.
Reading configuration from /etc/bird/bird.conf
/etc/bird/routes4-cn.conf:4:18 Invalid prefix

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

https://tanronggui.xyz/t/800581

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

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

© 2021 V2EX