# HG changeset patch # User Aleksej Bobylev # Date 1440367508 -10800 # Node ID 4f619f973b8882947de23e5858306a9cc1dce4da # Parent 5323427d34b909e2cc8e761bd113a6e526cb0643 network.cgi: use /etc/hosts as Ad/Spyware/Malicious sites blocker; faster build with stripall.sh diff -r 5323427d34b9 -r 4f619f973b88 lib/libtazpanel --- a/lib/libtazpanel Thu Aug 20 17:00:19 2015 +0300 +++ b/lib/libtazpanel Mon Aug 24 01:05:08 2015 +0300 @@ -397,6 +397,10 @@ # Get system database. NSS compatible. getdb() { - getent $1 2>/dev/null || cat /etc/$1 + if [ -n "$(which getent)" ]; then + getent "$1" + else + cat "/etc/$1" | sed '/^#/d' + fi } diff -r 5323427d34b9 -r 4f619f973b88 network.cgi --- a/network.cgi Thu Aug 20 17:00:19 2015 +0300 +++ b/network.cgi Mon Aug 24 01:05:08 2015 +0300 @@ -148,10 +148,10 @@ ether-wake -b $(GET iface) $pass fi ;; - *\ host\ *) - get_hostname="$(GET host)" - echo $(_ 'Changed hostname: %s' $get_hostname) | log - echo "$get_hostname" > /etc/hostname ;; + *\ hostname\ *) + hostname="$(GET hostname)" + echo $(_ 'Changed hostname: %s' "$hostname") | log + echo "$hostname" > /etc/hostname;; *\ rmarp\ *) arp -d $(urldecode "$(GET entry)") ;; *\ addarp\ *) @@ -617,6 +617,100 @@ ;; + *\ hosts\ *) + # Configure to use hosts as Ad blocker + xhtml_header "$(_ 'Use hosts file as Ad blocker')" + + term=$(GET term) + 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' + + if [ -n "$(GET add)" ]; then + # Add given host + + host="$(GET add)" + + echo "0.0.0.0 $host #U" >> /etc/hosts + echo -n '

' + _ 'Host "%s" added to /etc/hosts.' "$host" + echo '

' + + elif [ -n "$hosts" ]; then + # 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 '%d record disabled' \ + '%d records disabled' "$r" "$r" + echo '

' + fi + + # When search term given + if [ -z "$term" ]; then + getdb hosts | fgrep 0.0.0.0 > "$found" + r=$(wc -l < "$found") + echo -n '

' + _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 '%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 '

' + + cat < +
+ $(_ 'Hosts') +
+ + +
+
+
+EOT
+		sort "$found" | head -n100 | awk '{
+			printf "\n", $2, $2;
+		}'
+		rm "$found"
+		cat <
+		
+ +
+ + +
+
$(_ 'Add')
+
+
+ + $(_ 'Host:') + + +
+
+
+EOT + ;; + + *) # Main Network page starting with a summary xhtml_header "$(_ 'Manage network connections and services')" @@ -668,7 +762,17 @@
$(_ 'Hosts'; edit_button /etc/hosts)
-
$(getdb hosts)
+ $(r=$(getdb hosts | wc -l); + _p '%s record in the hosts DB' \ + '%s records in the hosts DB' "$r" \ + "$r") +
$(getdb hosts | fgrep -v 0.0.0.0)
+
+
+ + $(_ 'Use hosts file as Ad blocker') +
+
@@ -677,10 +781,9 @@