tazpanel rev 526
network.cgi: use /etc/hosts as Ad/Spyware/Malicious sites blocker; faster build with stripall.sh
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Mon Aug 24 01:05:08 2015 +0300 (2015-08-24) |
parents | 5323427d34b9 |
children | 3d4145137792 |
files | lib/libtazpanel network.cgi stripall.sh styles/default/base.css styles/default/header.html |
line diff
1.1 --- a/lib/libtazpanel Thu Aug 20 17:00:19 2015 +0300 1.2 +++ b/lib/libtazpanel Mon Aug 24 01:05:08 2015 +0300 1.3 @@ -397,6 +397,10 @@ 1.4 # Get system database. NSS compatible. 1.5 1.6 getdb() { 1.7 - getent $1 2>/dev/null || cat /etc/$1 1.8 + if [ -n "$(which getent)" ]; then 1.9 + getent "$1" 1.10 + else 1.11 + cat "/etc/$1" | sed '/^#/d' 1.12 + fi 1.13 } 1.14
2.1 --- a/network.cgi Thu Aug 20 17:00:19 2015 +0300 2.2 +++ b/network.cgi Mon Aug 24 01:05:08 2015 +0300 2.3 @@ -148,10 +148,10 @@ 2.4 ether-wake -b $(GET iface) $pass 2.5 fi 2.6 ;; 2.7 - *\ host\ *) 2.8 - get_hostname="$(GET host)" 2.9 - echo $(_ 'Changed hostname: %s' $get_hostname) | log 2.10 - echo "$get_hostname" > /etc/hostname ;; 2.11 + *\ hostname\ *) 2.12 + hostname="$(GET hostname)" 2.13 + echo $(_ 'Changed hostname: %s' "$hostname") | log 2.14 + echo "$hostname" > /etc/hostname;; 2.15 *\ rmarp\ *) 2.16 arp -d $(urldecode "$(GET entry)") ;; 2.17 *\ addarp\ *) 2.18 @@ -617,6 +617,100 @@ 2.19 ;; 2.20 2.21 2.22 + *\ hosts\ *) 2.23 + # Configure to use hosts as Ad blocker 2.24 + xhtml_header "$(_ 'Use hosts file as Ad blocker')" 2.25 + 2.26 + term=$(GET term) 2.27 + found=$(mktemp) 2.28 + 2.29 + # Find the hosts list 2.30 + hosts=$(echo "$QUERY_STRING&" | awk ' 2.31 + BEGIN { RS="&"; FS="=" } 2.32 + $1=="host" { printf "%s ", $2 } 2.33 + ') 2.34 + hosts=$(httpd -d "${hosts% }") 2.35 + # now hosts='host1 host2 ... hostn' 2.36 + 2.37 + if [ -n "$(GET add)" ]; then 2.38 + # Add given host 2.39 + 2.40 + host="$(GET add)" 2.41 + 2.42 + echo "0.0.0.0 $host #U" >> /etc/hosts 2.43 + echo -n '<p><span data-img="info"></span>' 2.44 + _ 'Host "%s" added to /etc/hosts.' "$host" 2.45 + echo '</p>' 2.46 + 2.47 + elif [ -n "$hosts" ]; then 2.48 + # Disable given hosts 2.49 + 2.50 + for host in $hosts; do 2.51 + sed -i "s|^0.0.0.0[ \t][ \t]*$host\$|#\0|" /etc/hosts 2.52 + sed -i "s|^0.0.0.0[ \t][ \t]*$host .*|#\0|" /etc/hosts 2.53 + done 2.54 + r=$(echo "$hosts" | tr ' ' '\n' | wc -l) 2.55 + echo -n '<p><span data-img="info"></span>' 2.56 + _p '%d record disabled' \ 2.57 + '%d records disabled' "$r" "$r" 2.58 + echo '</p>' 2.59 + fi 2.60 + 2.61 + # When search term given 2.62 + if [ -z "$term" ]; then 2.63 + getdb hosts | fgrep 0.0.0.0 > "$found" 2.64 + r=$(wc -l < "$found") 2.65 + echo -n '<p><span data-img="info"></span>' 2.66 + _p '%d record used for Ad blocking' \ 2.67 + '%d records used for Ad blocking' "$r" "$r" 2.68 + else 2.69 + getdb hosts | fgrep 0.0.0.0 | fgrep "$term" > "$found" 2.70 + r=$(wc -l < "$found") 2.71 + echo -n '<p><span data-img="info"></span>' 2.72 + _p '%d record found for "%s"' \ 2.73 + '%d records found for "%s"' "$r" "$r" "$term" 2.74 + fi 2.75 + 2.76 + [ "$r" -gt 100 ] && _ ' (The list is limited to the first 100 entries.)' 2.77 + echo '</p>' 2.78 + 2.79 + cat <<EOT 2.80 + <section> 2.81 + <header> 2.82 + <span data-icon="list">$(_ 'Hosts')</span> 2.83 + <form id="hosts"> 2.84 + <input type="hidden" name="hosts" value=""/> 2.85 + <input type="search" name="term" value="$(GET term)" results="5" autosave="hosts" autocomplete="on"/> 2.86 + </form> 2.87 + </header> 2.88 + <pre class="scroll"> 2.89 +EOT 2.90 + sort "$found" | head -n100 | awk '{ 2.91 + printf "<label><input type=\"checkbox\" name=\"host\" value=\"%s\" form=\"hosts\"/> %s</label>\n", $2, $2; 2.92 + }' 2.93 + rm "$found" 2.94 + cat <<EOT 2.95 +</pre> 2.96 + <footer> 2.97 + <button form="hosts" data-icon="delete" data-root>$(_ 'Disable selected')</button> 2.98 + </footer> 2.99 + </section> 2.100 + 2.101 + <section> 2.102 + <header><span data-icon="add">$(_ 'Add')</span></header> 2.103 + <form class="wide"> 2.104 + <div> 2.105 + <input type="hidden" name="hosts"/> 2.106 + $(_ 'Host:') 2.107 + <input type="text" name="add"/> 2.108 + <button type="submit" data-icon="add" data-root>$(_ 'Add')</button> 2.109 + </div> 2.110 + </form> 2.111 + </section> 2.112 +EOT 2.113 + ;; 2.114 + 2.115 + 2.116 *) 2.117 # Main Network page starting with a summary 2.118 xhtml_header "$(_ 'Manage network connections and services')" 2.119 @@ -668,7 +762,17 @@ 2.120 2.121 <section> 2.122 <header id="hosts">$(_ 'Hosts'; edit_button /etc/hosts)</header> 2.123 - <pre class="scroll">$(getdb hosts)</pre> 2.124 + <span data-icon="info">$(r=$(getdb hosts | wc -l); 2.125 + _p '%s record in the hosts DB' \ 2.126 + '%s records in the hosts DB' "$r" \ 2.127 + "$r")</span> 2.128 + <pre class="scroll">$(getdb hosts | fgrep -v 0.0.0.0)</pre> 2.129 + <footer> 2.130 + <form> 2.131 + <button name="hosts" data-icon="admin" data-root>$(_ 'Configure')</button> 2.132 + $(_ 'Use hosts file as Ad blocker') 2.133 + </form> 2.134 + </footer> 2.135 </section> 2.136 2.137 2.138 @@ -677,10 +781,9 @@ 2.139 <footer> 2.140 EOT 2.141 if [ -w '/etc/hostname' ]; then 2.142 - # was: name="hostname"; please don't use 'name' in name: unwanted webkit styling 2.143 cat <<EOT 2.144 <form> 2.145 - <input type="text" name="host" value="$(cat /etc/hostname)"/><!-- 2.146 + <input type="text" name="hostname" value="$(hostname)"/><!-- 2.147 --><button type="submit" data-icon="ok">$(_ 'Change')</button> 2.148 </form> 2.149 EOT
3.1 --- a/stripall.sh Thu Aug 20 17:00:19 2015 +0300 3.2 +++ b/stripall.sh Mon Aug 24 01:05:08 2015 +0300 3.3 @@ -7,6 +7,18 @@ 3.4 substitute_icons() { 3.5 grep -q 'data-icon="\|data-img"' $1 || return 3.6 3.7 + # Customize sed script 3.8 + cp "$sed_script" "$sed_script.do" 3.9 + sed -i "s|@@@|$1|" "$sed_script.do" 3.10 + # Run sed script 3.11 + sh "$sed_script.do" 3.12 + rm "$sed_script.do" 3.13 +} 3.14 + 3.15 + 3.16 +# Make script for substitution 3.17 + sed_script="$(mktemp)" 3.18 + echo -n "sed -i '" > "$sed_script" 3.19 echo -e "\ 3.20 add \n admin \n back \n battery 3.21 brightness \n cancel \n cd \n check 3.22 @@ -32,18 +44,17 @@ 3.23 msg \n msgerr \n msgwarn \n msgup 3.24 msgtip \n vpn " | \ 3.25 while read icon symbol; do 3.26 - echo -n "." 3.27 - sed -i "s|data-icon=\"$icon\"|data-icon=\"$symbol\"|g" $1 3.28 - sed -i "s|data-img=\"$icon\"|data-img=\"$symbol\"|g" $1 3.29 + echo -n "s|data-icon=\"$icon\"|data-icon=\"$symbol\"|g; " >> "$sed_script" 3.30 + echo -n "s|data-img=\"$icon\"|data-img=\"$symbol\"|g; " >> "$sed_script" 3.31 done 3.32 -} 3.33 + echo "' @@@" >> "$sed_script" 3.34 3.35 3.36 cd build 3.37 3.38 echo -e "\nStrip shell scripts" 3.39 for CGI in *.cgi tazpanel libtazpanel bootloader *.html; do 3.40 - echo -en "\nProcessing $CGI" 3.41 + echo "Processing $CGI" 3.42 3.43 mv $CGI $CGI.old 3.44 # Copy initial comment (down to empty line) 3.45 @@ -65,7 +76,7 @@ 3.46 3.47 echo -e "\n\nStrip CSS stylesheets" 3.48 for CSS in *.css; do 3.49 - echo -en "\nProcessing $CSS" 3.50 + echo "Processing $CSS" 3.51 3.52 mv $CSS $CSS.old 3.53 tr '\n' ' ' < $CSS.old > $CSS 3.54 @@ -97,4 +108,5 @@ 3.55 cat *.js > gz/tazpanel.js 3.56 gzip -9 gz/tazpanel.js 3.57 3.58 +rm "$sed_script" 3.59 echo
4.1 --- a/styles/default/base.css Thu Aug 20 17:00:19 2015 +0300 4.2 +++ b/styles/default/base.css Mon Aug 24 01:05:08 2015 +0300 4.3 @@ -159,7 +159,7 @@ 4.4 section header form, section footer form { 4.5 display: table-cell; vertical-align: baseline; line-height: 0; width: 1px; white-space: nowrap; 4.6 } 4.7 -section header button, section footer button { margin: 0; } 4.8 +section header button, section header input, section footer button, section footer input { margin: 0; } 4.9 4.10 section header a { float: right; } 4.11
5.1 --- a/styles/default/header.html Thu Aug 20 17:00:19 2015 +0300 5.2 +++ b/styles/default/header.html Mon Aug 24 01:05:08 2015 +0300 5.3 @@ -120,7 +120,7 @@ 5.4 root) icon='data-img="slitaz"'; warn='' 5.5 toggle_url="http://$HTTP_HOST/";; 5.6 *) icon='data-img="user"'; warn="$(_ 'Some features are disabled.')"$'\n' 5.7 - toggle_url="http://$HTTP_HOST/user/";; 5.8 + toggle_url="http://$RANDOM:*@$HTTP_HOST/user/";; 5.9 esac 5.10 5.11 cat <<EOT