summaryrefslogtreecommitdiff
path: root/cf-ipset.sh
blob: 082f9bb3e90fa094e7b48bbbda5c01f36f24c885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash

ipset_exists() {
	ipset -L "$1" >/dev/null 2>/dev/null
}

die() {
	echo "error: $@"
	exit 1
}

IPSET_NAME="$1"
[ -z "$IPSET_NAME" ] && {
	echo "usage: $0 IPSET_NAME"
	exit
}

if ! ipset_exists "$IPSET_NAME"; then
	echo "warn: set $IPSET_NAME doesn't exists, creating it for you..."
	ipset create $IPSET_NAME hash:net || die "failed to create ipset"
fi

list=$(curl -s "https://www.cloudflare.com/ips-v4")
[ -z "$list" ] && die "failed to fetch cf networks"

ipset flush $IPSET_NAME || die "failed to flush $IPSET_NAME"
for net in $list; do
	ipset add $IPSET_NAME $net || echo "error: failed to add $net to $IPSET_NAME"
done