V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dndx
V2EX  ›  Google

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

  •  5
     
  •   dndx · 2021-09-08 11:19:11 +08:00 · 22502 次点击
    这是一个创建于 932 天前的主题,其中的信息可能已经有所发展或是发生改变。

    注:本文只是提供一种思路,不是 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 频繁被定位成中国的不便。用了一段时间后稳定性和速度都很满意。

    第 1 条附言  ·  2021-11-08 20:17:53 +08:00
    到现在为止效果坚挺,Warp 的 IP 的确对 Google 自动定位到中国免疫(猜测可能 Google 那边有特殊处理)。
    24 条回复    2022-02-14 17:34:01 +08:00
    guanyin9cn
        1
    guanyin9cn  
       2021-09-08 15:41:14 +08:00
    what is Cloudflare Warp? It like TLS wrap?
    benjix
        2
    benjix  
       2021-09-08 16:17:02 +08:00
    试了下,成功!
    allenforrest
        3
    allenforrest  
       2021-09-08 16:20:34 +08:00
    @guanyin9cn based on WireGuard
    nbweb
        4
    nbweb  
       2021-09-08 17:31:11 +08:00
    我这里 google.com.hk 定位 us,google.com 定位本地。
    gstqc
        5
    gstqc  
       2021-09-08 21:43:33 +08:00 via Android
    发现 ip r 没有额外的路由记录
    birdc
    show route 是有路由表的
    bird.conf 除了 include route-google.conf 外需要修改其它配置吗
    guanyin9cn
        6
    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
        7
    guanyin9cn  
       2021-09-08 22:09:37 +08:00   ❤️ 1
    @gstqc #5 我也发现了,centos,bird not work,不知道什么原因,直接用 ip route add 添加,虽然粗鲁了一点,但是好用。
    guanyin9cn
        8
    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
        9
    dndx  
    OP
       2021-09-08 23:14:33 +08:00   ❤️ 1
    @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
        10
    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
        11
    1423  
       2021-09-08 23:33:25 +08:00
    在 vps 部署 clash 之类的,把谷歌的流量转发到那些一次付费终生使用的代理服务商,似乎更简单靠谱一点

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

    Warp 基本上是不限速的,上 Gbps 不好说,几百 Mbps 还是很轻松,再加上 Cloudflare 本来跟 Google 就有 peering,基本上延迟跟直连比起来就多 1 、2ms,基本上可以忽略不计。
    guanyin9cn
        13
    guanyin9cn  
       2021-09-08 23:45:17 +08:00   ❤️ 6
    @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
        14
    gstqc  
       2021-09-09 00:00:49 +08:00 via Android
    @dndx 知道原因了
    export all;这行默认是注释掉的,去掉注释就行了
    Ayahuasec
        15
    Ayahuasec  
       2021-09-09 00:28:48 +08:00 via Android
    之前在新加坡服务器上测试过,cf 有的时候会 reroute 到其他地方,比如日本、马来西亚、印度尼西亚之类,这个时候各种服务就开始疯狂地报异常登录
    i8i
        16
    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
        17
    xctcc  
       2021-09-09 07:01:16 +08:00 via iPhone
    warp 还可以解锁流媒体
    n1dragon
        18
    n1dragon  
       2021-09-09 07:42:20 +08:00
    可以利用 v2ray 出站协议的 sockopt mark 和 wireguard 的 table 来实现按策略走 WARP,这样可以不限于 IP rule 。
    guanyin9cn
        19
    guanyin9cn  
       2021-09-09 08:27:18 +08:00
    @i8i 貌似更简单些
    mytsing520
        20
    mytsing520  
       2021-09-10 13:09:58 +08:00
    engage.cloudflareclient.com 的 IP,国内已空路由
    bao3
        21
    bao3  
       2021-10-07 22:18:58 +08:00
    @gstqc bird.conf "export all" 去掉注释
    noyle
        22
    noyle  
       2021-11-08 16:40:11 +08:00
    抱歉我还没有试过,但有个问题。Cloudflare 说 Warp 会把用户 IP 发给目标站点的,并不会隐藏用户 IP ,只是重新路由并了加密了用户流量,不是传统的 VPN 。这样的话,这种方法真的会有效吗?
    dndx
        23
    dndx  
    OP
       2021-11-08 20:15:27 +08:00
    @noyle CF 只把真实 IP 发给使用 CF CDN 的站点,对于 Google 来说因为 Google 没有用 CF 的 CDN 这个信息他们就算想要也无从得知。
    titanium98118
        24
    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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3914 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 10:28 · PVG 18:28 · LAX 03:28 · JFK 06:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.