aboutsummaryrefslogtreecommitdiff
path: root/net/simple-adblock/files/simple-adblock.init
blob: 883a6e9218c26a1746a0aff0ef243bb3dc752227 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
#!/bin/sh /etc/rc.common
# Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
# shellcheck disable=SC2039,SC1091
PKG_VERSION='dev-test'

export START=94
export USE_PROCD=1
export LC_ALL=C

export EXTRA_COMMANDS='check dl killcache sizes show'
export EXTRA_HELP='	check	Checks if specified domain is found in current blacklist
	dl	Force-downloads all enabled block-list
	sizes	Displays the file-sizes of enabled block-lists
	show	Shows the service last-run status'

readonly packageName='simple-adblock'
readonly serviceName="$packageName $PKG_VERSION"
readonly addnhostsFile="/var/run/${packageName}.addnhosts"
readonly addnhostsCache="/var/run/${packageName}.addnhosts.cache"
readonly addnhostsGzip="/etc/${packageName}.addnhosts.gz"
readonly addnhostsOutputFilter='s|^|127.0.0.1 |;s|$||'
readonly addnhostsOutputFilterIPv6='s|^|:: |;s|$||'
readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
readonly dnsmasqCache="/var/run/${packageName}.dnsmasq.cache"
readonly dnsmasqGzip="/etc/${packageName}.dnsmasq.gz"
readonly dnsmasqOutputFilter='s|^|local=/|;s|$|/|'
readonly ipsetFile="/var/dnsmasq.d/${packageName}.ipset"
readonly ipsetCache="/var/run/${packageName}.ipset.cache"
readonly ipsetGzip="/etc/${packageName}.ipset.gz"
readonly ipsetOutputFilter='s|^|ipset=/|;s|$|/adb|'
readonly serversFile="/var/run/${packageName}.servers"
readonly serversCache="/var/run/${packageName}.servers.cache"
readonly serversGzip="/etc/${packageName}.servers.gz"
readonly serversOutputFilter='s|^|server=/|;s|$|/|'
readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
readonly unboundCache="/var/run/${packageName}.unbound.cache"
readonly unboundGzip="/etc/${packageName}.unbound.gz"
readonly unboundOutputFilter='s|^|local-zone: "|;s|$|" static|'
readonly A_TMP="/var/${packageName}.hosts.a.tmp"
readonly B_TMP="/var/${packageName}.hosts.b.tmp"
readonly PIDFile="/var/run/${packageName}.pid"
readonly jsonFile="/var/run/${packageName}.json"
readonly sharedMemoryError="/dev/shm/$packageName-error"
readonly sharedMemoryOutput="/dev/shm/$packageName-output"
readonly hostsFilter='/localhost/d;/^#/d;/^[^0-9]/d;s/^0\.0\.0\.0.//;s/^127\.0\.0\.1.//;s/[[:space:]]*#.*$//;s/[[:cntrl:]]$//;s/[[:space:]]//g;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
readonly domainsFilter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
readonly checkmark='\xe2\x9c\x93'
readonly xmark='\xe2\x9c\x97'
readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
readonly _ERROR_='\033[0;31mERROR\033[0m'

readonly messageSuccess='Success'
readonly messageFail='Fail'
readonly messageDownloading='Downloading'
readonly messageReloading='Reloading'
readonly messageRestarting='Restarting'
readonly messageStarting='Starting'
readonly messageForceReloading='Force-Reloading'
readonly messageProcessing='Processing'
readonly messageStopped='Stopped'
getStatusText() {
	local _ret
	case "$1" in
		statusNoInstall) _ret="$serviceName is not installed or not found";;
		statusStopped) _ret="Stopped";;
		statusStarting) _ret="Starting";;
		statusRestarting) _ret="Restarting";;
		statusForceReloading) _ret="Force Reloading";;
		statusDownloading) _ret="Downloading";;
		statusError) _ret="Error";;
		statusWarning) _ret="Warning";;
		statusFail) _ret="Fail";;
		statusSuccess) _ret="Success";;
	esac
	printf "%b" "$_ret"
}
getErrorText() {
	local _ret
	case "$1" in
		errorOutputFileCreate) _ret="failed to create $outputFile file";;
		errorFailDNSReload) _ret="failed to restart/reload DNS resolver";;
		errorSharedMemory) _ret="failed to access shared memory";;
		errorSorting) _ret="failed to sort data file";;
		errorOptimization) _ret="failed to optimize data file";;
		errorWhitelistProcessing) _ret="failed to process whitelist";;
		errorDataFileFormatting) _ret="failed to format data file";;
		errorMovingDataFile) _ret="failed to move data file '${A_TMP}' to '${outputFile}'";;
		errorCreatingCompressedCache) _ret="failed to create compressed cache";;
		errorRemovingTempFiles) _ret="failed to remove temporary files";;
		errorRestoreCompressedCache) _ret="failed to unpack compressed cache";;
		errorRestoreCache) _ret="failed to move '$outputCache' to '$outputFile'";;
		errorOhSnap) _ret="failed to create blocklist or restart DNS resolver";;
		errorStopping) _ret="failed to stop $serviceName";;
		errorDNSReload) _ret="failed to reload/restart DNS resolver";;
		errorDownloadingList) _ret="failed to download";;
		errorParsingList) _ret="failed to parse";;
	esac
	printf "%b" "$_ret"
}

