Skip to content

Instantly share code, notes, and snippets.

@someon
Last active July 1, 2018 02:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save someon/9609363 to your computer and use it in GitHub Desktop.
Save someon/9609363 to your computer and use it in GitHub Desktop.
adblock for OpenWrt
#!/bin/sh /etc/rc.common
START=90
H1='http://www.malwaredomainlist.com/hostslist/hosts.txt'
H2='http://winhelp2002.mvps.org/hosts.txt'
H3='http://sysctl.org/cameleon/hosts.win'
H4='http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=1&mimetype=plaintext'
WWW2=/var/www2
GIF=blank.gif
LAN_IP=$(uci get network.lan.ipaddr)
LAN2=br-lan:1
LAN2_IP=10.3.5.3
H_TMP=/tmp/hosts.tmp
H=/var/hosts/hosts.bad
DM=/etc/init.d/dnsmasq
UH=/etc/init.d/uhttpd
UH_CONF=/etc/config/uhttpd
ssd=start-stop-daemon
UPID=/var/run/uhttpd_www2.pid
start() {
if ping -c 1 `wget -qO- http://ipecho.net/plain`; then
case $LAN_IP in 10.3.*) LAN2_IP=10.4.6.4 ;; esac
if ! ifconfig |grep $LAN2 > /dev/null 2>&1; then
ifconfig $LAN2 inet $LAN2_IP netmask 255.255.255.255
fi
sed -i "s/0\.0\.0\.0/$LAN_IP/;s/$LAN2_IP/$LAN_IP/" $UH_CONF
if [ ! -d $WWW2 ]; then
mkdir -p $WWW2
fi
if [ ! -f $WWW2/$GIF ]; then
echo -ne 'GIF89a1010\x8000\xff\xff\xff000!\xf9\x0400000,000010100\x02\x02D10;' | tr 01 '\000\001' > $WWW2/$GIF
fi
if ! ps |grep uhttpd |grep $WWW2 > /dev/null 2>&1; then
$ssd -Sqbmp $UPID -x uhttpd -- -f -h $WWW2 -E /$GIF -I $GIF -p $LAN2_IP:80
if ! ps |grep uhttpd |grep $WWW2 > /dev/null 2>&1; then
$UH restart
$ssd -Sqbmp $UPID -x uhttpd -- -f -h $WWW2 -E /$GIF -I $GIF -p $LAN2_IP:80
fi
fi
wget -qO- $H1 >> $H_TMP
wget -qO- $H2 >> $H_TMP
wget -qO- $H3 >> $H_TMP
wget -qO- $H4 >> $H_TMP
sed -i '/localhost/d;s/^0\.0\.0\.0.//;s/^127\.0\.0\.1.//;s/[[:cntrl:]]$//;s/[[:cntrl:]]$/ /;/^#/d;/^$/d;s/[[:space:]]*#.*$//;s/^ //;s/^ //;s|^|'$LAN2_IP' |' $H_TMP
rm -f $H
sort $H_TMP|uniq > $H
rm -f $H_TMP
$DM restart
sleep 6h && start
else
sleep 10 && start >> /dev/null 2>&1 &
fi >> /dev/null 2>&1 &
}
stop() {
if ps |grep uhttpd |grep $WWW2 > /dev/null 2>&1; then
$ssd -Kqs HUP -p $UPID
rm -f $UPID
rm -rf $WWW2
fi
if ifconfig |grep $LAN2 > /dev/null 2>&1; then
ifconfig $LAN2 down
fi
if [ -f $H ]; then
rm -f $H
$DM restart
fi
for KILLPID in `ps | grep 'adblock' | awk ' { print $1;}'`; do
kill -9 $KILLPID;
done
}
@zero13th
Copy link

I use luci-ssl package. but to manage the router (both remote WAN and LAN), I only use the https protocol
Can you change the script so that only http protocol is replaced by $LAN_IP?

@magarto
Copy link

magarto commented Mar 2, 2015

Is there any way to include a "white list" in a file? I often use websites like adf.ly and I cannot download from them.
Thanks

@magarto
Copy link

magarto commented Mar 2, 2015

And one additional question... how can I restore the default files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment