-
Notifications
You must be signed in to change notification settings - Fork 3
/
install-rules.sh
executable file
·64 lines (46 loc) · 1.29 KB
/
install-rules.sh
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
countries=$( cat countries.txt )
date=$(date --iso-8601)
echo "making backup of iptables..."
mkdir -p backups
iptables-save > backups/$date-iptables.save
# for IPv4 only so far....
for cc in $countries; do
table="${cc}-blocker"
ipv4file="data/$date-$cc-ipv4.txt"
iptables -F $table && echo "flushed table $table"
iptables -N $table && echo " and created a new one"
# ################### IPv4
echo "Installing new netblock rules for country $cc."
echo "==============================================="
echo "(IPv4)"
echo
let i=0
for netblock in $(bzcat $ipv4file.bz2 | sort | uniq | iprange --optimize ); do
iptables -A $table -s $netblock -j DROP
result=$((i++ % 100))
if [ $result -eq 0 ]; then
echo -n "."
fi
done
echo "Done."
iptables -A $table -j RETURN
iptables -I INPUT -j $table
# ################### IPv6
ipv6file="data/$date-$cc-ipv6.txt"
ip6tables -F $table && echo "flushed table $table"
ip6tables -N $table && echo " and created a new one"
echo
echo "(IPv6)"
echo
let i=0
for netblock in $(bzcat $ipv6file.bz2 | sort | uniq ); do
ip6tables -A $table -s $netblock -j DROP
result=$((i++ % 100))
if [ $result -eq 0 ]; then
echo -n "."
fi
done
ip6tables -A $table -j RETURN
ip6tables -I INPUT -j $table
done