Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
2,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.16 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # adblocker.sh - by Todd Stein (toddbstein@gmail.com), 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. 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 (in case rc.local executes this script before connectivity is achie
  27. until ping -c1 -w3 google.com || ping -c1 -w3 yahoo.com; do
  28.         sleep 5
  29. done &>/dev/null
  30.  
  31. # initialize block list
  32. >"$BLOCKLIST"
  33.  
  34. # grab blacklisted domains if any have been specified
  35. [ -s "$BLACKLIST" ] && awk '/^[^#]/ { print "0.0.0.0",$1 }' "$BLACKLIST" >>"$BLOCKLIST"
  36.  
  37. # grab host lists from the internet
  38. 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 >>"$BLOCKLIS
  39.  
  40. # remove any whitelisted domains from the block list
  41. if [ -s "$WHITELIST" ]; then
  42.        # create a pipe-delimited list of all non-commented words in whitelist
  43.        white_listed_regex=`echo \`grep -o '^[^#]\+' "$WHITELIST"\` | tr ' ' '|'`
  44.        sed -ri "/$white_listed_regex/d" "$BLOCKLIST"
  45. fi
  46.  
  47. # add IPv6 blocking
  48. sed -ri 's/([^ ]+)$/\1\n::      \1/' "$BLOCKLIST"
  49.  
  50. #### add block list to dnsmasq config if it's not already there
  51. ###if ! uci get dhcp.@dnsmasq[0].addnhosts | grep -q "$BLOCKLIST"; then
  52. ###     uci add_list dhcp.@dnsmasq[0].addnhosts="$BLOCKLIST" && uci commit
  53. ###fi
  54.  
  55. # restart dnsmasq service
  56. ###/etc/init.d/dnsmasq restart
  57. kill -HUP `pidof dnsmasq`
  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" /tmp/cron.d/cron_jobs 2>/dev/null || cat >>/tmp/cron.d/cron_jobs <<-:EOF:
  81.        # Download updated ad and malware server lists every Tuesday at 3 AM
  82.        0 3 * * * /bin/sh $SCRIPT_NAME
  83. :EOF:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement