diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-01-11 21:34:22 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-01-11 21:34:22 +0300 |
commit | 284d151d65e0ea43e5a41ab0d409f6549cde464b (patch) | |
tree | 1d5ff43b4e768ab1ba6fd27bd1b5764641960baa | |
parent | ce7eb4316d90bbc727b956fd7732ace558654305 (diff) |
add ipset script
-rwxr-xr-x | cf-ipset.sh | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/cf-ipset.sh b/cf-ipset.sh new file mode 100755 index 0000000..29bfa66 --- /dev/null +++ b/cf-ipset.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +TEMP=$(mktemp) +IPSET_NAME="$1" + +ipset_exists() { + ipset -L "$1" >/dev/null 2>/dev/null +} + +die() { + echo "error: $@" + [ -f "$TEMP" ] && rm "$TEMP" + exit 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 |