create_lock() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
remove_lock() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
trap remove_lock EXIT
output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
# str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
# str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
compare_versions() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
is_ipset_procd() { compare_versions "$(sed -ne 's/^Version: //p' /usr/lib/opkg/info/firewall.control)" "2019-09-18"; }
led_on(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
led_off(){ if [ -n "${1}" ] &&  [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
dnsmasq_hup() { killall -q -HUP dnsmasq; }
dnsmasq_kill() { killall -q -KILL dnsmasq; }
dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
unbound_restart() { /etc/init.d/unbound restart >/dev/null 2>&1; }

output() {
# Can take a single parameter (text) to be output at any verbosity
# Or target verbosity level and text to be output at specifc verbosity
	local msg memmsg logmsg
	if [ $# -ne 1 ]; then
		if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
	fi
	[ -t 1 ] && printf "%b" "$1"
	msg="${1//$serviceName /service }";
	if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
		[ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
		logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
		logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
		rm -f "$sharedMemoryOutput"
	else
		printf "%b" "$msg" >> "$sharedMemoryOutput"
	fi
}

export serviceEnabled forceDNS parallelDL debug allowIDN compressedCache     
export targetDNS bootDelay dlTimeout curlRetry verbosity=1 led dnsInstance
export whitelist_domains blacklist_domains
export whitelist_domains_urls blacklist_domains_urls blacklist_hosts_urls
export wan_if wan_gw wanphysdev dl_command serviceStatus dl_flag
export outputFilter outputFilterIPv6 outputFile outputGzip outputCache ipv6Enabled
export is_ssl_supported

load_package_config() {
	config_load "$packageName"
	config_get_bool serviceEnabled        'config' 'enabled' 1
	config_get_bool forceDNS              'config' 'force_dns' 1
	config_get_bool parallelDL            'config' 'parallel_downloads' 1
	config_get_bool debug                 'config' 'debug' 0
	config_get_bool compressedCache       'config' 'compressed_cache' 0
	config_get_bool ipv6Enabled           'config' 'ipv6_enabled' 0
	config_get bootDelay                  'config' 'boot_delay' '120'
	config_get dlTimeout                  'config' 'download_timeout' '20'
	config_get curlRetry                  'config' 'curl_retry' '3'
	config_get verbosity                  'config' 'verbosity' '2'
	config_get led        	              'config' 'led'
	config_get targetDNS                  'config' 'dns' 'dnsmasq.servers'
	config_get dnsInstance                'config' 'dns_instance' '0'
	config_get whitelist_domains          'config' 'whitelist_domain'
	config_get blacklist_domains          'config' 'blacklist_domain'
	config_get whitelist_domains_urls     'config' 'whitelist_domains_url'
	config_get blacklist_domains_urls     'config' 'blacklist_domains_url'
	config_get blacklist_hosts_urls       'config' 'blacklist_hosts_url'

	if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
		 [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
		 [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
		targetDNS='dnsmasq.servers'
	fi

	case "$targetDNS" in
		dnsmasq.addnhosts)
			outputFilter="$addnhostsOutputFilter"
			outputFile="$addnhostsFile"
			outputCache="$addnhostsCache"
			outputGzip="$addnhostsGzip"
			[ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
			rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
			rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
			rm -f "$serversFile" "$serversCache" "$serversGzip"
			rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
			;;
		dnsmasq.conf)
			outputFilter="$dnsmasqOutputFilter"
			outputFile="$dnsmasqFile"
			outputCache="$dnsmasqCache"
			outputGzip="$dnsmasqGzip"
			rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
			rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
			rm -f "$serversFile" "$serversCache" "$serversGzip"
			rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
			;;
		dnsmasq.ipset)
			outputFilter="$ipsetOutputFilter"
			outputFile="$ipsetFile"
			outputCache="$ipsetCache"
			outputGzip="$ipsetGzip"
			rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
			rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
			rm -f "$serversFile" "$serversCache" "$serversGzip"
			rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
			;;
		dnsmasq.servers)
			outputFilter="$serversOutputFilter"
			outputFile="$serversFile"
			outputCache="$serversCache"
			outputGzip="$serversGzip"
			rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
			rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
			rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
			rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
			;;
		unbound.adb_list)
			outputFilter="$unboundOutputFilter"
			outputFile="$unboundFile"
			outputCache="$unboundCache"
			outputGzip="$unboundGzip"
			rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
			rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
			rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
			rm -f "$serversFile" "$serversCache" "$serversGzip"
			;;
	esac
	if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
		verbosity=1
	fi
	. /lib/functions/network.sh
	. /usr/share/libubox/jshn.sh
	# Prefer curl because it supports the file:// scheme.
	if [ -x /usr/bin/curl ]; then
		dl_command="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
		dl_flag="-o"
	elif wget --version 2>/dev/null | grep -q "+https"; then
		dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
		dl_flag="-O"
	else
		dl_command="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
		dl_flag="-O"
	fi
	led="${led:+/sys/class/leds/$led}"
	if curl --version 2>/dev/null | grep -q "https" \
		|| wget --version 2>/dev/null | grep -q "+https" \
		|| grep -q "libustream-mbedtls" /usr/lib/opkg/status \
		|| grep -q "libustream-openssl" /usr/lib/opkg/status \
		|| grep -q "libustream-wolfssl" /usr/lib/opkg/status; then
		is_ssl_supported=1
	else
		unset is_ssl_supported
	fi
}

is_enabled() {
	load_package_config

	if [ "$debug" -ne 0 ]; then
		exec 1>>/tmp/simple-adblock.log
		exec 2>&1
		set -x
	fi

	if [ "$serviceEnabled" -eq 0 ]; then
		case "$1" in
			on_start)
				output "$packageName is currently disabled.\\n"
				output "Run the following commands before starting service again:\\n"
				output "uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
				;;
		esac
		return 1
	fi

	case $targetDNS in
		dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
			if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
				allowIDN=0
			else
				allowIDN=1
			fi
			;;
		unbound.adb_list)
			allowIDN=1;;
	esac

	case $targetDNS in
		dnsmasq.ipset)
			if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
				output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
				targetDNS='dnsmasq.servers'
			fi
			if ! ipset help hash:net >/dev/null 2>&1; then
				output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
				targetDNS='dnsmasq.servers'
			fi
			;;
	esac

	[ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
	[ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
	[ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
	cacheOps 'testGzip' && return 0
	network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
	[ -n "$wan_gw" ] && return 0
	output "$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
}

dnsmasqOps() {
	local cfg="$1" param="$2"
	case "$param" in
		dnsmasq.addnhosts)
			if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
				uci -q del dhcp."$cfg".serversfile
			fi
			if ! uci -q get dhcp."$cfg".addnhosts | grep -q "$addnhostsFile"; then
				uci add_list dhcp."$cfg".addnhosts="$addnhostsFile"
			fi
			;;
		dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup)
			uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
			if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
				uci -q del dhcp."$cfg".serversfile
			fi
			;;
		dnsmasq.servers)
			uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
			if [ "$(uci -q get dhcp."$cfg".serversfile)" != "$serversFile" ]; then
				uci set dhcp."$cfg".serversfile="$serversFile"
			fi
			;;
	esac
}

