Skip to content

Instantly share code, notes, and snippets.

@flexchar
Last active June 17, 2023 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flexchar/cfdc73a7420cc442f6be064e4a828df5 to your computer and use it in GitHub Desktop.
Save flexchar/cfdc73a7420cc442f6be064e4a828df5 to your computer and use it in GitHub Desktop.
Allow Cloudflare's public IPv4 on Google VPC Firewall
#!/bin/bash
echo "Fetching cloudflare IPs"
IPS4=$(curl -s https://www.cloudflare.com/ips-v4)
# IPS6=$(curl -s https://www.cloudflare.com/ips-v6) # IPv6 is not supported by Google yet
IPS6=""
LIST=$(paste -sd',' <<<"$IPS4 $IPS6")
if [[ -n $LIST ]]; then
echo "Got it!"
echo $LIST
else
echo "Failed"
exit 1
fi
command="gcloud compute --project=[project name here] firewall-rules"
result=$($command list --filter cloudflare --format=text)
exists=$(echo $result | grep -n cloudflare)
# Check if exists network rules. then update
if [[ -n $exists ]]; then
echo "Rules found..."
# echo $result
$($command update allow-cloudflare-ipv4 --source-ranges=$LIST)
else # Otherwise create new
echo "No such rule found..."
$($command create allow-cloudflare-ipv4 --allow=tcp:443 --source-ranges=$LIST)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment