tazpanel view hosts.cgi @ rev 623

Add Italian; make pot; make msgmerge; make clean
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Jan 30 12:06:32 2018 +0200 (2018-01-30)
parents b39fc0acf12a
children bf8941ab3cc3
line source
1 #!/bin/sh
2 #
3 # Manage /etc/hosts CGI interface - to use hosts as Ad blocker
4 #
5 # Copyright (C) 2015 SliTaz GNU/Linux - BSD License
6 #
9 # Common functions from libtazpanel
11 . ./lib/libtazpanel
12 get_config
13 header
15 TITLE=$(_ 'Network')
16 xhtml_header "$(_ 'Use hosts file as Ad blocker')"
18 found=$(mktemp)
20 # Find the hosts list
21 hosts=$(echo "$QUERY_STRING&" | awk '
22 BEGIN { RS="&"; FS="=" }
23 $1=="host" { printf "%s ", $2 }
24 ')
25 hosts=$(httpd -d "${hosts% }")
26 # now hosts='host1 host2 ... hostn'
28 # Folder to save downloaded and installed hosts lists
29 HOSTSDIR='/var/lib/tazpanel/hosts'
30 mkdir -p "$HOSTSDIR"
35 # List the lists data: name, about URL, download URL, update frequency, code letter
36 listlist() {
37 # Another free to use list, but not free to modify with its EULA (and very big: 12MB!)
38 # hpHosts List http://hosts-file.net/
39 echo "
40 MVPs.org List http://winhelp2002.mvps.org/hosts.htm http://winhelp2002.mvps.org/hosts.zip monthly M
41 Dan Pollock List http://someonewhocares.org/hosts/ http://someonewhocares.org/hosts/zero/hosts regularly P
42 Malware Domain List http://www.malwaredomainlist.com/ http://www.malwaredomainlist.com/hostslist/hosts.txt regularly D
43 Peter Lowe List http://pgl.yoyo.org/adservers/ http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&mimetype=plaintext regularly L
44 "
45 }
47 # Get the list specifications: name and download URL
48 getlistspec() {
49 # Input: code letter
50 local line="$(listlist | grep "$1\$")"
51 name="$(echo "$line" | cut -d$'\t' -f1)"
52 url="$( echo "$line" | cut -d$'\t' -f3)"
53 }
55 # Install the list
56 instlist() {
57 # Input: list=code letter, url=download URL
58 file="$HOSTSDIR/$list"
59 [ -f "$file" ] && rm "$file"
60 busybox wget -O "$file" "$url"
61 case $url in
62 *zip)
63 mv "$file" "$file.zip"
64 busybox unzip "$file.zip" HOSTS >/dev/null
65 mv HOSTS "$file"
66 rm "$file.zip"
67 ;;
68 esac
69 # Add entries to hosts file
70 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
71 # Clean the list
72 echo -n > "$file"
73 touch "$file.checked"
75 # Remove the duplicate entries
76 hostsnew=$(mktemp)
77 grep "^0.0.0.0" /etc/hosts | sort -k2,2 -u -o "$hostsnew"
78 sed -i '/^0.0.0.0/d' /etc/hosts
79 cat "$hostsnew" >> /etc/hosts
80 rm "$hostsnew"
82 # Prevent user-disabled entries to re-appear
83 grep "^#0.0.0.0" /etc/hosts | while read null host; do
84 sed -i "/^0.0.0.0 $host #/d" /etc/hosts
85 done
86 }
88 # Remove the list
89 remlist() {
90 # Input: list=code letter
91 sed -i "/#$list$/d" /etc/hosts
92 file="$HOSTSDIR/$list"
93 rm "$file" "$file.checked" "$file.avail"
94 }
98 case " $(GET) " in
100 *\ add\ *)
101 # Add given host
103 host="$(GET add)"
105 echo "0.0.0.0 $host #U" >> /etc/hosts
106 echo -n '<p><span data-img="@info@"></span>'
107 _ 'Host "%s" added to /etc/hosts.' "$host"
108 echo '</p>'
109 ;;
111 *\ disable\ *)
112 # Disable given hosts
114 for host in $hosts; do
115 sed -i "s|^0.0.0.0[ \t][ \t]*$host\$|#\0|" /etc/hosts
116 sed -i "s|^0.0.0.0[ \t][ \t]*$host .*|#\0|" /etc/hosts
117 done
118 r=$(echo "$hosts" | tr ' ' '\n' | wc -l)
119 echo -n '<p><span data-img="@info@"></span>'
120 _p '%d record disabled' \
121 '%d records disabled' "$r" "$r"
122 echo '</p>'
123 ;;
125 *\ instlist\ *)
126 # Install list
128 list="$(GET instlist)"
129 getlistspec "$list"
130 echo "<p>$(_ 'Installing the "%s"...' "$name") "
131 instlist
132 echo "$(_ 'Done')</p>"
133 # Don't show diff because it's huge
134 rm "$HOSTSDIR/diff"
135 ;;
137 *\ uplist\ *)
138 # Update list
140 list="$(GET uplist)"
141 getlistspec "$list"
142 echo "<p>$(_ 'Updating the "%s"...' "$name") "
144 old_sublist=$(mktemp)
145 # Note, old sublist already sorted. Only hostnames here
146 awk -vlist="#$list" '$3 == list {print $2}' /etc/hosts > "$old_sublist"
148 remlist; instlist
150 new_sublist=$(mktemp)
151 awk -vlist="#$list" '$3 == list {print $2}' /etc/hosts > "$new_sublist"
153 # The diff: just '+' and '-', no header, no context
154 diff -dU0 "$old_sublist" "$new_sublist" | sed '1,2d;/^@/d' > "$HOSTSDIR/diff"
156 echo "$(_ 'Done')</p>"
158 # Show diff
159 if [ -s "$HOSTSDIR/diff" ]; then
160 echo '<section><pre class="scroll">'
161 cat "$HOSTSDIR/diff" | syntax_highlighter diff
162 echo '</pre></section>'
163 fi
165 # Clean
166 rm "$old_sublist" "$new_sublist" "$HOSTSDIR/diff"
167 ;;
169 *\ remlist\ *)
170 # Remove list
172 list="$(GET remlist)"
173 getlistspec "$list"
174 echo "<p>$(_ 'Removing the "%s"...' "$name") "
175 remlist
176 echo "$(_ 'Done')</p>"
177 ;;
179 esac
181 # When search term given
182 term=$(GET term)
183 if [ -z "$term" ]; then
184 getdb hosts | fgrep 0.0.0.0 > "$found"
185 r=$(wc -l < "$found")
186 echo -n '<p><span data-img="@info@"></span>'
187 _p '%d record used for Ad blocking' \
188 '%d records used for Ad blocking' "$r" "$r"
189 else
190 getdb hosts | fgrep 0.0.0.0 | fgrep "$term" > "$found"
191 r=$(wc -l < "$found")
192 echo -n '<p><span data-img="@info@"></span>'
193 _p '%d record found for "%s"' \
194 '%d records found for "%s"' "$r" "$r" "$term"
195 fi
197 [ "$r" -gt 100 ] && _ ' (The list is limited to the first 100 entries.)'
198 echo '</p>'
200 cat <<EOT
201 <section>
202 <header>
203 <span data-icon="@list@">$(_ 'Hosts')</span>
204 <form>
205 <input type="search" name="term" value="$(GET term)" results="5" autosave="hosts" autocomplete="on"/>
206 </form>
207 </header>
208 <form class="wide">
209 <pre class="scroll">
210 EOT
211 head -n100 "$found" | awk '{
212 printf "<label><input type=\"checkbox\" name=\"host\" value=\"%s\"/> %s</label>\n", $2, $2;
213 }'
214 rm "$found"
215 cat <<EOT
216 </pre>
217 <footer>
218 <button type="submit" name="disable" data-icon="@delete@" data-root>$(_ 'Disable selected')</button>
219 </footer>
220 </form>
221 </section>
223 <section>
224 <header><span data-icon="@add@">$(_ 'Add')</span></header>
225 <form class="wide">
226 <div>
227 $(_ 'Host:')
228 <input type="text" name="add"/>
229 </div>
230 <footer>
231 <button type="submit" data-icon="@add@" data-root>$(_ 'Add')</button>
232 </footer>
233 </form>
234 </section>
236 <section>
237 <header><span data-icon="@admin@">$(_ 'Manage lists')</span></header>
238 <div>$(_ 'You can use one or more prepared hosts files to block advertisements, malware and other irritants.')</div>
239 <form class="wide">
240 <table class="wide zebra">
241 <thead>
242 <tr>
243 <td>$(_ 'Name')</td>
244 <td>$(_ 'Details')</td>
245 <td>$(_ 'Updates')</td>
246 <td>$(_ 'Actions')</td>
247 </tr>
248 </thead>
249 <tbody>
250 EOT
252 IFS=$'\t'
253 listlist | while read name info url updated letter; do
254 [ -z "$name" ] && continue
256 cat <<EOT
257 <tr>
258 <td>$name</td>
259 <td><a data-icon="@info@" target="_blank" rel="noopener" href="$info">$(_ 'info')</a></td>
260 <td>
261 $([ "$updated" == 'monthly' ] && _ 'Updated monthly')
262 $([ "$updated" == 'regularly' ] && _ 'Updated regularly')
263 </td>
264 <td>
265 EOT
267 if [ -e "$HOSTSDIR/$letter" -o -n "$(grep -m1 "#$letter\$" /etc/hosts)" ]; then
268 # List installed
270 # If /var/run/tazpkg/hosts/ was mistakenly cleaned
271 [ ! -f "$HOSTSDIR/$letter" ] && touch "$HOSTSDIR/$letter"
273 # Check for upgrades (once a day)
274 if [ -f "$HOSTSDIR/$letter.checked" ]; then
275 # Update checked previously
276 if [ "$(($(date -u +%s) - 86400))" -gt "$(date -ur "$HOSTSDIR/$letter.checked" +%s)" ]; then
277 # Update checked more than one day (86400 seconds) ago
278 check='yes'
279 else
280 # Update checked within one day
281 check='no'
282 fi
283 else
284 # Update not checked yet
285 check='yes'
286 fi
288 if [ "$check" == 'yes' ]; then
289 # Check for update (not really download)
290 busybox wget -s --header "If-Modified-Since: $(date -Rur "$HOSTSDIR/$letter")" "$url"
291 if [ "$?" -eq 0 ]; then
292 # Update available
293 touch "$HOSTSDIR/$letter.avail"
294 else
295 # Update not available
296 rm "$HOSTSDIR/$letter.avail" 2>/dev/null
297 fi
298 touch "$HOSTSDIR/$letter.checked"
299 fi
301 if [ -f "$HOSTSDIR/$letter.avail" ]; then
302 cat <<EOT
303 <button name="uplist" value="$letter" data-icon="@upgrade@">$(_ 'Upgrade')</button>
304 EOT
305 fi
307 cat <<EOT
308 <button name="remlist" value="$letter" data-icon="@remove@">$(_ 'Remove')</button>
309 EOT
311 else
312 # List not installed
313 cat <<EOT
314 <button name="instlist" value="$letter" data-icon="@install@">$(_ 'Install')</button>
315 EOT
316 fi
317 echo '</td></tr>'
318 done
320 cat <<EOT
321 </tbody>
322 </table>
323 </form>
324 </section>
325 EOT
327 xhtml_footer
328 exit 0