如何让 Warp 只访问 Google?一文教你配置 WireGuard

内容目录

本文将教你如何通过修改配置文件来使用 Warp 只访问 Google,并且介绍了如何获取 Google IP 段并将其加入配置文件中,详细步骤包括修改配置文件、获取 Google IP 段、启动 WireGuard,以及禁用 IPv6 DNS 结果等内容。


1. 为什么只让 Warp 访问 Google?

在日常使用中,我们可能只希望通过 Warp 来访问特定的网站,比如 Google。这可以避免使用 Warp 访问其他网站时可能带来的速度下降或其他问题。那么,如何实现这个目标呢?下面我们一步一步来操作。

2. 修改 WireGuard 配置文件

首先,我们需要修改 WireGuard 的配置文件,让它只允许访问 Google 的 IP 段。以下是需要修改的配置文件部分:

[Interface]
...
PostUp = ip rule add from 主网口IP table main
PostDown = ip rule del from 主网口IP table main
...
[Peer]
...
# 删除以下两行
# AllowedIPs = 0.0.0.0/0
# AllowedIPs = ::/0

注意PostUpPostDown 的设置是为了确保入站 IP 不被 WireGuard 接管,这一步非常重要,一定要加上。

3. 获取 Google IP 段

接下来,我们需要获取 Google 的 IP 段。可以使用以下命令下载 Google 的 IPv4 段:

curl -sL https://raw.githubusercontent.com/phlinhng/warp-google/main/ip/google-v4.txt >> /tmp/goog_ips
#curl -sL https://raw.githubusercontent.com/phlinhng/warp-google/main/ip/google-v6.txt >> /tmp/goog_ips

将获取的 Google IP 段加入配置文件中

使用以下命令将获取到的 Google IP 段加入 WireGuard 的配置文件中:

for googip in `cat /tmp/goog_ips`; do echo "AllowedIPs = $googip" >> /etc/wireguard/wgcf.conf; done

4. 启动 WireGuard

完成上述配置后,我们可以启动 WireGuard:

wg-quick up wgcf

就是这么简单,现在 Warp 只会访问 Google 的 IP 段。

5. (可选) 禁用 IPv6 DNS 结果

为了避免 DNS 有时返回 AAAA 记录造成连接失败,建议禁用系统使用 IPv6 结果:

echo "precedence ::ffff:0:0/96  100" >> /etc/gai.conf

6. 补充:访问其他网站的配置

如果你除了访问 Google 还想访问其他网站,比如 Netflix,可以通过以下几种方法实现:

  1. 添加 Netflix 的 IPv4 段:比照本文方法,将 Netflix 的 IPv4 段加入 AllowedIPs
  2. 添加 Netflix 的 IPv6 段:通过 V2Ray / Xray 的 routing 指定 geosite:netflix 使用 IPv6。
  3. 全局接管 IPv6:将 AllowedIPs = ::/0 加回来,让 WireGuard 接管 Google 的 IPv4 和全球的 IPv6,并通过 V2Ray / Xray 的 routing 指定 geosite:netflix 使用 IPv6。
  4. 全局接管 IPv4 和 IPv6:将 AllowedIPs = ::/0 加回来,让 WireGuard 接管 Google 的 IPv4 和全球的 IPv6,并设置 DNS 优先使用 IPv6 结果。

注意:由于有些网站的 IPv6 部署情形不佳,有时即使拿到 AAAA 也连不上目标,不建议冒然使用全局 IPv6 优先的策略。

7. 其他 IP 段的配置

除了 Google,你还可以将其他服务的 IP 段加入配置文件中。以下是一些常见服务的配置方法:

Cloudflare

curl -sL https://www.cloudflare.com/ips-v4 >> /tmp/cf_ips
#curl -sL https://www.cloudflare.com/ips-v6 >> /tmp/cf_ips
for cfip in `cat /tmp/cf_ips`; do echo "AllowedIPs = $cfip" >> /etc/wireguard/wgcf.conf; done

Telegram

非官方列表,可能存在误差:

curl -sL https://raw.githubusercontent.com/phlinhng/warp-google/main/ip/as62041-v4.txt >> /tmp/tg_ips
#curl -sL https://raw.githubusercontent.com/phlinhng/warp-google/main/ip/as62041-v6.txt >> /tmp/tg_ips
for tgip in `cat /tmp/tg_ips`; do echo "AllowedIPs = $tgip" >> /etc/wireguard/wgcf.conf; done

Netflix

非官方列表,可能存在误差:

curl -sL https://raw.githubusercontent.com/phlinhng/warp-google/main/ip/as55095-v4.txt >> /tmp/nf_ips
#curl -sL https://raw.githubusercontent.com/phlinhng/warp-google/main/ip/as55095-v6.txt >> /tmp/nf_ips
for nfip in `cat /tmp/nf_ips`; do echo "AllowedIPs = $nfip" >> /etc/wireguard/wgcf.conf; done

所有公网 IPv4

由于 Wireguard 不支持排除特定 IP 段,若需要让所有公网连接走 Warp 并排除所有私网连接,可使用此列表:

curl -sL https://raw.githubusercontent.com/phlinhng/warp-google/main/ip/public-v4.txt >> /tmp/public_ips
for pubip in `cat /tmp/public_ips`; do echo "AllowedIPs = $pubip" >> /etc/wireguard/wgcf.conf; done

希望通过这篇文章,你可以顺利地配置 Warp 只访问 Google。如果在操作过程中有任何问题,欢迎留言讨论。