本文记录在 Google Cloud Platform (GCP) 上通过 Cloud Shell 批量配置防火墙出入站拦截规则,并结合 Proton VPN (WireGuard) 对主流 CDN 流量进行分流的实战步骤。通过将 CDN 流量重定向走免费 WireGuard 隧道,可以有效规避 GCP 直接出站访问 CDN 产生的昂贵流量费用。
一、GCP 防火墙配置拒绝 CDN 网段h2
使用 GCP 的 Cloud Shell 执行下方脚本,可以全自动批量创建针对各大 CDN 的 IPv4 / IPv6 出入站阻断(DENY)规则。
脚本中已自动通过 $(gcloud config get-value project) 提取当前项目 ID。如需指定特定项目,可手动修改 PROJECT_ID="your-project-id"。
1. Akamai CDNh3
Akamai 没有官方统一公开的 IP 列表,主要依靠社区通过 ASN 维护(数据参考:cdn-ranges)。
通过 bgpq4 工具向 IRR 路由数据库查询以下 ASN 对应的所有路由网段:
AS12222, AS16625, AS16702, AS17204, AS18680, AS18717, AS20189, AS20940, AS21342, AS21357, AS21399, AS22207, AS22452, AS23454, AS23455, AS23903, AS24319, AS26008, AS30675, AS31107, AS31108, AS31109, AS31110, AS31377, AS33047, AS33905, AS34164, AS34850, AS35204, AS35993, AS35994, AS36183, AS39836, AS43639, AS55409, AS55770, AS63949, AS133103, AS393560配置脚本:
PROJECT_ID=$(gcloud config get-value project)
IPV4=8.31.234.0/24,44.30.204.0/24,45.79.120.0/21,49.231.112.0/24,49.231.116.0/23,60.87.0.0/20,66.246.75.0/24,66.246.76.0/24,66.246.138.0/24,70.47.152.0/21,72.200.254.0/23,103.3.56.0/23,103.5.215.0/24,103.6.180.0/24,103.11.223.0/24,103.12.23.0/24,103.13.37.0/24,103.15.143.0/24,103.16.77.0/24,103.16.197.0/24,103.18.190.0/23,103.74.6.0/23,103.74.12.0/23,103.95.84.0/22,103.104.76.0/22,103.224.140.0/23,103.228.80.0/23,103.231.198.0/23,103.249.58.0/23,103.252.85.0/24,115.69.232.0/22,121.78.191.0/24,126.147.252.0/22,168.143.254.0/23,173.205.69.0/24,173.205.76.0/24,190.94.188.0/24,202.4.185.0/24,202.12.75.0/24,202.43.88.0/23,202.74.45.0/24,202.90.8.0/22,202.90.15.0/24,202.94.80.0/24,202.171.237.0/24,202.226.44.0/22,203.27.6.0/23,203.30.14.0/24,203.31.242.0/24,203.63.146.0/24,203.161.190.0/23,203.217.134.0/23,203.221.109.0/24,203.223.90.0/23,207.192.68.0/22,207.192.72.0/22,209.123.162.0/24,209.123.234.0/24,210.8.28.0/23,210.9.71.0/24,210.9.135.192/26,210.10.243.0/27,210.11.208.0/22,210.11.212.0/23,216.238.147.0/24,218.231.251.0/24,220.227.183.0/24,221.110.130.0/24,221.110.152.0/24,221.110.165.0/24,221.110.182.0/25,221.110.183.0/24,221.110.213.128/25,221.110.252.0/24,221.111.192.0/25,221.111.224.0/26IPV6=2001:57a:102::/47,2001:4546:1003::/48,2400:2000:6::/47,2400:2000:b::/48,2400:2000:c::/48,2400:2000:f::/48,2400:2000:10::/48,2403:c780:7203::/48,2405:9800:b::/48,2600:3c0f:32::/48,2600:3c12:b00::/40,2620:0:b60::/48,2620:104:a003::/48,2620:104:a004::/48,2620:104:a00a::/47,2620:104:a00c::/48,2c0f:fc00:b011::/48
gcloud compute --project="${PROJECT_ID}" firewall-rules create input-akamai-cdn-ipv4 --description='Akamai CDN' --direction=INGRESS --priority=10 --network=default --action=DENY --rules=all --source-ranges=$IPV4gcloud compute --project="${PROJECT_ID}" firewall-rules create input-akamai-cdn-ipv6 --description='Akamai CDN' --direction=INGRESS --priority=10 --network=default --action=DENY --rules=all --source-ranges=$IPV6
gcloud compute --project="${PROJECT_ID}" firewall-rules create output-akamai-cdn-ipv4 --description='Akamai CDN' --direction=EGRESS --priority=10 --network=default --action=DENY --rules=all --destination-ranges=$IPV4gcloud compute --project="${PROJECT_ID}" firewall-rules create output-akamai-cdn-ipv6 --description='Akamai CDN' --direction=EGRESS --priority=10 --network=default --action=DENY --rules=all --destination-ranges=$IPV62. Cloudflare CDNh3
Cloudflare 官方公布的 IP 段来源:https://www.cloudflare.com/ips
配置脚本:
PROJECT_ID=$(gcloud config get-value project)
IPV4=173.245.48.0/20,103.21.244.0/22,103.22.200.0/22,103.31.4.0/22,141.101.64.0/18,108.162.192.0/18,190.93.240.0/20,188.114.96.0/20,197.234.240.0/22,198.41.128.0/17,162.158.0.0/15,104.16.0.0/13,104.24.0.0/14,172.64.0.0/13,131.0.72.0/22IPV6=2400:cb00::/32,2606:4700::/32,2803:f800::/32,2405:b500::/32,2405:8100::/32,2a06:98c0::/29,2c0f:f248::/32
gcloud compute --project="${PROJECT_ID}" firewall-rules create input-cf-cdn-ipv4 --description='cloudflare CDN' --direction=INGRESS --priority=10 --network=default --action=DENY --rules=all --source-ranges=$IPV4gcloud compute --project="${PROJECT_ID}" firewall-rules create input-cf-cdn-ipv6 --description='cloudflare CDN' --direction=INGRESS --priority=10 --network=default --action=DENY --rules=all --source-ranges=$IPV6
gcloud compute --project="${PROJECT_ID}" firewall-rules create output-cf-cdn-ipv4 --description='cloudflare CDN' --direction=EGRESS --priority=10 --network=default --action=DENY --rules=all --destination-ranges=$IPV4gcloud compute --project="${PROJECT_ID}" firewall-rules create output-cf-cdn-ipv6 --description='cloudflare CDN' --direction=EGRESS --priority=10 --network=default --action=DENY --rules=all --destination-ranges=$IPV63. Fastly CDNh3
Fastly 官方 API 接口来源:https://api.fastly.com/public-ip-list
配置脚本:
PROJECT_ID=$(gcloud config get-value project)
IPV4=23.235.32.0/20,43.249.72.0/22,90.98.0.0/17,90.98.128.0/18,103.244.50.0/24,103.245.222.0/23,103.245.224.0/24,104.156.80.0/20,140.248.64.0/18,140.248.128.0/17,146.75.0.0/17,151.101.0.0/16,157.52.64.0/18,167.82.0.0/17,167.82.128.0/20,167.82.160.0/20,167.82.224.0/20,172.111.64.0/18,185.31.16.0/22,199.27.72.0/21,199.232.0.0/16IPV6=2a04:4e40::/32,2a04:4e42::/31,2a04:4e44::/32
gcloud compute --project="${PROJECT_ID}" firewall-rules create input-fastly-cdn-ipv4 --description='fastly CDN' --direction=INGRESS --priority=10 --network=default --action=DENY --rules=all --source-ranges=$IPV4gcloud compute --project="${PROJECT_ID}" firewall-rules create input-fastly-cdn-ipv6 --description='fastly CDN' --direction=INGRESS --priority=10 --network=default --action=DENY --rules=all --source-ranges=$IPV6
gcloud compute --project="${PROJECT_ID}" firewall-rules create output-fastly-cdn-ipv4 --description='fastly CDN' --direction=EGRESS --priority=10 --network=default --action=DENY --rules=all --destination-ranges=$IPV4gcloud compute --project="${PROJECT_ID}" firewall-rules create output-fastly-cdn-ipv6 --description='fastly CDN' --direction=EGRESS --priority=10 --network=default --action=DENY --rules=all --destination-ranges=$IPV6二、通过 Proton Free VPN (WireGuard) 分流出站h2
在配置上述防火墙规则后,GCP 实例直接发往 CDN 的流量将被阻断。接下来在 GCP 实例上部署 WireGuard 并接入免费 Proton VPN,通过精准配置 AllowedIPs,使所有目标为 CDN 的流量走 WireGuard 隧道分流出站。
1. 安装 WireGuard 客户端h3
sudo apt update && sudo apt install -y wireguard2. 获取并配置 Proton VPN WireGuard 配置h3
从 Proton VPN 官网账户后台获取免费节点的 WireGuard 配置文件:https://account.protonvpn.com/downloads
编辑配置文件 /etc/wireguard/wg0.conf:
[Interface]PrivateKey = YOUR_PRIVATE_KEYAddress = 10.2.0.2/32# DNS = 10.2.0.1 # 需注释掉默认 DNS,避免影响主机原有 DNS 解析
[Peer]PublicKey = PROTON_PEER_PUBLIC_KEY# 将上面获取的所有主流 CDN 网段填入 AllowedIPs,实现目标分流AllowedIPs = 8.31.234.0/24, 44.30.204.0/24, 45.79.120.0/21, 49.231.112.0/24, 49.231.116.0/23, 60.87.0.0/20, 66.246.75.0/24, 66.246.76.0/24, 66.246.138.0/24, 70.47.152.0/21, 72.200.254.0/23, 103.3.56.0/23, 103.5.215.0/24, 103.6.180.0/24, 103.11.223.0/24, 103.12.23.0/24, 103.13.37.0/24, 103.15.143.0/24, 103.16.77.0/24, 103.16.197.0/24, 103.18.190.0/23, 103.74.6.0/23, 103.74.12.0/23, 103.95.84.0/22, 103.104.76.0/22, 103.224.140.0/23, 103.228.80.0/23, 103.231.198.0/23, 103.249.58.0/23, 103.252.85.0/24, 115.69.232.0/22, 121.78.191.0/24, 126.147.252.0/22, 168.143.254.0/23, 173.205.69.0/24, 173.205.76.0/24, 190.94.188.0/24, 202.4.185.0/24, 202.12.75.0/24, 202.43.88.0/23, 202.74.45.0/24, 202.90.8.0/22, 202.90.15.0/24, 202.94.80.0/24, 202.171.237.0/24, 202.226.44.0/22, 203.27.6.0/23, 203.30.14.0/24, 203.31.242.0/24, 203.63.146.0/24, 203.161.190.0/23, 203.217.134.0/23, 203.221.109.0/24, 203.223.90.0/23, 207.192.68.0/22, 207.192.72.0/22, 209.123.162.0/24, 209.123.234.0/24, 210.8.28.0/23, 210.9.71.0/24, 210.9.135.192/26, 210.10.243.0/27, 210.11.208.0/22, 210.11.212.0/23, 216.238.147.0/24, 218.231.251.0/24, 220.227.183.0/24, 221.110.130.0/24, 221.110.152.0/24, 221.110.165.0/24, 221.110.182.0/25, 221.110.183.0/24, 221.110.213.128/25, 221.110.252.0/24, 221.111.192.0/25, 221.111.224.0/26, 173.245.48.0/20, 103.21.244.0/22, 103.22.200.0/22, 103.31.4.0/22, 141.101.64.0/18, 108.162.192.0/18, 190.93.240.0/20, 188.114.96.0/20, 197.234.240.0/22, 198.41.128.0/17, 162.158.0.0/15, 104.16.0.0/13, 104.24.0.0/14, 172.64.0.0/13, 131.0.72.0/22, 23.235.32.0/20, 43.249.72.0/22, 90.98.0.0/17, 90.98.128.0/18, 103.244.50.0/24, 103.245.222.0/23, 103.245.224.0/24, 104.156.80.0/20, 140.248.64.0/18, 140.248.128.0/17, 146.75.0.0/17, 151.101.0.0/16, 157.52.64.0/18, 167.82.0.0/17, 167.82.128.0/20, 167.82.160.0/20, 167.82.224.0/20, 172.111.64.0/18, 185.31.16.0/22, 199.27.72.0/21, 199.232.0.0/16Endpoint = PROTON_SERVER_IP:PORTPersistentKeepalive = 253. 启动并验证 WireGuardh3
# 启动 WireGuard 接口sudo wg-quick up wg0
# 设置开机自启sudo systemctl enable wg-quick@wg0启动后检查路由表与连接状态:
sudo wg showip route测试访问 CDN 资源,流量将走 wg0 接口,而其他常规网络流量保持原 GCP 默认路由不变:
