-
Notifications
You must be signed in to change notification settings - Fork 1
/
hosts.cgi
executable file
·327 lines (273 loc) · 7.8 KB
/
hosts.cgi
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/sh
#
# Manage /etc/hosts CGI interface - to use hosts as Ad blocker
#
# Copyright (C) 2015 SliTaz GNU/Linux - BSD License
#
# Common functions from libtazpanel
. ./lib/libtazpanel
get_config
header
TITLE=$(_ 'Network')
xhtml_header "$(_ 'Use hosts file as Ad blocker')"
found=$(mktemp)
# Find the hosts list
hosts=$(echo "$QUERY_STRING&" | awk '
BEGIN { RS="&"; FS="=" }
$1=="host" { printf "%s ", $2 }
')
hosts=$(httpd -d "${hosts% }")
# now hosts='host1 host2 ... hostn'
# Folder to save downloaded and installed hosts lists
HOSTSDIR='/var/lib/tazpanel/hosts'
mkdir -p "$HOSTSDIR"
# List the lists data: name, about URL, download URL, update frequency, code letter
listlist() {
# Another free to use list, but not free to modify with its EULA (and very big: 12MB!)
# hpHosts List http://hosts-file.net/
echo "
MVPs.org List http://winhelp2002.mvps.org/hosts.htm http://winhelp2002.mvps.org/hosts.zip monthly M
Dan Pollock List http://someonewhocares.org/hosts/ http://someonewhocares.org/hosts/zero/hosts regularly P
Malware Domain List http://www.malwaredomainlist.com/ http://www.malwaredomainlist.com/hostslist/hosts.txt regularly D
Peter Lowe List http://pgl.yoyo.org/adservers/ http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&mimetype=plaintext regularly L
"
}
# Get the list specifications: name and download URL
getlistspec() {
# Input: code letter
local line="$(listlist | grep "$1\$")"
name="$(echo "$line" | cut -d$'\t' -f1)"
url="$( echo "$line" | cut -d$'\t' -f3)"
}
# Install the list
instlist() {
# Input: list=code letter, url=download URL
file="$HOSTSDIR/$list"
[ -f "$file" ] && rm "$file"
busybox wget -O "$file" "$url"
case $url in
*zip)
mv "$file" "$file.zip"
busybox unzip "$file.zip" HOSTS >/dev/null
mv HOSTS "$file"
rm "$file.zip"
;;
esac
# Add entries to hosts file
awk -vl="$list" '$1=="0.0.0.0"||$1=="127.0.0.1"{printf "0.0.0.0 %s #%s\n", $2, l}' "$file" | fgrep -v localhost >> /etc/hosts
# Clean the list
echo -n > "$file"
touch "$file.checked"
# Remove the duplicate entries
hostsnew=$(mktemp)
grep "^0.0.0.0" /etc/hosts | sort -k2,2 -u -o "$hostsnew"
sed -i '/^0.0.0.0/d' /etc/hosts
cat "$hostsnew" >> /etc/hosts
rm "$hostsnew"
# Prevent user-disabled entries to re-appear
grep "^#0.0.0.0" /etc/hosts | while read null host; do
sed -i "/^0.0.0.0 $host #/d" /etc/hosts
done
}
# Remove the list
remlist() {
# Input: list=code letter
sed -i "/#$list$/d" /etc/hosts
file="$HOSTSDIR/$list"
rm "$file" "$file.checked" "$file.avail"
}
case " $(GET) " in
*\ add\ *)
# Add given host
host="$(GET add)"
echo "0.0.0.0 $host #U" >> /etc/hosts
echo -n '<p><span data-img="@info@"></span>'
_ 'Host "%s" added to /etc/hosts.' "$host"
echo '</p>'
;;
*\ disable\ *)
# Disable given hosts
for host in $hosts; do
sed -i "s|^0.0.0.0[ \t][ \t]*$host\$|#\0|" /etc/hosts
sed -i "s|^0.0.0.0[ \t][ \t]*$host .*|#\0|" /etc/hosts
done
r=$(echo "$hosts" | tr ' ' '\n' | wc -l)
echo -n '<p><span data-img="@info@"></span>'
_p '%d record disabled' \
'%d records disabled' "$r" "$r"
echo '</p>'
;;
*\ instlist\ *)
# Install list
list="$(GET instlist)"
getlistspec "$list"
echo "<p>$(_ 'Installing the "%s"...' "$name") "
instlist
echo "$(_ 'Done')</p>"
# Don't show diff because it's huge
rm "$HOSTSDIR/diff"
;;
*\ uplist\ *)
# Update list
list="$(GET uplist)"
getlistspec "$list"
echo "<p>$(_ 'Updating the "%s"...' "$name") "
old_sublist=$(mktemp)
# Note, old sublist already sorted. Only hostnames here
awk -vlist="#$list" '$3 == list {print $2}' /etc/hosts > "$old_sublist"
remlist; instlist
new_sublist=$(mktemp)
awk -vlist="#$list" '$3 == list {print $2}' /etc/hosts > "$new_sublist"
# The diff: just '+' and '-', no header, no context
diff -dU0 "$old_sublist" "$new_sublist" | sed '1,2d;/^@/d' > "$HOSTSDIR/diff"
echo "$(_ 'Done')</p>"
# Show diff
if [ -s "$HOSTSDIR/diff" ]; then
echo '<section><pre class="scroll">'
syntax_highlighter diff < "$HOSTSDIR/diff"
echo '</pre></section>'
fi
# Clean
rm "$old_sublist" "$new_sublist" "$HOSTSDIR/diff"
;;
*\ remlist\ *)
# Remove list
list="$(GET remlist)"
getlistspec "$list"
echo "<p>$(_ 'Removing the "%s"...' "$name") "
remlist
echo "$(_ 'Done')</p>"
;;
esac
# When search term given
term=$(GET term)
if [ -z "$term" ]; then
getdb hosts | fgrep 0.0.0.0 > "$found"
r=$(wc -l < "$found")
echo -n '<p><span data-img="@info@"></span>'
_p '%d record used for Ad blocking' \
'%d records used for Ad blocking' "$r" "$r"
else
getdb hosts | fgrep 0.0.0.0 | fgrep "$term" > "$found"
r=$(wc -l < "$found")
echo -n '<p><span data-img="@info@"></span>'
_p '%d record found for "%s"' \
'%d records found for "%s"' "$r" "$r" "$term"
fi
[ "$r" -gt 100 ] && _ ' (The list is limited to the first 100 entries.)'
echo '</p>'
cat <<EOT
<section>
<header>
<span data-icon="@list@">$(_ 'Hosts')</span>
<form>
<input type="search" name="term" value="$(GET term)" results="5" autosave="hosts" autocomplete="on"/>
</form>
</header>
<form class="wide">
<pre class="scroll">
EOT
head -n100 "$found" | awk '{
printf "<label><input type=\"checkbox\" name=\"host\" value=\"%s\"/> %s</label>\n", $2, $2;
}'
rm "$found"
cat <<EOT
</pre>
<footer>
<button type="submit" name="disable" data-icon="@delete@" data-root>$(_ 'Disable selected')</button>
</footer>
</form>
</section>
<section>
<header><span data-icon="@add@">$(_ 'Add')</span></header>
<form class="wide">
<div>
$(_ 'Host:')
<input type="text" name="add"/>
</div>
<footer>
<button type="submit" data-icon="@add@" data-root>$(_ 'Add')</button>
</footer>
</form>
</section>
<section>
<header><span data-icon="@admin@">$(_ 'Manage lists')</span></header>
<div>$(_ 'You can use one or more prepared hosts files to block advertisements, malware and other irritants.')</div>
<form class="wide">
<table class="wide zebra">
<thead>
<tr>
<td>$(_ 'Name')</td>
<td>$(_ 'Details')</td>
<td>$(_ 'Updates')</td>
<td>$(_ 'Actions')</td>
</tr>
</thead>
<tbody>
EOT
IFS=$'\t'
listlist | while read name info url updated letter; do
[ -z "$name" ] && continue
cat <<EOT
<tr>
<td>$name</td>
<td><a data-icon="@info@" target="_blank" rel="noopener" href="$info">$(_ 'info')</a></td>
<td>
$([ "$updated" = 'monthly' ] && _ 'Updated monthly')
$([ "$updated" = 'regularly' ] && _ 'Updated regularly')
</td>
<td>
EOT
if [ -e "$HOSTSDIR/$letter" ] || grep -qm1 "#$letter\$" /etc/hosts; then
# List installed
# If /var/run/tazpkg/hosts/ was mistakenly cleaned
[ ! -f "$HOSTSDIR/$letter" ] && touch "$HOSTSDIR/$letter"
# Check for upgrades (once a day)
if [ -f "$HOSTSDIR/$letter.checked" ]; then
# Update checked previously
if [ "$(($(date -u +%s) - 86400))" -gt "$(date -ur "$HOSTSDIR/$letter.checked" +%s)" ]; then
# Update checked more than one day (86400 seconds) ago
check='yes'
else
# Update checked within one day
check='no'
fi
else
# Update not checked yet
check='yes'
fi
if [ "$check" = 'yes' ]; then
# Check for update (not really download)
if busybox wget -s --header "If-Modified-Since: $(date -Rur "$HOSTSDIR/$letter")" "$url"; then
# Update available
touch "$HOSTSDIR/$letter.avail"
else
# Update not available
rm "$HOSTSDIR/$letter.avail" 2>/dev/null
fi
touch "$HOSTSDIR/$letter.checked"
fi
if [ -f "$HOSTSDIR/$letter.avail" ]; then
cat <<EOT
<button name="uplist" value="$letter" data-icon="@upgrade@">$(_ 'Upgrade')</button>
EOT
fi
cat <<EOT
<button name="remlist" value="$letter" data-icon="@remove@">$(_ 'Remove')</button>
EOT
else
# List not installed
cat <<EOT
<button name="instlist" value="$letter" data-icon="@install@">$(_ 'Install')</button>
EOT
fi
echo '</td></tr>'
done
cat <<EOT
</tbody>
</table>
</form>
</section>
EOT
xhtml_footer
exit 0