dnsOps() {
	local param output_text i
	case $1 in
		on_start)
			if [ ! -s "$outputFile" ]; then
				tmpfs set status "statusFail"
				tmpfs add error "errorOutputFileCreate"
				output "$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
				return 1
			fi

			config_load 'dhcp'
			if [ "$dnsInstance" = "*" ]; then
				config_foreach dnsmasqOps 'dnsmasq' "$targetDNS"
			elif [ -n "$dnsInstance" ]; then
				for i in $dnsInstance; do
					dnsmasqOps "@dnsmasq[$i]" "$targetDNS"
				done
			fi

			case "$targetDNS" in
				dnsmasq.addnhosts|dnsmasq.servers)
					param=dnsmasq_hup
					output_text='Reloading DNSMASQ'
					;;
				dnsmasq.conf|dnsmasq.ipset)
					param=dnsmasq_restart
					output_text='Restarting DNSMASQ'
					;;
				unbound.adb_list)
					param=unbound_restart
					output_text='Restarting Unbound'
					;;
			esac

			if [ -n "$(uci changes dhcp)" ]; then
				uci commit dhcp
				if [ "$param" = 'unbound_restart' ]; then
					param='dnsmasq_restart; unbound_restart;'
					output_text='Restarting Unbound/DNSMASQ'
				else
					param=dnsmasq_restart
					output_text='Restarting DNSMASQ'
				fi
			fi
			output 1 "$output_text "
			output 2 "$output_text "
			tmpfs set message "$output_text"
			if eval "$param"; then
				tmpfs set status "statusSuccess"
				led_on "$led"
				output_okn
			else 
				output_fail
				tmpfs set status "statusFail"
				tmpfs add error "errorDNSReload"
				output "$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
				return 1
			fi
			;;
		on_stop)
			case "$targetDNS" in
				dnsmasq.addnhosts | dnsmasq.servers)
					param=dnsmasq_hup
					;;
				dnsmasq.conf | dnsmasq.ipset)
					param=dnsmasq_restart
					;;
				unbound.adb_list)
					param=unbound_restart
					;;
			esac
			if [ -n "$(uci changes dhcp)" ]; then 
				uci -q commit dhcp
				if [ "$param" = 'unbound_restart' ]; then
					param='dnsmasq_restart; unbound_restart;'
				else
					param=dnsmasq_restart
				fi
			fi
			eval "$param"
			return $?
			;;
		quiet)
			case "$targetDNS" in
				dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
					param=dnsmasq_restart
					;;
				unbound.adb_list)
					param=unbound_restart
					;;
			esac
			eval "$param"
			return $?
			;;
	esac
}

