#!/bin/bash

URL="http://www.voipbl.org/update/"

set -e
echo "Downloading rules from VoIP Blacklist"
wget -qO - $URL -O /tmp/voipbl.txt

echo "Loading rules..."

# Create ipset in case it doesn't exist
/usr/sbin/ipset -N badips iphash -exist
  
# Create temporary chain
/usr/sbin/ipset -N badips_temp iphash -exist
/usr/sbin/ipset flush badips_temp
 
cat /tmp/voipbl.txt |\
  awk '{ print "if [ ! -z \""$1"\" -a \""$1"\"  != \"#\" ]; then /usr/sbin/ipset -A badips_temp \""$1"\" ;fi;"}' | sh
 
ipset swap badips_temp badips
ipset destroy badips_temp || true
 
echo "Done! Rules loaded"
