Adblock for Tomato Routers

我这里只是把这个帖子的信息集中整理一下,原文请参见 http://www.linksysinfo.org/forums/showthread.php?p=369696

Place this script under Administration > Scripts > WAN Up tab, then save and reboot.
You can generate the encoded part in this script with the second script under this one.


## ALL-U-NEED Ad Blocking v3.9b
## http://goo.gl/mhykQ
## Original script by YAQUI
## Updated by ~nephelim~, Syl, jochen, groosh, ng12345, ray123, mstombs
## base64 decoder by Danny Chouinard's

sleep 10

ADB="/tmp/ADBLOCK.sh"
{
cat <$ADB
#!/bin/sh

OPTIMISE="Y"
GETS="1 2 3 4"
TRIM_BEGIN=3
S1="http://pgl.yoyo.org/as/serverlist.php?hostformat=nohtml" #44K
S2="http://mirror1.malwaredomains.com/files/justdomains" #189K
S3="http://www.malwaredomainlist.com/hostslist/hosts.txt" #97K
S4="http://www.mvps.org/winhelp2002/hosts.txt" #620K
S5="http://hosts-file.net/hphosts-partial.asp" #460K
S6="http://hostsfile.mine.nu/Hosts" #2641K
S7="http://support.it-mate.co.uk/downloads/hosts.txt" #3851K

USEWHITELIST="Y" # N/Y/R for remote
WURL="http://example.com/whitelist.txt"
WHITE="intel.com"
BLACK=""

USEPIXELSERV="N"
PXL_IP=192.168.1.2
PXL_EXE="/tmp/pixelserv"
PXL_URL="http://example.com/pixelserv"

UPLOAD="N"
FTP_SERVER="example.com"
FTP_USER=""
FTP_PASS=""
FTP_PORT=21
FTP_PATH="/gen"

ADD_CONF="N"
USEHOSTS="N"
ROUTER="Y"

NIP="0.0.0.0"

ENDF
}

UPDATE="Y"
AUP() {
if [[ "$UPDATE" == "Y" ]] ; then
if [[ "$(cru l | grep AdUpd | cut -d '#' -f2)" != "AdUpd" ]] ; then
cru a AdUpd "0 4 * * * $ADB"
fi
fi
}

#### DO NOT EDIT BELOW ####

b64="openssl enc -base64 -d"
[[ "$(echo WQ==|$b64)" != "Y" ]] && b64="b64"