tmpfs() {
	local action="$1" instance="$2" value="$3"
	local status message error stats
	local readReload readRestart curReload curRestart ret
	if [ -s "$jsonFile" ]; then
		status="$(jsonfilter -i $jsonFile -l1 -e "@['data']['status']")"
		message="$(jsonfilter -i $jsonFile -l1 -e "@['data']['message']")"
		error="$(jsonfilter -i $jsonFile -l1 -e "@['data']['error']")"
		stats="$(jsonfilter -i $jsonFile -l1 -e "@['data']['stats']")"
		readReload="$(jsonfilter -i $jsonFile -l1 -e "@['data']['reload']")"
		readRestart="$(jsonfilter -i $jsonFile -l1 -e "@['data']['restart']")"
	fi
	case "$action" in
		get)
			case "$instance" in
				status)
					printf "%b" "$status"; return;;
				message) 
					printf "%b" "$message"; return;;
				error) 
					printf "%b" "$error"; return;;
				stats) 
					printf "%b" "$stats"; return;;
				triggers)
					curReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
					curRestart="$compressedCache $forceDNS $led"
					if [ ! -s "$jsonFile" ]; then
						ret='on_boot'
					elif [ "$curReload" != "$readReload" ]; then
						ret='download'
					elif [ "$curRestart" != "$readRestart" ]; then
						ret='restart'
					fi
					printf "%b" "$ret"
					return;;
			esac
			;;
		add)
			case "$instance" in
				status)
					[ -n "$status" ] && status="$status $value" || status="$value";;
				message) 
					[ -n "$message" ] && message="$message $value" || message="$value";;
				error)
					[ -n "$error" ] && error="$error $value" || error="$value";;
				stats) 
					[ -n "$stats" ] && stats="$stats $value" || stats="$value";;
			esac
			;;
		del)
			case "$instance" in
				all)
					unset status;
					unset message;
					unset error;
					unset stats;
					;;
				status)
					unset status;;
				message) 
					unset message;;
				error) 
					unset error;;
				stats) 
					unset stats;;
				triggers) 
					unset readReload; unset readRestart;;
			esac
			;;
		set)
			case "$instance" in
				status)
					status="$value";;
				message) 
					message="$value";;
				error) 
					error="$value";;
				stats) 
					stats="$value";;
				triggers) 
					readReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
					readRestart="$compressedCache $forceDNS $led"
					;;
			esac
			;;
	esac
	json_init
	json_add_object 'data'
	json_add_string version "$PKG_VERSION"
	json_add_string status "$status"
	json_add_string message "$message"
	json_add_string error "$error"
	json_add_string stats "$stats"
	json_add_string reload "$readReload"
	json_add_string restart "$readRestart"
	json_close_object
	json_dump > "$jsonFile"
	sync
}

cacheOps() {
	local R_TMP
	case "$1" in
		create|backup)
			[ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
			return $?
			;;
		restore|use)
			[ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
			return $?
			;;
		test)
			[ -s "$outputCache" ]
			return $?
			;;
		testGzip)
			[ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
			return $?
			;;
		createGzip)
			R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
			if gzip < "$outputFile" > "$R_TMP"; then
				if mv "$R_TMP" "$outputGzip"; then
					rm -f "$R_TMP"
					return 0
				else
					rm -f "$R_TMP"
					return 1
				fi
			else
				return 1
			fi
			;;
		expand|unpack|expandGzip|unpackGzip)
			[ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
			return $?
			;;
	esac
}

