Advertisement
Guest User

adblock merlin -- USB example

a guest
Aug 31st, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.38 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # services-start by mark3748 August 30, 2015
  4. # original script by Todd Stein (toddbstein@gmail.com), Saturday, October 25, 2014 -- https://github.com/tablespoon/fun/blob/master/adblocker.sh
  5. # modified for use on ASUS-WRT routers running Merlin firmware
  6.  
  7. # Periodically download lists of known ad and malware servers, and prevents traffic from being sent to them.
  8. # This is a complete rewrite of a script originally written by teffalump (https://gist.github.com/teffalump/7227752).
  9.  
  10.  
  11.  
  12. HOST_LISTS="
  13.     http://adaway.org/hosts.txt
  14.     http://www.malwaredomainlist.com/hostslist/hosts.txt
  15.     http://www.mvps.org/winhelp2002/hosts.txt
  16.     http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate%5Bday%5D=&startdate%5Bmonth%5D=&star
  17. "
  18.  
  19. BLOCKLIST=/tmp/mnt/My_Passport/ADBLOCK/adblocker_hostlist
  20. BLACKLIST=/tmp/mnt/My_Passport/ADBLOCK/adblocker_blacklist
  21. WHITELIST=/tmp/mnt/My_Passport/ADBLOCK/adblocker_whitelist
  22.  
  23. # get script's absolute path and escape spaces
  24. cd "${0%/*}"
  25. SCRIPT_NAME="$PWD/${0##*/}"
  26. SCRIPT_NAME="${SCRIPT_NAME// /' '}"
  27. cd "$OLDPWD"
  28.  
  29. # await internet connectivity before proceeding (in case rc.local executes this script before connectivity is achieved)
  30. until ping -c1 -w3 google.com || ping -c1 -w3 yahoo.com; do
  31.     sleep 5
  32. done &>/dev/null
  33.    
  34. # initialize block list
  35. >"$BLOCKLIST"
  36.    
  37. # grab blacklisted domains if any have been specified
  38. [ -s "$BLACKLIST" ] && awk '/^[^#]/ { print "0.0.0.0",$1 }' "$BLACKLIST" >>"$BLOCKLIST"
  39.    
  40. # grab host lists from the internet
  41. wget -qO- $HOST_LISTS | grep -w ^0.0.0.0 | sed $'s/\r$//' | sort -u > "$BLOCKLIST"
  42.  
  43. # remove any whitelisted domains from the block list
  44. if [ -s "$WHITELIST" ]; then
  45.     # create a pipe-delimited list of all non-commented words in whitelist
  46.     white_listed_regex=`echo \`grep -o '^[^#]\+' "$WHITELIST"\` | tr ' ' '|'`
  47.     sed -ri "/$white_listed_regex/d" "$BLOCKLIST"
  48. fi
  49.  
  50. # add IPv6 blocking
  51. sed -ri 's/([^ ]+)$/\1\n::      \1/' "$BLOCKLIST"
  52.  
  53. # add block list to dnsmasq config
  54. cp $BLOCKLIST  /jffs/dnsmasq.adblock.conf
  55. echo "#adblocking" > /tmp/mnt/My_Passport/ADBLOCK/dnsmasq.conf.add
  56. echo "address=/0.0.0.0/0.0.0.0" >> /jffs/configs/dnsmasq.conf.add
  57. echo "ptr-record=0.0.0.0.in-addr.arpa,0.0.0.0" >> /jffs/configs/dnsmasq.conf.add
  58. echo "addn-hosts=/tmp/mnt/My_Passport/ADBLOCK/dnsmasq.adblock.conf" >> /jffs/configs/dnsmasq.conf.add
  59.  
  60. # restart dnsmasq service
  61. service restart_dnsmasq
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement