Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 26.12 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Adapted from adblock web script originated by HunterZ, with additions from AndreDVJ, Almaz and probably others
  4. # http://www.linksysinfo.org/index.php?threads/script-clean-lean-and-mean-adblocking.68464/page-7#post-248495
  5. #
  6. # This version must be called via adblock.sh, and assumes it will inherit many
  7. # settings from adblock.  The result should be a simple drop-in script without
  8. # needing any modifications.
  9. #
  10. # See adblock.readme for release notes
  11.  
  12. # values inherited from adblock
  13. # =============================
  14. # $adblockscript    main adblock script
  15. # $binprefix        path to main adblock script
  16. # $blacklist        blacklist file
  17. # $blocklist        blocklist file
  18. # $chain        iptables chain used for firewall rules
  19. # $config       adblock config file
  20. # $dnsmasq_config   dnsmasq.conf file
  21. # $hostlink     HOST mode hosts file
  22. # $listprefix       path to transient files - blocklist, source files, etc. Should be the same as prefix unless RAMLIST=1
  23. # $modehost     use to test adblock mode - nslookup $LISTMODE.$modehost
  24. # $pixelbin     pixelserv executable
  25. # $prefix       path to permanent adblock files -  whitelist, blacklist
  26. # $redirip      ip used for pixelserv
  27. # $release      adblock script version
  28. # $testhost     use to test if adblock is loaded - nslookup $testhost
  29. # $weblink      symlink for web interface
  30. # $webscript        script for web interface  (this script)
  31. # $whitelist        whitelist file
  32. # $FWBRIDGE     interfaces allowed to access pixelserv
  33. # $PIXEL_IP     last octect of generated pixelserv ip - "0" means pixelserv disabled in adblock.
  34. # $LISTMODE     adblock listmode - LEGACY/OPTIMIZE/HOST
  35. #
  36. # $web*         any variables added to adblock config beginning with "web" will also be exported
  37. #
  38. # $web_refreshtime  page refresh time
  39. # $web_reportlines  how many lines of history to show in reports, default 100
  40. ##
  41.  
  42. # This script's name
  43. scriptname="${weblink##*"/"}"
  44.  
  45. # dnsmasq log location, default to syslog
  46. [ "$(nvram get log_file_custom)" = "1" ] && {
  47.   dnsmasqlog="$(nvram get log_file_path)"
  48. } || dnsmasqlog="/var/log/messages"
  49.  
  50. #use busybox nslookup
  51. alias nslookup=/usr/bin/nslookup
  52.  
  53. readconf() {
  54. loopcheck="$loopcheck ""$1"
  55. for c in $(head -n 100 $1 | sed 's/#.*$//g' | sed -e 's/^[ \t]*//' | egrep "log-queries|log-facility=|conf-file=|local-ttl=")
  56. do
  57.   r="${c#*=}"
  58.   case "${c%=*}" in
  59.     log-queries )
  60.       logging=1 ;;
  61.     log-facility )
  62.       facility="$r" ;;
  63.     local-ttl )
  64.       ttl="$r" ;;
  65.     conf-file )
  66.       if ! echo $loopcheck | grep "$r" ; then
  67.         readconf "$r"
  68.       fi
  69.       ;;
  70.   esac
  71. done
  72. }
  73.  
  74. urlDec() {
  75.   echo "$1" | \
  76.     sed 's/+/ /g;s/\%0[dD]//g' | \
  77.     awk '/%/{while(match($0,/\%[0-9a-fA-F][0-9a-fA-F]/)) \
  78.        {$0=substr($0,1,RSTART-1)sprintf("%c",0+("0x"substr($0,RSTART+1,2)))substr($0,RSTART+3);}}{print}'
  79. }
  80.  
  81. setQueryVars() {
  82.   local vars=${*//\*/%2A}
  83.   local var
  84.   for var in ${vars//&/ }; do
  85.     local value="$( urlDec "${var#*=}savenewlines" )"
  86.     value=$( echo -n "$value" | sed "s/'/\'\"'\"\'/g" )
  87.     eval "cgi_${var%=*}='${value%savenewlines}'"
  88.   done
  89. }
  90.  
  91. savefile() {
  92.   local v
  93.   if v="$( (echo -n "$cgi_contents" > "$1") 2>&1 )" ; then
  94.     echo -n SUCCESS
  95.   else
  96.     echo "ERROR: $v"
  97.   fi
  98. }
  99.  
  100. appendfile() {
  101.   local v
  102.   if grep -q '^'"$cgi_contents"'$' $1 ; then
  103.     echo -n SUCCESS
  104.   else
  105.     if v="$( (echo "$cgi_contents" >> "$1") 2>&1 )" ; then
  106.       echo -n SUCCESS
  107.     else
  108.       echo "ERROR: $v"
  109.     fi
  110.   fi
  111. }
  112.  
  113. pagescript() {
  114. if [ "$edit" = "1" -o "$edit" = "2" ]; then
  115. cat << EOF
  116.  
  117.   function disableInput(disabled) {
  118.     var inputs = document.getElementsByTagName("a");
  119.     for (var i = 0; i < inputs.length; i++) {
  120.       inputs[i].disabled = disabled;
  121.     }
  122.     var inputs = document.getElementsByTagName("input");
  123.     for (var i = 0; i < inputs.length; i++) {
  124.       inputs[i].disabled = disabled;
  125.     }
  126.     var selects = document.getElementsByTagName("select");
  127.     for (var i = 0; i < selects.length; i++) {
  128.       selects[i].disabled = disabled;
  129.     }
  130.     var textareas = document.getElementsByTagName("textarea");
  131.     for (var i = 0; i < textareas.length; i++) {
  132.       textareas[i].disabled = disabled;
  133.     }
  134.     var buttons = document.getElementsByTagName("button");
  135.     for (var i = 0; i < buttons.length; i++) {
  136.       buttons[i].disabled = disabled;
  137.     }
  138.   }
  139.  
  140.   function saveText() {
  141.     var xhr = new XMLHttpRequest();
  142.     var urlEncodedData = "";
  143.     var urlEncodedDataPairs = [];
  144.     var filename = this.id.replace("btn_", "");
  145.     var contents = document.getElementById(filename).value;
  146.     var msgElement = document.getElementById("msg_" + filename);
  147.     var msgTimeout = 7000;
  148.     if ( transformSupport() ) { msgTimeout=1000 };
  149.  
  150.     disableInput(true);
  151.     msgElement.innerHTML="...saving " + filename + "...";
  152.     msgElement.className="busy";
  153.  
  154.     urlEncodedDataPairs.push( encodeURIComponent("action") + "=" + encodeURIComponent("savefile") );
  155.     urlEncodedDataPairs.push( encodeURIComponent("filename") + "=" + encodeURIComponent(filename) );
  156.     urlEncodedDataPairs.push( encodeURIComponent("contents") + "=" + encodeURIComponent(contents) );
  157.  
  158.     urlEncodedData = urlEncodedDataPairs.join("&").replace(/%20/g, "+");
  159.  
  160.     xhr.onreadystatechange=function() {
  161.       if ( xhr.readyState==4 ) {
  162.         if ( xhr.status==200 && xhr.responseText=="SUCCESS") {
  163.           msgElement.innerHTML = "file saved"
  164.           msgElement.className = "success" ;
  165.           document.getElementById(filename).defaultValue=document.getElementById(filename).value;
  166.           setTimeout( function() {msgElement.className="clearmsg"}, msgTimeout )  ;
  167.           restartneeded = 1;
  168.           showRestart();
  169.         } else {
  170.           msgElement.title="ERROR \nState: " + xhr.readyState + "\nStatus: " + xhr.status + \
  171.                            "\nStatus Text:" + xhr.statusText + "\nresponse:\n" + xhr.responseText;
  172.           msgElement.innerHTML="!! ERROR - file not saved !!" ;
  173.           msgElement.className="error" ;
  174.           setTimeout( function() {msgElement.className="clearmsg"}, msgTimeout )  ;
  175.         }
  176.         disableInput(false) ;
  177.       }
  178.     };
  179.     xhr.open("POST", "$scriptname");
  180.     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  181.     xhr.setRequestHeader("Content-Length", urlEncodedData.length);
  182.     xhr.send(urlEncodedData);
  183.   }
  184.  
  185.   function transformSupport() {
  186.     return  ( "transform" in document.body.style);
  187.   }
  188.  
  189.   function isDirty() {
  190.     if (this.value != this.defaultValue) {
  191.       document.getElementById("msg_" + this.id).innerHTML = "changes are not saved" ;
  192.       document.getElementById("btn_" + this.id).disabled = false ;
  193.     } else {
  194.       document.getElementById("btn_" + this.id).disabled = true ;
  195.       document.getElementById("msg_" + this.id).innerHTML = "" ;
  196.     }
  197.   }
  198.  
  199. EOF
  200. else
  201. cat << EOF
  202.  
  203.   function appendText() {
  204.     var element = this;
  205.     var xhr = new XMLHttpRequest();
  206.     var urlEncodedData = "";
  207.     var urlEncodedDataPairs = [];
  208.     var contents = element.parentNode.getAttribute("data-hostname");
  209.     var filename = element.parentNode.parentNode.getAttribute("data-updatelist");
  210.     urlEncodedDataPairs.push( encodeURIComponent("action") + "=" + encodeURIComponent("appendfile") );
  211.     urlEncodedDataPairs.push( encodeURIComponent("filename") + "=" + encodeURIComponent(filename) );
  212.     urlEncodedDataPairs.push( encodeURIComponent("contents") + "=" + encodeURIComponent(contents) );
  213.  
  214.     urlEncodedData = urlEncodedDataPairs.join("&").replace(/%20/g, "+");
  215.  
  216.     xhr.onreadystatechange=function() {
  217.       if ( xhr.readyState==4 ) {
  218.         if ( xhr.status==200 && xhr.responseText=="SUCCESS") {
  219.             element.parentNode.className="savedline";
  220.             restartneeded = 1;
  221.             showRestart();
  222.             var lines = element.parentNode.parentNode.querySelectorAll(".line");
  223.             for ( var i = 0; i < lines.length; i++ ) {
  224.                if ( lines[i].getAttribute("data-hostname")==element.parentNode.getAttribute("data-hostname") ) { lines[i].className = 'savedline' } ;
  225.             }
  226.         } else {
  227.           element.parentNode.className="errorline";
  228.           alert( "ERROR \nState: " + xhr.readyState + "\nStatus: " + xhr.status + \
  229.                            "\nStatus Text:" + xhr.statusText + "\nresponse:\n" + xhr.responseText );
  230.         }
  231.       }
  232.     };
  233.     xhr.open("POST", "$scriptname");
  234.     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  235.     xhr.setRequestHeader("Content-Length", urlEncodedData.length);
  236.     xhr.send(urlEncodedData);
  237.   }
  238.  
  239.   var count=$REFRESHTIME;
  240.   if (count > 0) {
  241.     var counter=setInterval(timer, 1000);
  242.   }
  243.  
  244.   function timer()
  245.   {
  246.     count=count-1;
  247.     if ( document.getElementById("timer") ) { document.getElementById("timer").innerHTML = count } ;
  248.     if (count <= 0)
  249.     {
  250.       clearInterval(counter);
  251.       window.location = "$scriptname$NEXTACTION";
  252.       return;
  253.     }
  254.   }
  255.  
  256. EOF
  257. fi
  258. }
  259.  
  260. blockedhosts() {
  261.   [ "$REFRESHTIME" != "1" ] && egrep -hB1 "$grepstr .* is $redirip" $dnsmasqlog | \
  262.     egrep 'query.* from ' | grep -v 'from 127.0.0.1' | tail -n $reportlines 2>/dev/null | sed 's|^\(.*:..:..\) .* query\[A] |\1 |' | sort -r | \
  263.     awk '{
  264.           srcip=$6;
  265.           if ( hostnames[srcip] == ""  ) {
  266.             cmd="nslookup "srcip" | awk \47END{print $4}\47";
  267.             cmd | getline hostname;
  268.             close (cmd);
  269.             if ( hostname != "" ) hostnames[srcip] = hostname; else  hostnames[srcip] = srcip;
  270.            }
  271.            printf("<span title=\"%s\" class=\"line\" data-hostname=\"%s\">%s %s %s %-15s %s <span class=\"add\" >[+w]</span></span>\n", hostnames[srcip],$4,$1,$2,$3,srcip,$4)
  272.         }'
  273. }
  274.  
  275. oldresolvedhosts() {
  276.     echo 123
  277.   [ "$REFRESHTIME" != "1" ] &&  grep  "reply .* is .*" $dnsmasqlog | grep -v "NODATA\|NXDOMAIN" | \
  278.      sort -r | sed 's/\(.*:[0-9][0-9]:[0-9][0-9]\).*reply \(.*\) is .*/\1 \2/p' | awk '!a[$4]++' | head -n $reportlines | \
  279.      awk '{printf("<span class=\"line\" data-hostname=\"%s\">%s <span class=\"add\" >[+b]</span></span>\n", $4,$1" "$2" "$3" "$4)}'
  280. }
  281.  
  282. resolvedhosts() {
  283.   [ "$web_oldresolvedhosts" = "1" ] && { oldresolvedhosts ; return; }
  284.   [ "$REFRESHTIME" != "1" ] &&  egrep -hA1 "query\[A] .* from" $dnsmasqlog | sed '/^--$/d;N;s/\n/::::::/;/ from 127.0.0.1/d;/ is [$redirip|NXDOMAIN]/d' | \
  285.     sed 's/\(.*:[0-9][0-9]:[0-9][0-9]\).* query\[A] \(.*\) from \(.*\)::::::.*/\1 \2 \3/p' | \
  286.     sort -r | \
  287.     awk '!a[$4]++' | \
  288.     head -n $reportlines | \
  289.     awk '{
  290.           srcip=$5;
  291.           if ( hostnames[srcip] == ""  ) {
  292.             cmd="nslookup "srcip" | awk \47END{print $4}\47";
  293.             cmd | getline hostname;
  294.             close (cmd);
  295.             if ( hostname != "" ) hostnames[srcip] = hostname; else  hostnames[srcip] = srcip;
  296.            }
  297.            printf("<span title=\"%s\" class=\"line\" data-hostname=\"%s\">%s %s %s %-15s %s <span class=\"add\" >[+b]</span></span>\n", hostnames[srcip],$4,$1,$2,$3,srcip,$4)
  298.         }'
  299. }
  300.  
  301. if [ "$REQUEST_METHOD" = "POST" ]; then
  302.   local file=""
  303.   setQueryVars "$(cat)"
  304.   case "$cgi_filename" in
  305.     blacklist)
  306.       file="$blacklist"
  307.       ;;
  308.     whitelist)
  309.       file="$whitelist"
  310.       ;;
  311.     config)
  312.      file="$config"
  313.       ;;
  314.     *)
  315.       echo "Invalid File - $cgi_filename"
  316.       exit
  317.       ;;
  318.   esac
  319.  
  320.   case "$cgi_action" in
  321.     savefile)
  322.       savefile "$file"
  323.       ;;
  324.     appendfile)
  325.       appendfile "$file"
  326.       ;;
  327.     *)
  328.       echo "Unknown action - $cgi_action"
  329.       ;;
  330.   esac
  331.   exit
  332. fi
  333.  
  334. ttl=0
  335.  
  336. readconf $dnsmasq_config
  337.  
  338. if [ "$facility" != "" ]; then
  339.   dnsmasqlog="$facility"
  340. fi
  341.  
  342. if [ "$LISTMODE" = "HOST" ]; then
  343.   grepstr="$hostlink"
  344. else
  345.   grepstr="config"
  346. fi
  347.  
  348. [ "$PIXEL_IP" != "0" ] && pixmsg="retrieving status...<br>" || pixmsg="adblock is not configured to use pixelserv<br>"
  349.  
  350. if nslookup $LISTMODE.$modehost 2> /dev/null | grep -q $redirip ; then
  351.   liststatus="up"
  352. else
  353.   liststatus="down"
  354. fi
  355.  
  356. blocklistcontents=$(    echo "Mode: $LISTMODE"
  357.             echo "blocklist contents - $blocklist"
  358.             head -n5 $blocklist 2>/dev/null | ( while read line; do echo "    $line"; done )
  359.                 echo "    ..."
  360.                 tail -n2 $blocklist 2>/dev/null | ( while read line; do echo "    $line"; done )
  361.              ) 2> /dev/null
  362.  
  363. ipt=$(iptables -vnL $chain 2> /dev/null)
  364. fwlines=$(iptables -nL $chain 2>/dev/null | wc -l)
  365.  
  366. if iptables -nL | grep -q "$chain .* $redirip" 2>/dev/null  && [ $fwlines -gt 2 ] ; then
  367.   iptstatus="up - $(( fwlines - 2 )) rules"
  368. else
  369.   iptstatus="down"
  370. fi
  371.  
  372. if ps | grep -v grep | grep -q "${pixelbin##*"/"} .*$redirip"; then
  373.   pixstatus="up"
  374. else
  375.   pixstatus="down"
  376. fi
  377.  
  378. if [ "$logging" = "1" ] ; then
  379.   logstatus="up"
  380. else
  381.   logstatus="down"
  382. fi
  383.  
  384. optionshtml="<br><a href=$scriptname?force>force</a>
  385.             <br><a href=$scriptname?start>start/update</a>
  386.             <br><a href=$scriptname?restart>restart</a>
  387.             <br><a href=$scriptname?stop>stop</a>
  388.             <br>
  389.             <br><a href=$scriptname?editlists>edit lists</a>
  390.             <br><a href=$scriptname?editconfig>edit config</a>
  391.            "
  392.  
  393. statushtml="<span title='$blocklistcontents'>
  394.  blocklist: $liststatus
  395.  </span>
  396.  <br><span title=\"$ipt\">iptables: $iptstatus
  397.  </span>
  398.  <br>pixelserv: $pixstatus
  399.  <br>logging: $logstatus
  400.  <br>hosts: $( tail -n1 $blocklist 2>/dev/null | grep -oE "[0-9]*")
  401.  <br>ttl: $ttl
  402. "
  403.  
  404.  
  405. addstatdesc(){
  406. awk  '
  407. BEGIN {
  408. RS="<br>|</body>"
  409. ORS=""
  410. stat["uts"] = "uptime in seconds"
  411. stat["req"] = "connection requests"
  412. stat["avg"] = "average request size in bytes"
  413. stat["rmx"] = "maximum request size in bytes"
  414. stat["tav"] = "average request processing time in milliseconds"
  415. stat["tmx"] = "maximum request processing time in milliseconds"
  416. stat["err"] = "connections resulting in processing errors (syslog may have details)"
  417. stat["tmo"] = "connections that timed out while trying to read a request from the client"
  418. stat["cls"] = "connections that were closed by the client while reading or replying to the request"
  419. stat["nou"] = "requests that failed to include a URL"
  420. stat["pth"] = "requests for a path that could not be parsed"
  421. stat["nfe"] = "requests for a file with no extension"
  422. stat["ufe"] = "requests for an unrecognized/unhandled file extension"
  423. stat["gif"] = "requests for GIF images"
  424. stat["bad"] = "requests for unrecognized/unhandled HTTP methods"
  425. stat["txt"] = "requests for plaintext data formats"
  426. stat["jpg"] = "requests for JPEG images"
  427. stat["png"] = "requests for PNG images"
  428. stat["swf"] = "requests for Adobe Shockwave Flash files"
  429. stat["ico"] = "requests for ICO files (usually favicons)"
  430. stat["ssl"] = "SSL connection requests"
  431. stat["sta"] = "requests for HTML stats"
  432. stat["stt"] = "requests for plaintext stats"
  433. stat["204"] = "requests for /generate_204 URLs"
  434. stat["rdr"] = "requests resulting in a redirect"
  435. stat["pst"] = "requests for HTTP POST method"
  436. stat["hed"] = "requests for HTTP HEAD method"
  437. }
  438.  
  439. /req,.*err/  {
  440.        for (i = 1; i <= NF; i++) {
  441.                s=$i
  442.                sub(",", "", s)
  443.                if ( s in stat )  { ;
  444.                        $(i-1) =  "<span title=\"" stat[s] "\" >" $(i-1) ;
  445.                        $i = $i"</span>" ;
  446.                }
  447.     }
  448.     $0="<br>"$0"</body>"
  449. }
  450.  
  451. { print }
  452.  
  453. ' #end of awk
  454.  
  455. }
  456.  
  457. pixstat() {
  458.   if [ "$PIXEL_IP" = "0" ]; then
  459.      echo $pixmsg
  460.      exit
  461.   fi
  462.   pixmsg="$(wget -q -t 1 -T 5 -O - "http://$redirip/servstats" 2>/dev/null || echo error)"
  463.   if [ "$pixmsg" != "" -a "$pixmsg" != "error" ]; then
  464.     echo "$pixmsg" | addstatdesc
  465.   elif [ "$pixmsg" = "error" ]; then
  466.     echo -n "<HTML><BODY>ERROR: No response from pixelserv."
  467.     if [ "$pixstatus" = "up" ] &&  ! (echo "$FWBRIDGE" | grep -q lo) ; then
  468.       echo "..<br>update config - loopback interface is not included in FWBRIDGE($FWBRIDGE)"
  469.     elif [ "$pixstatus" = "down" ] ; then
  470.       echo "..<br>pixelserv is not runnng on router for $redirip"
  471.     fi
  472.     echo "</BODY></HTML>"
  473.   else
  474.     echo Running version of pixelserv does not support status reporting.
  475.   fi
  476. }
  477. [ "$thisconfig" != "$lastconfig" ] && restartneeded=1
  478. [ "$web_refreshtime" -eq "$web_refreshtime" ] &> /dev/null && REFRESHTIME=$web_refreshtime || REFRESHTIME=120
  479. [ "$web_reportlines" -eq "$web_reportlines" ] &> /dev/null && reportlines=$web_reportlines || reportlines=100
  480. NEXTACTION=""
  481. action=""
  482. actionhtml="<br><i>please wait...</i>"
  483. blockedhosts="<b>logging not enabled - enable in dnsmasq for updated reports.</b>"
  484. resolvedhosts="$blockedhosts"
  485. blockwidth="100%"
  486. divfocus="#f6f6f6"
  487. focus="blocks"
  488.  
  489. case $QUERY_STRING in
  490.   dopixstat)
  491.     pixstat
  492.     exit
  493.   ;;
  494.   force)
  495.     REFRESHTIME=1;
  496.     NEXTACTION="?doforce";
  497.     statushtml="force updating adblock..."
  498.   ;;
  499.   start)
  500.     REFRESHTIME=1;
  501.     NEXTACTION="?dostart";
  502.     statushtml="starting/updating adblock..."
  503.   ;;
  504.   restart)
  505.     REFRESHTIME=1;
  506.     NEXTACTION="?dorestart";
  507.     statushtml="restarting adblock..."
  508.   ;;
  509.   stop)
  510.     REFRESHTIME=1;
  511.     NEXTACTION="?dostop";
  512.     statushtml="stopping adblock..."
  513.   ;;
  514.   doforce)
  515.     REFRESHTIME=0;
  516.     statushtml="force updating adblock..."
  517.     action="force"
  518.   ;;
  519.   dostart)
  520.     REFRESHTIME=0;
  521.     statushtml="starting/updating adblock..."
  522.     action="start"
  523.   ;;
  524.   dorestart)
  525.     REFRESHTIME=0;
  526.     statushtml="restarting adblock..."
  527.     action="restart"
  528.   ;;
  529.   dostop)
  530.     REFRESHTIME=0;
  531.     action="stop"
  532.     statushtml="stopping adblock..."
  533.   ;;
  534.   editlists)
  535.     edit="1"
  536.     blacklist_text="$(cat $blacklist 2> /dev/null; echo -n x)"
  537.     blacklist_text="${blacklist_text%x}"
  538.     whitelist_text="$(cat $whitelist 2> /dev/null; echo -n x)"
  539.     whitelist_text="${whitelist_text%x}"
  540.     focus="blacklist"
  541.     divfocus="inherit"
  542.     blockwidth="50%"
  543.     REFRESHTIME=0;
  544.     actionhtml=$optionshtml
  545.   ;;
  546.   editconfig)
  547.     edit="2"
  548.     config_text="$(cat $config 2> /dev/null; echo -n x)"
  549.     config_text="${config_text%x}"
  550.     focus="config"
  551.     divfocus="inherit"
  552.     blockwidth="100%"
  553.     REFRESHTIME=0;
  554.     actionhtml=$optionshtml
  555.   ;;
  556.   *)
  557.     blockwidth="50%"
  558.     actionhtml=$optionshtml
  559.     [ "$logging" = "1" ] && blockedhosts="recently blocked hosts:"
  560.     [ "$logging" = "1" ] && resolvedhosts="recently resolved hosts:"
  561.   ;;
  562. esac
  563.  
  564. [ "$NEXTACTION$action" != "" ] && restartneeded=0
  565.  
  566. if [ $REFRESHTIME -gt 0 ]; then
  567.   refreshhtml="page will automatically <a href=\"$scriptname\">refresh</a> in <span id=\"timer\">$REFRESHTIME</span> seconds"
  568. else
  569.   refreshhtml="<a href=\"$scriptname\">adblock home</a>"
  570. fi
  571.  
  572. cat << EOF
  573. <!DOCTYPE html>
  574. <html>
  575. <head>
  576. <title>adblock status</title>
  577.  
  578. <script>
  579.   $(pagescript)
  580.  
  581.   var timeinfo ;
  582.   var restartneeded ;
  583.  
  584.   function showRestart() {
  585.     if ( restartneeded == 1 ) {
  586.       document.getElementById("configstatus").innerHTML = "<B>Start/Update Adblock</B><br>configuration has changed since adblock was last run..." ;
  587.       document.getElementById("configstatus").style.color = "Red" ;
  588.     }
  589.   }
  590.  
  591.   function lineclick()
  592.   {
  593.     var ele = this;
  594.     if ( ele.className=="line" ) {
  595.       ele.className="clickedline";
  596.       setTimeout( function() {if (ele.className == "clickedline") { ele.className = "line";}} , 2500);
  597.     }
  598.   }
  599.  
  600.   function pixstat()
  601.   {
  602.     var xhr=new XMLHttpRequest();
  603.     xhr.onreadystatechange=function()
  604.     {
  605.       if (xhr.readyState==4 && xhr.status==200)
  606.       {
  607.         document.getElementById("pixstat").innerHTML=xhr.responseText;
  608.       } else if (xhr.readyState==4) {
  609.         document.getElementById("pixstat").innerHTML="ERROR: Could not query status<br>";
  610.       }
  611.     }
  612.     xhr.open("GET","$scriptname?dopixstat",true);
  613.     xhr.send();
  614.   }
  615.  
  616.   window.onload = function()
  617.   {
  618.         timeinfo = document.getElementById("configstatus").innerHTML;
  619.         restartneeded = 0$restartneeded ;
  620.         showRestart();
  621.  
  622.         var textareas = document.getElementsByTagName("textarea");
  623.         for (var i = 0; i < textareas.length; i++) {
  624.           textareas[i].onkeyup = isDirty;
  625.           textareas[i].onblur = isDirty;
  626.         }
  627.  
  628.         var buttons = document.getElementsByTagName("button");
  629.         for (var i = 0; i < buttons.length; i++) {
  630.           buttons[i].disabled = true;
  631.           buttons[i].onclick = saveText;
  632.         }
  633.  
  634.     var elements = document.querySelectorAll(".line");
  635.         for (var i = 0; i < elements.length; i++) {
  636.           elements[i].onclick = lineclick;
  637.         }
  638.  
  639.     var elements = document.querySelectorAll(".add");
  640.         for (var i = 0; i < elements.length; i++) {
  641.           elements[i].onclick = appendText;
  642.           elements[i].title = "add " + elements[i].parentNode.getAttribute("data-hostname") + " to " + elements[i].parentNode.parentNode.getAttribute("data-updatelist");
  643.         }
  644.  
  645.     if (document.getElementById("$focus").focus) { document.getElementById("$focus").focus() };
  646.  
  647.     pixstat() ;
  648.   }
  649.  
  650. </script>
  651.  
  652. <style type="text/css">
  653.  
  654. html {
  655.   background-color: #ffffff;
  656.   height: 100%;
  657. }
  658. body {
  659.   font-size: small;
  660.   font-family: verdana, geneva, sans-serif;
  661.   margin: 0px;
  662.   overflow-y: auto;
  663. }
  664. pre {
  665.   font-family: "Courier New", Courier, monospace;
  666.   font-size: 100%;
  667. }
  668. div {
  669.   outline-width: 0px;
  670.   box-sizing: border-box;
  671.   margin: 0px;
  672.   border-color: #888;
  673.   border-style: solid;
  674.   border-width: 0px;
  675.   padding: 2px;
  676.   display: block;
  677. }
  678. #banner {
  679.   position: fixed;
  680.   width: 100%;
  681.   top: 0px;
  682.   left: 0px;
  683.   hssseight: 150px;
  684.   height: 12em;
  685.   padding: 0px;
  686.   border-bottom-width: 1px;
  687.   overflow: hidden;
  688. }
  689. #status {
  690.   height: 100%;
  691.   float: left;
  692.   width: 15% ;
  693.   min-width: 120px ;
  694.   max-width: 180px ;
  695.   border-right-width: 1px;
  696.   overflow-y: auto;
  697. }
  698. #actions {
  699.   height: 100%;
  700.   float: left;
  701.   width: 15% ;
  702.   min-width: 120px ;
  703.   max-width: 150px ;
  704.   border-right-width: 1px;
  705.   overflow-y: auto;
  706. }
  707. #time {
  708.   height: 100%;
  709.   overflow-y: auto;
  710. }
  711. #blocks {
  712.   position:fixed;
  713.   top: 12em;
  714.   bottom: 0px;
  715.   left: 0px;
  716.   width: $blockwidth;
  717.   overflow: auto;
  718. }
  719. #blocks2 {
  720.   position:fixed;
  721.   top: 12em;
  722.   bottom: 0px;
  723.   right: 0px;
  724.   width: 50%;
  725.   overflow: auto;
  726.  }
  727. #msg_blacklist, #msg_whitelist, #msg_config {
  728.   position:absolute;
  729.   height: 1.7em;
  730.   box-sizing: border-box;
  731.   top: 0px;
  732.   left: 0px;
  733.   right: 0px;
  734.   text-align: center;
  735.   font-weight: bold;
  736. }
  737. #btn_div {
  738.   position: absolute;
  739.   box-sizing: border-box;
  740.   top: 0px;
  741.   right: 0px;
  742. }
  743. #listname {
  744.   position: absolute;
  745.   box-sizing: border-box;
  746.   top: 0px;
  747.   left: 0px;
  748. }
  749. #mytext {
  750.   position:absolute;
  751.   box-sizing: border-box;
  752.   top: 1.6em;
  753.   bottom: 0px;
  754.   left: 0px;
  755.   right: 0px;
  756. }
  757. textarea {
  758.   box-sizing: border-box;
  759.   top: 0px;
  760.   left: 0px;
  761.   resize: none;
  762.   width: 100%;
  763.   height: 99.5%;
  764.   overflow: auto;
  765.   padding: 0.5em;
  766. }
  767. input[type="submit"], button {
  768.   top: 1px;
  769.   padding: 1px 5px 1px 5px;
  770.   position:absolute;
  771.   box-sizing: border-box;
  772.   right: 2px;
  773. }
  774. #whitelist:focus, #blacklist:focus, #config:focus {
  775.   background-color: #f6f6f6;
  776. }
  777. #blocks:focus, #blocks2:focus {
  778.   background-color: $divfocus;
  779. }
  780. .busy {
  781.   background-color: #ffff00;
  782.   transition: background-color .5s ease;
  783. }
  784. .success {
  785.   background-color: #00ff00;
  786.   transition: background-color .5s ease;
  787. }
  788. .error {
  789.   background-color: #ff3333;
  790.   transition: background-color .5s ease;
  791. }
  792. .clearmsg {
  793.   background-color: inherit;
  794.   transition: background-color 7s ease;
  795. }
  796. .line {
  797.   color: inherit;
  798. }
  799. .add {
  800.   display: none;
  801.   cursor: pointer;
  802. }
  803. .savedline {
  804.   color: green;
  805.   font-weight: bold;
  806.   transition: color .5s ease;
  807. }
  808. .errorline {
  809.   color: red;
  810.   font-weight: bold;
  811.   transition: color .5s ease;
  812. }
  813. .line:hover, .errorline:hover, .clickedline {
  814.   color: blue;
  815.   font-weight: bold;
  816. }
  817. .line:hover .add , .errorline:hover .add, .clickedline .add {
  818.   display: inline;
  819. }
  820.  
  821. </style>
  822. </head>
  823.  
  824. <body>
  825. <div id="banner">
  826.   <div id="status">
  827.     <b title="release $release">adblock status:</b><br>
  828.     <span id="statustxt">
  829.       $statushtml
  830.    </span>
  831.   </div>
  832.  
  833.   <div id="actions">
  834.     <b>adblock actions:</b>
  835.     <span id="actiontxt">
  836.       $actionhtml
  837.     </span>
  838.   </div>
  839.  
  840.   <div id="time">
  841.     <span id="configstatus"><b>time info:</b><br>
  842.     $(uptime)</span>
  843.     <br><br><b>pixelserv info:</b>
  844.     <br><span  id="pixstat">$pixmsg</span>
  845.     <br><br>$refreshhtml
  846.   </div>
  847. </div>
  848. EOF
  849.  
  850.  
  851. if [ "$action" != "" ] ; then
  852.   [ "$action" = "start" ] && cmdaction="" || cmdaction=$action
  853.   echo '<div id="blocks"><pre>'
  854.   $adblockscript $cmdaction
  855.   echo "</pre><p></div>
  856.        <script>
  857.          document.getElementById(\"statustxt\").innerHTML=\"$action complete\";
  858.          document.getElementById(\"actiontxt\").innerHTML=\"$(echo $optionshtml)\";
  859.        </script>
  860.     </body></html>"
  861.   exit
  862. fi
  863.  
  864.  
  865. if [ "$edit" = "1" ]; then
  866. echo '
  867.  <div id="blocks">
  868.    <div id="msg_blacklist"></div>
  869.    <div id="listname"><b title="'"$blacklist"'">blacklist</b></div>
  870.    <button type="button" id="btn_blacklist" tabindex="20">save
  871.    </button>
  872.    <div id="mytext">
  873.      <textarea  id="blacklist" wrap="off" tabindex="10" accesskey="b">'"$blacklist_text"'</textarea>
  874.    </div>
  875.  </div>
  876.  
  877.  <div id="blocks2">
  878.    <div id="msg_whitelist"></div>
  879.    <div id="listname"><b title="'"$whitelist"'">whitelist</b></div>
  880.    <button type="button" id="btn_whitelist" tabindex="40">save
  881.    </button>
  882.    <div id="mytext">
  883.      <textarea  id="whitelist" wrap="off" tabindex="30" accesskey="b">'"$whitelist_text"'</textarea>
  884.    </div>
  885.  </div>
  886. </body>
  887. </html>
  888. '
  889. exit
  890. fi
  891.  
  892. if [ "$edit" = "2" ]; then
  893. echo '
  894.  <div id="blocks">
  895.    <div id="msg_config"></div>
  896.    <div id="listname"><b title="'"$config"'">config</b></div>
  897.    <button type="button" id="btn_config" tabindex="20">save
  898.    </button>
  899.    <div id="mytext">
  900.      <textarea  id="config" wrap="off" tabindex="10" accesskey="b">'"$config_text"'</textarea>
  901.    </div>
  902.  </div>
  903. </body>
  904. </html>
  905. '
  906. exit
  907. fi
  908.  
  909. cat << EOF
  910.   <div id="blocks" accesskey="b" tabindex="1">
  911.     <span title="$dnsmasqlog">
  912.     $( [ "$REFRESHTIME" != "1" ] && echo "$blockedhosts") </span>
  913.     <pre data-updatelist="whitelist">$( blockedhosts )
  914.     </pre>
  915.  
  916.   </div>
  917.   <div id="blocks2" accesskey="r" tabindex="1">
  918.     $( [ "$REFRESHTIME" != "1" ] && echo "$resolvedhosts") <br>
  919.     <pre data-updatelist="blacklist">$( resolvedhosts )
  920.     </pre>
  921.   </div>
  922. </body>
  923. </html>
  924.  
  925. EOF
  926.  
  927. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement