• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
20set -u
21
22MBEDTLS_BASE="$(dirname -- "$0")/../.."
23TPXY_BIN="$MBEDTLS_BASE/programs/test/udp_proxy"
24SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2"
25
26: ${VERBOSE:=0}
27
28stop_proxy() {
29    if [ -n "${tpxy_pid:-}" ]; then
30        echo
31        echo "  * Killing proxy (pid $tpxy_pid) ..."
32        kill $tpxy_pid
33    fi
34}
35
36stop_server() {
37    if [ -n "${srv_pid:-}" ]; then
38        echo
39        echo "  * Killing server (pid $srv_pid) ..."
40        kill $srv_pid >/dev/null 2>/dev/null
41    fi
42}
43
44cleanup() {
45    stop_server
46    stop_proxy
47    exit 129
48}
49
50trap cleanup INT TERM HUP
51
52# Extract the proxy parameters
53tpxy_cmd_snippet='"$TPXY_BIN"'
54while [ $# -ne 0 ] && [ "$1" != "--" ]; do
55    tail="$1" quoted=""
56    while [ -n "$tail" ]; do
57        case "$tail" in
58            *\'*) quoted="${quoted}${tail%%\'*}'\\''" tail="${tail#*\'}";;
59            *) quoted="${quoted}${tail}"; tail=; false;;
60        esac
61    done
62    tpxy_cmd_snippet="$tpxy_cmd_snippet '$quoted'"
63    shift
64done
65unset tail quoted
66if [ $# -eq 0 ]; then
67    echo "  * No server arguments (must be preceded by \" -- \") - exit"
68    exit 3
69fi
70shift
71
72dtls_enabled=
73ipv6_in_use=
74server_port_orig=
75server_addr_orig=
76for param; do
77    case "$param" in
78        server_port=*) server_port_orig="${param#*=}";;
79        server_addr=*:*) server_addr_orig="${param#*=}"; ipv6_in_use=1;;
80        server_addr=*) server_addr_orig="${param#*=}";;
81        dtls=[!0]*) dtls_enabled=1;;
82    esac
83done
84
85if [ -z "$dtls_enabled" ] || [ -n "$ipv6_in_use" ]; then
86    echo >&2 "$0: Couldn't find DTLS enabling, or IPv6 is in use - immediate fallback to server application..."
87    if [ $VERBOSE -gt 0 ]; then
88        echo "[ $SRV_BIN $* ]"
89    fi
90    exec "$SRV_BIN" "$@"
91fi
92
93if [ -z "$server_port_orig" ]; then
94    server_port_orig=4433
95fi
96echo "  * Server port:       $server_port_orig"
97tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_port=\$server_port_orig\""
98tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_port=\$server_port\""
99
100if [ -n "$server_addr_orig" ]; then
101    echo "  * Server address:    $server_addr_orig"
102    tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_addr=\$server_addr_orig\""
103    tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_addr=\$server_addr_orig\""
104fi
105
106server_port=$(( server_port_orig + 1 ))
107set -- "$@" "server_port=$server_port"
108echo "  * Intermediate port: $server_port"
109
110echo "  * Start proxy in background ..."
111if [ $VERBOSE -gt 0 ]; then
112    echo "[ $tpxy_cmd_snippet ]"
113fi
114eval exec "$tpxy_cmd_snippet" >/dev/null 2>&1 &
115tpxy_pid=$!
116
117if [ $VERBOSE -gt 0 ]; then
118    echo "  * Proxy ID:          $TPXY_PID"
119fi
120
121echo "  * Starting server ..."
122if [ $VERBOSE -gt 0 ]; then
123    echo "[ $SRV_BIN $* ]"
124fi
125
126exec "$SRV_BIN" "$@" >&2 &
127srv_pid=$!
128
129wait $srv_pid
130
131stop_proxy
132return 0
133