-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathipc-tap.sh
executable file
·189 lines (147 loc) · 3.59 KB
/
ipc-tap.sh
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
#!/bin/bash
# ipc-tap.sh - Interceptor for toolbox IPC PubSub messages
# Copyright (C) 2022-2023 Matthias Kruk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
add_topic() {
# local name="$1" # not needed
local value="$2"
# topics is inherited from main() via opt_parse()
topics+=("$value")
return 0
}
add_hook() {
# local name="$1" # not needed
local value="$2"
local topic
local hook
topic="${value%%:*}"
hook="${value#*:}"
# hooks is inherited from main() via opt_parse()
hooks["$topic"]="$hook"
}
signal_handler() {
signal_received=1
}
output_message() {
local type="$1"
local data="$2"
local message
if ! message=$(json_object "type" "$type" \
"data" "$data"); then
return 1
fi
if ! base64 -w 0 <<< "$message"; then
return 1
fi
printf '\n'
return 0
}
invoke_hooks() {
local topic="$1"
local message="$2"
local data
local hook
hook="${hooks[$topic]}"
if [[ -n "$hook" ]] &&
data=$("$hook" <<< "$message") &&
[[ -n "$data" ]]; then
output_message "HookData" "$data"
fi
return 0
}
tap_topics() {
local endpoint_name="$1"
local topics=("${@:2}")
local endpoint
local topic
local -i err
local -ig signal_received
err=0
signal_received=0
if ! endpoint=$(ipc_endpoint_open "$endpoint_name"); then
return 1
fi
log_info "Using endpoint $endpoint"
trap signal_handler INT HUP TERM ABRT ALRM USR1 USR2 PIPE
for topic in "${topics[@]}"; do
log_info "Subscribing $endpoint to $topic"
if ! ipc_endpoint_subscribe "$endpoint" "$topic"; then
log_error "Could not subscribe $endpoint to $topic"
err=1
break
fi
done
if (( err == 0 )); then
log_info "Waiting for messages"
while (( signal_received == 0 )); do
local message
local topic
if ! message=$(ipc_endpoint_recv "$endpoint" 5); then
continue
fi
if ! topic=$(ipc_msg_get_topic "$message"); then
log_warn "Dropping message without topic"
continue
fi
invoke_hooks "$topic" "$message"
output_message "IPCMessage" "$message"
done
fi
log_info "Closing endpoint $endpoint"
ipc_endpoint_close "$endpoint"
return "$err"
}
main() {
local endpoint
local proto
local topics
declare -gA hooks
topics=()
opt_add_arg "t" "topic" "rv" "" \
"A topic to tap into" \
'' \
add_topic
opt_add_arg "k" "hook" "v" "" \
"Hook to execute upon receipt" \
'^[^:]+:.+$' \
add_hook
opt_add_arg "p" "proto" "v" "ipc" \
"The IPC protocol to tap" \
'^u?ipc$'
opt_add_arg "e" "endpoint" "v" "" \
"The IPC endpoint to use"
if ! opt_parse "$@"; then
return 1
fi
proto=$(opt_get "proto")
if ! include "$proto"; then
return 1
fi
endpoint=$(opt_get "endpoint")
if ! tap_topics "$endpoint" "${topics[@]}"; then
return 1
fi
return 0
}
{
if ! . toolbox.sh; then
exit 1
fi
if ! include "log" "opt" "json"; then
exit 1
fi
main "$@"
exit "$?"
}