tazpanel annotate hosts.cgi @ rev 638

boot.iso: allow /dev/cdrom
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Jan 05 15:14:01 2022 +0000 (2022-01-05)
parents bf8941ab3cc3
children
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
pascal@613 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@537 29 HOSTSDIR='/var/lib/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@529 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@537 73 touch "$file.checked"
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
paul@540 82 # Prevent user-disabled entries to re-appear
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@537 93 rm "$file" "$file.checked" "$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@558 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@558 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@537 133 # Don't show diff because it's huge
al@537 134 rm "$HOSTSDIR/diff"
al@527 135 ;;
al@527 136
al@527 137 *\ uplist\ *)
al@527 138 # Update list
al@527 139
al@527 140 list="$(GET uplist)"
al@527 141 getlistspec "$list"
al@527 142 echo "<p>$(_ 'Updating the "%s"...' "$name") "
al@537 143
al@537 144 old_sublist=$(mktemp)
al@537 145 # Note, old sublist already sorted. Only hostnames here
al@537 146 awk -vlist="#$list" '$3 == list {print $2}' /etc/hosts > "$old_sublist"
al@537 147
al@527 148 remlist; instlist
al@537 149
al@537 150 new_sublist=$(mktemp)
al@537 151 awk -vlist="#$list" '$3 == list {print $2}' /etc/hosts > "$new_sublist"
al@537 152
al@537 153 # The diff: just '+' and '-', no header, no context
al@537 154 diff -dU0 "$old_sublist" "$new_sublist" | sed '1,2d;/^@/d' > "$HOSTSDIR/diff"
al@537 155
al@527 156 echo "$(_ 'Done')</p>"
al@537 157
al@537 158 # Show diff
al@537 159 if [ -s "$HOSTSDIR/diff" ]; then
al@537 160 echo '<section><pre class="scroll">'
pascal@637 161 syntax_highlighter diff < "$HOSTSDIR/diff"
al@537 162 echo '</pre></section>'
al@537 163 fi
al@537 164
al@537 165 # Clean
al@537 166 rm "$old_sublist" "$new_sublist" "$HOSTSDIR/diff"
al@527 167 ;;
al@527 168
al@527 169 *\ remlist\ *)
al@527 170 # Remove list
al@527 171
al@527 172 list="$(GET remlist)"
al@527 173 getlistspec "$list"
al@527 174 echo "<p>$(_ 'Removing the "%s"...' "$name") "
al@527 175 remlist
al@527 176 echo "$(_ 'Done')</p>"
al@527 177 ;;
al@527 178
al@527 179 esac
al@527 180
al@527 181 # When search term given
al@527 182 term=$(GET term)
al@527 183 if [ -z "$term" ]; then
al@527 184 getdb hosts | fgrep 0.0.0.0 > "$found"
al@527 185 r=$(wc -l < "$found")
al@558 186 echo -n '<p><span data-img="@info@"></span>'
al@527 187 _p '%d record used for Ad blocking' \
al@527 188 '%d records used for Ad blocking' "$r" "$r"
al@527 189 else
al@527 190 getdb hosts | fgrep 0.0.0.0 | fgrep "$term" > "$found"
al@527 191 r=$(wc -l < "$found")
al@558 192 echo -n '<p><span data-img="@info@"></span>'
al@527 193 _p '%d record found for "%s"' \
al@527 194 '%d records found for "%s"' "$r" "$r" "$term"
al@527 195 fi
al@527 196
al@527 197 [ "$r" -gt 100 ] && _ ' (The list is limited to the first 100 entries.)'
al@527 198 echo '</p>'
al@527 199
al@527 200 cat <<EOT
al@527 201 <section>
al@527 202 <header>
al@558 203 <span data-icon="@list@">$(_ 'Hosts')</span>
al@527 204 <form>
al@527 205 <input type="search" name="term" value="$(GET term)" results="5" autosave="hosts" autocomplete="on"/>
al@527 206 </form>
al@527 207 </header>
al@527 208 <form class="wide">
al@527 209 <pre class="scroll">
al@527 210 EOT
al@527 211 head -n100 "$found" | awk '{
al@527 212 printf "<label><input type=\"checkbox\" name=\"host\" value=\"%s\"/> %s</label>\n", $2, $2;
al@527 213 }'
al@527 214 rm "$found"
al@527 215 cat <<EOT
al@527 216 </pre>
al@527 217 <footer>
al@558 218 <button type="submit" name="disable" data-icon="@delete@" data-root>$(_ 'Disable selected')</button>
al@527 219 </footer>
al@527 220 </form>
al@527 221 </section>
al@527 222
al@527 223 <section>
al@558 224 <header><span data-icon="@add@">$(_ 'Add')</span></header>
al@527 225 <form class="wide">
al@527 226 <div>
al@527 227 $(_ 'Host:')
al@527 228 <input type="text" name="add"/>
al@527 229 </div>
al@527 230 <footer>
al@558 231 <button type="submit" data-icon="@add@" data-root>$(_ 'Add')</button>
al@527 232 </footer>
al@527 233 </form>
al@527 234 </section>
al@527 235
al@527 236 <section>
al@558 237 <header><span data-icon="@admin@">$(_ 'Manage lists')</span></header>
al@527 238 <div>$(_ 'You can use one or more prepared hosts files to block advertisements, malware and other irritants.')</div>
al@527 239 <form class="wide">
al@527 240 <table class="wide zebra">
al@527 241 <thead>
al@527 242 <tr>
al@527 243 <td>$(_ 'Name')</td>
al@527 244 <td>$(_ 'Details')</td>
al@527 245 <td>$(_ 'Updates')</td>
al@527 246 <td>$(_ 'Actions')</td>
al@527 247 </tr>
al@527 248 </thead>
al@527 249 <tbody>
al@527 250 EOT
al@527 251
al@527 252 IFS=$'\t'
al@527 253 listlist | while read name info url updated letter; do
al@527 254 [ -z "$name" ] && continue
al@527 255
al@527 256 cat <<EOT
al@527 257 <tr>
al@527 258 <td>$name</td>
pascal@606 259 <td><a data-icon="@info@" target="_blank" rel="noopener" href="$info">$(_ 'info')</a></td>
al@527 260 <td>
pascal@626 261 $([ "$updated" = 'monthly' ] && _ 'Updated monthly')
pascal@626 262 $([ "$updated" = 'regularly' ] && _ 'Updated regularly')
al@527 263 </td>
al@527 264 <td>
al@527 265 EOT
al@527 266
pascal@637 267 if [ -e "$HOSTSDIR/$letter" ] || grep -qm1 "#$letter\$" /etc/hosts; then
al@527 268 # List installed
al@527 269
al@529 270 # If /var/run/tazpkg/hosts/ was mistakenly cleaned
al@529 271 [ ! -f "$HOSTSDIR/$letter" ] && touch "$HOSTSDIR/$letter"
al@529 272
al@527 273 # Check for upgrades (once a day)
al@537 274 if [ -f "$HOSTSDIR/$letter.checked" ]; then
al@527 275 # Update checked previously
al@537 276 if [ "$(($(date -u +%s) - 86400))" -gt "$(date -ur "$HOSTSDIR/$letter.checked" +%s)" ]; then
al@527 277 # Update checked more than one day (86400 seconds) ago
al@527 278 check='yes'
al@527 279 else
al@527 280 # Update checked within one day
al@527 281 check='no'
al@527 282 fi
al@527 283 else
al@527 284 # Update not checked yet
al@527 285 check='yes'
al@527 286 fi
al@527 287
pascal@626 288 if [ "$check" = 'yes' ]; then
al@527 289 # Check for update (not really download)
pascal@637 290 if busybox wget -s --header "If-Modified-Since: $(date -Rur "$HOSTSDIR/$letter")" "$url"; then
al@529 291 # Update available
al@527 292 touch "$HOSTSDIR/$letter.avail"
al@527 293 else
al@529 294 # Update not available
al@527 295 rm "$HOSTSDIR/$letter.avail" 2>/dev/null
al@527 296 fi
al@537 297 touch "$HOSTSDIR/$letter.checked"
al@527 298 fi
al@527 299
al@527 300 if [ -f "$HOSTSDIR/$letter.avail" ]; then
al@527 301 cat <<EOT
al@558 302 <button name="uplist" value="$letter" data-icon="@upgrade@">$(_ 'Upgrade')</button>
al@527 303 EOT
al@527 304 fi
al@527 305
al@527 306 cat <<EOT
al@558 307 <button name="remlist" value="$letter" data-icon="@remove@">$(_ 'Remove')</button>
al@527 308 EOT
al@527 309
al@527 310 else
al@527 311 # List not installed
al@527 312 cat <<EOT
al@558 313 <button name="instlist" value="$letter" data-icon="@install@">$(_ 'Install')</button>
al@527 314 EOT
al@527 315 fi
al@527 316 echo '</td></tr>'
al@527 317 done
al@527 318
al@527 319 cat <<EOT
al@527 320 </tbody>
al@527 321 </table>
al@527 322 </form>
al@527 323 </section>
al@527 324 EOT
al@527 325
al@527 326 xhtml_footer
al@527 327 exit 0