Skip to content

Instantly share code, notes, and snippets.

@aarmot
Last active September 7, 2016 18:04
Show Gist options
  • Save aarmot/5730468 to your computer and use it in GitHub Desktop.
Save aarmot/5730468 to your computer and use it in GitHub Desktop.
Adblock skript for OpenWrt. Tested on Attitude Adjustment 12.09
#!/bin/sh /etc/rc.common
# Copy this skript to /etc/init.d/adblock and enable:
# chmod +x /etc/init.d/adblock
# /etc/init.d/adblock enable
START=90
ADS='http://pgl.yoyo.org/as/serverlist.php?hostformat=dnsmasq&showintro=0&mimetype=plaintext'
WWW2="/tmp/www2"
GIF1="1.gif"
DM_CONF=/var/etc/dnsmasq.conf
DM_BAD=/var/etc/dnsmasq.bad
ssd=start-stop-daemon
UPID1=/var/run/uhttpd_main.pid
UPID2=/var/run/uhttpd_www2.pid
UH=/usr/sbin/uhttpd
start() {
mkdir -p $WWW2
echo -ne 'GIF89a1010\x8000\xff\xff\xff000!\xf9\x0400000,000010100\x02\x02D10;' | tr 01 '\000\001' > $WWW2/$GIF1
# Choose an unused IP
ip=$(uci get network.lan.ipaddr)
ip2=10.3.5.3
case $ip in 10.3.*) ip2=10.4.6.4 ;; esac
ifconfig br-lan:1 inet $ip2 netmask 255.255.255.255
# Restart uhttpd to listen only LAN
if [ -f "$UPID1" ]; then
uargs="$(cat /proc/$(cat $UPID1)/cmdline | tr '\000' ' ' | cut -d ' ' -f 2- )"
if echo "$uargs" | grep -q '0.0.0.0'; then
uargs=$(echo $uargs | sed -e "s/0\.0\.0\.0/$ip/")
$ssd -Kqs HUP -p $UPID1
sleep 1
$ssd -Sqbmp $UPID1 -x $UH -- $uargs
fi
fi
# Start another uhttpd
$ssd -Sqbmp $UPID2 -x $UH -- -f -h $WWW2 -E /$GIF1 -I $GIF1 -p $ip2:80
# Add rules to dnsmasq
wget -qO $DM_BAD "$ADS" && sed -i -e "s/127\.0\.0\.1/$ip2/" $DM_BAD
if [ -f $DM_CONF -a -f $DM_BAD ] && ! grep -q $DM_BAD $DM_CONF; then
$ssd -Kqn dnsmasq
sleep 1
echo "conf-file=$DM_BAD" >> $DM_CONF
$ssd -Sqx dnsmasq -- -C $DM_CONF
fi
}
stop() {
$ssd -Kqs HUP -p $UPID2
}
@someon
Copy link

someon commented Mar 17, 2014

Thanks!
Here is my suggestion https://gist.github.com/someon/9609363
This script run in background and updates bad hosts list every 6 hours.

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