Skip to content

Instantly share code, notes, and snippets.

@iGlitch
Last active October 4, 2022 13:52
Show Gist options
  • Save iGlitch/6d5f45eea140dc4ed3de403d413a39ff to your computer and use it in GitHub Desktop.
Save iGlitch/6d5f45eea140dc4ed3de403d413a39ff to your computer and use it in GitHub Desktop.
divblock mod for OpenWRT dnsmasq Adblocking
#!/bin/sh /etc/rc.common
#License: GPL-2.0-or-later
#Original by Tad @ https://divested.dev/unofficial-openwrt-builds/mvebu-linksys/patches/work/divblock.sh
#This mod adds up to five hosts for a bit more granular control
#on which hosts you would like to use, in case the original host
#list is too strict for your use case.
START=99
USE_PROCD=1
#add hosts here
DIVBLOCK_HOSTS="https://dnsmasq.oisd.nl";
DIVBLOCK_HOSTS2="";
DIVBLOCK_HOSTS3="";
DIVBLOCK_HOSTS4="";
DIVBLOCK_HOSTS5="";
DIVBLOCK_OUTPUT="/tmp/dnsmasq.d/divblock.conf.tmp"; #where the blocked domains will save
DIVBLOCK_EXCLUSIONS="/etc/config/whitelist"; #add domains you don't want blocked here
reload_service()
{
stop "$@"
start "$@"
}
start_service()
{
if /etc/init.d/dnsmasq enabled; then
sleep 15; #wait for network and system to settle after boot XXX: ugly
if [ ! -e "$DIVBLOCK_EXCLUSIONS" ]; then touch "$DIVBLOCK_EXCLUSIONS"; fi;
if wget -q $DIVBLOCK_HOSTS $DIVBLOCK_HOSTS2 $DIVBLOCK_HOSTS3 $DIVBLOCK_HOSTS4 $DIVBLOCK_HOSTS5 -O - | grep -v -f "$DIVBLOCK_EXCLUSIONS" > $DIVBLOCK_OUTPUT; then
sed -i -e '/^#/d' "$DIVBLOCK_OUTPUT"; # remove comments
sed -i -e 's/#//' "$DIVBLOCK_OUTPUT"; # just remove hashtags end of each line (saves a bit of space)
sed -i -e '/^$/d' "$DIVBLOCK_OUTPUT"; # remove empty lines
cat "$DIVBLOCK_OUTPUT" | sort | uniq >> /tmp/dnsmasq.d/divblock.conf # check and remove possible dupes
rm "$DIVBLOCK_OUTPUT" #remove tmp file so that we don't use 2x space
echo "downloaded";
/etc/init.d/dnsmasq restart;
echo "restarted dnsmasq";
else
echo "failed to download";
fi;
else
echo "dnsmasq is disabled, not starting";
fi;
}
stop_service()
{
if rm $DIVBLOCK_OUTPUT &>/dev/null; then echo "deleted"; fi;
if /etc/init.d/dnsmasq running; then
/etc/init.d/dnsmasq restart;
echo "restarted dnsmasq";
else
echo "dnsmasq stopped, not restarting";
fi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment