tazpanel view hosts.cgi @ rev 529

hosts.cgi: change folder for saving lists state to /var/run/...
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Aug 26 13:15:14 2015 +0300 (2015-08-26)
parents 3d4145137792
children 34508d2309a6
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/run/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.up"
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-appeare
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.up" "$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 ;;
135 *\ uplist\ *)
136 # Update list
138 list="$(GET uplist)"
139 getlistspec "$list"
140 echo "<p>$(_ 'Updating the "%s"...' "$name") "
141 remlist; instlist
142 echo "$(_ 'Done')</p>"
143 ;;
145 *\ remlist\ *)
146 # Remove list
148 list="$(GET remlist)"
149 getlistspec "$list"
150 echo "<p>$(_ 'Removing the "%s"...' "$name") "
151 remlist
152 echo "$(_ 'Done')</p>"
153 ;;
155 esac
157 # When search term given
158 term=$(GET term)
159 if [ -z "$term" ]; then
160 getdb hosts | fgrep 0.0.0.0 > "$found"
161 r=$(wc -l < "$found")
162 echo -n '<p><span data-img="info"></span>'
163 _p '%d record used for Ad blocking' \
164 '%d records used for Ad blocking' "$r" "$r"
165 else
166 getdb hosts | fgrep 0.0.0.0 | fgrep "$term" > "$found"
167 r=$(wc -l < "$found")
168 echo -n '<p><span data-img="info"></span>'
169 _p '%d record found for "%s"' \
170 '%d records found for "%s"' "$r" "$r" "$term"
171 fi
173 [ "$r" -gt 100 ] && _ ' (The list is limited to the first 100 entries.)'
174 echo '</p>'
176 cat <<EOT
177 <section>
178 <header>
179 <span data-icon="list">$(_ 'Hosts')</span>
180 <form>
181 <input type="search" name="term" value="$(GET term)" results="5" autosave="hosts" autocomplete="on"/>
182 </form>
183 </header>
184 <form class="wide">
185 <pre class="scroll">
186 EOT
187 head -n100 "$found" | awk '{
188 printf "<label><input type=\"checkbox\" name=\"host\" value=\"%s\"/> %s</label>\n", $2, $2;
189 }'
190 rm "$found"
191 cat <<EOT
192 </pre>
193 <footer>
194 <button type="submit" name="disable" data-icon="delete" data-root>$(_ 'Disable selected')</button>
195 </footer>
196 </form>
197 </section>
199 <section>
200 <header><span data-icon="add">$(_ 'Add')</span></header>
201 <form class="wide">
202 <div>
203 $(_ 'Host:')
204 <input type="text" name="add"/>
205 </div>
206 <footer>
207 <button type="submit" data-icon="add" data-root>$(_ 'Add')</button>
208 </footer>
209 </form>
210 </section>
212 <section>
213 <header><span data-icon="admin">$(_ 'Manage lists')</span></header>
214 <div>$(_ 'You can use one or more prepared hosts files to block advertisements, malware and other irritants.')</div>
215 <form class="wide">
216 <table class="wide zebra">
217 <thead>
218 <tr>
219 <td>$(_ 'Name')</td>
220 <td>$(_ 'Details')</td>
221 <td>$(_ 'Updates')</td>
222 <td>$(_ 'Actions')</td>
223 </tr>
224 </thead>
225 <tbody>
226 EOT
228 IFS=$'\t'
229 listlist | while read name info url updated letter; do
230 [ -z "$name" ] && continue
232 cat <<EOT
233 <tr>
234 <td>$name</td>
235 <td><a data-icon="info" target="_blank" href="$info">$(_ 'info')</a></td>
236 <td>
237 $([ "$updated" == 'monthly' ] && _ 'Updated monthly')
238 $([ "$updated" == 'regularly' ] && _ 'Updated regularly')
239 </td>
240 <td>
241 EOT
243 if [ -e "$HOSTSDIR/$letter" -o -n "$(grep -m1 "#$letter\$" /etc/hosts)" ]; then
244 # List installed
246 # If /var/run/tazpkg/hosts/ was mistakenly cleaned
247 [ ! -f "$HOSTSDIR/$letter" ] && touch "$HOSTSDIR/$letter"
249 # Check for upgrades (once a day)
250 if [ -f "$HOSTSDIR/$letter.up" ]; then
251 # Update checked previously
252 if [ "$(($(date -u +%s) - 86400))" -gt "$(date -ur "$HOSTSDIR/$letter.up" +%s)" ]; then
253 # Update checked more than one day (86400 seconds) ago
254 check='yes'
255 else
256 # Update checked within one day
257 check='no'
258 fi
259 else
260 # Update not checked yet
261 check='yes'
262 fi
264 if [ "$check" == 'yes' ]; then
265 # Check for update (not really download)
266 busybox wget -s --header "If-Modified-Since: $(date -Rur "$HOSTSDIR/$letter")" "$url"
267 if [ "$?" -eq 0 ]; then
268 # Update available
269 touch "$HOSTSDIR/$letter.avail"
270 else
271 # Update not available
272 rm "$HOSTSDIR/$letter.avail" 2>/dev/null
273 fi
274 touch "$HOSTSDIR/$letter.up"
275 fi
277 if [ -f "$HOSTSDIR/$letter.avail" ]; then
278 cat <<EOT
279 <button name="uplist" value="$letter" data-icon="upgrade">$(_ 'Upgrade')</button>
280 EOT
281 fi
283 cat <<EOT
284 <button name="remlist" value="$letter" data-icon="remove">$(_ 'Remove')</button>
285 EOT
287 else
288 # List not installed
289 cat <<EOT
290 <button name="instlist" value="$letter" data-icon="install">$(_ 'Install')</button>
291 EOT
292 fi
293 echo '</td></tr>'
294 done
296 cat <<EOT
297 </tbody>
298 </table>
299 </form>
300 </section>
301 EOT
303 xhtml_footer
304 exit 0