summaryrefslogtreecommitdiffstats
path: root/net/adblock/files/adblock.init
blob: 93869155763109ddc71c142b69d62ab9dd76f81c (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
#!/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 "search" "<domain> Search 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_rundir="/var/run/adblock"
adb_pidfile="/var/run/adblock/adblock.pid"
adb_rtfile="/var/run/adblock/adblock.runtime.json"

if [ -z "${IPKG_INSTROOT}" ]; then

	# ensure runtime directory exists
	#
	[ ! -d "${adb_rundir}" ] && mkdir -p "${adb_rundir}"

	# check for running instance and handle boot trigger
	#
	case "${action}" in
	"boot")
		"${adb_init}" running && exit 0
		;;
	esac

	# reset pidfile if no/stale process is found,
	# otherwise exit with error to prevent multiple instances
	#
	if [ -s "${adb_pidfile}" ]; then
		pid="$(cat "${adb_pidfile}" 2>/dev/null)"
		if [ -n "${pid}" ] && kill -0 "${pid}" 2>/dev/null; then
			case "${action}" in
			"start" | "stop" | "restart" | "reload" | "report" | "suspend" | "resume" | "search")
				exit 1
				;;
			esac
		else
			: >"${adb_pidfile}"
		fi
	fi
fi

boot() {
	rc_procd start_service boot
}

start_service() {
	local status backend blocklist

	if "${adb_init}" enabled; then
		if [ "${action}" = "boot" ]; then
			[ -n "$(uci_get adblock global adb_trigger)" ] && return 0
		fi
		if [ "${1}" = "refresh" ]; then
			json_init
			json_load_file "${adb_rtfile}" >/dev/null 2>&1
			json_get_var status "adblock_status"
			json_get_var backend "dns_backend"
			if [ -n "${status}" ] && [ "${status}" != "error" ]; then
				[ "${status}" != "enabled" ] && return 0
				blocklist="${backend#*, }"
				blocklist="${blocklist%%,*}/adb_list.overall"
				[ -s "${blocklist}" ] && return 0
			fi
		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
}

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

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

status() {
	status_service
}

status_service() {
	local key keylist type value values

	json_init
	json_load_file "${adb_rtfile}" >/dev/null 2>&1
	json_get_keys keylist
	if [ -n "${keylist}" ]; then
		printf '%s\n' "::: adblock runtime information"
		for key in ${keylist}; do
			[ "${key}" = "backup_cnt" ] && continue
			json_get_type type "${key}" >/dev/null 2>&1
			if [ "${type}" = "array" ]; then
				json_get_values values "${key}" >/dev/null 2>&1
				value="${values// /, }"
			else
				json_get_var value "${key}" >/dev/null 2>&1
			fi
			printf '  + %-15s : %s\n' "${key}" "${value:-"-"}"
		done
	else
		printf '%s\n' "::: no adblock runtime information available"
	fi
}

service_triggers() {
	local iface delay trigger

	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.*" "${iface}" "${adb_init}" start refresh
	done
}