-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathservice-event.sh
More file actions
359 lines (300 loc) · 13.2 KB
/
service-event.sh
File metadata and controls
359 lines (300 loc) · 13.2 KB
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
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Execute commands when specific service events occurs
#
# Implements basic service-event script handler from Asuswrt-Merlin:
# https://github.com/RMerl/asuswrt-merlin.ng/wiki/User-scripts
#
# There is no blocking so there is no guarantee that this script will run before the event happens.
# You will probably want add extra code if you want to run code after the event happens.
# Scripts from this repository are already integrated.
#
#jas-update=service-event.sh
#shellcheck shell=ash
#shellcheck disable=SC2155
#shellcheck source=./common.sh
readonly common_script="$(dirname "$0")/common.sh"
if [ -f "$common_script" ]; then . "$common_script"; else { echo "$common_script not found" >&2; exit 1; } fi
SYSLOG_FILE="/tmp/syslog.log" # target syslog file to read
SLEEP=1 # how to long to wait between each syslog reading iteration, increase to reduce load but introduce delays in action execution
SLEEP_INTEGRATION=3 # how many seconds to wait before executing integration related scripts, this also delays execution of EXECUTE_COMMAND when applicable, set to empty to disable
NO_INTEGRATION=false # set to true to disable integration with jacklul/asuswrt-scripts, this can potentially break their functionality
EXECUTE_COMMAND="" # command to execute in addition to build-in script (receives arguments: $1 = event, $2 = target, $3 = normalized event name)
load_script_config
is_merlin_firmware && merlin=true
[ -z "$SLEEP" ] && SLEEP=1 # this cannot be empty, set to the lowest possible value
state_file="$TMP_DIR/$script_name"
readonly CHECK_CHAIN="jas-$script_name"
readonly CHECK_IP="127.83.69.33/8" # asci SE! = service event !
custom_checks() {
change_interface=false
change_firewall=false
#change_wan=false
if ! ip addr show dev lo 2> /dev/null | grep -Fq "inet $CHECK_IP "; then
ip -4 addr add "$CHECK_IP" dev lo label lo:se
change_interface_detected=true
elif [ -n "$change_interface_detected" ]; then
change_interface=true
change_interface_detected=
fi
if ! iptables -nL "$CHECK_CHAIN" > /dev/null 2>&1; then
iptables -N "$CHECK_CHAIN"
change_firewall_detected=true
elif [ -n "$change_firewall_detected" ]; then
change_firewall=true
change_firewall_detected=
fi
# Currently disabled as it triggers on script start because $wan_state_last is not stored persistently
#wan_state="$(nvram get wan0_state_t 2> /dev/null)$(nvram get wan1_state_t 2> /dev/null)"
#if [ "$wan_state" != "$wan_state_last" ]; then
# wan_state_last="$wan_state"
# change_wan_detected=true
#elif [ -n "$change_wan_detected" ]; then
# change_wan=true
# change_wan_detected=
#fi
if [ "$change_interface" = true ] || [ "$change_firewall" = true ]; then # || [ "$change_wan" = true ]
return 1
fi
return 0
}
trigger_event() {
local _action="$1"
local _target="$2"
if [ "$3" = "ccheck" ]; then # this argument disables event verification timers in the event handler
lockfile check "event_${_action}_${_target}" && return # already processing this event
logecho "Running script (args: '$_action' '$_target') *" alert
else
logecho "Running script (args: '$_action' '$_target')" alert
fi
sh "$script_path" event "$_action" "$_target" "$3" &
}
service_monitor() {
[ ! -f "$SYSLOG_FILE" ] && { logecho "Error: Syslog log file does not exist: $SYSLOG_FILE" error; exit 1; }
lockfile lockfail || { echo "Failed to start - already running! ($lockpid)" >&2; exit 1; }
set -e
logecho "Started service event monitoring..." alert
local _last_line _initialized
if [ -f "$state_file" ]; then
_last_line="$(cat "$state_file")"
_initialized=true
else
_last_line="$(wc -l < "$SYSLOG_FILE")"
_last_line="$((_last_line+1))"
custom_checks || true
fi
local _total_lines _new_lines _matching_lines _last_line_old _new_line _line_number _events _oldIFS _event _event_action _event_target _event_triggered
while true; do
_total_lines="$(wc -l < "$SYSLOG_FILE")"
if [ "$_total_lines" -lt "$((_last_line-1))" ]; then
logecho "Log file has been rotated, resetting line pointer..." alert
_last_line=1
continue
fi
_new_lines="$(tail "$SYSLOG_FILE" -n "+$_last_line")"
if [ -n "$_new_lines" ]; then
_matching_lines="$(echo "$_new_lines" | grep -En 'rc_service.*notify_rc' || echo '')"
if [ -n "$_matching_lines" ]; then
_last_line_old=$_last_line
IFS="$(printf '\n\b')"
for _new_line in $_matching_lines; do
_line_number="$(echo "$_new_line" | cut -d ':' -f 1)"
_last_line="$((_last_line_old+_line_number))"
if [ -n "$_initialized" ]; then
_events="$(echo "$_new_line" | awk -F 'notify_rc ' '{print $2}')"
_oldIFS=$IFS
IFS=';'
for _event in $_events; do
if [ -n "$_event" ]; then
_event_action="$(echo "$_event" | cut -d '_' -f 1)"
_event_target="$(echo "$_event" | cut -d '_' -f 2- | cut -d ' ' -f 1)"
trigger_event "$_event_action" "$_event_target"
_event_triggered=true
fi
done
IFS=$_oldIFS
fi
done
else
_total_lines="$(echo "$_new_lines" | wc -l)"
_last_line="$((_last_line+_total_lines))"
fi
fi
if [ -z "$_event_triggered" ] && ! custom_checks; then
if [ "$change_interface" = true ]; then # || [ "$change_wan" = true ]
trigger_event "restart" "net" "ccheck"
fi
if [ "$change_firewall" = true ]; then
trigger_event "restart" "firewall" "ccheck"
fi
fi
echo "$_last_line" > "$state_file"
[ -z "$_initialized" ] && _initialized=true
_event_triggered=
sleep "$SLEEP"
done
lockfile unlock
}
integrated_event() {
[ "$NO_INTEGRATION" = true ] && return
[ -z "$merlin" ] && [ -n "$SLEEP_INTEGRATION" ] && sleep "$SLEEP_INTEGRATION"
local _tmp_script_path
# $1 = type, $2 = event, $3 = target, $4 = extra
case "$1" in
"firewall")
execute_script_basename "vpn-kill-switch.sh" run
execute_script_basename "vpn-firewall.sh" run
execute_script_basename "wgs-lan-only.sh" run
execute_script_basename "force-dns.sh" run
execute_script_basename "vpn-ip-routes.sh" run
execute_script_basename "vpn-vserver.sh" run
execute_script_basename "vpn-samba.sh" run
;;
"network")
execute_script_basename "usb-network.sh" run
execute_script_basename "extra-ip.sh" run
execute_script_basename "dynamic-dns.sh" run
# most services here also restart firewall, wireless and/or recreate configs so execute these too
integrated_event firewall "$2" "$3" "$4"
integrated_event wireless "$2" "$3" "$4"
integrated_event custom_configs "$2" "$3" "$4"
;;
"wireless")
# probably a good idea to run this just in case
execute_script_basename "disable-wps.sh" run
# this service event recreates rc_support so we have to re-run this script
_tmp_script_path="$(resolve_script_basename "modify-features.sh")"
if [ -n "$_tmp_script_path" ] && [ -x "$_tmp_script_path" ]; then
if [ -z "$merlin" ] && [ "$4" != "ccheck" ]; then
local timer=30; while { # wait till rc_support is modified
sh "$_tmp_script_path" check
} && [ "$timer" -ge 0 ]; do
timer=$((timer-1))
sleep 1
done
fi
sh "$_tmp_script_path" run
fi
;;
"usb_idle")
# re-run in case script exited due to USB idle being set and now it has been disabled
execute_script_basename "swap.sh" run
;;
"custom_configs")
_tmp_script_path="$(resolve_script_basename "custom-configs.sh")"
if [ -n "$_tmp_script_path" ] && [ -x "$_tmp_script_path" ] && [ -z "$merlin" ]; then
# delay the execution to let the calling service create their config
{ sleep 10 && sh "$_tmp_script_path" run; } &
fi
;;
esac
}
run_in_background() {
[ -n "$merlin" ] && exit # Do not run on Asuswrt-Merlin firmware
lockfile check && { [ -n "$IS_INTERACTIVE" ] && echo "Already running! ($lockpid)" >&2; exit 1; }
if is_started_by_system && [ "$PPID" -ne 1 ]; then
{ nohup "$script_path" run > /dev/null 2>&1 & } &
else
service_monitor
fi
}
case "$1" in
"run")
run_in_background
;;
"event")
if [ "$4" = "ccheck" ]; then
lockfile lockfail "event_${2}_${3}" || { echo "Error: This event is already being processed (args: '$2' '$3')" >&2; exit 1; }
else
lockfile lockwait "event_${2}_${3}"
fi
event="$3"
# $2 = event, $3 = target, $4 = extra
case "$3" in
"firewall"|"vpnc_dev_policy"|"pms_device"|"ftpd"|"ftpd_force"|"tftpd"|"aupnpc"|"chilli"|"CP"|"radiusd"|"webdav"|"enable_webdav"|"time"|"snmpd"|"vpnc"|"vpnd"|"pptpd"|"openvpnd"|"wgs"|"yadns"|"dnsfilter"|"tr"|"tor")
event=firewall
if [ -z "$merlin" ] && [ "$4" != "ccheck" ]; then
timer=10; while { # wait till our chains disappear
iptables -nL "$CHECK_CHAIN" > /dev/null 2>&1
} && [ "$timer" -ge 0 ]; do
timer=$((timer-1))
sleep 1
done
fi
integrated_event firewall "$2" "$3" "$4"
;;
"allnet"|"net_and_phy"|"net"|"multipath"|"subnet"|"wan"|"wan_if"|"dslwan_if"|"dslwan_qis"|"dsl_wireless"|"wan_line"|"wan6"|"wan_connect"|"wan_disconnect"|"isp_meter")
event=network
if [ -z "$merlin" ] && [ "$4" != "ccheck" ]; then
timer=10; while { # wait until wan goes down
{ [ "$(nvram get wan0_state_t)" = "2" ] || [ "$(nvram get wan0_state_t)" = "0" ] || [ "$(nvram get wan0_state_t)" = "5" ] ; } &&
{ [ "$(nvram get wan1_state_t)" = "2" ] || [ "$(nvram get wan1_state_t)" = "0" ] || [ "$(nvram get wan1_state_t)" = "5" ] ; };
} && [ "$timer" -ge 0 ]; do
timer=$((timer-1))
sleep 1
done
timer=30; while { # wait until wan goes up
[ "$(nvram get wan0_state_t)" != "2" ] &&
[ "$(nvram get wan1_state_t)" != "2" ];
} && [ "$timer" -ge 0 ]; do
timer=$((timer-1))
sleep 1
done
fi
integrated_event network "$2" "$3" "$4"
;;
"wireless")
integrated_event wireless "$2" "$3" "$4"
;;
"usb_idle")
integrated_event usb_idle "$2" "$3" "$4"
;;
"custom_configs"|"nasapps"|"ftpsamba"|"samba"|"samba_force"|"pms_account"|"media"|"dms"|"mt_daapd"|"upgrade_ate"|"mdns"|"dnsmasq"|"dhcpd"|"stubby"|"upnp"|"quagga")
event=custom_configs
integrated_event custom_configs "$2" "$3" "$4"
;;
esac
case "${2}_${3}" in
"ipsec_set"|"ipsec_start"|"ipsec_restart") # these do not follow the naming scheme ("<ACTION>_<SERVICE>")
event=firewall
integrated_event firewall "$2" "$3" "$4"
;;
esac
[ -n "$EXECUTE_COMMAND" ] && eval "$EXECUTE_COMMAND $2 $3 $event"
lockfile unlock "event_${2}_${3}"
exit
;;
"start")
if [ -n "$merlin" ]; then # use service-event-end on Asuswrt-Merlin firmware
if [ ! -f /jffs/scripts/service-event-end ]; then
cat <<EOT > /jffs/scripts/service-event-end
#!/bin/sh
EOT
chmod 0755 /jffs/scripts/service-event-end
fi
if ! grep -Fq "$script_path" /jffs/scripts/service-event-end; then
echo "$script_path event \"\$1\" \"\$2\" & # https://github.com/jacklul/asuswrt-scripts" >> /jffs/scripts/service-event-end
fi
else
crontab_entry add "*/1 * * * * $script_path run"
if is_started_by_system; then
run_in_background
else
echo "Will launch within one minute by cron..." >&2
fi
fi
;;
"stop")
crontab_entry delete
lockfile kill
;;
"restart")
sh "$script_path" stop
sh "$script_path" start
;;
*)
echo "Usage: $0 run|start|stop|restart"
exit 1
;;
esac