Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # adblocker.sh - by Todd Stein ([email protected]), Saturday, October 25, 2014
- # for use on routers running Tomato firmware
- # Periodically download lists of known ad and malware servers, and prevents traffic from being sent to them.
- # This is a complete rewrite of a script originally written by teffalump (https://gist.github.com/teffalump/7227752
- HOST_LISTS="
- http://adaway.org/hosts.txt
- http://www.malwaredomainlist.com/hostslist/hosts.txt
- http://www.mvps.org/winhelp2002/hosts.txt
- http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate%5Bday%5D=&startdate%5Bm
- "
- BLOCKLIST=/tmp/adblocker_hostlist
- BLACKLIST=/etc/adblocker_blacklist
- WHITELIST=/etc/adblocker_whitelist
- # get script's absolute path and escape spaces
- cd "${0%/*}"
- SCRIPT_NAME="$PWD/${0##*/}"
- SCRIPT_NAME="${SCRIPT_NAME// /' '}"
- cd "$OLDPWD"
- # await internet connectivity before proceeding
- until ping -c1 -w3 google.com || ping -c1 -w3 yahoo.com; do
- sleep 5
- done &>/dev/null
- if [[ -f "$BLOCKLIST" ]]; then
- premd5=$(md5sum "$BLOCKLIST" | awk '{print $1}')
- fi
- # initialize block list
- > "$BLOCKLIST"
- # grab blacklisted domains if any have been specified
- [ -s "$BLACKLIST" ] && awk '/^[^#]/ { print "0.0.0.0",$1 }' "$BLACKLIST" >> "$BLOCKLIST"
- # grab host lists from the internet
- 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"
- # remove any whitelisted domains from the block list
- if [ -s "$WHITELIST" ]; then
- # create a pipe-delimited list of all non-commented words in whitelist
- white_listed_regex=`echo \`grep -o '^[^#]\+' "$WHITELIST"\` | tr ' ' '|'`
- sed -ri "/$white_listed_regex/d" "$BLOCKLIST"
- fi
- # add IPv6 blocking
- sed -ri 's/([^ ]+)$/\1\n:: \1/' "$BLOCKLIST"
- # Add script to first empty custom scheduler if not already present
- nvram show 2>/dev/null | grep sch | grep -Fq "$SCRIPT_NAME"
- if [[ $? -ne "0" ]]; then
- # Find an empty scheduler, we have up to five
- for i in $(seq 1 5); do
- if [[ -z "$(nvram get sch_c$i\_cmd)" ]]; then
- tsch=$i
- break
- fi
- done
- if [[ -n "$tsch" ]]; then
- # Set target scheduler to run daily at 3 am.
- nvram set sch_c$tsch\_cmd="/bin/sh $SCRIPT_NAME"
- nvram set sch_c$tsch=1,180,127
- # Restart the scheduler
- service sched restart >/dev/null
- fi
- fi
- # Add block list to dnsmasq config if not already present
- dnsmsq=$(nvram get dnsmasq_custom)
- echo "$dnsmsq" | grep -Fq "$BLOCKLIST"
- if [[ $? -ne "0" ]]; then
- # This can be a multi-line value, easier to dump it to a file and manipulate from there
- echo "$dnsmsq" > /tmp/$$.tmp
- echo "addn-hosts=$BLOCKLIST" >> /tmp/$$.tmp
- nvram set dnsmasq_custom="$(cat /tmp/$$.tmp)" && rm -f /tmp/$$.tmp
- fi
- # Restart dnsmasq if we need to
- postmd5=$(md5sum "$BLOCKLIST" | awk '{print $1}')
- if [[ "$premd5" != "$postmd5" ]]; then
- # Restart dnsmasq to pickup changes
- service dnsmasq restart >/dev/null
- fi
Advertisement
Add Comment
Please, Sign In to add comment