Created
May 3, 2018 19:47
-
-
Save handsomematt/7bdd276ae1e8f429acf78b0fa5460174 to your computer and use it in GitHub Desktop.
Creates or updates a firewall on DigitalOcean based on CloudFlare's current list of IPs.
This file contains 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 | |
FIREWALL_NAME="cloudflare" | |
# for new use: POST /v2/firewalls | |
# for update use: PUT /v2/firewalls/$FIREWALL_ID | |
API_URL="https://api.digitalocean.com/v2/firewalls" | |
API_METHOD="POST" | |
API_TOKEN="" | |
ips=`curl https://www.cloudflare.com/ips-v4` | |
print_cfips() { | |
local passedFirst=0 | |
for ip in $ips | |
do | |
if [ "$passedFirst" == "0" ] | |
then | |
passedFirst=1 | |
else | |
json+=", " | |
fi | |
json+="\"$ip\"" | |
done | |
} | |
# print a member of inbound_rules | |
print_inbound() { | |
json+="{\"protocol\": \"tcp\", \"ports\": $1, \"sources\": { \"addresses\": [" | |
print_cfips | |
json+="]}}" | |
} | |
create_firewall() { | |
json="{\"name\": \"$FIREWALL_NAME\", \"inbound_rules\": [" | |
print_inbound 80 | |
json+=", " | |
print_inbound 443 | |
json+="], \"outbound_rules\": [], \"droplet_ids\": null, \"tags\": null}" | |
} | |
create_firewall | |
curl -X POST "https://api.digitalocean.com/v2/firewalls" \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $API_TOKEN" \ | |
--data "$json" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment