-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-blacklist.sh
executable file
·66 lines (48 loc) · 1.48 KB
/
get-blacklist.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
61
62
63
64
65
#!/bin/sh
config="config.txt"
cache_dir="cache"
local_hosts="hosts.txt"
remote_hosts="http://winhelp2002.mvps.org/hosts.txt"
blacklist="blacklist.hosts"
cd "$(dirname "$0")" || exit 1
add_hosts=""
remove_hosts=""
if [ -f "$config" ]; then
while IFS="= " read key value; do
case "$key" in
blacklist_add_host)
add_hosts="$add_hosts $value";;
blacklist_remove_host)
remove_hosts="$remove_hosts $value";;
blacklist_upload_dest)
upload_dest="$value";;
esac
done < "$config"
fi
if [ ! -d "$cache_dir" ]; then
mkdir "$cache_dir"
fi
logger -s "Downloading $remote_hosts..."
if ! curl -sSo "$cache_dir/$local_hosts" -z "$cache_dir/$local_hosts" "$remote_hosts"; then
logger -s "Error: could not download $remote_hosts"
exit 1
fi
logger -s "Done"
logger -s "Building $blacklist..."
sed "s/\r//" "$cache_dir/$local_hosts" > "$blacklist"
for add_host in $add_hosts; do
echo "0.0.0.0 $add_host" >> "$blacklist"
done
for remove_host in $remove_hosts; do
sed -i "s/^0.0.0.0 $remove_host/#0.0.0.0 $remove_host/" "$blacklist"
done
logger -s "Done"
if [ -n "$upload_dest" ]; then
if ! echo "$upload_dest" | grep -Eq ".+@.+:.+"; then
logger -s "Error: upload destination is not valid"
exit 1
fi
logger -s "Uploading to $upload_dest..."
cat "$blacklist" | ssh "${upload_dest%:*}" "cat > ${upload_dest#*:}; /etc/init.d/dnsmasq restart"
logger -s "Done"
fi