socat-chain.sh, socat-mux.sh, and socat-broker.sh work with older Socat versions

This commit is contained in:
Gerhard Rieger 2024-02-23 10:50:55 +01:00
parent 9fe8206b52
commit 1241600b81
5 changed files with 55 additions and 19 deletions

View file

@ -23,6 +23,9 @@ Features:
Total inactivity timeout option -T 0 now means 0.0 seconds; up to Total inactivity timeout option -T 0 now means 0.0 seconds; up to
version 1.8.0.0 it meant no total inactivity timeout. version 1.8.0.0 it meant no total inactivity timeout.
Changed socat-chain.sh, socat-mux.sh, and socat-broker.sh to work with
older Socat versions.
Porting: Porting:
Changes for building and testing on NetBSD Changes for building and testing on NetBSD

View file

@ -21,7 +21,7 @@ usage () {
$ECHO " <listener> is a passive address like TCP4-L or SSL-L" $ECHO " <listener> is a passive address like TCP4-L or SSL-L"
$ECHO " <options>:" $ECHO " <options>:"
$ECHO " -d* -S -t <timeout> -T <timeout> are passed to socat" $ECHO " -d* -S -t <timeout> -T <timeout> are passed to socat"
$ECHO " -V prints the socat command before starting it" $ECHO " -V Shows executed Socat commands and some infos"
$ECHO "For example:" $ECHO "For example:"
$ECHO " $0 \\" $ECHO " $0 \\"
$ECHO " TCP4-L:1234" $ECHO " TCP4-L:1234"
@ -60,15 +60,29 @@ if ! [[ "$LISTENER" =~ .*,fork ]] || [[ "$LISTENER" =~ .*,fork, ]]; then
fi fi
case "$0" in case "$0" in
*/*) SOCAT=${0%/*}/socat ;; */*) if [ -x ${0%/*}/socat ]; then SOCAT=${0%/*}/socat; fi ;;
*) SOCAT=socat ;;
esac esac
if [ -z "$SOCAT" ]; then SOCAT=socat; fi
[ "$VERBOSE" ] && echo "# $0: Using executable $SOCAT" >&2
# We need a free UDP port (on loopback)
PORT=$($SOCAT -d -d -T 0.000001 UDP4-RECV:0 /dev/null 2>&1 |grep bound |sed 's/.*:\([1-9][0-9]*\)$/\1/') PORT=$($SOCAT -d -d -T 0.000001 UDP4-RECV:0 /dev/null 2>&1 |grep bound |sed 's/.*:\([1-9][0-9]*\)$/\1/')
if [ -z "$PORT" ]; then if [ -z "$PORT" ]; then
echo "$0: Failed to determine free UDP port" >&2 # Probably old Socat version, use a different approach
exit 1 if type ss >/dev/null 2>&1; then
:
elif type netstat >/dev/null 2>&1; then
alias ss=netstat
else
echo "$0: Failed to determine free UDP port (old Socat version, no ss, no netstat?)" >&2
exit 1
fi
PORT=
while [ -z "$PORT" ] || ss -aun |grep -e ":$PORT\>" >/dev/null; do
PORT=$((16384+RANDOM))
done
fi fi
[ "$VERBOSE" ] && echo "# $0: Using UDP port $PORT" >&2
BCADDR=127.255.255.255 BCADDR=127.255.255.255

View file

@ -56,7 +56,7 @@ usage () {
$ECHO " <address3> is typically a client address with protocol like OPENSSL" $ECHO " <address3> is typically a client address with protocol like OPENSSL"
$ECHO " <options>:" $ECHO " <options>:"
$ECHO " -d* -S <sigmask> -t <timeout> -T <timeout> are passed to socat" $ECHO " -d* -S <sigmask> -t <timeout> -T <timeout> are passed to socat"
$ECHO " -V prints the socat commands before starting them" $ECHO " -V Shows executed Socat commands and some infos"
$ECHO "Example to drive SOCKS over TLS:" $ECHO "Example to drive SOCKS over TLS:"
$ECHO " $0 \\" $ECHO " $0 \\"
$ECHO " TCP4-L:1234,reuseaddr,fork \\" $ECHO " TCP4-L:1234,reuseaddr,fork \\"
@ -130,10 +130,12 @@ else
fi fi
case "$0" in case "$0" in
*/*) SOCAT=${0%/*}/socat ;; */*) if [ -x ${0%/*}/socat ]; then SOCAT=${0%/*}/socat; fi ;;
*) SOCAT=socat ;;
esac esac
if [ -z "$SOCAT" ]; then SOCAT=socat; fi
[ "$VERBOSE" ] && echo "# $0: Using executable $SOCAT" >&2
# We need a free TCP port (on loopback)
PORT=$($SOCAT -d -d TCP4-L:0,accept-timeout=0.000001 /dev/null 2>&1 |grep listening |sed 's/.*:\([1-9][0-9]*\)$/\1/') PORT=$($SOCAT -d -d TCP4-L:0,accept-timeout=0.000001 /dev/null 2>&1 |grep listening |sed 's/.*:\([1-9][0-9]*\)$/\1/')
if [ -z "$PORT" ]; then if [ -z "$PORT" ]; then
echo "$0: Failed to determine free TCP port" >&2 echo "$0: Failed to determine free TCP port" >&2

View file

@ -28,7 +28,7 @@ usage () {
$ECHO "data provided by 10.2.3.4 is sent to ALL clients" $ECHO "data provided by 10.2.3.4 is sent to ALL clients"
$ECHO " <options>:" $ECHO " <options>:"
$ECHO "\t-h\tShow this help text and exit" $ECHO "\t-h\tShow this help text and exit"
$ECHO "\t-V\tShow Socat commands" $ECHO "\t-V\tShows executed Socat commands and some infos"
$ECHO "\t-q\tSuppress most messages" $ECHO "\t-q\tSuppress most messages"
$ECHO "\t-d*\tOptions beginning with -d are passed to Socat processes" $ECHO "\t-d*\tOptions beginning with -d are passed to Socat processes"
$ECHO "\t-l*\tOptions beginning with -l are passed to Socat processes" $ECHO "\t-l*\tOptions beginning with -l are passed to Socat processes"
@ -67,19 +67,31 @@ if ! [[ "$LISTENER" =~ .*,fork ]] || [[ "$LISTENER" =~ .*,fork, ]]; then
fi fi
case "$0" in case "$0" in
*/*) SOCAT=${0%/*}/socat ;; */*) if [ -x ${0%/*}/socat ]; then SOCAT=${0%/*}/socat; fi ;;
*) SOCAT=socat ;;
esac esac
if [ -z "$SOCAT" ]; then SOCAT=socat; fi
[ "$VERBOSE" ] && echo "# $0: Using executable $SOCAT" >&2
# We need two free UDP ports (on loopback)
PORT1=$($SOCAT -d -d -T 0.000001 UDP4-RECV:0 /dev/null 2>&1 |grep bound |sed 's/.*:\([1-9][0-9]*\)$/\1/') PORT1=$($SOCAT -d -d -T 0.000001 UDP4-RECV:0 /dev/null 2>&1 |grep bound |sed 's/.*:\([1-9][0-9]*\)$/\1/')
PORT2=$($SOCAT -d -d -T 0.000001 UDP4-RECV:0 /dev/null 2>&1 |grep bound |sed 's/.*:\([1-9][0-9]*\)$/\1/') PORT2=$($SOCAT -d -d -T 0.000001 UDP4-RECV:0 /dev/null 2>&1 |grep bound |sed 's/.*:\([1-9][0-9]*\)$/\1/')
if [ -z "$PORT1" -o -z "$PORT2" ]; then if [ -z "$PORT1" -o -z "$PORT2" ]; then
echo "$0: Failed to determine free UDP ports" >&2 # Probably old Socat version, use a different approach
exit 1 if type ss >/dev/null 2>&1; then
fi :
if [ "$PORT1" = "$PORT2" ]; then # seen on etch elif type netstat >/dev/null 2>&1; then
PORT2=$((PORT1+1)) alias ss=netstat
else
echo "$0: Failed to determine free UDP ports (old Socat version, no ss, no netstat?)" >&2
exit 1
fi
PORT1= PORT2=
while [ -z "$PORT1" -o -z "$PORT2" -o "$PORT1" = "$PORT2" ] || ss -aun |grep -e ":$PORT1\>" -e ":$PORT2\>" >/dev/null; do
PORT1=$((16384+RANDOM))
PORT2=$((16384+RANDOM))
done
fi fi
[ "$VERBOSE" ] && echo "# $0: Using UDP ports $PORT1, $PORT2" >&2
IFADDR=127.0.0.1 IFADDR=127.0.0.1
BCADDR=127.255.255.255 BCADDR=127.255.255.255

11
test.sh
View file

@ -30,6 +30,11 @@ fi
ECHO="echo $E" ECHO="echo $E"
PRINTF="printf" PRINTF="printf"
GREP_E="grep -E"
GREP_F="grep -F"
TRUE=$(type -p true)
usage() { usage() {
$ECHO "Usage: $0 <options> [<test-spec> ...]" $ECHO "Usage: $0 <options> [<test-spec> ...]"
$ECHO "options:" $ECHO "options:"
@ -18025,7 +18030,7 @@ elif [ "$UNAME" = "NetBSD" ]; then
$PRINTF "test $F_n $TEST... ${YELLOW}might hang on $UNAME${NORMAL}\n" $N $PRINTF "test $F_n $TEST... ${YELLOW}might hang on $UNAME${NORMAL}\n" $N
numCANT=$((numCANT+1)) numCANT=$((numCANT+1))
listCANT="$listCANT $N" listCANT="$listCANT $N"
elif ! F=$(testfeats STDIO SYSTEM SOCKETPAIR); then elif ! F=$(testfeats SYCLS STDIO SYSTEM SOCKETPAIR); then
$PRINTF "test $F_n $TEST... ${YELLOW}Feature $F not configured in $SOCAT${NORMAL}\n" $N $PRINTF "test $F_n $TEST... ${YELLOW}Feature $F not configured in $SOCAT${NORMAL}\n" $N
numCANT=$((numCANT+1)) numCANT=$((numCANT+1))
listCANT="$listCANT $N" listCANT="$listCANT $N"
@ -19325,7 +19330,7 @@ case "$TESTS" in
*%$N%*|*%functions%*|*%scripts%*|*%socat-chain%*|*%listen%*|*%fork%*|*%ip4%*|*%tcp4%*|*%unix%*|*%socks4%*|*%socket%*|*%$NAME%*) *%$N%*|*%functions%*|*%scripts%*|*%socat-chain%*|*%listen%*|*%fork%*|*%ip4%*|*%tcp4%*|*%unix%*|*%socks4%*|*%socket%*|*%$NAME%*)
TEST="$NAME: test socat-chain.sh with SOCKS4 over UNIX-socket" TEST="$NAME: test socat-chain.sh with SOCKS4 over UNIX-socket"
# Run a socks4 server on UNIX-listen # Run a socks4 server on UNIX-listen
# Connect with socat-chein.sh; check if data transfer is correct # Connect with socat-chain.sh; check if data transfer is correct
if ! eval $NUMCOND; then : if ! eval $NUMCOND; then :
# Remove unneeded checks, adapt lists of the remaining ones # Remove unneeded checks, adapt lists of the remaining ones
elif ! cond=$(checkconds \ elif ! cond=$(checkconds \
@ -19396,7 +19401,7 @@ case "$TESTS" in
*%$N%*|*%functions%*|*%scripts%*|*%socat-chain%*|*%listen%*|*%fork%*|*%ip4%*|*%tcp4%*|*%openssl%*|*%unix%*|*%socket%*|*%pty%*|*%$NAME%*) *%$N%*|*%functions%*|*%scripts%*|*%socat-chain%*|*%listen%*|*%fork%*|*%ip4%*|*%tcp4%*|*%openssl%*|*%unix%*|*%socket%*|*%pty%*|*%$NAME%*)
TEST="$NAME: test socat-chain.sh with SSL over PTY" TEST="$NAME: test socat-chain.sh with SSL over PTY"
# Run a socat-chain.sh instance with SSL listening behind a PTY; # Run a socat-chain.sh instance with SSL listening behind a PTY;
# open the PTY with socat-chein.sh using SSL; # open the PTY with socat-chain.sh using SSL;
# check if data transfer is correct # check if data transfer is correct
if ! eval $NUMCOND; then : if ! eval $NUMCOND; then :
# Remove unneeded checks, adapt lists of the remaining ones # Remove unneeded checks, adapt lists of the remaining ones