fw3Ops() {
	local action="$1" param="$2" _restart
	case "$action" in
		reload) /etc/init.d/firewall reload >/dev/null 2>&1;;
		restart) /etc/init.d/firewall restart >/dev/null 2>&1;;
		remove)
			case "$param" in
				dns_redirect) uci -q del firewall.simple_adblock_dns_redirect;;
				ipset)        uci -q del firewall.simple_adblock_ipset
											uci -q del firewall.simple_adblock_ipset_rule;;
				*)
					uci -q del firewall.simple_adblock_dns_redirect
					uci -q del firewall.simple_adblock_ipset
					uci -q del firewall.simple_adblock_ipset_rule
					;;
			esac
			;;
		insert)
			case "$param" in
				dns_redirect)
					if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
						uci -q set firewall.simple_adblock_dns_redirect=redirect
						uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
						uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
						uci -q set firewall.simple_adblock_dns_redirect.src=lan
						uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
						uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
						uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
					fi
					;;
				ipset)
					if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
						uci -q set firewall.simple_adblock_ipset=ipset
						uci -q set firewall.simple_adblock_ipset.name=adb
						uci -q set firewall.simple_adblock_ipset.match=dest_net
						uci -q set firewall.simple_adblock_ipset.storage=hash
						uci -q set firewall.simple_adblock_ipset.enabled=1
						_restart=1
					fi
					if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
						uci -q set firewall.simple_adblock_ipset_rule=rule
						uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
						uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
						uci -q set firewall.simple_adblock_ipset_rule.src=lan
						uci -q set firewall.simple_adblock_ipset_rule.dest='*'
						uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
						uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
						uci -q set firewall.simple_adblock_ipset_rule.enabled=1
					fi
					;;
				*)
					if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
						uci -q set firewall.simple_adblock_dns_redirect=redirect
						uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
						uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
						uci -q set firewall.simple_adblock_dns_redirect.src=lan
						uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
						uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
						uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
					fi
					if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
						uci -q set firewall.simple_adblock_ipset=ipset
						uci -q set firewall.simple_adblock_ipset.name=adb
						uci -q set firewall.simple_adblock_ipset.match=dest_net
						uci -q set firewall.simple_adblock_ipset.storage=hash
						uci -q set firewall.simple_adblock_ipset.enabled=1
						_restart=1
					fi
					if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
						uci -q set firewall.simple_adblock_ipset_rule=rule
						uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
						uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
						uci -q set firewall.simple_adblock_ipset_rule.src=lan
						uci -q set firewall.simple_adblock_ipset_rule.dest='*'
						uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
						uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
						uci -q set firewall.simple_adblock_ipset_rule.enabled=1
					fi
					;;
		esac
	esac
	if [ -n "$(uci changes firewall)" ]; then
		uci -q commit firewall
		if [ -z "$_restart" ]; then
			fw3Ops 'reload'
		else
			fw3Ops 'restart'
		fi
	fi
}

process_url() {
	local label type D_TMP R_TMP
	if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
	label="${1##*//}"; label="${label%%/*}";
	if [ "$2" = 'hosts' ]; then
		label="Hosts:   $label"; filter="$hostsFilter";
	else
		label="Domains: $label"; filter="$domainsFilter";
	fi
	if [ "$3" = 'blocked' ]; then
		type='Blocked'; D_TMP="$B_TMP";
	else
		type='Allowed'; D_TMP="$A_TMP";
	fi
	if [ "${1:0:5}" == "https" ] && [ -z "$is_ssl_supported" ]; then
		output 1 "$_FAIL_"
		output 2 "[DL] $type $label $__FAIL__\\n"
		echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
		return 0
	fi
	while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
		R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
	done
	if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
		output 1 "$_FAIL_"
		output 2 "[DL] $type $label $__FAIL__\\n"
		echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
	else
		sed -i "$filter" "$R_TMP"
		if [ ! -s "$R_TMP" ]; then
			output 1 "$_FAIL_"
			output 2 "[DL] $type $label $__FAIL__\\n"
			echo "errorParsingList|${1}" >> "$sharedMemoryError"
		else
			cat "${R_TMP}" >> "$D_TMP"
			output 1 "$_OK_"
			output 2 "[DL] $type $label $__OK__\\n"
		fi
	fi
	rm -f "$R_TMP"
	return 0
}

