Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
5,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 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.  
  59. # carefully add script to /etc/rc.local if it's not already there
  60. if ! grep -Fq "$SCRIPT_NAME" /etc/rc.local; then
  61. # using awk and cat ensures that no symlinks (if any exist) are clobbered by BusyBox's feature-poor sed.
  62. awk -v command="$SCRIPT_NAME" '
  63. ! /^exit( 0)?$/ {
  64. print $0
  65. }
  66. /^exit( 0)?$/ {
  67. print command "\n" $0
  68. entry_added=1
  69. }
  70. END {
  71. if (entry_added != 1) {
  72. print command
  73. }
  74. }' /etc/rc.local >/tmp/rc.local.new
  75. cat /tmp/rc.local.new >/etc/rc.local
  76. rm -f /tmp/rc.local.new
  77. fi
  78.  
  79. # add script to root's crontab if it's not already there
  80. grep -Fq "$SCRIPT_NAME" /etc/crontabs/root 2>/dev/null || cat >>/etc/crontabs/root <<-:EOF:
  81. # Download updated ad and malware server lists every Tuesday at 3 AM
  82. 0 3 * * 2 /bin/sh $SCRIPT_NAME
  83. :EOF:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement