aboutsummaryrefslogtreecommitdiff
path: root/net/adblock/files/adblock.init
blob: 4efb7d46dc608d34c7014c5bcad415010c443a69 (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
#!/bin/sh /etc/rc.common
# 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

START=30
USE_PROCD=1

if [ -n "$(type -t extra_command)" ]
then
	extra_command "suspend" "Suspend adblock processing"
	extra_command "resume" "Resume adblock processing"
	extra_command "query" "<domain> Query active blocklists and backups for a specific domain"
	extra_command "report" "[<search>] Print DNS statistics with an optional search parameter"
	extra_command "list" "[<add>|<add_sha>|<add_utc>|<add_eng>|<add_stb>|<remove>|<remove_sha>|<remove_utc>|<remove_eng>|<remove_stb>] <source(s)> List/Edit available sources"
	extra_command "timer" "[<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals"
	extra_command "version" "Print version information"
else
	EXTRA_COMMANDS="status suspend resume query report list timer version"
	EXTRA_HELP="	status	Service status
	suspend	Suspend adblock processing
	resume	Resume adblock processing
	query	<domain> Query active blocklists and backups for a specific domain
	report	[<search>] Print DNS statistics with an optional search parameter
	list	[<add>|<add_sha>|<add_utc>|<add_eng>|<add_stb>|<remove>|<remove_sha>|<remove_utc>|<remove_eng>|<remove_stb>] <source(s)> List/Edit available sources
	timer	[<add> <tasks> <hour> [<minute>] [<weekday>]]|[<remove> <line no.>] List/Edit cron update intervals
	version	Print version information"
fi

adb_init="/etc/init.d/adblock"
adb_script="/usr/bin/adblock.sh"
adb_pidfile="/var/run/adblock.pid"

if [ -s "${adb_pidfile}" ] && { [ "${action}" = "start" ] || [ "${action}" = "stop" ] || \
	[ "${action}" = "restart" ] || [ "${action}" = "reload" ] || [ "${action}" = "report" ] || \
	[ "${action}" = "suspend" ] || [ "${action}" = "resume" ] || [ "${action}" = "query" ] || \
	{ [ "${action}" = "list" ] && [ -n "${1}" ]; }; }
then
	exit 0
fi

version()
{
	rc_procd "${adb_script}" version
}

boot()
{
	[ -s "${adb_pidfile}" ] && > "${adb_pidfile}"
	rc_procd start_service
}

start_service()
{
	if [ "$("${adb_init}" enabled; printf "%u" ${?})" -eq 0 ]
	then
		if [ "${action}" = "boot" ]
		then
			if [ -n "$(uci_get adblock global adb_trigger)" ]
			then
				return 0
			fi
		fi
		procd_open_instance "adblock"
		procd_set_param command "${adb_script}" "${@}"
		procd_set_param pidfile "${adb_pidfile}"
		procd_set_param nice "$(uci_get adblock global adb_nice "0")"
		procd_set_param stdout 1
		procd_set_param stderr 1
		procd_close_instance
	fi
}

reload_service()
{
	rc_procd start_service reload
}

stop_service()
{
	rc_procd "${adb_script}" stop
}

restart()
{
	rc_procd start_service restart
}

suspend()
{
	rc_procd start_service suspend
}

resume()
{
	rc_procd start_service resume
}

query()
{
	rc_procd "${adb_script}" query "${1}"
}

report()
{
	rc_procd "${adb_script}" report "${1:-"+"}" "${2:-"50"}" "${3:-"true"}" "${4:-"cli"}"
}

list()
{
	local src_archive src_file src_enabled enabled name sha_list utc_list action="${1}"

	if [ "${action%_*}" = "add" ] || [ "${action%_*}" = "remove" ]
	then
		shift
		for name in "${@}"
		do
			case "${action}" in
				"add")
					if [ -z "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
					then
						uci_add_list adblock global adb_sources "${name}"
						printf "%s\n" "::: adblock source '${name}' added to config"
					fi
				;;
				"remove")
					if [ -n "$(uci_get adblock global adb_sources | grep -Fo "${name}")" ]
					then
						uci_remove_list adblock global adb_sources "${name}"
						printf "%s\n" "::: adblock source '${name}' removed from config"
					fi
				;;
				"add_sha")
					if [ -z "$(uci_get adblock global adb_sha_sources | grep -Fo "${name}")" ]
					then
						uci_add_list adblock global adb_sha_sources "${name}"
						printf "%s\n" "::: adblock shallalist '${name}' added to config"
					fi
				;;
				"remove_sha")
					if [ -n "$(uci_get adblock global adb_sha_sources | grep -Fo "${name}")" ]
					then
						uci_remove_list adblock global adb_sha_sources "${name}"
						printf "%s\n" "::: adblock shallalist '${name}' removed from config"
					fi
				;;
				"add_utc")
					if [ -z "$(uci_get adblock global adb_utc_sources | grep -Fo "${name}")" ]
					then
						uci_add_list adblock global adb_utc_sources "${name}"
						printf "%s\n" "::: adblock utcapitole '${name}' added to config"
					fi
				;;
				"remove_utc")
					if [ -n "$(uci_get adblock global adb_utc_sources | grep -Fo "${name}")" ]
					then
						uci_remove_list adblock global adb_utc_sources "${name}"
						printf "%s\n" "::: adblock utcapitole '${name}' removed from config"
					fi
				;;
				"add_eng")
					if [ -z "$(uci_get adblock global adb_eng_sources | grep -Fo "${name}")" ]
					then
						uci_add_list adblock global adb_eng_sources "${name}"
						printf "%s\n" "::: adblock energized '${name}' added to config"
					fi
				;;
				"remove_eng")
					if [ -n "$(uci_get adblock global adb_eng_sources | grep -Fo "${name}")" ]
					then
						uci_remove_list adblock global adb_eng_sources "${name}"
						printf "%s\n" "::: adblock energized '${name}' removed from config"
					fi
				;;
				"add_stb")
					if [ -z "$(uci_get adblock global adb_stb_sources | grep -Fo "${name}")" ]
					then
						uci_add_list adblock global adb_stb_sources "${name}"
						printf "%s\n" "::: adblock stevenblack '${name}' added to config"
					fi
				;;
				"remove_stb")
					if [ -n "$(uci_get adblock global adb_stb_sources | grep -Fo "${name}")" ]
					then
						uci_remove_list adblock global adb_stb_sources "${name}"
						printf "%s\n" "::: adblock stevenblack '${name}' removed from config"
					fi
				;;
			esac
		done
		if [ -n "$(uci -q changes adblock)" ]
		then
			uci_commit adblock
			"${adb_init}" start
		fi
	else
		src_archive="$(uci_get adblock global adb_srcarc "/etc/adblock/adblock.sources.gz")"
		src_file="$(uci_get adblock global adb_srcfile "/tmp/adb_sources.json")"
		src_enabled="$(uci -q show adblock.global.adb_sources)"
		if [ -r "${src_archive}" ]
		then
			zcat "${src_archive}" > "${src_file}"
		else
			printf "%s\n" "::: adblock source archive '${src_archive}' not found"
		fi
		if [ -r "${src_file}" ]
		then
			src_enabled="${src_enabled#*=}"
			src_enabled="${src_enabled//\'}"
			printf "%s\n" "::: Available adblock sources"
			printf "%s\n" ":::"
			printf "%-25s%-10s%-7s%-21s%s\n" "    Name" "Enabled" "Size" "Focus" "Info URL"
			printf "%s\n" "    -------------------------------------------------------------------"
			json_load_file "${src_file}"
			json_get_keys keylist
			for key in ${keylist}
			do
				json_select "${key}"
				json_get_var size "size"
				json_get_var focus "focus"
				json_get_var descurl "descurl"
				json_get_var url "url"
				json_get_var rule "rule"
				if [ -n "${url}" ] && [ -n "${rule}" ]
				then
					if [ -n "$(printf "%s" "${src_enabled}" | grep -Fo "${key}")" ]
					then
						enabled="x"
					else
						enabled=" "
					fi
					src_enabled="${src_enabled/${key}}"
					printf "  + %-21s%-10s%-7s%-21s%s\n" "${key:0:20}" "${enabled}" "${size:0:3}" "${focus:0:20}" "${descurl:0:50}"
				else
					src_enabled="${src_enabled} ${key}"
				fi
				json_select ..
			done
			sha_list="$(uci_get adblock global adb_sha_sources "-")"
			utc_list="$(uci_get adblock global adb_utc_sources "-")"
			eng_list="$(uci_get adblock global adb_eng_sources "-")"
			stb_list="$(uci_get adblock global adb_stb_sources "-")"
			printf "%s\n" "    ---------------------------------------------------------------------------"
			printf "  * %s\n" "Configured shallalist categories: ${sha_list// /, }"
			printf "  * %s\n" "Configured utcapitole categories: ${utc_list// /, }"
			printf "  * %s\n" "Configured energized variants: ${eng_list// /, }"
			printf "  * %s\n" "Configured stevenblack variants: ${stb_list// /, }"

			if [ -n "${src_enabled// }" ]
			then
				printf "%s\n" "    ---------------------------------------------------------------------------"
				printf "%s\n" "    Sources without valid configuration"
				printf "%s\n" "    ---------------------------------------------------------------------------"
				for key in ${src_enabled}
				do
					printf "  - %s\n" "${key:0:20}"
				done
			fi
		else
			printf "%s\n" "::: adblock source file '${src_file}' not found"
		fi
	fi
}