download_lists() {
	local hf w_filter j=0 R_TMP

	tmpfs set message "${messageDownloading}..."
	tmpfs set status "statusDownloading"

	rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
	if [ "$(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
		output 3 'Low free memory, restarting resolver... '
		if dnsOps 'quiet'; then
			output_okn
		else 
			output_fail
		fi
	fi
	touch $A_TMP; touch $B_TMP;
	output 1 'Downloading lists '
	rm -f "$sharedMemoryError"
	if [ -n "$blacklist_hosts_urls" ]; then
		for hf in ${blacklist_hosts_urls}; do
			if [ "$parallelDL" -gt 0 ]; then
				process_url "$hf" 'hosts' 'blocked' &
			else
				process_url "$hf" 'hosts' 'blocked'
			fi
		done
	fi
	if [ -n "$blacklist_domains_urls" ]; then
		for hf in ${blacklist_domains_urls}; do
			if [ "$parallelDL" -gt 0 ]; then
				process_url "$hf" 'domains' 'blocked' &
			else
				process_url "$hf" 'domains' 'blocked'
			fi
		done
	fi
	if [ -n "$whitelist_domains_urls" ]; then
		for hf in ${whitelist_domains_urls}; do
			if [ "$parallelDL" -gt 0 ]; then
				process_url "$hf" 'domains' 'allowed' &
			else
				process_url "$hf" 'domains' 'allowed'
			fi
		done
	fi
	wait
	output 1 '\n'
	if [ -s "$sharedMemoryError" ]; then
		while IFS= read -r line; do
			tmpfs add error "$line"
		done < "$sharedMemoryError"
		rm -f "$sharedMemoryError"
	fi

	[ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
	whitelist_domains="${whitelist_domains}
$(cat $A_TMP)"
	[ -n "$whitelist_domains" ] && for hf in ${whitelist_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done

	[ ! -s "$B_TMP" ] && return 1

	output 1 'Processing downloads '
	output 2 'Sorting combined list '
	tmpfs set message "$messageProcessing: sorting combined list"
	if [ "$allowIDN" -gt 0 ]; then
		if sort -u "$B_TMP" > "$A_TMP"; then
			output_ok
		else
			output_failn
			tmpfs add error "errorSorting"
		fi
	else
		if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
			output_ok
		else
			output_failn
			tmpfs add error "errorSorting"
		fi
	fi

	if [ "$targetDNS" = 'dnsmasq.conf' ] || \
		 [ "$targetDNS" = 'dnsmasq.ipset' ] || \
		 [ "$targetDNS" = 'dnsmasq.servers' ] || \
		 [ "$targetDNS" = 'unbound.adb_list' ]; then
		# TLD optimization written by Dirk Brenken (dev@brenken.org)
		output 2 'Optimizing combined list '
		tmpfs set message "$messageProcessing: optimizing combined list"
# 	sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
		if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
			if sort "$B_TMP" > "$A_TMP"; then
				if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
					if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
						if sort -u "$A_TMP" > "$B_TMP"; then
							output_ok
						else
							output_failn
							tmpfs add error "errorOptimization"
							mv "$A_TMP" "$B_TMP"
						fi
					else
						output_failn
						tmpfs add error "errorOptimization"
					fi
				else
					output_failn
					tmpfs add error "errorOptimization"
					mv "$A_TMP" "$B_TMP"
				fi
			else
				output_failn
				tmpfs add error "errorOptimization"
			fi
		else
			output_failn
			tmpfs add error "errorOptimization"
			mv "$A_TMP" "$B_TMP"
		fi
	else
		mv "$A_TMP" "$B_TMP"
	fi

	output 2 'Whitelisting domains '
	tmpfs set message "$messageProcessing: whitelisting domains"
	if sed -i "$w_filter" "$B_TMP"; then
		output_ok
	else
		output_failn
		tmpfs add error "errorWhitelistProcessing"
	fi

	output 2 'Formatting merged file '
	tmpfs set message "$messageProcessing: formatting merged file"
	if [ -z "$outputFilterIPv6" ]; then
		if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
			output_ok
		else
			output_failn
			tmpfs add error "errorDataFileFormatting"
		fi
	else
		case "$targetDNS" in
			dnsmasq.addnhosts)
				if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
					sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
					output_ok
				else
					output_failn
					tmpfs add error "errorDataFileFormatting"
				fi
				;;
		esac
	fi

	case "$targetDNS" in
		dnsmasq.addnhosts)
			output 2 'Creating DNSMASQ addnhosts file '
			tmpfs set message "$messageProcessing: creating DNSMASQ addnhosts file"
			;;
		dnsmasq.conf)
			output 2 'Creating DNSMASQ config file '
			tmpfs set message "$messageProcessing: creating DNSMASQ config file"
			;;
		dnsmasq.ipset)
			output 2 'Creating DNSMASQ ipset file '
			tmpfs set message "$messageProcessing: creating DNSMASQ ipset file"
			;;
		dnsmasq.servers)
			output 2 'Creating DNSMASQ servers file '
			tmpfs set message "$messageProcessing: creating DNSMASQ servers file"
			;;
		unbound.adb_list)
			output 2 'Creating Unbound adb_list file '
			tmpfs set message "$messageProcessing: creating Unbound adb_list file"
			;;
	esac
	if mv "$A_TMP" "$outputFile"; then
		output_ok
	else
		output_failn
		tmpfs add error "errorMovingDataFile"
	fi
	if [ "$compressedCache" -gt 0 ]; then
		output 2 'Creating compressed cache '
		tmpfs set message "$messageProcessing: creating compressed cache"
		if cacheOps 'createGzip'; then
			output_ok
		else
			output_failn
			tmpfs add error "errorCreatingCompressedCache"
		fi
	else
		rm -f "$outputGzip"
	fi
	output 2 'Removing temporary files '
	tmpfs set message "$messageProcessing: removing temporary files"
	rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
	if [ $j -eq 0 ]; then
		output_ok
	else
		output_failn
		tmpfs add error "errorRemovingTempFiles"
	fi
	output 1 '\n'
}

