aboutsummaryrefslogtreecommitdiff
path: root/packages/openwrt/net/nDPId-testing/nDPId-testing.init
blob: 4ccee51a061c83abc20b853326f931eccffa66bd (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
#!/bin/sh /etc/rc.common

START=99
STOP=80

USE_PROCD=1

NDPID_SUFFIX="-testing"
NDPISRVD_NAME="nDPIsrvd${NDPID_SUFFIX}"
NDPISRVD_BIN="/usr/bin/${NDPISRVD_NAME}"
NDPISRVD_ANALYSED_NAME="nDPIsrvd${NDPID_SUFFIX}-analysed"
NDPISRVD_ANALYSED_BIN="/usr/bin/${NDPISRVD_ANALYSED_NAME}"
NDPISRVD_CAPTURED_NAME="nDPIsrvd${NDPID_SUFFIX}-captured"
NDPISRVD_CAPTURED_BIN="/usr/bin/${NDPISRVD_CAPTURED_NAME}"
NDPID_NAME="nDPId${NDPID_SUFFIX}"
NDPID_BIN="/usr/sbin/${NDPID_NAME}"
DISTRIBUTOR_SOCKET="/var/run/${NDPID_NAME}/nDPIsrvd-distributor.sock"

print_arg_bool() {
	local cfg="$1"
	local var="$2"
	local opt="$3"
	local aux

	config_get_bool aux "$cfg" "$var" '0'
	if [ $aux -ne 0 ]; then
		printf ' %s' "$opt"
	fi
}

print_arg_str() {
	local cfg="$1"
	local var="$2"
	local opt="$3"
	local aux

	config_get aux "$cfg" "$var"
	if [ ! -z "$aux" ]; then
		printf ' %s' "$opt$aux"
	fi
}

start_ndpid_instance() {
	local cfg=$1
	local aux
	local args
	local netif
	local sink

	config_get_bool aux "$cfg" 'enabled' '0'
	if [ "$aux" -eq 0 ]; then
		return 1
	fi

	# General
	config_get_bool netif "$cfg" 'interface'
	if [ -z "$netif" ]; then
		netif='default'
	fi

	config_get sink "$cfg" 'udp_connect'
	if [ -z "$sink" ]; then
		sink="/var/run/${NDPID_NAME}/nDPIsrvd-collector.sock"
	fi

	args="$(print_arg_str "$cfg" 'interface' '-i')"
	args="$args$(print_arg_bool "$cfg" 'use_pfring' '-r')"
	args="$args$(print_arg_bool "$cfg" 'internal_only' '-I')"
	args="$args$(print_arg_bool "$cfg" 'external_only' '-E')"
	args="$args$(print_arg_str "$cfg" 'bpf_filter' '-B')"
	args="$args$(print_arg_bool "$cfg" 'use_poll' '-e')"
	args="$args$(print_arg_str "$cfg" 'proto_file' '-P')"
	args="$args$(print_arg_str "$cfg" 'cat_file' '-C')"
	args="$args$(print_arg_str "$cfg" 'ja4_file' '-J')"
	args="$args$(print_arg_str "$cfg" 'ssl_file' '-S')"
	args="$args$(print_arg_str "$cfg" 'alias' '-a')"
	args="$args$(print_arg_bool "$cfg" 'analysis' '-A')"
	args="$args$(print_arg_bool "$cfg" 'compression' '-z')"

	# Tuning
	args="$args$(print_arg_str "$cfg" 'max_flows_per_thread' '-omax-flows-per-thread=')"
	args="$args$(print_arg_str "$cfg" 'max_idle_flows_per_thread' '-omax-idle-flows-per-thread=')"
	args="$args$(print_arg_str "$cfg" 'max_reader_threads' '-omax-reader-threads=')"
	args="$args$(print_arg_str "$cfg" 'daemon_status_interval' '-odaemon-status-interval=')"
	args="$args$(print_arg_str "$cfg" 'compression_scan_interval' '-ocompression-scan-interval=')"
	args="$args$(print_arg_str "$cfg" 'compression_flow_inactivity' '-ocompression-flow-inactivity=')"
	args="$args$(print_arg_str "$cfg" 'flow_scan_interval' '-oflow-scan-interval=')"
	args="$args$(print_arg_str "$cfg" 'generic_max_idle_time' '-ogeneric-max-idle-time=')"
	args="$args$(print_arg_str "$cfg" 'icmp_max_idle_time' '-oicmp-max-idle-time=')"
	args="$args$(print_arg_str "$cfg" 'udp_max_idle_time' '-oudp-max-idle-time=')"
	args="$args$(print_arg_str "$cfg" 'tcp_max_idle_time' '-otcp-max-idle-time=')"
	args="$args$(print_arg_str "$cfg" 'tcp_max_post_end_flow_time' '-otcp-max-post-end-flow-time=')"
	args="$args$(print_arg_str "$cfg" 'max_packets_per_flow_to_send' '-omax-packets-per-flow-to-send=')"
	args="$args$(print_arg_str "$cfg" 'max_packets_per_flow_to_process' '-omax-packets-per-flow-to-process=')"
	args="$args$(print_arg_str "$cfg" 'max_packets_per_flow_to_analyse' '-omax-packets-per-flow-to-analyse=')"
	args="$args$(print_arg_str "$cfg" 'error_event_threshold_n' '-oerror-event-threshold-n=')"
	args="$args$(print_arg_str "$cfg" 'error_event_threshold_time' '-oerror-event-threshold-time=')"

	procd_open_instance
	procd_set_param command ${NDPID_BIN}
	procd_set_param pidfile "/var/run/${NDPID_NAME}/nDPId-${netif}.pid"
	procd_append_param command -undpi -gndpi -p"/var/run/${NDPID_NAME}/nDPId-${netif}.pid" -c"${sink}" $args

	config_get_bool aux "$cfg" 'respawn' '0'
	[ "$aux" = 1 ] && procd_set_param respawn

	procd_close_instance
}

validate_ndpid_section() {
	uci_load_validate ${NDPID_NAME} nDPId "$1" "$2" \
		'enabled:bool:0' \
		'respawn:bool:0' \
		'interface:string' \
		'internal_only:bool:0' \
		'external_only:bool:0' \
		'bpf_filter:string' \
		'udp_connect:string' \
		'proto_file:string' \
		'cat_file:string' \
		'ja4_file:string' \
		'ssl_file:string' \
		'alias:string' \
		'analysis:bool:0' \
		'compression:bool:0' \
		'max_flows_per_thread:uinteger' \
		'max_idle_flows_per_thread:uinteger' \
		'max_reader_threads:uinteger' \
		'daemon_status_interval:uinteger' \
		'compression_scan_interval:uinteger' \
		'compression_flow_inactivity:uinteger' \
		'flow_scan_interval:uinteger' \
		'generic_max_idle_time:uinteger' \
		'icmp_max_idle_time:uinteger' \
		'udp_max_idle_time:uinteger' \
		'tcp_max_idle_time:uinteger' \
		'tcp_max_post_end_flow_time:uinteger' \
		'max_packets_per_flow_to_send:uinteger' \
		'max_packets_per_flow_to_process:uinteger' \
		'max_packets_per_flow_to_analyse:uinteger' \
		'error_event_threshold_n:uinteger' \
		'error_event_threshold_time:uinteger'
}

start_ndpisrvd_instance() {
	local cfg=$1
	local aux
	local args
	local collector_sock

	config_get_bool aux "$cfg" 'enabled' '0'
	if [ "$aux" -eq 0 ]; then
		return 1
	fi

	collector_sock="/var/run/${NDPID_NAME}/nDPIsrvd-collector.sock"

	args="$(print_arg_str "$cfg" 'tcp_listen' '-S')"
	args="$args$(print_arg_str "$cfg" 'max_remotes' '-m')"
	args="$args$(print_arg_str "$cfg" 'max_buffered_json_lines' '-C')"

	procd_open_instance
	procd_set_param command ${NDPISRVD_BIN}
	procd_set_param pidfile "/var/run/${NDPID_NAME}/nDPIsrvd.pid"
	procd_append_param command -undpi -gndpi -p"/var/run/${NDPID_NAME}/nDPIsrvd.pid" -c"${collector_sock}" -s"${DISTRIBUTOR_SOCKET}" $args

	config_get_bool aux "$cfg" 'respawn' '0'
	[ "$aux" = 1 ] && procd_set_param respawn

	procd_close_instance
}

validate_ndpisrvd_section() {
	uci_load_validate ${NDPID_NAME} nDPId "$1" "$2" \
		'enabled:bool:0' \
		'respawn:bool:0' \
		'tcp_listen:string' \
		'max_remotes:uinteger' \
		'max_buffered_json_lines:uinteger'
}

start_ndpisrvd_analysed_instance() {
	local cfg="$1"
	local csv_dir

	config_get_bool aux "$cfg" 'enabled' '0'
	if [ "$aux" -eq 0 ]; then
		return 1
	fi

	config_get aux "$cfg" 'csv_dir'
	if [ -z "$aux" ]; then
		return 1
	fi
	mkdir -p "$aux"
	chown ndpi:ndpi "$aux"

	csv_dir="$(print_arg_str "$cfg" 'csv_dir' '')"
    csv_dir="-o${csv_dir}/nDPId-analyse.csv"

	procd_open_instance
	procd_set_param command ${NDPISRVD_ANALYSED_BIN}
	procd_set_param pidfile "/var/run/${NDPID_NAME}/nDPIsrvd-analysed.pid"
	procd_append_param command -undpi -gndpi -p"/var/run/${NDPID_NAME}/nDPIsrvd-analysed.pid" -s"${DISTRIBUTOR_SOCKET}" ${csv_dir}

	config_get_bool aux "$cfg" 'respawn' '0'
	[ "$aux" = 1 ] && procd_set_param respawn

	procd_close_instance
}

validate_ndpisrvd_analysed_section() {
	uci_load_validate ${NDPID_NAME} nDPId "$1" "$2" \
		'enabled:bool:0' \
		'respawn:bool:0' \
		'csv_dir:string'
}

start_ndpisrvd_captured_instance() {
	local cfg="$1"
	local args
	local risk_args

	config_get_bool aux "$cfg" 'enabled' '0'
	if [ "$aux" -eq 0 ]; then
		return 1
	fi

	config_get aux "$cfg" 'datadir'
	if [ -z "$aux" ]; then
		return 1
	fi
	mkdir -p "$aux"
	chown ndpi:ndpi "$aux"

	handle_capture_risks() {
		local risk=" -R$1"
		printf '%s' "$risk"
	}
	risk_args="$(config_list_foreach "$cfg" capture_risk handle_capture_risks)"

	args="$(print_arg_str "$cfg" 'pcap_rotation' '-r')"
	args="$args$(print_arg_str "$cfg" 'datadir' '-D')"
	args="$args$(print_arg_bool "$cfg" 'capture_guessed' '-G')"
	args="$args$(print_arg_bool "$cfg" 'capture_undetected' '-U')"
	args="$args$(print_arg_bool "$cfg" 'capture_midstream' '-M')"
	args="$args$(print_arg_bool "$cfg" 'ignore_empty_l4' '-E')"

	procd_open_instance
	procd_set_param command ${NDPISRVD_CAPTURED_BIN}
	procd_set_param pidfile "/var/run/${NDPID_NAME}/nDPIsrvd-captured.pid"
	procd_append_param command -undpi -gndpi -p"/var/run/${NDPID_NAME}/nDPIsrvd-captured.pid" -s"${DISTRIBUTOR_SOCKET}" $args ${risk_args}

	config_get_bool aux "$cfg" 'respawn' '0'
	[ "$aux" = 1 ] && procd_set_param respawn

	procd_close_instance
}

validate_ndpisrvd_captured_section() {
	uci_load_validate ${NDPID_NAME} nDPId "$1" "$2" \
		'enabled:bool:0' \
		'respawn:bool:0' \
		'pcap_rotation:uinteger' \
		'datadir:string' \
		'capture_guessed:bool:1' \
		'capture_undetected:bool:1' \
		'capture_midstream:bool:1' \
		'ignore_empty_l4:bool:1' \
		'capture_risk:list(string)'
}

start_service() {
	config_load ${NDPID_NAME}

	user_exists ndpi 6543 || user_add ndpi 6543
	group_exists ndpi 6543 || group_add ndpi 6543

	mkdir -p "/var/run/${NDPID_NAME}"
	chown ndpi:ndpi "/var/run/${NDPID_NAME}"

	config_foreach validate_ndpisrvd_section nDPIsrvd start_ndpisrvd_instance
	config_foreach validate_ndpisrvd_analysed_section nDPIsrvd-analysed start_ndpisrvd_analysed_instance
	config_foreach validate_ndpisrvd_captured_section nDPIsrvd-captured start_ndpisrvd_captured_instance
	config_foreach validate_ndpid_section nDPId start_ndpid_instance
}