status()
{
	status_service
}

status_service()
{
	local key keylist value idxval values rtfile

	rtfile="$(uci_get adblock global adb_rtfile "/tmp/adb_runtime.json")"

	json_load_file "${rtfile}" >/dev/null 2>&1
	json_get_keys keylist
	if [ -n "${keylist}" ]
	then
		printf "%s\n" "::: adblock runtime information"
		for key in ${keylist}
		do
			json_get_var value "${key}" >/dev/null 2>&1
			if [ "${key%_*}" = "active" ]
			then
				printf "  + %-15s : " "${key}"
				json_select "${key}" >/dev/null 2>&1
				values=""
				index=1
				while json_get_type type "${index}" && [ "${type}" = "object" ]
				do
					json_get_values idxval "${index}" >/dev/null 2>&1
					if [ "${index}" = "1" ]
					then
						values="${idxval}"
					else
						values="${values}, ${idxval}"
					fi
					index=$((index+1))
				done
				values="$(printf "%s" "${values}" | awk '{NR=1;max=98;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max)}else{printf"%-22s%s\n","",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}')"
				printf "%s\n" "${values:-"-"}"
				json_select ".."
			else
				printf "  + %-15s : %s\n" "${key}" "${value:-"-"}"
			fi
		done
	else
		printf "%s\n" "::: no adblock runtime information available"
	fi
}