b64(){
awk 'BEGIN{b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"}
{for(i=1;i<=length($0);i++){c=index(b64,substr($0,i,1));if(c--)
for(b=0;b<6;b++){o=o*2+int(c/32);c=(c*2)%64;if(++obc==8){if(o)
{printf"%c",o}else{system("echo -en \"\\"")}obc=o=0}}}}';}

{
cat <>$ADB
H4sIALPm10wCA60Xa2/aSPC7f8V046shV2PINXcV1JEIj4BKAIFz6SkkFbUXWBVs
ajuPXuh/v9lZGwwkbaReIGJ3dnber60Ozoa2fqCdNbo2s+LF0ppyn2nOeT/Zxnyx
ZFqt120mADfwJ0xr9YbOMIHMgiiOmHbZbHcaCeh+JmLONPwTE7i6Ar2KfOCVDUW4
vobVSsKYnltGsIJpyJdgcjBMU9I2UpDhB58D75uRZ2DbwBjerEA8474WLsCcgI4y
FxbecXY7H0exNhGSca0zyOXhMXOaLlG5dCn1Stckv/Zd3u1/7NBdJTzTL4aNfvtj
ozNsDP5W0vyTEUdhvQLzAXS8+anxsbE5vJ/yGMze5oQWF4OO5s4WgQe/by5JwcVE
2kBM4XNYLBfVUbu/FiS3FF4wgaV44POIh3d7ttnh0+5XIJpztGYJKkD0X0hImwfT
KQ+hWj/t9GofoDEY9AZlcMe+H8QQxeMw3tzW5I/G78ZzYN123054M6kRfsmm3RqZ
9KJbbzTtojYJQhAgfOkZZ1gBLwBF4Dac22ykD3XBQOuXbD3H3VkAOsJXEHEPjGh1
M4vj5VV0fVi2rKsb6/pwlLMKh6O8vhqVVkZea/382iinLubxYnpNyvQgZSrBEfwh
ZdJiseApLYxR1mpU62jaErQcp2+VCqVROPJbmABl0Fu0qQW+z91YBD5aax5EXALZ
SvNdMO/hWKLBu+JKRb2ADkaseR54YiK4V17FIZgesFHI8uQrdJUUgcnckd5JvPw5
5OMv5FAv8Pk2pr2FqexdIlxlEYl1ckLp4vN7RUBR0AkZ9fyKFkgJ7GQXEVLoWXCK
rp3Xpe0xLaPbxeZ45d7GpBh+zEkpD4h3tIOHwuyjJZohVaUYro7Wyu3EaBn8ANzZ
2J/yCCLhuxx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X
lj7tfEgGnvBOYKNMlMbpP1i+ROwUXz3szImYc6WAkv/J46wdmzQBkYU2L0Ujubb3
ToSU7W76p3xEBOGt72MV/lH/wOiTjxtZq1MnY7VOlmvRcD6Qr0T5rMFBTU4yNAjI
siSbBP1SkdF+WkEoOymsk8Ahw6Py9JCVjLT/AAHexXemDwAA
ENDF
}

chmod 775 $ADB
$ADB
AUP

这其中的b64可以产生如下代码:

#!/bin/sh
{
cat <>$GEN.new
done

if [ $UNDEF -eq 1 ]; then rm -f $GEN.last; fi

if [ -f $GEN.last ]; then
MD1=$(md5sum $GEN.last|cut -d " " -f1)
MD2=$(md5sum $GEN.new|cut -d " " -f1)
if [ "$MD1" == "$MD2" ]; then
logger ADBLOCK: no changes since last time, exiting.
rm -f $GEN.new
exit
fi
fi
mv -f $GEN.new $GEN.last
}

TRIM() {
sed -ie '
s/\#.*$//
s/^127\.0\.0\.1[ \t]*//
s/[ \t]*$//
s/^::1[ \t]*//
s/localhost$//
/^$/d' $TMP
}

DS() {
for i in $GETS; do
eval url="\$S$i"
if wget $url -O - | tr -d "\r" > $TMP ; then
if [[ $i -ge $TRIM_BEGIN ]]; then TRIM ; fi
cat $TMP >> $GEN
logger ADBLOCK: $url
else
logger ADBLOCK ERROR: cannot get $url
fi
done
}

TST(){
MD5=$(md5sum $GEN|cut -d " " -f1)
if [[ -f $GEN.md5 ]] && [[ $MD5 == $(cat $GEN.md5) ]]; then
logger ADBLOCK: no changes since last time, exiting.
CLR; exit
else
echo $MD5>$GEN.md5
fi
}

LWHT() {
if [[ "$USEWHITELIST" == "Y" ]]; then
for site in $WHITE
do
sed -i "/$(echo $site|sed 's/\./\\./g')/d" $GEN
done
elif [[ "$USEWHITELIST" == "R" ]]; then
if wget $WURL -O - | tr -d "\r" > $WFILE ; then
logger ADBLOCK: whitelist $WURL
sed -i -e '/\#.*$/ s/\#.*$//' -e '/^$/d' $WFILE
for site in $(cat $WFILE)
do
sed -i "/$(echo $site|sed 's/\./\\./g')/d" $GEN
done
else
logger ADBLOCK ERROR: cannot get whitelist $WURL
fi
fi
echo "$BLACK" |sed 's/[ \t]*/\n/g'|sed '/^$/d' >> $GEN
}

OPT() {
if [[ "$OPTIMISE" == "Y" ]]; then
logger ADBLOCK: sorting hosts...
if [[ "$USEHOSTS" == "Y" ]]; then
sort -u -o $TMP $GEN
else
awk -F '.' 'BEGIN{ORS=""}{for(i=NF;i>0;i--)print $i"#";print "\n"}' $GEN|sort|
awk -F '#' 'BEGIN{ORS="";d = "%"}{if(index($0,d)!=1){d=$0;print $--NF;
for(i=--NF;i>0;i--)print "."$i;print "\n"}}' > $TMP
fi
logger ADBLOCK: hosts sorted.
fi
mv -f $TMP $GEN
}

CNT() {
TOT=$(wc -l > $CONF
cat >> $CONF <> $GEN
fi
}

ADDCFG() {
if [[ "$ADD_CONF" == "Y" ]]; then
if [[ "$USEHOSTS" == "Y" ]]; then
eval "CFG=$CONF"
else
eval "CFG=$GEN"
fi
cat >> $CFG <<EOF
dhcp-authoritative
cache-size=2048
log-async=5
EOF
fi
}

LBLK() {
service dnsmasq stop
if [[ "$USEHOSTS" == "Y" ]]; then
dnsmasq --conf-file=$CONF
else
dnsmasq --conf-file=$GEN
fi
}

FS() {
if ps | grep 'dnsmasq' | grep 'nobody' ; then
logger ADBLOCK: dnsmasq is running
else
logger ADBLOCK ERROR: restarting dnsmasq...
dnsmasq
fi
}

CLR
PXL

NC
DS
TST
LWHT
CNT
OPT
CNT
FTPUP

if [[ "$ROUTER" == "Y" ]]; then
FDNSM
LCFG
ADDCFG
LBLK
FS
fi

CLR

ENDF
}

AdBlock Sources

OPTIMISE="Y"
GETS="1 2 3 4"
TRIM_BEGIN=3
S1="http://pgl.yoyo.org/as/serverlist.php?hostformat=nohtml" #44K
S2="http://www.malwaredomains.com/files/justdomains" #189K
S3="http://www.malwaredomainlist.com/hostslist/hosts.txt" #97K
S4="http://www.mvps.org/winhelp2002/hosts.txt" #620K
S5="http://hosts-file.net/hphosts-partial.asp" #283K
S6="http://hostsfile.mine.nu/Hosts" #2641K
S7="http://support.it-mate.co.uk/downloads/hosts.txt" #3851K

You can choose which hosts files will be downloaded to block ads, update the variable GETS.
In the default setup, Adblock will download S1 to S4.
S6 and S7 require a router with a lot of memory, use it if you know what you’re doing.

A little explanation about TRIM_BEGIN=3:
S1 and S2 are files with hostname only, these files will be copied without formatting.
S3 to S7 are file formatted to replace hosts file, these files will be formatted to works with AdBlock.
TRIM_BEGIN tells AdBlock which sources need formatting (from S3 to the end),
updated it if you add a source, between S2 and S3, which doesn’t need formatting.

You can also add your custom blacklist here. Add a new source and update GETS.

If OPTIMISE=”Y”, AdBlock will remove duplicated entries in the hosts file. See Hosts file Vs. DNS Cache Poisoning for more details.

Whitelist and Blacklist


USEWHITELIST="Y" # N/Y/R for remote
WURL="http://example.com/whitelist.txt"
WHITE="intel.com"
BLACK="facebook.com
facebook.net
fbcdn.net"

You can add inline whitelist and blacklist to the script,
look at the example above to see how to add domains to the blacklist.
If USEWHITELIST=”R”, AdBlock will download the whitelist from the URL specified by WURL.

Pixelserv

If you don’t want to run pixelserv, you can update NIP=”0.0.0.0″ if you want to redirect blocked hosts to another HTTP server.
If you don’t have a HTTP server, don’t modify this variable.

如果想使用Pixelserv,有两种使用方法。

方法一:
You can download pixelserv from this thread.
Instead of redirecting blocked hosts to 0.0.0.0, pixelserv will return a 1×1 pixel.
Pixelserv removes error message in the browser by doing that.
Code:

USEPIXELSERV=”Y”
PXL_IP=192.168.1.2
PXL_EXE=”/tmp/pixelserv”
PXL_URL=”http://example.com/pixelserv&#8221;

Update USEPIXELSERV=”Y” to use it.
PXL_IP is the IP AdBlock will use to redirect hosts to pixelserv, you MUST specify an unused IP.
PXL_EXE is where pixelserv is located or will be downloaded. You can change this location if you want to store it on your usb drive if you have one connected to your router.
If AdBlock can’t find pixelserv to the specified location, it will download it from PXL_URL and save it on the router to PXL_EXE location.
For example, you can store it on dropbox.
You can see how many ads was blocked with this command: kill -SIGHUP $(pidof pixelserv)

方法二

也可以将Pixelserv装在Nvram中。这个方法详见以下链接

在Tomato的webshell(Tools标签)中,运行以下代码(只需一次,这段代码将Pixelserv装入Nvram,重启后也不受影响)。


# pixelserv tiny V19 for TeddyBear builds by mstombs: http://bit.ly/a11SKr
#To set pixelserv path change the PXL variable at the bottom of this script

e="echo";[ $0 == "-sh" ]||eval $(grep -ow "PXL=[^[]*$" $0);setup(){
ENC="H4sIAGBJ00wCAJVYScwMQRR+3WbsS9u3QWMYsYyxJbZE2/ddYglpszSGMTOZblsIY3cQQyJEQuxL
RIztIEI4iLMDNwcSwsGBg1gixveqi25tf3/e/+qr99WrV696uqt7y/hpExRFoW+iUn1ipNU3aDDs
puZE9TSaNZh0qke9qB21pbpE8LEaQp8SCQ1DQ9A60FYKq+EqY+lTXJViCG2sEVSMZy/7Zd9ooVeA
H0HrSr+K/70UVgPqzV9PzjH7hZOhoHj+Pwr7++eyqf65TL81Y9FIx+1CPCFyUhUSto+C3GD31zOo
CUGaGWJcfdUQazgaNkSuI+sY1BB2J/iNYDVoG6JiFGtqTRBZqwSwgn4ohWFVXissEcbDct7NYXvC
toBtB9sSlmN+qdUKReRRg+Xxb2C7Af+v9IAiPsRdWxe5ny2g0QBXhXb14Wa+NufUWta6M7QTtL30
dZC2obSKtE2lbeCuS9S0LdcTGqGfRZe2lbTNpdUC++gX3o/ubv2pnVxvnQCnYwB3o/+TOQpyw16y
aMhKU7w1tqS3tYXs3y/9wFMV8TuTuAF1BY7K8c1R+Qzwbc3jLwngIcCJb3zgtcBPff5igP+M4G+K
+eS+Tg7U1II+9PkfQu/5sA294sP15LUfEbgRvYY94flFrYsNDNpfBxjrywHv9/xin8s+/ID5PvwE
utSHD0Fn+fASqOHD76AJH94F1X34AFTz4Y6BvX7MDZ9fcX/jcn316Rjsmyae/yXX04e3cM18+CI3
gCeRu/5BgflucX19/E9cXx8mM5vPOmQugyHTdG9H5upkNg+UsUrW8qztWCVzWSm52gJ3WQH9v+yd
stacI/vH5pK2bdlkO6V8uriB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UxgjE2yx5qLX8Ngb+L3g7fbxW4LX6DW+vjxcBL/KbeQ/4eDp4+m94h328KHjR
3/BOeDwtAV4CvD2BujD3rI93E7ybv4l3iesm570H3m3w9F/U+SrH896/meeJ55NzSpG8u7/gfQVs
+LAkPBUAAA=="
n="nvram";m="md5sum";if [ "$($e "$ENC"|$m)" != "$($e|$n get pxl|$m)" ]
then $n set pxl="$ENC";$n commit;logger $n pxl updated;rm -f $PXL;fi;}
setup
# after the script ran once you can remove this line and all lines above this

再将以下代码贴入admin->Scripts->Firewall,并重启路由即可。

Advertisements