boot() { 
	load_package_config
	if create_lock; then
		sleep "$bootDelay"
		remove_lock
		rc_procd start_service 'on_boot' && rc_procd service_triggers
	fi
}

start_service() {
	is_enabled 'on_start' || return 1
	local action status error message stats c
	if ! create_lock; then
		output 3 "$serviceName: another instance is starting up "; output_fail
		return 0
	fi

	status="$(tmpfs get status)"
	error="$(tmpfs get error)"
	message="$(tmpfs get message)"
	stats="$(tmpfs get stats)"
	action="$(tmpfs get triggers)"

	if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
		if cacheOps 'testGzip' || cacheOps 'test'; then
			action='restore'
		else
			action='download'
		fi
	elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
		action='download'
	elif [ ! -s "$outputFile" ]; then 
		if cacheOps 'testGzip' || cacheOps 'test'; then
			action='restore'
		else
			action='download'
		fi
	elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
		action='restart'
	elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
		[ "$1" != 'hotplug' ] && showstatus
		exit 0
	else
		action='download'
	fi

	tmpfs del all
	tmpfs set triggers

	if is_chaos_calmer || ! is_ipset_procd; then
		if [ "$forceDNS" -ne 0 ]; then
			fw3Ops 'insert' 'dns_redirect'
		else
			fw3Ops 'remove' 'dns_redirect'
		fi
		if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
			fw3Ops 'insert' 'ipset'
		else
			fw3Ops 'remove' 'ipset'
		fi
		procd_open_instance 'main'
		procd_set_param command /bin/true
		procd_set_param stdout 1
		procd_set_param stderr 1
		procd_close_instance
	else
		procd_open_instance 'main'
		procd_set_param command /bin/true
		procd_set_param stdout 1
		procd_set_param stderr 1
		procd_open_data
		json_add_array firewall
		if [ "$forceDNS" -ne 0 ]; then
			json_add_object ''
			json_add_string type redirect
			json_add_string name simple_adblock_dns_redirect
			json_add_string target DNAT
			json_add_string src lan
			json_add_string proto tcpudp
			json_add_string src_dport 53
			json_add_string dest_port 53
			json_add_string reflection 0
			json_close_object
		fi
		if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
			json_add_object ''
			json_add_string type ipset
			json_add_string name adb
			json_add_string match dest_net
			json_add_string storage hash
			json_add_string enabled 1
			json_close_object
			json_add_object ''
			json_add_string type rule
			json_add_string name simple_adblock_ipset_rule
			json_add_string ipset adb
			json_add_string src lan
			json_add_string dest '*'
			json_add_string proto tcpudp
			json_add_string target REJECT
			json_add_string enabled 1
			json_close_object
		fi
		json_close_array
		procd_close_data
		procd_close_instance
	fi

	if [ "$action" = 'restore' ]; then
		output 0 "Starting $serviceName... "
		output 3 "Starting $serviceName...\\n"
		tmpfs set status "statusStarting"
		if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
			output 3 'Found compressed cache file, unpacking it '
			tmpfs set message 'found compressed cache file, unpacking it.'
			if cacheOps 'unpackGzip'; then
				output_okn
			else
				output_fail
				tmpfs add error "errorRestoreCompressedCache"
				output "$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
				action='download'
			fi
		fi
		if cacheOps 'test' && [ ! -s "$outputFile" ]; then
			output 3 'Found cache file, reusing it '
			tmpfs set message 'found cache file, reusing it.'
			if cacheOps 'restore'; then 
				output_okn
				dnsOps 'on_start'
			else
				output_fail
				tmpfs add error "errorRestoreCache"
				output "$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
				action='download'
			fi
		fi
	fi
	case "$action" in
		download)
			if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
				output 0 "Force-reloading $serviceName... "
				output 3 "Force-reloading $serviceName...\\n"
				tmpfs set status "statusForceReloading"
			else
				output 0 "Starting $serviceName... "
				output 3 "Starting $serviceName...\\n"
				tmpfs set status "statusStarting"
			fi
			download_lists
			dnsOps 'on_start'
		;;
		restart)
			output 0 "Restarting $serviceName... "
			output 3 "Restarting $serviceName...\\n"
			tmpfs set status "statusRestarting"
			dnsOps 'on_start'
		;;
		start)
			output 0 "Starting $serviceName... "
			output 3 "Starting $serviceName...\\n"
			tmpfs set status "statusStarting"
			dnsOps 'on_start'
		;;
	esac
	if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "statusFail" ]; then
		output 0 "$__OK__\\n";
		tmpfs del message
		tmpfs set status "statusSuccess"
		c="$(wc -l < "$outputFile")"
		tmpfs set stats "$serviceName is blocking $c domains (with ${targetDNS})"
		showstatus
	else
		output 0 "$__FAIL__\\n";
		tmpfs set status "statusFail"
		tmpfs add error "errorOhSnap"
		showstatus
	fi
	remove_lock
}

