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