本文方法来源于 用 Cloudflare Warp 彻底解决 Google IP 定位中国的问题
此方法适用于在海外落地机上自动分流 Google 流量。
原理:通过 Google 提供的 IP 段列表,自动生成指向 Warp 接口的静态路由表,然后用 BIRD 注入到内核中。
建立 Warp VPN 通过 https://github.com/ViRb3/wgcf 创建一个 WireGuard 接口配置,假设名为 wgcf。 复制生成的 wgcf.conf 到 /etc/wireguard 目录下 修改 wgcf.conf,将自动路由表禁用,因为我们后面要手动添加静态路由。这一步是防止所有流量都走 Warp 的关键:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [Interface] PrivateKey = xxxxxxxxxx Address = 172.16.0.2/32 Address = fd01:xxxx/128 DNS = 1.1.1.1 MTU = 1280 Table = off [Peer] PublicKey = xxxxxxxxxx AllowedIPs = 0.0.0.0/0 AllowedIPs = ::/0 Endpoint = engage.cloudflareclient.com:2408
安装 Wiregard 安装依赖:
1 apt-get install sudo net-tools openresolv -y
安装主程序,Debian 需要添加 unstable 源,Ubuntu 则只需要添加库即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #Debian 添加 unstable 源 echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable-wireguard.list printf 'Package: *\nPin: release a=unstable\nPin-Priority: 150\n' > /etc/apt/preferences.d/limit-unstable #更新源并安装 apt-get update apt-get install wireguard-dkms wireguard-tools #Ubuntu 添加库 add-apt-repository ppa:wireguard/wireguard #更新源并安装 apt-get update apt-get install wireguard
添加服务并设置 Wiregard 开机自启
1 2 3 4 5 6 #允许配置文件为 wgcf 的开机自启 systemctl enable wg-quick@wgcf.service #重载 deamon 配置 systemctl daemon-reload #启动 wgcf 配置文件的进程 systemctl start wg-quick@wgcf
Bird 配置 安装 BIRD,编辑 /etc/bird/bird.conf,添加:
1 2 3 protocol static google { include "routes4-google.conf"; }
同理,编辑 /etc/bird/bird6.conf,添加:
1 2 3 protocol static google { include "routes6-google.conf"; }
编辑 kernel 项配置开启 export all 选项:
1 2 3 4 5 protocol kernel { scan time 60; import none; export all; # Actually insert routes into the kernel routing table }
并且开启 bird 和 bird6 自动启动。
通过以下脚本生成 Google 服务 IPv4 和 IPv6 路由表(如果 WireGuard 接口名字不是 wgcf 则相应修改,需要安装 jq):
1 2 3 4 5 6 7 8 9 10 11 #!/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 "wgcf";/g' > /etc/bird/routes4-google.conf jq '.prefixes[].ipv6Prefix | select(.!=null)' < goog.json | sed 's/"//g' | sed 's/^/route /g' | sed 's/$/ via "wgcf";/g' > /etc/bird/routes6-google.conf rm goog.json sudo /usr/sbin/birdc configure sudo /usr/sbin/birdc6 configure
创建一个 crontab,每周自动执行一次上面的脚本,更新 Google IP 段和 reload BIRD:
1 0 0 * * 0 /path/to/google.sh