service_started() { is_ipset_procd && procd_set_config_changed firewall; }
service_stopped() { is_ipset_procd && procd_set_config_changed firewall; }
restart_service() { rc_procd start_service 'restart'; }
reload_service() { restart_service; }
restart() { restart_service; }
reload() { restart_service; }
dl() { rc_procd start_service 'download'; }
killcache() {
	rm -f "$addnhostsCache" "$addnhostsGzip"
	rm -f "$dnsmasqCache" "$dnsmasqGzip"
	rm -f "$ipsetCache" "$ipsetGzip"
	rm -f "$serversCache" "$serversGzip"
	rm -f "$unboundCache" "$unboundGzip"
	config_load 'dhcp'
	config_foreach dnsmasqOps 'dnsmasq' 'cleanup'
	uci -q commit 'dhcp'
	return 0
}

show() { showstatus; }
status_service() { showstatus; }
showstatus() {
	local status="$(tmpfs get status)"
	local message="$(tmpfs get message)"
	local error="$(tmpfs get error)"
	local stats="$(tmpfs get stats)"
	local c url
	if [ "$status" = "statusSuccess" ]; then
		output "$stats "; output_okn;
	else
		[ -n "$status" ] && status="$(getStatusText "$status")"
		if [ -n "$status" ] && [ -n "$message" ]; then 
			status="${status}: $message"
		fi
		[ -n "$status" ] && output "$serviceName $status\\n"
	fi
	if [ -n "$error" ]; then
		for c in $error; do
			url="${c##*|}"
			c="${c%|*}"
			case "$c" in
				errorDownloadingList|errorParsingList)
					output "$_ERROR_: $(getErrorText "$c") $url!\\n";;
				*)
					output "$_ERROR_: $(getErrorText "$c")!\\n";;
			esac
			let n=n+1
		done
	fi
}

stop_service() {
	load_package_config
	fw3Ops 'remove' 'all'
	if [ -s "$outputFile" ]; then
		output "Stopping $serviceName... "
		cacheOps 'create'
		if dnsOps 'on_stop'; then
			led_off "$led"
			output 0 "$__OK__\\n"; output_okn;
			tmpfs set status "statusStopped"
			tmpfs del message
		else 
			output 0 "$__FAIL__\\n"; output_fail;
			tmpfs set status "statusFail"
			tmpfs add error "errorStopping"
			output "$_ERROR_: $(getErrorText 'errorStopping')!\\n"
		fi
	fi
}

service_triggers() {
	procd_add_reload_trigger 'simple-adblock'
}

check() {
	load_package_config
	local string="$1" 
	local c="$(grep -c "$string" "$outputFile")"
	if [ ! -s "$outputFile" ]; then
		echo "No blacklist ('$outputFile') found."
	elif [ -z "$string" ]; then
		echo "Usage: /etc/init.d/${packageName} check string"
	elif [ "$c" -gt 0 ]; then
		if [ "$c" -gt 1 ]; then
			echo "Found $c matches for '$string' in '$outputFile':"
		else
			echo "Found 1 match for '$string' in '$outputFile':"
		fi
		case "$targetDNS" in
			dnsmasq.addnhosts)
				grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
			dnsmasq.conf)
				grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
			dnsmasq.ipset)
				grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
			dnsmasq.servers)
				grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
			unbound.adb_list)
				grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
		esac
	else
		echo "The $string is not found in current blacklist ('$outputFile')."
	fi
}

sizes() {
	local i
	load_package_config
	echo "# $(date)"

	for i in $blacklist_domains_urls; do
		[ "${i//melmac}" != "$i" ] && continue
		if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
			echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
			if compare_versions "$(du -sk /tmp/sast)" "500"; then
				echo "# blocklist too big for most routers"
			elif compare_versions "$(du -sk /tmp/sast)" "100"; then
				echo "# blocklist may be too big for some routers"
			fi
			rm -rf /tmp/sast
			echo "  list blacklist_domains_url '$i'"
			echo ""
		else
			echo "# site was down on last check"
			echo "#  list blacklist_domains_url '$i'"
			echo ""
		fi
	done

	for i in $blacklist_hosts_urls; do
		if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
			echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
			if compare_versions "$(du -sk /tmp/sast)" "500"; then
				echo "# blocklist too big for most routers"
			elif compare_versions "$(du -sk /tmp/sast)" "100"; then
				echo "# blocklist may be too big for some routers"
			fi
			rm -rf /tmp/sast
			echo "  list blacklist_hosts_url '$i'"
			echo ""
		else
			echo "# site was down on last check"
			echo "#  list blacklist_hosts_url '$i'"
			echo ""
		fi
	done
}