summaryrefslogtreecommitdiffstats
path: root/net/adblock/files/adblock.init
blob: 798a8bda9508309f95b611e21e7a798e5b7d24e1 (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
#!/bin/sh /etc/rc.common
# Copyright (c) 2015-2026 Dirk Brenken (dev@brenken.org)
# This is free software, licensed under the GNU General Public License v3.

# (s)hellcheck exceptions
# shellcheck disable=all

START=30
USE_PROCD=1

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" "[<cli>|<mail>|<gen>|<json>] Print DNS statistics"

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

if [ -z "${IPKG_INSTROOT}" ]; then
	if [ "${action}" = "boot" ] && "${adb_init}" running; then
		exit 0
	elif [ -s "${adb_pidfile}" ] &&
		{ [ "${action}" = "start" ] || [ "${action}" = "stop" ] || [ "${action}" = "restart" ] ||
		[ "${action}" = "reload" ] || [ "${action}" = "report" ] || [ "${action}" = "suspend" ] ||
		[ "${action}" = "resume" ] || [ "${action}" = "query" ]; }; then
		exit 1
	fi
fi

boot() {
	: >"${adb_pidfile}"
	rc_procd start_service boot
}

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

restart() {
	stop_service "restart"
	rc_procd start_service restart
}

reload_service() {
	rc_procd start_service reload
}

stop_service() {
	[ -z "${1}" ] && rc_procd "${adb_script}" stop
}

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:-"cli"}" "${2}" "${3}" "${4}"
}

status() {
	status_service
}

status_service() {
	local key keylist value values

	json_init
	json_load_file "/var/run/adb_runtime.json" >/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_feeds" ]; then
				json_get_values values "${key}" >/dev/null 2>&1
				value="${values}"
			fi
			printf "  + %-15s : %s\n" "${key}" "${value:-"-"}"
		done
	else
		printf "%s\n" "::: no adblock runtime information available"
	fi
}

service_triggers() {
	local iface delay

	delay="$(uci_get adblock global adb_triggerdelay "5")"
	trigger="$(uci_get adblock global adb_trigger)"

	PROCD_RELOAD_DELAY="$((delay * 1000))"
	for iface in ${trigger}; do
		procd_add_interface_trigger "interface.*.up" "${iface}" "${adb_init}" start
	done
}