Skip to content

Instantly share code, notes, and snippets.

@sportshead
Last active February 24, 2024 15:03
Show Gist options
  • Save sportshead/98f59af0cd20a54d875d9841ebcd6884 to your computer and use it in GitHub Desktop.
Save sportshead/98f59af0cd20a54d875d9841ebcd6884 to your computer and use it in GitHub Desktop.
Automatically allow Cloudflare IPs in UFW
#!/bin/bash
# simple script to fetch cloudflare IPs and allow them in UFW
# inspired by https://github.com/Paul-Reed/cloudflare-ufw
UFW="/usr/sbin/ufw"
DIR="$HOME/cf-ufw"
rm $DIR/ips.old
mv $DIR/ips $DIR/ips.old
curl -sw '\n' https://www.cloudflare.com/ips-v{4,6}/ > $DIR/ips
# print diff if changed, will be emailed by cron
git diff --no-index --no-renames $DIR/ips.old $DIR/ips
while read ip; do $UFW delete allow proto tcp from "$ip" to any port 80,443 comment 'Cloudflare' > /dev/null; done < $DIR/ips.old
while read ip; do $UFW allow proto tcp from "$ip" to any port 80,443 comment 'Cloudflare' > /dev/null; done < $DIR/ips
$UFW reload > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment