udp_proxy_wrapper.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh
  2. # -*-sh-basic-offset: 4-*-
  3. # Usage: udp_proxy_wrapper.sh [PROXY_PARAM...] -- [SERVER_PARAM...]
  4. #
  5. # Copyright The Mbed TLS Contributors
  6. # SPDX-License-Identifier: Apache-2.0
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. # not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. set -u
  20. MBEDTLS_BASE="$(dirname -- "$0")/../.."
  21. TPXY_BIN="$MBEDTLS_BASE/programs/test/udp_proxy"
  22. SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2"
  23. : ${VERBOSE:=0}
  24. stop_proxy() {
  25. if [ -n "${tpxy_pid:-}" ]; then
  26. echo
  27. echo " * Killing proxy (pid $tpxy_pid) ..."
  28. kill $tpxy_pid
  29. fi
  30. }
  31. stop_server() {
  32. if [ -n "${srv_pid:-}" ]; then
  33. echo
  34. echo " * Killing server (pid $srv_pid) ..."
  35. kill $srv_pid >/dev/null 2>/dev/null
  36. fi
  37. }
  38. cleanup() {
  39. stop_server
  40. stop_proxy
  41. exit 129
  42. }
  43. trap cleanup INT TERM HUP
  44. # Extract the proxy parameters
  45. tpxy_cmd_snippet='"$TPXY_BIN"'
  46. while [ $# -ne 0 ] && [ "$1" != "--" ]; do
  47. tail="$1" quoted=""
  48. while [ -n "$tail" ]; do
  49. case "$tail" in
  50. *\'*) quoted="${quoted}${tail%%\'*}'\\''" tail="${tail#*\'}";;
  51. *) quoted="${quoted}${tail}"; tail=; false;;
  52. esac
  53. done
  54. tpxy_cmd_snippet="$tpxy_cmd_snippet '$quoted'"
  55. shift
  56. done
  57. unset tail quoted
  58. if [ $# -eq 0 ]; then
  59. echo " * No server arguments (must be preceded by \" -- \") - exit"
  60. exit 3
  61. fi
  62. shift
  63. dtls_enabled=
  64. ipv6_in_use=
  65. server_port_orig=
  66. server_addr_orig=
  67. for param; do
  68. case "$param" in
  69. server_port=*) server_port_orig="${param#*=}";;
  70. server_addr=*:*) server_addr_orig="${param#*=}"; ipv6_in_use=1;;
  71. server_addr=*) server_addr_orig="${param#*=}";;
  72. dtls=[!0]*) dtls_enabled=1;;
  73. esac
  74. done
  75. if [ -z "$dtls_enabled" ] || [ -n "$ipv6_in_use" ]; then
  76. echo >&2 "$0: Couldn't find DTLS enabling, or IPv6 is in use - immediate fallback to server application..."
  77. if [ $VERBOSE -gt 0 ]; then
  78. echo "[ $SRV_BIN $* ]"
  79. fi
  80. exec "$SRV_BIN" "$@"
  81. fi
  82. if [ -z "$server_port_orig" ]; then
  83. server_port_orig=4433
  84. fi
  85. echo " * Server port: $server_port_orig"
  86. tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_port=\$server_port_orig\""
  87. tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_port=\$server_port\""
  88. if [ -n "$server_addr_orig" ]; then
  89. echo " * Server address: $server_addr_orig"
  90. tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_addr=\$server_addr_orig\""
  91. tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_addr=\$server_addr_orig\""
  92. fi
  93. server_port=$(( server_port_orig + 1 ))
  94. set -- "$@" "server_port=$server_port"
  95. echo " * Intermediate port: $server_port"
  96. echo " * Start proxy in background ..."
  97. if [ $VERBOSE -gt 0 ]; then
  98. echo "[ $tpxy_cmd_snippet ]"
  99. fi
  100. eval exec "$tpxy_cmd_snippet" >/dev/null 2>&1 &
  101. tpxy_pid=$!
  102. if [ $VERBOSE -gt 0 ]; then
  103. echo " * Proxy ID: $TPXY_PID"
  104. fi
  105. echo " * Starting server ..."
  106. if [ $VERBOSE -gt 0 ]; then
  107. echo "[ $SRV_BIN $* ]"
  108. fi
  109. exec "$SRV_BIN" "$@" >&2 &
  110. srv_pid=$!
  111. wait $srv_pid
  112. stop_proxy
  113. return 0