-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
adblock.sh
executable file
·1889 lines (1808 loc) · 58.2 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
# dns based ad/abuse domain blocking
# Copyright (c) 2015-2021 Dirk Brenken (dev@brenken.org)
# This is free software, licensed under the GNU General Public License v3.
# set (s)hellcheck exceptions
# shellcheck disable=1091,2010,2016,2034,2039,2059,2086,2091,2129,2143,2154,2181,2183,2188
# set initial defaults
#
export LC_ALL=C
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
set -o pipefail
adb_ver="4.1.3"
adb_enabled=0
adb_debug=0
adb_forcedns=0
adb_maxqueue=4
adb_dnsflush=0
adb_dnstimeout=20
adb_safesearch=0
adb_safesearchlist=""
adb_safesearchmod=0
adb_report=0
adb_trigger=""
adb_triggerdelay=0
adb_backup=1
adb_mail=0
adb_mailcnt=0
adb_jail=0
adb_dns=""
adb_dnsprefix="adb_list"
adb_locallist="blacklist whitelist iplist"
adb_tmpbase="/tmp"
adb_backupdir="${adb_tmpbase}/adblock-Backup"
adb_reportdir="${adb_tmpbase}/adblock-Report"
adb_jaildir="/tmp"
adb_pidfile="/var/run/adblock.pid"
adb_blacklist="/etc/adblock/adblock.blacklist"
adb_whitelist="/etc/adblock/adblock.whitelist"
adb_mailservice="/etc/adblock/adblock.mail"
adb_dnsfile="${adb_dnsprefix}.overall"
adb_dnsjail="${adb_dnsprefix}.jail"
adb_srcarc="/etc/adblock/adblock.sources.gz"
adb_srcfile="${adb_tmpbase}/adb_sources.json"
adb_rtfile="${adb_tmpbase}/adb_runtime.json"
adb_loggercmd="$(command -v logger)"
adb_dumpcmd="$(command -v tcpdump)"
adb_lookupcmd="$(command -v nslookup)"
adb_fetchutil=""
adb_zonelist=""
adb_portlist=""
adb_repiface=""
adb_replisten="53"
adb_repchunkcnt="5"
adb_repchunksize="1"
adb_lookupdomain="example.com"
adb_action="${1:-"start"}"
adb_packages=""
adb_sources=""
adb_cnt=""
# load & check adblock environment
#
f_load()
{
local bg_pid iface port ports
adb_sysver="$(ubus -S call system board 2>/dev/null | jsonfilter -e '@.model' -e '@.release.description' | \
"${adb_awk}" 'BEGIN{ORS=", "}{print $0}' | "${adb_awk}" '{print substr($0,1,length($0)-2)}')"
f_conf
if [ "${adb_action}" != "report" ]
then
f_dns
f_fetch
fi
if [ "${adb_enabled}" = "0" ]
then
f_extconf
f_temp
f_rmdns
f_jsnup "disabled"
f_log "info" "adblock is currently disabled, please set the config option 'adb_enabled' to '1' to use this service"
exit 0
fi
if [ "${adb_report}" = "1" ] && [ ! -x "${adb_dumpcmd}" ]
then
f_log "info" "Please install the package 'tcpdump' or 'tcpdump-mini' to use the reporting feature"
elif [ "${adb_report}" = "0" ] && [ "${adb_action}" = "report" ]
then
f_log "info" "Please enable the 'DNS Report' option to use the reporting feature"
exit 0
fi
bg_pid="$(pgrep -f "^${adb_dumpcmd}.*adb_report\\.pcap$" | "${adb_awk}" '{ORS=" "; print $1}')"
if [ -x "${adb_dumpcmd}" ] && { [ "${adb_report}" = "0" ] || { [ -n "${bg_pid}" ] && { [ "${adb_action}" = "stop" ] || [ "${adb_action}" = "restart" ]; }; }; }
then
if [ -n "${bg_pid}" ]
then
kill -HUP "${bg_pid}" 2>/dev/null
while $(kill -0 "${bg_pid}" 2>/dev/null)
do
sleep 1
done
unset bg_pid
fi
fi
if [ -x "${adb_dumpcmd}" ] && [ "${adb_report}" = "1" ] && [ -z "${bg_pid}" ] && [ "${adb_action}" != "report" ] && [ "${adb_action}" != "stop" ]
then
for port in ${adb_replisten}
do
if [ -z "${ports}" ]
then
ports="port ${port}"
else
ports="${ports} or port ${port}"
fi
done
if [ -z "${adb_repiface}" ]
then
network_get_device iface "lan"
if [ -n "${iface}" ]
then
adb_repiface="${iface}"
else
network_get_physdev iface "lan"
if [ -n "${iface}" ]
then
adb_repiface="${iface}"
fi
fi
if [ -n "${adb_repiface}" ]
then
uci_set adblock global adb_repiface "${adb_repiface}"
f_uci "adblock"
fi
fi
if [ -n "${adb_reportdir}" ] && [ ! -d "${adb_reportdir}" ]
then
mkdir -p "${adb_reportdir}"
f_log "info" "report directory '${adb_reportdir}' created"
fi
if [ -n "${adb_repiface}" ] && [ -d "${adb_reportdir}" ]
then
( "${adb_dumpcmd}" -nn -p -s0 -l -i ${adb_repiface} ${ports} -C${adb_repchunksize} -W${adb_repchunkcnt} -w "${adb_reportdir}/adb_report.pcap" >/dev/null 2>&1 & )
bg_pid="$(pgrep -f "^${adb_dumpcmd}.*adb_report\\.pcap$" | "${adb_awk}" '{ORS=" "; print $1}')"
else
f_log "info" "Please set the name of the reporting network device 'adb_repiface' manually"
fi
fi
}
# check & set environment
#
f_env()
{
adb_starttime="$(date "+%s")"
f_log "info" "adblock instance started ::: action: ${adb_action}, priority: ${adb_nice:-"0"}, pid: ${$}"
f_jsnup "running"
f_extconf
f_temp
if [ "${adb_dnsflush}" = "1" ]
then
printf "${adb_dnsheader}" > "${adb_dnsdir}/${adb_dnsfile}"
f_dnsup
fi
if [ ! -r "${adb_srcfile}" ]
then
if [ -r "${adb_srcarc}" ]
then
zcat "${adb_srcarc}" > "${adb_srcfile}"
else
f_log "err" "adblock source archive not found"
fi
fi
if [ -r "${adb_srcfile}" ] && [ "${adb_action}" != "report" ]
then
json_init
json_load_file "${adb_srcfile}"
else
f_log "err" "adblock source file not found"
fi
}
# load adblock config
#
f_conf()
{
local cnt=0 cnt_max=10
if [ ! -r "/etc/config/adblock" ] || [ -z "$(uci -q show adblock.global.adb_safesearch)" ]
then
f_log "err" "no valid adblock config found, please re-install the package via opkg with the '--force-reinstall --force-maintainer' options"
fi
config_cb()
{
option_cb()
{
local option="${1}"
local value="${2}"
eval "${option}=\"${value}\""
}
list_cb()
{
local option="${1}"
local value="${2}"
if [ "${option}" = "adb_sources" ]
then
eval "${option}=\"$(printf "%s" "${adb_sources}") ${value}\""
elif [ "${option}" = "adb_eng_sources" ]
then
eval "${option}=\"$(printf "%s" "${adb_eng_sources}") ${value}\""
elif [ "${option}" = "adb_sha_sources" ]
then
eval "${option}=\"$(printf "%s" "${adb_sha_sources}") ${value}\""
elif [ "${option}" = "adb_stb_sources" ]
then
eval "${option}=\"$(printf "%s" "${adb_stb_sources}") ${value}\""
elif [ "${option}" = "adb_utc_sources" ]
then
eval "${option}=\"$(printf "%s" "${adb_utc_sources}") ${value}\""
elif [ "${option}" = "adb_denyip" ]
then
eval "${option}=\"$(printf "%s" "${adb_denyip}") ${value}\""
elif [ "${option}" = "adb_allowip" ]
then
eval "${option}=\"$(printf "%s" "${adb_allowip}") ${value}\""
elif [ "${option}" = "adb_safesearchlist" ]
then
eval "${option}=\"$(printf "%s" "${adb_safesearchlist}") ${value}\""
elif [ "${option}" = "adb_zonelist" ]
then
eval "${option}=\"$(printf "%s" "${adb_zonelist}") ${value}\""
elif [ "${option}" = "adb_portlist" ]
then
eval "${option}=\"$(printf "%s" "${adb_portlist}") ${value}\""
fi
}
}
config_load adblock
if [ -z "${adb_fetchutil}" ] || [ -z "${adb_dns}" ]
then
while [ -z "${adb_packages}" ] && [ "${cnt}" -le "${cnt_max}" ]
do
adb_packages="$(opkg list-installed 2>/dev/null)"
cnt=$((cnt+1))
sleep 1
done
if [ -z "${adb_packages}" ]
then
f_log "err" "local opkg package repository is not available, please set 'adb_fetchutil' and 'adb_dns' manually"
fi
fi
}
# status helper function
#
f_char()
{
local result input="${1}"
if [ "${input}" = "1" ]
then
result="✔"
else
result="✘"
fi
printf "%s" "${result}"
}
# load dns backend config
#
f_dns()
{
local util utils dns_up cnt=0
if [ -z "${adb_dns}" ]
then
utils="knot-resolver bind unbound dnsmasq raw"
for util in ${utils}
do
if [ "${util}" = "raw" ] || [ -n "$(printf "%s" "${adb_packages}" | grep "^${util}")" ]
then
if [ "${util}" = "knot-resolver" ]
then
util="kresd"
elif [ "${util}" = "bind" ]
then
util="named"
fi
if [ "${util}" = "raw" ] || [ -x "$(command -v "${util}")" ]
then
adb_dns="${util}"
uci_set adblock global adb_dns "${util}"
f_uci "adblock"
break
fi
fi
done
elif [ "${adb_dns}" != "raw" ] && [ ! -x "$(command -v "${adb_dns}")" ]
then
unset adb_dns
fi
if [ -n "${adb_dns}" ]
then
case "${adb_dns}" in
"dnsmasq")
adb_dnscachecmd="-"
adb_dnsinotify="${adb_dnsinotify:-"0"}"
adb_dnsinstance="${adb_dnsinstance:-"0"}"
adb_dnsuser="${adb_dnsuser:-"dnsmasq"}"
adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}"
adb_dnsheader="${adb_dnsheader}"
adb_dnsdeny="${adb_dnsdeny:-"${adb_awk} '{print \"address=/\"\$0\"/\"}'"}"
adb_dnsallow="${adb_dnsallow:-"${adb_awk} '{print \"local=/\"\$0\"/#\"}'"}"
adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awk} -v item=\"\$item\" '{print \"address=/\"\$0\"/\"item\"\"}'"}"
adb_dnsstop="${adb_dnsstop:-"address=/#/"}"
;;
"unbound")
adb_dnscachecmd="$(command -v unbound-control || printf "%s" "-")"
adb_dnsinotify="${adb_dnsinotify:-"0"}"
adb_dnsinstance="${adb_dnsinstance:-"0"}"
adb_dnsuser="${adb_dnsuser:-"unbound"}"
adb_dnsdir="${adb_dnsdir:-"/var/lib/unbound"}"
adb_dnsheader="${adb_dnsheader}"
adb_dnsdeny="${adb_dnsdeny:-"${adb_awk} '{print \"local-zone: \\042\"\$0\"\\042 static\"}'"}"
adb_dnsallow="${adb_dnsallow:-"${adb_awk} '{print \"local-zone: \\042\"\$0\"\\042 transparent\"}'"}"
adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awk} -v item=\"\$item\" '{type=\"AAAA\";if(match(item,/^([0-9]{1,3}\.){3}[0-9]{1,3}$/)){type=\"A\"}}{print \"local-data: \\042\"\$0\" \"type\" \"item\"\\042\"}'"}"
adb_dnsstop="${adb_dnsstop:-"local-zone: \".\" static"}"
;;
"named")
adb_dnscachecmd="$(command -v rndc || printf "%s" "-")"
adb_dnsinotify="${adb_dnsinotify:-"0"}"
adb_dnsinstance="${adb_dnsinstance:-"0"}"
adb_dnsuser="${adb_dnsuser:-"bind"}"
adb_dnsdir="${adb_dnsdir:-"/var/lib/bind"}"
adb_dnsheader="${adb_dnsheader:-"\$TTL 2h\n@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)\n IN NS localhost.\n"}"
adb_dnsdeny="${adb_dnsdeny:-"${adb_awk} '{print \"\"\$0\" CNAME .\\n*.\"\$0\" CNAME .\"}'"}"
adb_dnsallow="${adb_dnsallow:-"${adb_awk} '{print \"\"\$0\" CNAME rpz-passthru.\\n*.\"\$0\" CNAME rpz-passthru.\"}'"}"
adb_dnsdenyip="${adb_dnsdenyip:-"${adb_awk} '{print \"\"\$0\".rpz-client-ip CNAME .\"}'"}"
adb_dnsallowip="${adb_dnsallowip:-"${adb_awk} '{print \"\"\$0\".rpz-client-ip CNAME rpz-passthru.\"}'"}"
adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awk} -v item=\"\$item\" '{print \"\"\$0\" CNAME \"item\".\\n*.\"\$0\" CNAME \"item\".\"}'"}"
adb_dnsstop="${adb_dnsstop:-"* CNAME ."}"
;;
"kresd")
adb_dnscachecmd="-"
adb_dnsinotify="${adb_dnsinotify:-"0"}"
adb_dnsinstance="${adb_dnsinstance:-"0"}"
adb_dnsuser="${adb_dnsuser:-"root"}"
adb_dnsdir="${adb_dnsdir:-"/etc/kresd"}"
adb_dnsheader="${adb_dnsheader:-"\$TTL 2h\n@ IN SOA localhost. root.localhost. (1 6h 1h 1w 2h)\n"}"
adb_dnsdeny="${adb_dnsdeny:-"${adb_awk} '{print \"\"\$0\" CNAME .\\n*.\"\$0\" CNAME .\"}'"}"
adb_dnsallow="${adb_dnsallow:-"${adb_awk} '{print \"\"\$0\" CNAME rpz-passthru.\\n*.\"\$0\" CNAME rpz-passthru.\"}'"}"
adb_dnssafesearch="${adb_dnssafesearch:-"${adb_awk} -v item=\"\$item\" '{type=\"AAAA\";if(match(item,/^([0-9]{1,3}\.){3}[0-9]{1,3}$/)){type=\"A\"}}{print \"\"\$0\" \"type\" \"item\"\"}'"}"
adb_dnsstop="${adb_dnsstop:-"* CNAME ."}"
;;
"raw")
adb_dnscachecmd="-"
adb_dnsinotify="${adb_dnsinotify:-"0"}"
adb_dnsinstance="${adb_dnsinstance:-"0"}"
adb_dnsuser="${adb_dnsuser:-"root"}"
adb_dnsdir="${adb_dnsdir:-"/tmp"}"
adb_dnsheader="${adb_dnsheader}"
adb_dnsdeny="${adb_dnsdeny:-"0"}"
adb_dnsallow="${adb_dnsallow:-"1"}"
adb_dnssafesearch="${adb_dnssafesearch:-"0"}"
adb_dnsstop="${adb_dnsstop:-"0"}"
;;
esac
fi
if [ "${adb_dns}" != "raw" ] && { [ -z "${adb_dns}" ] || [ ! -x "$(command -v "${adb_dns}")" ]; }
then
f_log "err" "dns backend not found, please set 'adb_dns' manually"
fi
if [ "${adb_dns}" != "raw" ] && { [ "${adb_dnsdir}" = "${adb_tmpbase}" ] || [ "${adb_dnsdir}" = "${adb_backupdir}" ] || [ "${adb_dnsdir}" = "${adb_reportdir}" ]; }
then
f_log "err" "dns directory '${adb_dnsdir}' has been misconfigured, it must not point to the 'adb_tmpbase', 'adb_backupdir', 'adb_reportdir'"
fi
if [ "${adb_action}" = "start" ] && [ -z "${adb_trigger}" ]
then
sleep ${adb_triggerdelay}
fi
if [ "${adb_dns}" != "raw" ] && [ "${adb_action}" != "stop" ]
then
while [ "${cnt}" -le 30 ]
do
dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" 2>/dev/null | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running" 2>/dev/null)"
if [ "${dns_up}" = "true" ]
then
break
fi
sleep 1
cnt=$((cnt+1))
done
if [ -n "${adb_dnsdir}" ] && [ ! -d "${adb_dnsdir}" ]
then
mkdir -p "${adb_dnsdir}"
if [ "${?}" = "0" ]
then
f_log "info" "dns backend directory '${adb_dnsdir}' created"
else
f_log "err" "dns backend directory '${adb_dnsdir}' could not be created"
fi
fi
if [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ]
then
printf "${adb_dnsheader}" > "${adb_dnsdir}/${adb_dnsfile}"
fi
if [ "${dns_up}" != "true" ]
then
f_dnsup 4
if [ "${?}" != "0" ]
then
f_log "err" "dns backend '${adb_dns}' not running or executable"
fi
fi
if [ "${adb_backup}" = "1" ] && [ -n "${adb_backupdir}" ] && [ ! -d "${adb_backupdir}" ]
then
mkdir -p "${adb_backupdir}"
if [ "${?}" = "0" ]
then
f_log "info" "backup directory '${adb_backupdir}' created"
else
f_log "err" "backup directory '${adb_backupdir}' could not be created"
fi
fi
if [ -n "${adb_jaildir}" ] && [ ! -d "${adb_jaildir}" ]
then
mkdir -p "${adb_jaildir}"
if [ "${?}" = "0" ]
then
f_log "info" "jail directory '${adb_jaildir}' created"
else
f_log "err" "jail directory '${adb_jaildir}' could not be created"
fi
fi
fi
f_log "debug" "f_dns ::: dns: ${adb_dns}, dns_dir: ${adb_dnsdir}, dns_file: ${adb_dnsfile}, dns_user: ${adb_dnsuser}, dns_inotify: ${adb_dnsinotify}, dns_instance: ${adb_dnsinstance}, backup: ${adb_backup}, backup_dir: ${adb_backupdir}, jail_dir: ${adb_jaildir}"
}
# load fetch utility
#
f_fetch()
{
local util utils insecure cnt=0
if [ -z "${adb_fetchutil}" ]
then
utils="aria2c curl wget uclient-fetch"
for util in ${utils}
do
if { [ "${util}" = "uclient-fetch" ] && [ -n "$(printf "%s" "${adb_packages}" | grep "^libustream-")" ]; } || \
{ [ "${util}" = "wget" ] && [ -n "$(printf "%s" "${adb_packages}" | grep "^wget -")" ]; } || \
[ "${util}" = "curl" ] || [ "${util}" = "aria2c" ]
then
if [ -x "$(command -v "${util}")" ]
then
adb_fetchutil="${util}"
uci_set adblock global adb_fetchutil "${util}"
f_uci "adblock"
break
fi
fi
done
elif [ ! -x "$(command -v "${adb_fetchutil}")" ]
then
unset adb_fetchutil
fi
case "${adb_fetchutil}" in
"aria2c")
if [ "${adb_fetchinsecure}" = "1" ]
then
insecure="--check-certificate=false"
fi
adb_fetchparm="${adb_fetchparm:-"${insecure} --timeout=20 --allow-overwrite=true --auto-file-renaming=false --log-level=warn --dir=/ -o"}"
;;
"curl")
if [ "${adb_fetchinsecure}" = "1" ]
then
insecure="--insecure"
fi
adb_fetchparm="${adb_fetchparm:-"${insecure} --connect-timeout 20 --silent --show-error --location -o"}"
;;
"uclient-fetch")
if [ "${adb_fetchinsecure}" = "1" ]
then
insecure="--no-check-certificate"
fi
adb_fetchparm="${adb_fetchparm:-"${insecure} --timeout=20 -O"}"
;;
"wget")
if [ "${adb_fetchinsecure}" = "1" ]
then
insecure="--no-check-certificate"
fi
adb_fetchparm="${adb_fetchparm:-"${insecure} --no-cache --no-cookies --max-redirect=0 --timeout=20 -O"}"
;;
esac
if [ -n "${adb_fetchutil}" ] && [ -n "${adb_fetchparm}" ]
then
adb_fetchutil="$(command -v "${adb_fetchutil}")"
else
f_log "err" "download utility with SSL support not found, please install 'uclient-fetch' with a 'libustream-*' variant or another download utility like 'wget', 'curl' or 'aria2'"
fi
f_log "debug" "f_fetch ::: fetch_util: ${adb_fetchutil:-"-"}, fetch_parm: ${adb_fetchparm:-"-"}"
}
# create temporary files, directories and set dependent options
#
f_temp()
{
local cpu core cores
cpu="$(grep -c '^processor' /proc/cpuinfo 2>/dev/null)"
core="$(grep -cm1 '^core id' /proc/cpuinfo 2>/dev/null)"
if [ "${cpu}" = "0" ]
then
cpu=1
fi
if [ "${core}" = "0" ]
then
core=1
fi
cores=$((cpu*core))
if [ -d "${adb_tmpbase}" ]
then
adb_tmpdir="$(mktemp -p "${adb_tmpbase}" -d)"
adb_tmpload="$(mktemp -p "${adb_tmpdir}" -tu)"
adb_tmpfile="$(mktemp -p "${adb_tmpdir}" -tu)"
adb_srtopts="--temporary-directory=${adb_tmpdir} --compress-program=gzip --batch-size=32 --parallel=${cores}"
else
f_log "err" "the temp base directory '${adb_tmpbase}' does not exist/is not mounted yet, please create the directory or raise the 'adb_triggerdelay' to defer the adblock start"
fi
if [ ! -s "${adb_pidfile}" ]
then
printf "%s" "${$}" > "${adb_pidfile}"
fi
f_log "debug" "f_temp ::: tmp_base: ${adb_tmpbase:-"-"}, tmp_dir: ${adb_tmpdir:-"-"}, cores: ${cores:-"-"}, sort_options: ${adb_srtopts}, pid_file: ${adb_pidfile:-"-"}"
}
# remove temporary files and directories
#
f_rmtemp()
{
if [ -d "${adb_tmpdir}" ]
then
rm -rf "${adb_tmpdir}"
fi
rm -f "${adb_srcfile}"
> "${adb_pidfile}"
f_log "debug" "f_rmtemp ::: tmp_dir: ${adb_tmpdir:-"-"}, src_file: ${adb_srcfile:-"-"}, pid_file: ${adb_pidfile:-"-"}"
}
# remove dns related files
#
f_rmdns()
{
local status
status="$(ubus -S call service list '{"name":"adblock"}' 2>/dev/null | jsonfilter -l1 -e '@["adblock"].instances.*.running' 2>/dev/null)"
if [ "${adb_dns}" = "raw" ] || { [ -n "${adb_dns}" ] && [ -n "${status}" ]; }
then
> "${adb_rtfile}"
if [ "${adb_backup}" = "1" ]
then
rm -f "${adb_backupdir}/${adb_dnsprefix}".*.gz
fi
printf "${adb_dnsheader}" > "${adb_dnsdir}/${adb_dnsfile}"
f_dnsup 4
fi
f_rmtemp
f_log "debug" "f_rmdns ::: dns: ${adb_dns}, status: ${status:-"-"}, dns_dir: ${adb_dnsdir}, dns_file: ${adb_dnsfile}, rt_file: ${adb_rtfile}, backup_dir: ${adb_backupdir:-"-"}"
}
# commit uci changes
#
f_uci()
{
local change config="${1}"
if [ -n "${config}" ]
then
change="$(uci -q changes "${config}" | "${adb_awk}" '{ORS=" "; print $0}')"
if [ -n "${change}" ]
then
uci_commit "${config}"
case "${config}" in
"firewall")
"/etc/init.d/firewall" reload >/dev/null 2>&1
;;
"resolver")
printf "${adb_dnsheader}" > "${adb_dnsdir}/${adb_dnsfile}"
f_count
f_jsnup "running"
"/etc/init.d/${adb_dns}" reload >/dev/null 2>&1
;;
esac
fi
f_log "debug" "f_uci ::: config: ${config}, change: ${change}"
fi
}
# get list counter
#
f_count()
{
local file mode="${1}" name="${2}"
adb_cnt=0
case "${mode}" in
"iplist")
if [ -s "${adb_tmpdir}/tmp.add.${name}" ]
then
adb_cnt="$(wc -l 2>/dev/null < "${adb_tmpdir}/tmp.add.${name}")"
fi
;;
"blacklist")
if [ -s "${adb_tmpfile}.${name}" ]
then
adb_cnt="$(wc -l 2>/dev/null < "${adb_tmpfile}.${name}")"
fi
;;
"whitelist")
if [ -s "${adb_tmpdir}/tmp.raw.${name}" ]
then
adb_cnt="$(wc -l 2>/dev/null < "${adb_tmpdir}/tmp.raw.${name}")"
rm -f "${adb_tmpdir}/tmp.raw.${name}"
fi
;;
"safesearch")
if [ -s "${adb_tmpdir}/tmp.safesearch.${name}" ]
then
adb_cnt="$(wc -l 2>/dev/null < "${adb_tmpdir}/tmp.safesearch.${name}")"
fi
;;
"merge")
if [ -s "${adb_tmpdir}/${adb_dnsfile}" ]
then
adb_cnt="$(wc -l 2>/dev/null < "${adb_tmpdir}/${adb_dnsfile}")"
fi
;;
"download"|"backup"|"restore")
if [ -s "${src_tmpfile}" ]
then
adb_cnt="$(wc -l 2>/dev/null < "${src_tmpfile}")"
fi
;;
"final")
if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
then
adb_cnt="$(wc -l 2>/dev/null < "${adb_dnsdir}/${adb_dnsfile}")"
if [ -s "${adb_tmpdir}/tmp.add.whitelist" ]
then
adb_cnt=$((adb_cnt-$(wc -l 2>/dev/null < "${adb_tmpdir}/tmp.add.whitelist")))
fi
for file in "${adb_tmpdir}/tmp.safesearch".*
do
if [ -r "${file}" ]
then
adb_cnt=$((adb_cnt-$(wc -l 2>/dev/null < "${file}")))
fi
done
if [ -n "${adb_dnsheader}" ]
then
adb_cnt=$(((adb_cnt-$(printf "${adb_dnsheader}" | grep -c "^"))/2))
fi
fi
;;
esac
}
# set external config options
#
f_extconf()
{
local config config_dir config_file section zone port fwcfg
case "${adb_dns}" in
"dnsmasq")
config="dhcp"
config_dir="$(uci_get dhcp "@dnsmasq[${adb_dnsinstance}]" confdir | grep -Fo "${adb_dnsdir}")"
if [ "${adb_enabled}" = "1" ] && [ -z "${config_dir}" ]
then
uci_set dhcp "@dnsmasq[${adb_dnsinstance}]" confdir "${adb_dnsdir}" 2>/dev/null
fi
;;
"kresd")
config="resolver"
config_file="$(uci_get resolver kresd rpz_file | grep -Fo "${adb_dnsdir}/${adb_dnsfile}")"
if [ "${adb_enabled}" = "1" ] && [ -z "${config_file}" ]
then
uci -q add_list resolver.kresd.rpz_file="${adb_dnsdir}/${adb_dnsfile}"
elif [ "${adb_enabled}" = "0" ] && [ -n "${config_file}" ]
then
uci -q del_list resolver.kresd.rpz_file="${adb_dnsdir}/${adb_dnsfile}"
fi
;;
esac
f_uci "${config}"
config="firewall"
fwcfg="$(uci -qNX show "${config}" | "${adb_awk}" 'BEGIN{FS="[.=]"};/adblock_/{if(zone==$2){next}else{ORS=" ";zone=$2;print zone}}')"
if [ "${adb_enabled}" = "1" ] && [ "${adb_forcedns}" = "1" ] && \
[ "$(/etc/init.d/firewall enabled; printf "%u" ${?})" = "0" ]
then
for zone in ${adb_zonelist}
do
for port in ${adb_portlist}
do
if [ -z "$(printf "%s" "${fwcfg}" | grep -o -m1 "adblock_${zone}${port}[ |\$]")" ]
then
uci -q batch <<-EOC
set firewall."adblock_${zone}${port}"="redirect"
set firewall."adblock_${zone}${port}".name="Adblock DNS (${zone}, ${port})"
set firewall."adblock_${zone}${port}".src="${zone}"
set firewall."adblock_${zone}${port}".proto="tcp udp"
set firewall."adblock_${zone}${port}".src_dport="${port}"
set firewall."adblock_${zone}${port}".dest_port="${port}"
set firewall."adblock_${zone}${port}".target="DNAT"
EOC
fi
fwcfg="${fwcfg/adblock_${zone}${port}[ |\$]/}"
done
done
fwcfg="${fwcfg#"${fwcfg%%[![:space:]]*}"}"
fwcfg="${fwcfg%"${fwcfg##*[![:space:]]}"}"
fi
if [ "${adb_enabled}" = "0" ] || [ "${adb_forcedns}" = "0" ] || [ -n "${fwcfg}" ]
then
for section in ${fwcfg}
do
uci_remove firewall "${section}"
done
fi
f_uci "${config}"
}
# restart dns backend
#
f_dnsup()
{
local dns_service dns_up dns_pid dns_procfile restart_rc cnt=0 out_rc=4 in_rc="${1:-0}"
if [ "${adb_dns}" = "raw" ] || { [ "${in_rc}" = "0" ] && [ "${adb_dnsinotify}" = "1" ]; }
then
out_rc=0
else
if [ "${in_rc}" = "0" ] && [ "${adb_dnsflush}" = "0" ]
then
case "${adb_dns}" in
"unbound")
if [ -x "${adb_dnscachecmd}" ] && [ -d "${adb_tmpdir}" ] && [ -f "${adb_dnsdir}/unbound.conf" ]
then
"${adb_dnscachecmd}" -c "${adb_dnsdir}/unbound.conf" dump_cache > "${adb_tmpdir}/adb_cache.dump" 2>/dev/null
fi
"/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
restart_rc="${?}"
;;
"named")
if [ -x "${adb_dnscachecmd}" ] && [ -f "/etc/bind/rndc.conf" ]
then
"${adb_dnscachecmd}" -c "/etc/bind/rndc.conf" reload >/dev/null 2>&1
restart_rc="${?}"
fi
if [ -z "${restart_rc}" ] || { [ -n "${restart_rc}" ] && [ "${restart_rc}" != "0" ]; }
then
"/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
restart_rc="${?}"
fi
;;
*)
"/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
restart_rc="${?}"
;;
esac
fi
if [ -z "${restart_rc}" ]
then
"/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
restart_rc="${?}"
fi
fi
if [ "${restart_rc}" = "0" ]
then
while [ "${cnt}" -le "${adb_dnstimeout}" ]
do
dns_service="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}")"
dns_up="$(printf "%s" "${dns_service}" | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running")"
dns_pid="$(printf "%s" "${dns_service}" | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.pid")"
dns_procfile="$(ls -l "/proc/${dns_pid}/fd" 2>/dev/null | grep -Fo "${adb_dnsdir}/${adb_dnsfile}")"
if [ "${dns_up}" = "true" ] && [ -n "${dns_pid}" ] && [ -z "${dns_procfile}" ]
then
if [ -x "${adb_lookupcmd}" ] && [ "${adb_lookupdomain}" != "false" ]
then
"${adb_lookupcmd}" "${adb_lookupdomain}" >/dev/null 2>&1
if [ "${?}" = "0" ]
then
out_rc=0
break
fi
else
sleep ${adb_dnstimeout}
cnt=${adb_dnstimeout}
out_rc=0
break
fi
fi
cnt=$((cnt+1))
sleep 1
done
if [ "${out_rc}" = "0" ] && [ "${adb_dns}" = "unbound" ]
then
if [ -x "${adb_dnscachecmd}" ] && [ -d "${adb_tmpdir}" ] && [ -s "${adb_tmpdir}/adb_cache.dump" ]
then
"${adb_dnscachecmd}" -c "${adb_dnsdir}/unbound.conf" load_cache < "${adb_tmpdir}/adb_cache.dump" >/dev/null 2>&1
restart_rc="${?}"
fi
fi
fi
f_log "debug" "f_dnsup ::: dns: ${adb_dns}, cache_cmd: ${adb_dnscachecmd:-"-"}, lookup_cmd: ${adb_lookupcmd:-"-"}, lookup_domain: ${adb_lookupdomain:-"-"}, restart_rc: ${restart_rc:-"-"}, dns_flush: ${adb_dnsflush}, dns_inotify: ${adb_dnsinotify}, dns_timeout: ${adb_dnstimeout}, dns_cnt: ${cnt}, in_rc: ${in_rc}, out_rc: ${out_rc}"
return "${out_rc}"
}
# backup/restore/remove blocklists
#
f_list()
{
local hold file rset item array safe_url safe_ips safe_cname safe_domains ip out_rc mode="${1}" src_name="${2:-"${src_name}"}" in_rc="${src_rc:-0}" cnt="1" ffiles="-maxdepth 1 -name ${adb_dnsprefix}.*.gz"
case "${mode}" in
"iplist")
src_name="${mode}"
if [ "${adb_dns}" = "named" ]
then
rset="BEGIN{FS=\"[.:]\";pfx=\"32\"}{if(match(\$0,/:/))pfx=\"128\"}{printf \"%s.\",pfx;for(seg=NF;seg>=1;seg--)if(seg==1)printf \"%s\n\",\$seg;else if(\$seg>=0)printf \"%s.\",\$seg; else printf \"%s.\",\"zz\"}"
if [ -n "${adb_allowip}" ]
then
> "${adb_tmpdir}/tmp.raw.${src_name}"
for ip in ${adb_allowip}
do
printf "%s" "${ip}" | "${adb_awk}" "${rset}" >> "${adb_tmpdir}/tmp.raw.${src_name}"
done
eval "${adb_dnsallowip}" "${adb_tmpdir}/tmp.raw.${src_name}" > "${adb_tmpdir}/tmp.add.${src_name}"
out_rc="${?}"
fi
if [ -n "${adb_denyip}" ] && { [ -z "${out_rc}" ] || [ "${out_rc}" = "0" ]; }
then
> "${adb_tmpdir}/tmp.raw.${src_name}"
for ip in ${adb_denyip}
do
printf "%s" "${ip}" | "${adb_awk}" "${rset}" >> "${adb_tmpdir}/tmp.raw.${src_name}"
done
eval "${adb_dnsdenyip}" "${adb_tmpdir}/tmp.raw.${src_name}" >> "${adb_tmpdir}/tmp.add.${src_name}"
out_rc="${?}"
fi
rm -f "${adb_tmpdir}/tmp.raw.${src_name}"
fi
;;
"blacklist"|"whitelist")
src_name="${mode}"
if [ "${src_name}" = "blacklist" ] && [ -f "${adb_blacklist}" ]
then
rset="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}"
"${adb_awk}" "${rset}" "${adb_blacklist}" | \
"${adb_awk}" 'BEGIN{FS="."}{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' > "${adb_tmpdir}/tmp.raw.${src_name}"
"${adb_sort}" ${adb_srtopts} -u "${adb_tmpdir}/tmp.raw.${src_name}" 2>/dev/null > "${adb_tmpfile}.${src_name}"
out_rc="${?}"
rm -f "${adb_tmpdir}/tmp.raw.${src_name}"
elif [ "${src_name}" = "whitelist" ] && [ -f "${adb_whitelist}" ]
then
rset="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower(\$1)}"
"${adb_awk}" "${rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.raw.${src_name}"
out_rc="${?}"
if [ "${out_rc}" = "0" ]
then
rset="/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{gsub(\"\\\\.\",\"\\\\.\",\$1);print tolower(\"^(|.*\\\\.)\"\$1\"$\")}"
"${adb_awk}" "${rset}" "${adb_tmpdir}/tmp.raw.${src_name}" > "${adb_tmpdir}/tmp.rem.${src_name}"
out_rc="${?}"
if [ "${out_rc}" = "0" ] && [ "${adb_dnsallow}" != "1" ]
then
eval "${adb_dnsallow}" "${adb_tmpdir}/tmp.raw.${src_name}" > "${adb_tmpdir}/tmp.add.${src_name}"
out_rc="${?}"
if [ "${out_rc}" = "0" ] && [ "${adb_jail}" = "1" ] && [ "${adb_dnsstop}" != "0" ]
then
> "${adb_jaildir}/${adb_dnsjail}"
if [ -n "${adb_dnsheader}" ]
then
printf "${adb_dnsheader}" >> "${adb_jaildir}/${adb_dnsjail}"
fi
cat "${adb_tmpdir}/tmp.add.${src_name}" >> "${adb_jaildir}/${adb_dnsjail}"
printf "%s\n" "${adb_dnsstop}" >> "${adb_jaildir}/${adb_dnsjail}"
fi
fi
fi
fi
;;
"safesearch")
case "${src_name}" in
"google")
rset="/^(\\.[[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{printf \"%s\n%s\n\",tolower(\"www\"\$1),tolower(substr(\$1,2,length(\$1)))}"
safe_url="https://www.google.com/supported_domains"
safe_cname="forcesafesearch.google.com"
safe_domains="${adb_tmpdir}/tmp.load.safesearch.${src_name}"
if [ "${adb_backup}" = "1" ] && [ -s "${adb_backupdir}/safesearch.${src_name}.gz" ]
then
zcat "${adb_backupdir}/safesearch.${src_name}.gz" > "${safe_domains}"
out_rc="${?}"
else
"${adb_fetchutil}" ${adb_fetchparm} "${safe_domains}" "${safe_url}" 2>/dev/null
out_rc="${?}"
if [ "${adb_backup}" = "1" ] && [ "${out_rc}" = "0" ]
then
gzip -cf "${safe_domains}" > "${adb_backupdir}/safesearch.${src_name}.gz"
fi
fi
if [ "${out_rc}" = "0" ]
then
if [ -x "${adb_lookupcmd}" ]
then
safe_ips="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
if [ -n "${safe_ips}" ]
then
"${adb_awk}" "${rset}" "${safe_domains}" > "${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
fi
fi
out_rc="${?}"
fi
;;
"bing")
safe_cname="strict.bing.com"
safe_domains="www.bing.com"
if [ -x "${adb_lookupcmd}" ]
then
safe_ips="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
if [ -n "${safe_ips}" ]
then
printf "%s\n" ${safe_domains} > "${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
fi
fi
out_rc="${?}"
;;
"duckduckgo")
safe_cname="safe.duckduckgo.com"
safe_domains="duckduckgo.com"
if [ -x "${adb_lookupcmd}" ]
then
safe_ips="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
if [ -n "${safe_ips}" ]
then
printf "%s\n" ${safe_domains} > "${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
fi
fi
out_rc="${?}"
;;
"pixabay")
safe_cname="safesearch.pixabay.com"
safe_domains="pixabay.com"
if [ -x "${adb_lookupcmd}" ]
then
safe_ips="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
if [ -n "${safe_ips}" ]
then
printf "%s\n" ${safe_domains} > "${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
fi
fi
out_rc="${?}"
;;
"yandex")
safe_cname="familysearch.yandex.ru"
safe_domains="ya.ru yandex.ru yandex.com yandex.com.tr yandex.ua yandex.by yandex.ee yandex.lt yandex.lv yandex.md yandex.uz yandex.tm yandex.tj yandex.az yandex.kz"
if [ -x "${adb_lookupcmd}" ]
then
safe_ips="$("${adb_lookupcmd}" "${safe_cname}" 2>/dev/null | "${adb_awk}" '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
if [ -n "${safe_ips}" ]
then
printf "%s\n" ${safe_domains} > "${adb_tmpdir}/tmp.raw.safesearch.${src_name}"
fi
fi
out_rc="${?}"
;;
"youtube")
if [ "${adb_safesearchmod}" = "0" ]