Guest User

adblocker.sh - adapted for dd-wrt

a guest
Aug 30th, 2015
1,521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.21 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 OpenWRT 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.  
  10. HOST_LISTS="
  11.        http://adaway.org/hosts.txt
  12.        http://www.malwaredomainlist.com/hostslist/hosts.txt
  13.        http://www.mvps.org/winhelp2002/hosts.txt
  14.        http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate%5Bday%5D=&startdate%5Bmonth%5D=&star
  15. "
  16.  
  17. BLOCKLIST=/tmp/adblocker_hostlist
  18. BLACKLIST=/etc/adblocker_blacklist
  19. WHITELIST=/etc/adblocker_whitelist
  20.  
  21. # get script's absolute path and escape spaces
  22. cd "${0%/*}"
  23. SCRIPT_NAME="$PWD/${0##*/}"
  24. SCRIPT_NAME="${SCRIPT_NAME// /' '}"
  25. cd "$OLDPWD"
  26.  
  27. # await internet connectivity before proceeding (in case rc.local executes this script before connectivity is achieved)
  28. until ping -c1 -w3 google.com || ping -c1 -w3 yahoo.com; do
  29.         sleep 5
  30. done &>/dev/null
  31.  
  32. # initialize block list
  33. >"$BLOCKLIST"
  34.  
  35. # grab blacklisted domains if any have been specified
  36. [ -s "$BLACKLIST" ] && awk '/^[^#]/ { print "0.0.0.0",$1 }' "$BLACKLIST" >>"$BLOCKLIST"
  37.  
  38. # grab host lists from the internet
  39. 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"
  40.  
  41. # remove any whitelisted domains from the block list
  42. if [ -s "$WHITELIST" ]; then
  43.         # create a pipe-delimited list of all non-commented words in whitelist
  44.         white_listed_regex=`echo \`grep -o '^[^#]\+' "$WHITELIST"\` | tr ' ' '|'`
  45.         sed -ri "/$white_listed_regex/d" "$BLOCKLIST"
  46. fi
  47.  
  48. # add IPv6 blocking
  49. sed -ri 's/([^ ]+)$/\1\n::      \1/' "$BLOCKLIST"
  50.  
  51. # # add block list to dnsmasq config if it's not already there
  52. # if ! uci get dhcp.@dnsmasq[0].addnhosts | grep -q "$BLOCKLIST"; then
  53. #         uci add_list dhcp.@dnsmasq[0].addnhosts="$BLOCKLIST" && uci commit
  54. # fi
  55.  
  56. # restart dnsmasq service
  57. # /etc/init.d/dnsmasq restart
  58. kill -HUP `pidof dnsmasq`
  59.  
  60. # # carefully add script to /etc/rc.local if it's not already there
  61. # if ! grep -Fq "$SCRIPT_NAME" /etc/rc.local; then
  62. #         # using awk and cat ensures that no symlinks (if any exist) are clobbered by BusyBox's feature-poor sed.
  63. #         awk -v command="$SCRIPT_NAME" '
  64. #                 ! /^exit( 0)?$/ {
  65. #                         print $0
  66. #                 }
  67. #                 /^exit( 0)?$/ {
  68. #                         print command "\n" $0
  69. #                         entry_added=1
  70. #                 }
  71. #                 END {
  72. #                         if (entry_added != 1) {
  73. #                                 print command
  74. #                         }
  75. #                 }' /etc/rc.local >/tmp/rc.local.new
  76. #         cat /tmp/rc.local.new >/etc/rc.local
  77. #         rm -f /tmp/rc.local.new
  78. # fi
  79.  
  80. # add script to root's crontab if it's not already there
  81. grep -Fq "$SCRIPT_NAME" /tmp/cron.d/cron_jobs 2>/dev/null || cat >>/tmp/cron.d/cron_jobs <<-:EOF:
  82.         # Download updated ad and malware server lists every Tuesday at 3 AM
  83.         0 3 * * 2 /bin/sh $SCRIPT_NAME
  84. :EOF:
Advertisement
Add Comment
Please, Sign In to add comment