From 257cfaae26eb20db54762e2d0e7211e5f08553d0 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Thu, 9 Mar 2023 20:11:38 +0900 Subject: [PATCH] utils: Make ipc-tap and ipc-inject use the same endpoint When ipc-sshtunnel is used to tunnel a pubsub topic in both directions, ipc-tap will receive messages sent from the ipc-inject instance of the same tunnel. This makes it impossible to use ipc-sshtunnel to relay a topic in both directions. This commit modifies ipc-tap and ipc-inject so that the caller may pass an endpoint name that the scripts will use for IPC communication. This further modifies ipc-sshtunnel so that it passes the same endpoint name to ipc-tap and ipc-inject, causing the same endpoint to be used for sending and receiving of messages. This causes messages from ipc-inject not to be sent to ipc-tap. --- utils/ipc-inject.sh | 12 ++++++++---- utils/ipc-sshtunnel.sh | 30 ++++++++++++++++++------------ utils/ipc-tap.sh | 19 ++++++++++++------- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/utils/ipc-inject.sh b/utils/ipc-inject.sh index 004e829..ff7beab 100755 --- a/utils/ipc-inject.sh +++ b/utils/ipc-inject.sh @@ -1,7 +1,7 @@ #!/bin/bash # ipc-inject.sh - Injector for toolbox IPC PubSub messages -# Copyright (C) 2022 Matthias Kruk +# 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 @@ -123,6 +123,7 @@ handle_message() { main() { local proto local endpoint + local endpoint_name local message declare -gA hooks declare -gi signal_received @@ -133,13 +134,15 @@ main() { signal_received=0 - opt_add_arg "k" "hook" "v" "" \ + opt_add_arg "k" "hook" "v" "" \ "Command for handling hook data" \ '^[^:]+:.*$' \ add_hook - opt_add_arg "p" "proto" "v" "ipc" \ + opt_add_arg "p" "proto" "v" "ipc" \ "The IPC protocol to inject" \ '^u?ipc$' + opt_add_arg "e" "endpoint" "v" "" \ + "The IPC endpoint to use" if ! opt_parse "$@"; then return 1 @@ -150,7 +153,8 @@ main() { return 1 fi - if ! endpoint=$(ipc_endpoint_open); then + endpoint_name=$(opt_get "endpoint") + if ! endpoint=$(ipc_endpoint_open "$endpoint_name"); then return 1 fi diff --git a/utils/ipc-sshtunnel.sh b/utils/ipc-sshtunnel.sh index 091f844..91c6947 100755 --- a/utils/ipc-sshtunnel.sh +++ b/utils/ipc-sshtunnel.sh @@ -1,7 +1,7 @@ #!/bin/bash # ipc-sshtunnel.sh - Tunnel toolbox PubSub IPC messages over SSH -# Copyright (C) 2022 Matthias Kruk +# 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 @@ -45,9 +45,10 @@ _array_add() { establish_ipc_tunnel() { local direction="$1" local remote="$2" - local -n ref_topics="$3" - local -n ref_tap_hooks="$4" - local -n ref_inject_hooks="$5" + local endpoint="$3" + local -n ref_topics="$4" + local -n ref_tap_hooks="$5" + local -n ref_inject_hooks="$6" local proto local topic @@ -57,10 +58,12 @@ establish_ipc_tunnel() { proto=$(opt_get "proto") tap_args=( - --proto "$proto" + --proto "$proto" + --endpoint "$endpoint" ) inject_args=( - --proto "$proto" + --proto "$proto" + --endpoint "$endpoint" ) for topic in "${ref_topics[@]}"; do @@ -102,13 +105,14 @@ process_is_running() { spawn_tunnel() { local direction="$1" local remote="$2" - local ref_topics="$3" - local ref_tap_hooks="$4" - local ref_inject_hooks="$5" + local endpoint="$3" + local ref_topics="$4" + local ref_tap_hooks="$5" + local ref_inject_hooks="$6" local -i tunnel - if tunnel=$(establish_ipc_tunnel "$direction" "$remote" "$ref_topics" "$ref_tap_hooks" "$ref_inject_hooks"); then + if tunnel=$(establish_ipc_tunnel "$direction" "$remote" "$endpoint" "$ref_topics" "$ref_tap_hooks" "$ref_inject_hooks"); then while inst_running && process_is_running "$tunnel"; do sleep 5 done @@ -127,6 +131,7 @@ main() { declare -gxa tap_hooks declare -gxa inject_hooks local remote + local endpoint input_topics=() output_topics=() @@ -149,10 +154,11 @@ main() { return 1 fi + endpoint="ipc-sshtunnel-$HOSTNAME-$$" remote=$(opt_get "remote") - if ! inst_start spawn_tunnel "in" "$remote" input_topics tap_hooks inject_hooks || - ! inst_start spawn_tunnel "out" "$remote" output_topics tap_hooks inject_hooks; then + if ! inst_start spawn_tunnel "in" "$remote" "$endpoint" input_topics tap_hooks inject_hooks || + ! inst_start spawn_tunnel "out" "$remote" "$endpoint" output_topics tap_hooks inject_hooks; then return 1 fi diff --git a/utils/ipc-tap.sh b/utils/ipc-tap.sh index 452f0c6..c72b313 100755 --- a/utils/ipc-tap.sh +++ b/utils/ipc-tap.sh @@ -1,7 +1,7 @@ #!/bin/bash # ipc-tap.sh - Interceptor for toolbox IPC PubSub messages -# Copyright (C) 2022 Matthias Kruk +# 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 @@ -81,7 +81,8 @@ invoke_hooks() { } tap_topics() { - local topics=("$@") + local endpoint_name="$1" + local topics=("${@:2}") local endpoint local topic @@ -91,7 +92,7 @@ tap_topics() { err=0 signal_received=0 - if ! endpoint=$(ipc_endpoint_open); then + if ! endpoint=$(ipc_endpoint_open "$endpoint_name"); then return 1 fi @@ -136,23 +137,26 @@ tap_topics() { } main() { + local endpoint local proto local topics declare -gA hooks topics=() - opt_add_arg "t" "topic" "rv" "" \ + opt_add_arg "t" "topic" "rv" "" \ "A topic to tap into" \ '' \ add_topic - opt_add_arg "k" "hook" "v" "" \ + opt_add_arg "k" "hook" "v" "" \ "Hook to execute upon receipt" \ '^[^:]+:.+$' \ add_hook - opt_add_arg "p" "proto" "v" "ipc" \ + 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 @@ -163,7 +167,8 @@ main() { return 1 fi - if ! tap_topics "${topics[@]}"; then + endpoint=$(opt_get "endpoint") + if ! tap_topics "$endpoint" "${topics[@]}"; then return 1 fi -- 2.47.3