Skip to content

Instantly share code, notes, and snippets.

@pierrebeaucamp
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pierrebeaucamp/dd230a0cab59a86becdd to your computer and use it in GitHub Desktop.
Save pierrebeaucamp/dd230a0cab59a86becdd to your computer and use it in GitHub Desktop.
OpenWRT AdBlock Script
#!/bin/sh
# The MIT License (MIT)
#
# Copyright (c) 2015 Pierre Beaucamp <mail@pierrebeaucamp.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
echo "Author: Pierre Beaucamp <mail@pierrebeaucamp.com>"
echo -e "Version: 1.0.0\n"
echo "Adding Cron entry"
grep -q "/etc/adblock.sh" /etc/crontabs/root ||
echo "0 4 * * * sh /etc/adblock.sh" >> /etc/crontabs/root
echo "Adding Firewall Rules"
FW="iptables -t nat -I PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53"
grep -q "$FW" /etc/firewall.user ||
echo -e "\n$FW" >> /etc/firewall.user &&
FIREWALL_EDITED="1"
FW="iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53"
grep -q "$FW" /etc/firewall.user ||
echo "$FW" >> /etc/firewall.user &&
FIREWALL_EDITED="1"
echo "Updating DHCP Config"
uci get dhcp.@dnsmasq[0].addnhosts > /dev/null 2>&1 ||
uci add_list dhcp.@dnsmasq[0].addnhosts=/etc/block.hosts &&
uci commit &&
DNSMASQ_EDITED="1"
echo "Deleting the old block.hosts file"
if [ -s "/etc/block.hosts" ]
then
rm /etc/block.hosts
fi
echo "Downloading the host lists"
wget -qO- http://www.malwaredomainlist.com/hostslist/hosts.txt >> /tmp/hosts0
wget -qO- http://someonewhocares.org/hosts/hosts >> /tmp/hosts0
echo "Adding black list"
if [ -s "/etc/black.list" ]
then
awk '/^[^#]/ { print "0.0.0.0",$1 }' /etc/black.list >> /tmp/hosts0
fi
echo "Adding white list"
if test -s /etc/white.list
then
egrep -v "^[[:space:]]*$" /etc/white.list |
awk '/^[^#]/ {sub(/\r$/,"");print $1}' |
grep -vf - /tmp/hosts0 > /etc/block.hosts
else
cat /tmp/hosts0 > /etc/block.hosts
fi
echo "Formatting hosts list"
sed -i 's/\r$//' /etc/block.hosts
sed -i '2,$s/127.0.0.1/0.0.0.0/g; s/[[:space:]]*#.*$//g;' /etc/block.hosts
sed -i -re 's/^(0\.0\.0\.0) (.*)$/\1 \2\n:: \2/g' /etc/block.hosts
echo "Deleting temporary list"
rm -f /tmp/hosts0
echo "Reloading Firewall"
if [ "$FIREWALL_EDITED" == "1" ]
then
/etc/init.d/firewall restart > /dev/null 2>&1
fi
echo "Reloading DNSMasq"
if [ "$DNSMASQ_EDITED" == "1" ]
then
killall -HUP dnsmasq
else
/etc/init.d/dnsmasq restart
fi
echo "Finished"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment