-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadblock.sh
1156 lines (985 loc) · 29.9 KB
/
adblock.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
## Modified from Clean, Lean and Mean Adblock v4.5 by haarp
##
## http://www.linksysinfo.org/index.php?threads/script-clean-lean-and-mean-adblocking.68464/
##
## Use at your own risk
##
## See adblock.readme for release notes
##
#########################################################
# #
# Static values - these cannot / should not be changed #
# in the config file. #
# #
#########################################################
umask 0022
alias iptables='/usr/sbin/iptables'
alias nslookup='/usr/bin/nslookup'
alias ls='/bin/ls'
alias df='/bin/df'
alias ifconfig='/sbin/ifconfig'
pidfile=/var/run/adblock.pid
release="2015-11-11"
# buffer for log messages in cgimode or firemode
msgqueue=""
# router memory
ram=$(awk '/^MemTotal/ {print int($2/1024)}' < /proc/meminfo)
# this script
me="$(cd $(dirname "$0") && pwd)/${0##*/}"
# Is this run as CGI?
[ -n "$REQUEST_METHOD" ] && cgimode=1
[ "${me##*"."}" = "fire" ] && firemode=1
# path to script - was script called via an autorun link?
if [ "${me##*"."}" = "fire" -o "${me##*"."}" = "wanup" -o "${me##*"."}" = "shut" ]; then
# yes - find true script folder
s="$( ls -l "$me" )"; s="${s##*" -> "}"
binprefix="$(cd "$(dirname "$me")" && cd "$(dirname "$s")" && pwd)"
adblockscript="$binprefix/${s##*"/"}"
islink=1
elif [ -L $me -a "$cgimode" = "1" -a -e $me.weblink ]; then
# called via a link, we are in cgi environment, and weblink file exists
# so follow the link for binprefix location
s="$( ls -l "$me" )"; s="${s##*" -> "}"
binprefix="$(cd "$(dirname "$me")" && cd "$(dirname "$s")" && pwd)"
adblockscript="$binprefix/${s##*"/"}"
islink=1
else
# no - use folder of $me
binprefix="$(dirname "$me")"
adblockscript="$me"
islink=0
fi
# base name to use when looking for config files
configname=adblock
# legacy config file
config=$binprefix/config
# list of config files to look for if legacy file is missing
# use a fully pathed name if custonizing
# first match found wins
ini="$configname.ini"
configlist="$binprefix/$ini
$binprefix/$configname/$ini
/jffs/$configname/$ini
/jffs/etc/$ini
/jffs/$ini
/opt/$configname/$ini
/opt/etc/$ini
/opt/$ini
/mmc/$configname/$ini
/mmc/etc/$ini
/mmc/$ini
/cifs1/$configname/$ini
/cifs1/etc/$ini
/cifs1/$ini
/cifs2/$configname/$ini
/cifs2/etc/$ini
/cifs2/$ini
/tmp/$configname/$ini
/tmp/$ini
"
#########################################################
# End of static values #
#########################################################
#########################################################
# #
# Default values - can be changed in config file. #
# #
#########################################################
# path to list files
prefix=$binprefix
# pixelserv executable
pixelbin=$binprefix/pixelserv
# temp folder for stripped white/blacklist
tmp=/tmp
# what to consider a small disk in MB
smalldisk=64
# what to consider a small tmp folder in MB
smalltmp=24
# firewall autorun script
fire=/etc/config/99.adblock.fire
# wanup autorun script
wanup=/etc/config/99.adblock.wanup
# shutdown autorun script
shut=/etc/config/00.adblock.shut
# hosts file link
hostlink=/etc/dnsmasq/hosts/zzz.adblock.hosts
# virtual interface name, should be unique enough to avoid grep overmatching
vif=adblk
# iptables chain name, should be unique enough to avoid grep overmatching
chain=adblk.fw
# testhost
testhost="adblock.is.loaded"
# modehost
modehost="mode.is.loaded"
# listtmp set to /tmp folder for blocklist generation to reduce writes to jffs/usb if more than 64MB ram
# defaults to previous behavior if less ram, or can be set explicitly in config
listtmp=""
# list of generated source files
sourcelistfile=$tmp/sourcelist.$$.tmp
# default cron schedule standard cru format
schedule="10 02 * * *"
cronid=adblock.update
# minimum age of blocklist in hours before we re-build
age2update=4
# symlink for web interface
weblink=/www/user/adblock.sh
# script for web interface
webscript=adblockweb.sh
# Add Adblock link to Tomato GUI
tomatolink=1
# don't output log for cgi wrapper mode
quietcgi=1
# don't output log for firewall mode
quietfire=1
# path to dnsmasq.conf
dnsmasq_config="/etc/dnsmasq.conf"
# enable logging - a value of "1" will add "log-queries" to $CONF
# and restart dnsmasq if necessary
#
# has no effect if logging is already enabled in dnsmasq.conf
dnsmasq_logqueries=""
# !**** CAUTION ****!
# dnsmasq_custom - use at your own risk
#
# value will be appended to $CONF as entered
#
# example:
# dnsmasq_custom='
# log-facility=/tmp/mylogfile
# log-dhcp
# log-queries
# local-ttl=600
# '
#
# !! do not use unless you know what you are doing !!
#
# dnsmasq is very sensitive and will not start with invalid entries, entries
# that conflict with directives in the primary config, and some duplicated
# entries
#
# no validation of the content is performed by adblock
#
# !**** CAUTION ****!
dnsmasq_custom=""
# additional options for wget
wget_opts=""
# list mode
LISTMODE="OPTIMIZE"
# default to using primary lan interface
BRIDGE="$(nvram get lan_ifname)"
# default to strict firewall rules
FWRULES=STRICT
# default interface(s) for firewall rules
# supports multiple interfaces as well, ie: "br0 br1 br3"
FWBRIDGE="br+ lo"
# set haarp config defaults - config file overrides
# 0: disable pixelserv, 1-254: last octet of IP to run pixelserv on
PIXEL_IP=254
# let system determin pixelserv ip based on PIXEL_IP and existing
redirip=""
# additional options for pixelserv
PIXEL_OPTS=""
# 1: keep blocklist in RAM (e.g. for small JFFS)
RAMLIST=0
# dnsmasq custom config (must be sourced by dnsmasq!) confused? then leave this be!
CONF=/etc/dnsmasq.custom
# whitelist and blacklist contents
BLACKLIST=""
WHITELIST=""
#########################################################
# End of default values #
#########################################################
elog() {
local tag="ADBLOCK[$$]"
local myline
local pad=" "
local len=${2:-"0"}
pad=${pad:0:$len}
local p1=${1:-"-"}
[ "$cgimode" = "1" -o "$firemode" = "1" ] && {
[ "$p1" = "-" ] && {
[ -t 0 ] || while read myline; do msgqueue="$msgqueue""$pad$myline\n" ; done
} || msgqueue="$msgqueue""$pad$p1\n"
} || {
[ "$p1" = "-" ] && {
[ -t 0 ] || while read myline; do logger -st "$tag" "$pad$myline"; done
} || logger -st "$tag" "$pad$p1"
}
}
flushlog() {
# display queue and disable cgi/fire modes
[ "$msgqueue" != "" ] && {
cgimode=0
firemode=0
[ "$msgqueue" != "" ] && echo -ne "$msgqueue" | elog
msgqueue=""
}
}
pexit() {
flushlog
elog "Exiting $me $@"
rm -f "$pidfile" &>/dev/null
logvars2
exit $@
}
logfw() {
elog "iptables"
{ echo -e "filter\n========================================================================"
iptables -vnL
echo -e "\nnat\n========================================================================"
iptables -vnL -t nat
echo -e "\nmangle\n========================================================================"
iptables -vnL -t mangle
} | elog - 4
}
logvars() {
[ "$debug" != "1" ] && return
elog "Running on $( nvram get os_version )"
elog "PID $(ps -w | grep $$ | grep -v grep) SHLVL $SHLVL"
elog "PPID $(ps -w | grep $PPID | grep -v grep)"
elog "Initialized Environment:"
set | elog - 4
elog "Mounted Drives"
mount | elog - 4
elog "Free Space"
df -h | elog - 4
elog "prefix folder - $prefix"
ls -lh $prefix | elog - 4
elog "listprefix folder - $listprefix"
ls -lh $listprefix | elog - 4
elog "listtmp folder - $listtmp"
ls -lh $listtmp | elog - 4
elog "config file contents - $config"
cat $config | elog - 4
logfw
}
logvars2() {
[ "$debug" != "1" ] && return
elog "Environment at exit:"
elog "Free Space"
df -h | elog - 4
elog "prefix folder - $prefix"
ls -lh $prefix | elog - 4
elog "listprefix folder - $listprefix"
ls -lh $listprefix | elog - 4
elog "listtmp folder - $listtmp"
ls -lh $listtmp | elog - 4
elog "blocklist contents - $blocklist"
head $blocklist | elog - 4
elog " ..."
tail -n2 $blocklist | elog - 4
elog "CONF contents - $CONF"
cat $CONF | elog - 4
logfw
}
readdnsmasq() {
[ "$3" != "r" ] && loopcheck=""
loopcheck="$loopcheck ""$1"
for c in $( head -n 100 $1 | sed 's/#.*$//g' | sed -e 's/^[ \t]*//' 2> /dev/null )
do
l="${c%=*}"
r="${c#*=}"
case "$l" in
$2 )
echo "$r"
;;
conf-file )
if ! echo $loopcheck | grep "$r " ; then
readdnsmasq "$r" "$2" "r"
fi
;;
esac
done
}
startserver() {
if [ "$PIXEL_IP" != "0" ]; then
if ! ifconfig | grep -q $BRIDGE:$vif; then
elog "Setting up $rediripandmask on $BRIDGE:$vif"
ifconfig $BRIDGE:$vif $rediripandmask up
fi
if ps -w | grep -v grep | grep -q "${pixelbin##*"/"} $redirip"; then
elog "pixelserv already running, skipping"
else
elog "Setting up pixelserv on $redirip"
"$pixelbin" $redirip $PIXEL_OPTS 2>&1 | elog
fi
# create autorun links
[ -d /etc/config ] || mkdir /etc/config
[ $islink = 0 ] && ln -sf "$me" "$fire"
[ $islink = 0 ] && ln -sf "$me" "$shut"
else
# something odd has happened if we need this, but better safe...
rm -f "$fire" &>/dev/null
rm -f "$shut" &>/dev/null
stopserver
fi
fire
}
stopserver() {
killall pixelserv
ifconfig $BRIDGE:$vif down
} &> /dev/null
rmfiles() {
{
rm -f "$fire"
rm -f "$shut"
rm -f "$hostlink"
rm -f "$tmpstatus"
} &>/dev/null
CONFchanged=0
if [ -e "$CONF" ]; then
local CONFmd51=$(md5sum "$CONF" 2>/dev/null)
echo -n > "$CONF"
local CONFmd52=$(md5sum "$CONF" 2>/dev/null)
if [ "$CONFmd51" = "$CONFmd52" ]; then
elog "CONF file $CONF unchanged"
else
CONFchanged=1
elog "CONF file $CONF truncated"
fi
fi
}
stop() {
elog "Stopping"
rmfiles
stopserver
cleanfire
restartdns
currentmode="OFF"
}
restartdns() {
[ $LISTMODE = "HOST" ] && [ "$logging" = "$dnsmasq_logqueries" ] && [ "$CONFchanged" != "1" ] && {
[ $currentmode = "HOST" -o $currentmode = "OFF" ] && {
elog "Loading hosts file for dnsmasq"
kill -HUP $( pidof dnsmasq )
return
}
}
elog "Restarting dnsmasq"
service dnsmasq restart | elog
}
writeconf() {
[ ! -e "$CONF" ] && echo -n > "$CONF"
local CONFmd51=$(md5sum "$CONF" 2>/dev/null)
echo -n > "$CONF"
if [ ! -f $blocklist -o ! -s $blocklist ]; then
elog "Blocklist Missing or empty - REMOVING DNSMASQ FILES / ADBLOCK MAY BE DISABLED!"
rm -f "$hostlink" &>/dev/null
return
fi
if [ $LISTMODE = "HOST" ] ; then
elog "Creating Hosts File Link $hostlink"
if ! ln -sf "$blocklist" "$hostlink" ; then
elog "Could not create host file link $hostlink"
rm -f "$hostlink" &>/dev/null
return
fi
else
elog "Writing File $CONF"
rm -f "$hostlink" &>/dev/null
echo "conf-file=$blocklist" >> "$CONF"
fi
# enable logging if needed
[ "$dnsmasq_logqueries" = "1" ] && echo "log-queries" >> "$CONF"
# add custom dnsmasq settings
[ "$dnsmasq_custom" != "" ] && echo "$dnsmasq_custom" >> "$CONF"
local CONFmd52=$(md5sum "$CONF" 2>/dev/null)
if [ "$CONFmd51" = "$CONFmd52" ]; then
CONFchanged=0
elog "CONF file $CONF unchanged"
else
CONFchanged=1
elog "CONF file $CONF changed"
fi
}
cleanfiles() {
cru d $cronid
stop
elog "Cleaning files"
rm -f $prefix/lastmod-* &> /dev/null
rm -f $prefix/source-* &> /dev/null
rm -f $tmpstatus &> /dev/null
rm -f $blocklist &> /dev/null
rm -f $weblink &> /dev/null
rm -f $weblink.weblink &> /dev/null
rmtomatolink
elog "The following files remain for manual removal:"
ls -1Ad $me $config $listprefix/* $prefix/* 2>/dev/null| sort -u | elog - 4
}
shutdown() {
rmfiles
stopserver
}
fire() {
cleanfire
# Nothing to do if not running pixelserv
[ "$PIXEL_IP" = "0" ] && return
[ "$FWRULES" = "NONE" ] && return
[ $(( $(nvram get log_in) & 1 )) = 1 ] && {
drop=logdrop
logreject=1
limit=$(nvram get log_limit)
[ $limit = 0 ] && limitstr="" || limitstr=" -m limit --limit $limit/m "
} || {
drop=DROP
logreject=0
}
[ $(( $(nvram get log_in) & 2 )) = 2 ] && accept=logaccept || accept=ACCEPT
vpnline=$( iptables --line-numbers -vnL INPUT | grep -Em1 "ACCEPT .* all.*(tun[0-9]|tap[0-9]).*0.0.0.0.*0.0.0.0/0" | cut -f 1 -d " ")
stateline=$(iptables --line-numbers -vnL INPUT | grep -m1 "ACCEPT.*state.*RELATED,ESTABLISHED" | cut -f 1 -d" ")
[ "$vpnline" != "" ] && [ "$vpnline" -lt "$stateline" ] && inputline="" || inputline=$(( stateline + 1 ))
iptables -N $chain
iptables -I INPUT $inputline -d $redirip -j $chain
iptables -A $chain -m state --state INVALID -j DROP
iptables -A $chain -m state --state RELATED,ESTABLISHED -j ACCEPT
for i in $FWBRIDGE; do
netstat -ltn | grep -q "$redirip:443" && {
# we are listening for ssl, so let both 80 and 443 through
iptables -A $chain -i $i -p tcp -m multiport --dports 443,80 -j $accept
} || {
# else only allow port 80 and redirect 443 (assumes pixelserv v32 or later)
iptables -A $chain -i $i -p tcp --dport 80 -j $accept
# comment following lines if v31 or earlier
iptables -t nat -nL $chain &>/dev/null || {
iptables -t nat -N $chain
iptables -t nat -A PREROUTING -p tcp -d $redirip --dport 443 -j $chain
}
iptables -t nat -A $chain -i $i -p tcp -d $redirip --dport 443 -j DNAT --to $redirip:80
}
iptables -A $chain -i $i -p icmp --icmp-type echo-request -j $accept
[ $logreject = 1 ] && iptables -A $chain -i $i $limitstr -j LOG --log-prefix "REJECT " --log-macdecode --log-tcp-sequence --log-tcp-options --log-ip-option
iptables -A $chain -i $i -p tcp -j REJECT --reject-with tcp-reset
iptables -A $chain -i $i -p all -j REJECT --reject-with icmp-host-prohibited
done
[ "$FWRULES" = "STRICT" ] && iptables -A $chain -j $drop
}
rmtomatolink() {
if grep -q "/www/tomato.js" /proc/mounts ; then
if [ -f "$jsflag" ]; then
umount /www/tomato.js
else
elog "tomatos.js was mounted by something else"
mountjs=0
fi
fi
rm -f "$jsfile"
rm -f "$jsflag"
}
addtomatolink() {
if [ "$tomatolink" = "1" ]; then
mountjs=1
if [ "$web_dir" != "" ] && [ "$web_dir" != "default" ]; then
elog "Skip adding tomato link, non default web_dir($web_dir)"
mountjs=0
elif ! grep -q "'log.asp'] ] ],$" /www/tomato.js ; then
elog "Skip adding tomato link, could not find insertion point in tomato.js"
mountjs=0
fi
rmtomatolink
if [ "$mountjs" = "1" ]; then
elog "Adding tomato menu item"
sed "/'log.asp'] ] ],$/ a ['Adblock', '${weblink#*/www/}\" target=\"adblock\"']," /www/tomato.js > "$jsfile"
mount -o bind "$jsfile" /www/tomato.js
touch "$jsflag"
fi
else
rmtomatolink
fi
}
cleanfire() {
iptables -D INPUT "0$( iptables --line-numbers -vnL INPUT | grep -Fm1 "$chain" | cut -f 1 -d " ")" &>/dev/null
iptables -F $chain &>/dev/null
iptables -X $chain &>/dev/null
iptables -t nat -D PREROUTING "0$( iptables --line-numbers -t nat -vnL PREROUTING | grep -Fm1 "$chain" | cut -f 1 -d " ")" &>/dev/null
iptables -t nat -F $chain &>/dev/null
iptables -t nat -X $chain &>/dev/null
}
grabsource() {
local host=$(echo $1 | awk -F"/" '{print $3}')
local path=$(echo $1 | awk -F"/" '{print substr($0, index($0,$4))}')
local lastmod=$(echo -e "HEAD /$path HTTP/1.1\r\nHost: $host\r\n\r\n" | nc -w30 $host 80 | tr -d '\r' | grep "Last-Modified")
local lmfile="$listprefix/lastmod-$(echo $1 | md5sum | cut -c 1-8)"
local sourcefile="$listprefix/source-$(echo $1 | md5sum | cut -c 1-8)"
local sourcesize=$(ls -l "$sourcefile" 2>/dev/null | awk '{ print int(($5/1024/1024) + 0.5) }')
local freedisk=$(df "$prefix" | awk '!/File/{print int($4/1024)}')
[ "$force" != "1" -a -f "$sourcefile" -a -n "$lastmod" -a "$lastmod" = "$(cat "$lmfile" 2>/dev/null)" ] && {
elog "Unchanged: $1 ($lastmod)"
echo -n "$sourcefile " >> "$sourcelistfile"
echo 2 >>"$tmpstatus"
return 2
}
# delete the source file we are replacing if larger than free space
[ -s "$sourcefile" ] && [ "$freedisk" -le "$(( sourcesize + 1 ))" ] && {
elog "removing $sourcefile size:$sourcesize free:$freedisk"
rm -f "$sourcefile" &>/dev/null
}
elog "Downloading: $1"
{
if wget $1 -O - $wget_opts ; then
elog "Completed: $1"
echo 0 >>"$tmpstatus"
else
elog "Failed: $1"
echo 1 >>"$tmpstatus"
fi
} | tr -d "\r" | sed -e '/^[[:alnum:]:]/!d' | awk '{print $2}' | sed -e '/^localhost$/d' > "$sourcefile.$$.tmp"
if [ -s "$sourcefile.$$.tmp" ] ; then
[ -n "$lastmod" ] && echo "$lastmod" > "$lmfile"
mv -f "$sourcefile.$$.tmp" "$sourcefile"
echo -n "$sourcefile " >> "$sourcelistfile"
else
rm -f "$sourcefile.$$.tmp" &>/dev/null
fi
}
buildlist() {
elog "Download starting"
tmpstatus=$tmp/status.$$.tmp
until ping -q -c1 google.com >/dev/null; do
elog "Waiting for connectivity..."
sleep 30
done
trap 'elog "Signal received, cancelling"; rm -f "$listprefix"/source-* "$listprefix"/lastmod-* "$tmpstatus" &>/dev/null; pexit 130' SIGQUIT SIGINT SIGTERM SIGHUP
echo -n "" > "$tmpstatus"
echo -n "" > "$sourcelistfile"
for s in $SOURCES; do
grabsource $s &
done
wait
while read ret; do
case "$ret" in
0) downloaded=1;;
1) failed=1;;
2) unchanged=1;;
esac
done < "$tmpstatus"
rm "$tmpstatus"
sourcelist=$(cat "$sourcelistfile")
rm -f "$sourcelistfile" &>/dev/null
trap - SIGQUIT SIGINT SIGTERM SIGHUP
if [ -z "$sourcelist" ] && [ -n "$BLACKLIST" -o -s "$blacklist" ]; then
elog "Processing blacklist only"
confgen
elif [ -z "$sourcelist" ]; then
elog "No source files found"
pexit 3
elif [ "$downloaded" = "1" ]; then
elog "Downloaded"
confgen
elif [ "$unchanged" = "1" ]; then
elog "Filters unchanged"
if [ ! -f "$blocklist" -o ! -s "$blocklist" ]; then
elog "Blocklist does not exist"
confgen
elif [ "$LISTMODE" != "$currentmode" -a "$currentmode" != "OFF" ]; then
elog "Mode changed"
confgen
elif [ "$LISTMODE" = "$currentmode" ]; then
elog "Mode unchanged"
# no changes to list and already running in current mode
writeconf # re-write conf or link if needed
# if no dnsmasq_custom changes, nothing else to do, so exit
[ "$CONFchanged" = "0" ] && pexit 2
fi
else
elog "Download failed"
if [ -s "$blocklist" ] && [ ! -f "$CONF" -o ! -s "$CONF" -o "$logging" != "$dnsmasq_logqueries" -o "$dnsmasq_custom" != "" ]; then #needlink
:
else pexit 3
fi
fi
}
confgen() {
cg1=$(date +%s)
elog "Generating $blocklist - $LISTMODE mode"
tmpwhitelist="$tmp/whitelist.$$.tmp"
tmpblocklist="$listtmp/blocklist.$$.tmp"
trap 'elog "Signal received, cancelling"; rm -f "$tmpwhitelist" "$tmpblocklist" &>/dev/null; echo -n "" > "$blocklist"; pexit 140' SIGQUIT SIGINT SIGTERM SIGHUP
{
# only allow valid hostname characters
echo "[^a-zA-Z0-9._-]+"
if [ -f "$whitelist" ]; then
# strip comments, blank lines, spaces and carriage returns from whitelist
sed -e 's/#.*$//g;s/^[ |\t|\r]*//;/^$/d' "$whitelist" 2>/dev/null
fi
# add config file whitelist entries to temp file
for w in $WHITELIST; do
echo $w
done
} > "$tmpwhitelist"
{
# use sourcefiles list (and not all files in folder which could have old/unwanted files)
[ -n "$sourcelist" ] && cat $sourcelist | grep -Ev -f "$tmpwhitelist"
rm -f "$tmpwhitelist" &>/dev/null
[ -f "$blacklist" ] && {
# strip comments, blank lines, spaces and carriage returns from blacklist
sed -e 's/#.*$//g;s/^[ |\t|\r]*//;/^$/d' "$blacklist" 2>/dev/null
}
for b in $BLACKLIST; do
echo "$b"
done
# add hosts to test if adblock is loaded
echo $testhost
echo $LISTMODE.$modehost
} > "$tmpblocklist"
{
# add header to blocklist, used to determine what mode the list was built for
# do not alter format without adjusting the grep regex that tests the mode/ip
echo "# adblock blocklist, MODE=$LISTMODE, IP=$redirip, generated $(date)"
case $LISTMODE in
HOST)
sort -u "$tmpblocklist" |
sed -e "s:^:$redirip :"
;;
OPTIMIZE)
sed -e :a -e 's/\([^\.]*\)\.\([^\.]*\)/\2#\1/;ta' "$tmpblocklist" | sort |
awk -F '#' 'BEGIN{d = "%"} { if(index($0"#",d)!=1&&NF!=0){d=$0"#";print $0;} }' |
sed -e :a -e 's/\([^#]*\)#\([^#]*\)/\2\.\1/;ta' -e "s/\(.*\)/address=\/\1\/$redirip/"
;;
LEGACY)
sort -u "$tmpblocklist" |
sed -e '/^$/d' -e "s/\(.*\)/address=\/\1\/$redirip/"
;;
esac
hostcount=$(( $(wc -l < "$blocklist") - 1 ))
echo "# $hostcount records"
rm -f "$tmpblocklist" &>/dev/null
elog "Blocklist generated - $(( $(date +%s) - cg1 )) seconds"
elog "$hostcount unique hosts to block"
} > "$blocklist"
trap - SIGQUIT SIGINT SIGTERM SIGHUP
}
loadconfig() {
ignoredlist=""
configfound="0"
# look for haarp config file, but since "config" is so generic
# do at least a minimal check with grep on the contents
[ -f $config ] && grep -q "SOURCES=" $config && {
# haarp legacy single folder mode
# everything defaults to the script location
configfound="1"
}
# if haarp legacy config does not exist, try to find another file
for c in $configlist; do
[ -f $c -a "$configfound" = "0" ] && grep -q "SOURCES=" $c && {
cfolder=$(dirname $c)
# if config is already in a folder named "adblock" use it, otherwise create an adblock subfolder
[ "${cfolder##*"/"}" = "$configname" ] && prefix=$cfolder || prefix=$cfolder/$configname
config=$c
configfound=1
} || {
[ -f $c ] && ignoredlist="$ignoredlist $c"
}
done
elog "Using config file $config"
# Warn other files were found but ignored
for c in $ignoredlist; do
elog "Ignoring extra config file $c"
done
[ -f "$config" ] || {
elog "$config not found!"
pexit 11
}
grep -q "SOURCES=" "$config" || {
elog "$config does not seem valid!"
pexit 11
}
# silently check for/create prefix folder
# but don't exit yet if we fail - we may redefine in config
[ -d "$prefix" ] || {
mkdir "$prefix" &>/dev/null
oldprefix=$prefix
createdprefix=1
}
#ensure tthe correct path
cd "$prefix" &>/dev/null
# load config
source "$config"
# if we created the prefix folder, but aren't using it, remove it.
[ "$prefix" != "$oldprefix" -a "$createdprefix" = "1" ] && rmdir "$oldprefix" &> /dev/null
# check prefix folder again - exit on fail this time
[ -d "$prefix" ] || mkdir "$prefix" || {
elog "Prefix folder ($prefix) does not exist and cannot be created"
pexit 12
}
#ensure tthe correct path
cd "$prefix" &>/dev/null
if [ "$PIXEL_IP" = "0" ]; then
[ "$redirip" = "" ] && redirip="0.0.0.0"
else
[ "$redirip" = "" ] || {
elog "PIXEL_IP should be \"0\" if redirip is set in config!"
pexit 10
}
[ -x "$pixelbin" ] || {
elog "$pixelbin not found/executable!"
pexit 10
}
fi
#########################################################
# redirip can be explicitly set in the config file, #
# but make sure it is valid as no checks are done #
# #
# PIXEL_IP still needs to be set to non-zero for #
# pixelserv to be started #
#########################################################
[ "$redirip" = "" ] && {
rediripandmask=$(ifconfig $BRIDGE | awk -F ' +|:' '/inet addr/{sub(/[0-9]*$/,'$PIXEL_IP',$4); print $4" netmask "$8}')
redirip=${rediripandmask%% *}
}
# $FWRULES must be NONE, LOOSE, or STRICT, if value is unknown, default to STRICT
FWRULES=$(echo $FWRULES | tr "[a-z]" "[A-Z]")
echo $FWRULES | grep -Eq "(^NONE$|^LOOSE$|^STRICT$)" || {
elog "Unknown FWRULES value ($FWRULES), using STRICT settings"
FWRULES="STRICT"
}
# $LISTMODE must be LEGACY, OPTIMIZE, or HOST, if value is unknown, default to OPTIMIZE
LISTMODE=$(echo $LISTMODE | tr "[a-z]" "[A-Z]")
echo $LISTMODE | grep -Eq "(^LEGACY$|^OPTIMIZE$|^HOST$)" && {
elog "Requested list mode is $LISTMODE"
} || {
elog "Unknown LISTMODE value ($LISTMODE), using OPTIMIZE settings"
LISTMODE="OPTIMIZE"
}
if [ "$RAMLIST" = "1" ]; then
listprefix="/var/lib/adblock"
else
listprefix="$prefix"
fi
[ -d "$listprefix" ] || mkdir "$listprefix" || {
elog "Blocklist folder ($listprefix) does not exist and cannot be created"
pexit 12
}
freedisk=$(df "$prefix" | awk '!/Filesys/{print int($4/1024)}')
freetmp=$(df "$tmp" | awk '!/Filesys/{print int($4/1024)}')
# if listtmp hasn't been explicitly set and more than $smalltmp available on /tmp
if [ "$listtmp" = "" -a "$freetmp" -gt "$smalltmp" ]; then
# use /tmp for temp blocklist file
listtmp=$tmp
elif [ "$listtmp" = "" ]; then
# if not set, default to legacy behavior for compatibility
listtmp="$listprefix"
else
# if specified in config, make sure it's there
[ -d "$listtmp" ] || mkdir "$listtmp" || {
elog "Blocklist temp folder ($listtmp) does not exist and cannot be created"
pexit 12
}
fi
if [ "$dnsmasq_logqueries" = "1" ]; then
elog "Enabling dnsmasq logging"
fi
if [ "$(readdnsmasq "$dnsmasq_config" "log-queries")" != "" ]; then
logging=1
elog "Logging previously enabled"
fi
dnslogfile="$(readdnsmasq "$dnsmasq_config" "log-facility")"
if [ "$dnsmasq_logqueries" = "1" -o "$logging" = "1" ]; then
if [ "$dnslogfile" = "" ]; then
if [ "$(nvram get log_file)" = 1 ]; then
elog "Logging to syslog"
else
elog "Warning: dnsmasq logging to syslog, but syslog is disabled"
fi
else
elog "Logging to $dnslogfile"
fi
fi
jsfile="$(dirname $weblink)/tomato.js.adblock"
jsflag="$jsfile.mount"
web_dir="$(nvram get web_dir)"
currentmode=OFF
nslookup $testhost &>/dev/null && currentmode=UNKNOWN
nslookup host.$modehost &>/dev/null && currentmode=HOST
nslookup legacy.$modehost &>/dev/null && currentmode=LEGACY
nslookup optimize.$modehost &>/dev/null && currentmode=OPTIMIZE
blocklist="$listprefix/blocklist"
whitelist="$prefix/whitelist"
blacklist="$prefix/blacklist"
thisconfig="$config:$(date -r "$config" 2>/dev/null)"
thisconfig="$thisconfig|$whitelist:$(date -r "$whitelist" 2>/dev/null)"
thisconfig="$thisconfig|$blacklist:$(date -r "$blacklist" 2>/dev/null)"
thisconfig="$thisconfig|$adblockscript:$(date -r "$adblockscript" 2>/dev/null)"
lastconfig="$(cat "$prefix/lastmod-config" 2>/dev/null)"
}
elog "Running as $me $@"
loadconfig
if [ -L $me -a "$cgimode" = "1" -a -e $me.weblink ]; then
if [ "$me" != "$(cat "$me.weblink")" ]; then
# apparently called as cgi wrapper, but name doesn't match
elog "<br>"
elog "weblink file exists in script folder but running script name does not match <br>"
elog "script name: $me, weblink value: $(cat $me.weblink) <br>"
pexit 20
fi
fi
# if called via weblink, execute $webscript
if [ "$me" = "$weblink" ]; then
for e in $(set | grep "^web.*=")
do
export "${e%=*}"
done
export adblockscript
export binprefix
export blacklist
export blocklist
export chain
export config
export dnsmasq_config
export hostlink
export listprefix
export modehost
export pixelbin
export prefix
export redirip
export release
export testhost
export weblink
export webscript
export whitelist
export FWBRIDGE
export PIXEL_IP
export LISTMODE
export thisconfig
export lastconfig
if [ -x "$binprefix/$webscript" ]; then
# use the script in this folder if it exists
export webscript="$binprefix/$webscript"