mirror of
https://github.com/yuzu-emu/mbedtls
synced 2024-11-24 22:58:18 +00:00
udp_proxy_wrapper.sh: more robust
Don't mangle arguments containing spaces and other special characters, pass them unchanged to the proxy or server as applicable. More robust parsing of server parameters: don't hit on partial words; use ssl_server2's default values. Minor style improvements.
This commit is contained in:
parent
a677cdd459
commit
afc4f892d1
1 changed files with 69 additions and 55 deletions
|
@ -1,4 +1,6 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
# -*-sh-basic-offset: 4-*-
|
||||||
|
# Usage: udp_proxy_wrapper.sh [PROXY_PARAM...] -- [SERVER_PARAM...]
|
||||||
|
|
||||||
set -u
|
set -u
|
||||||
|
|
||||||
|
@ -7,24 +9,21 @@ TPXY_BIN="$MBEDTLS_BASE/programs/test/udp_proxy"
|
||||||
SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2"
|
SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2"
|
||||||
|
|
||||||
: ${VERBOSE:=0}
|
: ${VERBOSE:=0}
|
||||||
FULL_PARAMS=$*
|
|
||||||
PROXY_PARAMS=${FULL_PARAMS%%" -- "*}
|
|
||||||
SERVER_PARAMS=${FULL_PARAMS#*" -- "}
|
|
||||||
|
|
||||||
stop_proxy() {
|
stop_proxy() {
|
||||||
test -n "${TPXY_PID:-}" &&
|
if [ -n "${tpxy_pid:-}" ]; then
|
||||||
(
|
echo
|
||||||
echo "\n * Killing proxy (pid $TPXY_PID) ..."
|
echo " * Killing proxy (pid $tpxy_pid) ..."
|
||||||
kill $TPXY_PID
|
kill $tpxy_pid
|
||||||
)
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
stop_server() {
|
stop_server() {
|
||||||
test -n "${SRV_PID:-}" &&
|
if [ -n "${srv_pid:-}" ]; then
|
||||||
(
|
echo
|
||||||
echo "\n * Killing server (pid $SRV_PID) ..."
|
echo " * Killing server (pid $srv_pid) ..."
|
||||||
kill $SRV_PID >/dev/null 2>/dev/null
|
kill $srv_pid >/dev/null 2>/dev/null
|
||||||
)
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
|
@ -35,69 +34,84 @@ cleanup() {
|
||||||
|
|
||||||
trap cleanup INT TERM HUP
|
trap cleanup INT TERM HUP
|
||||||
|
|
||||||
DTLS_ENABLED=$(echo " $SERVER_PARAMS" | grep " dtls=1")
|
# Extract the proxy parameters
|
||||||
IPV6_IN_USE=$(echo " $SERVER_PARAMS" | grep " server_addr=::1" )
|
tpxy_cmd_snippet='"$TPXY_BIN"'
|
||||||
|
while [ $# -ne 0 ] && [ "$1" != "--" ]; do
|
||||||
|
tail="$1" quoted=""
|
||||||
|
while [ -n "$tail" ]; do
|
||||||
|
case "$tail" in
|
||||||
|
*\'*) quoted="${quoted}${tail%%\'*}'\\''" tail="${tail#*\'}";;
|
||||||
|
*) quoted="${quoted}${tail}"; tail=; false;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
tpxy_cmd_snippet="$tpxy_cmd_snippet '$quoted'"
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
unset tail quoted
|
||||||
|
if [ $# -eq 0 ]; then
|
||||||
|
echo " * No server arguments (must be preceded by \" -- \") - exit"
|
||||||
|
exit 3
|
||||||
|
fi
|
||||||
|
shift
|
||||||
|
|
||||||
if [ -z "$DTLS_ENABLED" ] || [ -n "$IPV6_IN_USE" ]; then
|
dtls_enabled=
|
||||||
echo " * Couldn't find DTLS enabling, or IPv6 is in use - immediate fallback to server application..."
|
ipv6_in_use=
|
||||||
|
server_port_orig=
|
||||||
|
server_addr_orig=
|
||||||
|
for param; do
|
||||||
|
case "$param" in
|
||||||
|
server_port=*) server_port_orig="${param#*=}";;
|
||||||
|
server_addr=*:*) server_addr_orig="${param#*=}"; ipv6_in_use=1;;
|
||||||
|
server_addr=*) server_addr_orig="${param#*=}";;
|
||||||
|
dtls=[!0]*) dtls_enabled=1;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -z "$dtls_enabled" ] || [ -n "$ipv6_in_use" ]; then
|
||||||
|
echo >&2 "$0: Couldn't find DTLS enabling, or IPv6 is in use - immediate fallback to server application..."
|
||||||
if [ $VERBOSE -gt 0 ]; then
|
if [ $VERBOSE -gt 0 ]; then
|
||||||
echo "[ $SRV_BIN $SERVER_PARAMS ]"
|
echo "[ $SRV_BIN $* ]"
|
||||||
fi
|
fi
|
||||||
$SRV_BIN $SERVER_PARAMS >&1 2>&1 &
|
exec "$SRV_BIN" "$@"
|
||||||
SRV_PID=$!
|
|
||||||
wait $SRV_PID
|
|
||||||
exit 0
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
SERVER_PORT_ORIG=$(echo "$SERVER_PARAMS" | sed -n "s/^.*server_port=\([0-9]*\).*$/\1/p")
|
if [ -z "$server_port_orig" ]; then
|
||||||
if [ -z "$SERVER_PORT_ORIG" ]; then
|
server_port_orig=4433
|
||||||
echo " * No server port specified - exit"
|
fi
|
||||||
exit 1
|
echo " * Server port: $server_port_orig"
|
||||||
|
tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_port=\$server_port_orig\""
|
||||||
|
tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_port=\$server_port\""
|
||||||
|
|
||||||
|
if [ -n "$server_addr_orig" ]; then
|
||||||
|
echo " * Server address: $server_addr_orig"
|
||||||
|
tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_addr=\$server_addr_orig\""
|
||||||
|
tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_addr=\$server_addr_orig\""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
SERVER_ADDR_ORIG=$(echo "$SERVER_PARAMS" | sed -n "s/^.*server_addr=\([a-zA-Z0-9\.]*\).*$/\1/p")
|
server_port=$(( server_port_orig + 1 ))
|
||||||
if [ -z "$SERVER_ADDR_ORIG" ]; then
|
set -- "$@" "server_port=$server_port"
|
||||||
echo " * No server address specified - exit"
|
echo " * Intermediate port: $server_port"
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo " * Server address: $SERVER_ADDR_ORIG"
|
|
||||||
echo " * Server port: $SERVER_PORT_ORIG"
|
|
||||||
|
|
||||||
SERVER_PORT=$(( $SERVER_PORT_ORIG + 1 ))
|
|
||||||
echo " * Intermediate port: $SERVER_PORT"
|
|
||||||
|
|
||||||
TPXY_CMD=\
|
|
||||||
"$TPXY_BIN $PROXY_PARAMS "\
|
|
||||||
"listen_port=$SERVER_PORT_ORIG "\
|
|
||||||
"server_port=$SERVER_PORT "\
|
|
||||||
"server_addr=$SERVER_ADDR_ORIG "\
|
|
||||||
"listen_addr=$SERVER_ADDR_ORIG"
|
|
||||||
|
|
||||||
echo " * Start proxy in background ..."
|
echo " * Start proxy in background ..."
|
||||||
if [ $VERBOSE -gt 0 ]; then
|
if [ $VERBOSE -gt 0 ]; then
|
||||||
echo "[ $TPXY_CMD ]"
|
echo "[ $tpxy_cmd_snippet ]"
|
||||||
fi
|
fi
|
||||||
|
eval "$tpxy_cmd_snippet" >/dev/null 2>&1 &
|
||||||
$TPXY_CMD >/dev/null 2>&1 &
|
tpxy_pid=$!
|
||||||
TPXY_PID=$!
|
|
||||||
|
|
||||||
if [ $VERBOSE -gt 0 ]; then
|
if [ $VERBOSE -gt 0 ]; then
|
||||||
echo " * Proxy ID: $TPXY_PID"
|
echo " * Proxy ID: $TPXY_PID"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
SERVER_PARAMS_NEW=$(echo "$SERVER_PARAMS" | sed -n "s/^\(.*server_port=\)[0-9]*\(.*\)$/\1$SERVER_PORT\2/p")
|
|
||||||
SRV_CMD="$SRV_BIN $SERVER_PARAMS_NEW"
|
|
||||||
|
|
||||||
echo " * Starting server ..."
|
echo " * Starting server ..."
|
||||||
if [ $VERBOSE -gt 0 ]; then
|
if [ $VERBOSE -gt 0 ]; then
|
||||||
echo "[ $SRV_CMD ]"
|
echo "[ $SRV_BIN $* ]"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
$SRV_CMD >&2 &
|
"$SRV_BIN" "$@" >&2 &
|
||||||
SRV_PID=$!
|
srv_pid=$!
|
||||||
|
|
||||||
wait $SRV_PID
|
wait $srv_pid
|
||||||
|
|
||||||
stop_proxy
|
stop_proxy
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Reference in a new issue