timer()
{
	local cron_file cron_content cron_lineno action="${1:-"list"}" cron_tasks="${2}" hour="${3}" minute="${4:-0}" weekday="${5:-"*"}"

	cron_file="/etc/crontabs/root"

	if [ -s "${cron_file}" ] && [ "${action}" = "list" ]
	then
		awk '{print NR ">  " $0}' "${cron_file}"
	elif [ "${action}" = "add" ]
	then
		hour="${hour//[[:alpha:]]/}"
		minute="${minute//[[:alpha:]]/}"
		if [ -n "${cron_tasks}" ] && [ -n "${hour}" ] && [ -n "${minute}" ] && [ -n "${weekday}" ] && \
			[ "${hour}" -ge 0 ] && [ "${hour}" -le 23 ] && \
			[ "${minute}" -ge 0 ] && [ "${minute}" -le 59 ]
		then
			printf "%02d %02d %s\n" "${minute}" "${hour}" "* * ${weekday} ${adb_init} ${cron_tasks}" >> "${cron_file}"
			/etc/init.d/cron restart
		fi
	elif [ -s "${cron_file}" ] && [ "${action}" = "remove" ]
	then
		cron_tasks="${cron_tasks//[[:alpha:]]/}"
		cron_lineno="$(awk 'END{print NR}' "${cron_file}")"
		cron_content="$(awk '{print $0}' "${cron_file}")"
		if [ "${cron_tasks:-"0"}" -le "${cron_lineno:-"1"}" ] && [ -n "${cron_content}" ]
		then
			printf "%s\n" "${cron_content}" | awk "NR!~/^${cron_tasks}$/" > "${cron_file}"
			/etc/init.d/cron restart
		fi
	fi
}

service_triggers()
{
	local iface delay

	iface="$(uci_get adblock global adb_trigger)"
	delay="$(uci_get adblock global adb_triggerdelay "5")"
	PROCD_RELOAD_DELAY=$((delay*1000))

	if [ -z "${iface}" ]
	then
		. "/lib/functions/network.sh"
		network_find_wan iface
		if [ -n "${iface}" ]
		then
			uci_set adblock global adb_trigger "${iface}"
			uci_commit "adblock"
		fi
	fi
	if [ -n "${iface}" ]
	then
		procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" "start"
	fi
	procd_add_reload_trigger "adblock"
}