udp_proxy_wrapper.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 OR GPL-2.0-or-later
  7. #
  8. # This file is provided under the Apache License 2.0, or the
  9. # GNU General Public License v2.0 or later.
  10. #
  11. # **********
  12. # Apache License 2.0:
  13. #
  14. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  15. # not use this file except in compliance with the License.
  16. # You may obtain a copy of the License at
  17. #
  18. # http://www.apache.org/licenses/LICENSE-2.0
  19. #
  20. # Unless required by applicable law or agreed to in writing, software
  21. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. # See the License for the specific language governing permissions and
  24. # limitations under the License.
  25. #
  26. # **********
  27. #
  28. # **********
  29. # GNU General Public License v2.0 or later:
  30. #
  31. # This program is free software; you can redistribute it and/or modify
  32. # it under the terms of the GNU General Public License as published by
  33. # the Free Software Foundation; either version 2 of the License, or
  34. # (at your option) any later version.
  35. #
  36. # This program is distributed in the hope that it will be useful,
  37. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. # GNU General Public License for more details.
  40. #
  41. # You should have received a copy of the GNU General Public License along
  42. # with this program; if not, write to the Free Software Foundation, Inc.,
  43. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  44. #
  45. # **********
  46. set -u
  47. MBEDTLS_BASE="$(dirname -- "$0")/../.."
  48. TPXY_BIN="$MBEDTLS_BASE/programs/test/udp_proxy"
  49. SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2"
  50. : ${VERBOSE:=0}
  51. stop_proxy() {
  52. if [ -n "${tpxy_pid:-}" ]; then
  53. echo
  54. echo " * Killing proxy (pid $tpxy_pid) ..."
  55. kill $tpxy_pid
  56. fi
  57. }
  58. stop_server() {
  59. if [ -n "${srv_pid:-}" ]; then
  60. echo
  61. echo " * Killing server (pid $srv_pid) ..."
  62. kill $srv_pid >/dev/null 2>/dev/null
  63. fi
  64. }
  65. cleanup() {
  66. stop_server
  67. stop_proxy
  68. exit 129
  69. }
  70. trap cleanup INT TERM HUP
  71. # Extract the proxy parameters
  72. tpxy_cmd_snippet='"$TPXY_BIN"'
  73. while [ $# -ne 0 ] && [ "$1" != "--" ]; do
  74. tail="$1" quoted=""
  75. while [ -n "$tail" ]; do
  76. case "$tail" in
  77. *\'*) quoted="${quoted}${tail%%\'*}'\\''" tail="${tail#*\'}";;
  78. *) quoted="${quoted}${tail}"; tail=; false;;
  79. esac
  80. done
  81. tpxy_cmd_snippet="$tpxy_cmd_snippet '$quoted'"
  82. shift
  83. done
  84. unset tail quoted
  85. if [ $# -eq 0 ]; then
  86. echo " * No server arguments (must be preceded by \" -- \") - exit"
  87. exit 3
  88. fi
  89. shift
  90. dtls_enabled=
  91. ipv6_in_use=
  92. server_port_orig=
  93. server_addr_orig=
  94. for param; do
  95. case "$param" in
  96. server_port=*) server_port_orig="${param#*=}";;
  97. server_addr=*:*) server_addr_orig="${param#*=}"; ipv6_in_use=1;;
  98. server_addr=*) server_addr_orig="${param#*=}";;
  99. dtls=[!0]*) dtls_enabled=1;;
  100. esac
  101. done
  102. if [ -z "$dtls_enabled" ] || [ -n "$ipv6_in_use" ]; then
  103. echo >&2 "$0: Couldn't find DTLS enabling, or IPv6 is in use - immediate fallback to server application..."
  104. if [ $VERBOSE -gt 0 ]; then
  105. echo "[ $SRV_BIN $* ]"
  106. fi
  107. exec "$SRV_BIN" "$@"
  108. fi
  109. if [ -z "$server_port_orig" ]; then
  110. server_port_orig=4433
  111. fi
  112. echo " * Server port: $server_port_orig"
  113. tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_port=\$server_port_orig\""
  114. tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_port=\$server_port\""
  115. if [ -n "$server_addr_orig" ]; then
  116. echo " * Server address: $server_addr_orig"
  117. tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_addr=\$server_addr_orig\""
  118. tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_addr=\$server_addr_orig\""
  119. fi
  120. server_port=$(( server_port_orig + 1 ))
  121. set -- "$@" "server_port=$server_port"
  122. echo " * Intermediate port: $server_port"
  123. echo " * Start proxy in background ..."
  124. if [ $VERBOSE -gt 0 ]; then
  125. echo "[ $tpxy_cmd_snippet ]"
  126. fi
  127. eval exec "$tpxy_cmd_snippet" >/dev/null 2>&1 &
  128. tpxy_pid=$!
  129. if [ $VERBOSE -gt 0 ]; then
  130. echo " * Proxy ID: $TPXY_PID"
  131. fi
  132. echo " * Starting server ..."
  133. if [ $VERBOSE -gt 0 ]; then
  134. echo "[ $SRV_BIN $* ]"
  135. fi
  136. exec "$SRV_BIN" "$@" >&2 &
  137. srv_pid=$!
  138. wait $srv_pid
  139. stop_proxy
  140. return 0