Guest User

adblocker.sh for Tomato firmware

a guest
Aug 31st, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.98 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # adblocker.sh - by Todd Stein ([email protected]), Saturday, October 25, 2014
  4. # for use on routers running Tomato firmware
  5.  
  6. # Periodically download lists of known ad and malware servers, and prevents traffic from being sent to them.
  7. # This is a complete rewrite of a script originally written by teffalump (https://gist.github.com/teffalump/7227752
  8.  
  9. HOST_LISTS="
  10.       http://adaway.org/hosts.txt
  11.       http://www.malwaredomainlist.com/hostslist/hosts.txt
  12.       http://www.mvps.org/winhelp2002/hosts.txt
  13.       http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate%5Bday%5D=&startdate%5Bm
  14. "
  15.  
  16. BLOCKLIST=/tmp/adblocker_hostlist
  17. BLACKLIST=/etc/adblocker_blacklist
  18. WHITELIST=/etc/adblocker_whitelist
  19.  
  20. # get script's absolute path and escape spaces
  21. cd "${0%/*}"
  22. SCRIPT_NAME="$PWD/${0##*/}"
  23. SCRIPT_NAME="${SCRIPT_NAME// /' '}"
  24. cd "$OLDPWD"
  25.  
  26. # await internet connectivity before proceeding
  27. until ping -c1 -w3 google.com || ping -c1 -w3 yahoo.com; do
  28.   sleep 5
  29. done &>/dev/null
  30.  
  31. if [[ -f "$BLOCKLIST" ]]; then
  32.   premd5=$(md5sum "$BLOCKLIST" | awk '{print $1}')
  33. fi
  34.  
  35. # initialize block list
  36. > "$BLOCKLIST"
  37.  
  38. # grab blacklisted domains if any have been specified
  39. [ -s "$BLACKLIST" ] && awk '/^[^#]/ { print "0.0.0.0",$1 }' "$BLACKLIST" >> "$BLOCKLIST"
  40.  
  41. # grab host lists from the internet
  42. wget -qO- $HOST_LISTS | sed -rn 's/^(127.0.0.1|0.0.0.0)/0.0.0.0/p' | awk '{ print $1,$2 }' | sort -uk2 >> "$BLOCKLIST"
  43.  
  44. # remove any whitelisted domains from the block list
  45. if [ -s "$WHITELIST" ]; then
  46.   # create a pipe-delimited list of all non-commented words in whitelist
  47.   white_listed_regex=`echo \`grep -o '^[^#]\+' "$WHITELIST"\` | tr ' ' '|'`
  48.   sed -ri "/$white_listed_regex/d" "$BLOCKLIST"
  49. fi
  50.  
  51. # add IPv6 blocking
  52. sed -ri 's/([^ ]+)$/\1\n::      \1/' "$BLOCKLIST"
  53.  
  54. # Add script to first empty custom scheduler if not already present
  55. nvram show 2>/dev/null | grep sch | grep -Fq "$SCRIPT_NAME"
  56. if [[ $? -ne "0" ]]; then
  57.   # Find an empty scheduler, we have up to five
  58.   for i in $(seq 1 5); do
  59.     if [[ -z "$(nvram get sch_c$i\_cmd)" ]]; then
  60.       tsch=$i
  61.       break
  62.     fi
  63.   done
  64.   if [[ -n "$tsch" ]]; then
  65.     # Set target scheduler to run daily at 3 am.
  66.     nvram set sch_c$tsch\_cmd="/bin/sh $SCRIPT_NAME"
  67.     nvram set sch_c$tsch=1,180,127
  68.     # Restart the scheduler
  69.     service sched restart >/dev/null
  70.   fi
  71. fi
  72.  
  73. # Add block list to dnsmasq config if not already present
  74. dnsmsq=$(nvram get dnsmasq_custom)
  75. echo "$dnsmsq" | grep -Fq "$BLOCKLIST"
  76. if [[ $? -ne "0" ]]; then
  77.   # This can be a multi-line value, easier to dump it to a file and manipulate from there
  78.   echo "$dnsmsq" > /tmp/$$.tmp
  79.   echo "addn-hosts=$BLOCKLIST" >> /tmp/$$.tmp
  80.   nvram set dnsmasq_custom="$(cat /tmp/$$.tmp)" && rm -f /tmp/$$.tmp
  81. fi
  82.  
  83. # Restart dnsmasq if we need to
  84. postmd5=$(md5sum "$BLOCKLIST" | awk '{print $1}')
  85. if [[ "$premd5" != "$postmd5" ]]; then
  86.   # Restart dnsmasq to pickup changes
  87.   service dnsmasq restart >/dev/null
  88. fi
Advertisement
Add Comment
Please, Sign In to add comment