/
adblock-oisd
103 lines (72 loc) · 2.27 KB
/
adblock-oisd
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash /etc/rc.common
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
export HOME=/root
exec &> /var/log/adblock-oisd.log
START=99
STOP=4
check_dnsmasq()
{
check_return=0;
pgrep -x dnsmasq &> /dev/null
(($? != 0)) && check_return=1;
ping -c 1 google.com &> /dev/null
(($? != 0)) && check_return=1
ping -c 1 cloudflare.com &> /dev/null
(($? != 0)) && check_return=1
return $check_return
}
start()
{
for ((retries=0; retries<3; retries++))
do
wget https://dnsmasq.oisd.nl -O /tmp/oisd.txt &> /dev/null
(($? == 0)) && break
sleep 5
done
if [[ ! -f /tmp/oisd.txt ]]; then
logger -t adblock-oisd "Download of new oisd.txt file failed."
return
fi
read oisd_file_size_KB< <(du -bk /tmp/oisd.txt)
oisd_file_size_KB=${oisd_file_size_KB//[!0-9]/}
if (( oisd_file_size_KB > 20000 )); then
logger -t adblock-oisd "New oisd.txt file size: $oisd_file_size_KB KB too large."
return
fi
sed -i '\|^address=/[[:alnum:]]|!d;\|/#$|!d' /tmp/oisd.txt
if [[ -f /tmp/dnsmasq.d/oisd.txt ]]; then
gzip -c /tmp/dnsmasq.d/oisd.txt > /tmp/prev_oisd.txt.gz
fi
mv /tmp/oisd.txt /tmp/dnsmasq.d/oisd.txt
/etc/init.d/dnsmasq restart &> /dev/null
sleep 10
if ((check_dnsmasq!=0)); then
logger -t adblock-oisd "dnsmasq check failed with new oisd.txt file."
rm /tmp/dnsmasq.d/oisd.txt
if [[ -f /tmp/prev_oisd.txt.gz ]]; then
logger -t adblock-oisd "Found previous oisd.txt file. Attempting to recover."
gunzip -c /tmp/prev_oisd.txt.gz > /tmp/dnsmasq.d/oisd.txt
/etc/init.d/dnsmasq restart &> /dev/null
if ((check_dnsmasq!=0)); then
logger -t adblock-oisd "dnsmasq check failed with previous oisd.txt file. Stopping adblock-oisd."
stop
else
logger -t adblock-oisd "Previous oisd.txt recovered and dnsmasq check passed."
fi
else
logger -t adblock-oisd "No previous oisd.txt file found. Stopping adblock-oisd"
stop
fi
else
logger -t adblock-oisd "New oisd.txt downloaded and dnsmasq check passed."
[[ -f /tmp/prev_oisd.txt.gz ]] && rm /tmp/prev_oisd.txt.gz
fi
if [[ -f /tmp/prev_oisd.txt.gz ]]; then
rm /tmp/prev_oisd.txt.gz
fi
}
stop()
{
[[ -f /tmp/dnsmasq.d/oisd.txt ]] && rm /tmp/dnsmasq.d/oisd.txt
/etc/init.d/dnsmasq restart &> /dev/null
}