Last active
May 25, 2024 03:11
-
-
Save parsapoorsh/a72b82bcf9575fd700aaf2a3bc00b0a0 to your computer and use it in GitHub Desktop.
a bash script to create ufw rules for Cloudflare on port 80 and 433
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "$1" == "insert" ]; then | |
for cfip in `curl -sw '\n' https://www.cloudflare.com/ips-v{4,6}`; | |
do | |
ufw allow proto tcp from $cfip to any port 80,443 comment 'Cloudflare'; | |
done | |
echo $0 'Inserted' | |
elif [ "$1" == "delete" ]; then | |
while ufw status numbered | grep -q 'Cloudflare'; | |
do | |
rule_num=$(ufw status numbered | grep 'Cloudflare' | awk -F'[][]' '{ print $2 }' | head -n 1); | |
yes | ufw delete $rule_num; | |
done | |
echo $0 'Deleted' | |
elif [ "$1" == "reload" ]; then | |
$0 delete | |
$0 insert | |
ufw reload | |
echo $0 'Deleted & Inserted' | |
else | |
echo "Error: Invalid command. Please use insert, delete or reload" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
FAQ
Q: How fix this error:
-bash: ./ufw-cloudflare.sh: Permission denied
?A: You need to give permission to file
Q: How can i give permission to file?