3 # Copyright Gerhard Rieger and contributors (see file CHANGES)
4 # Published under the GNU General Public License V.2, see file COPYING
6 # perform primitive simulation of a proxy server.
7 # accepts and answers correct HTTP CONNECT requests on stdio, and tries to
8 # establish the connection to the given server.
9 # it is required for socats test.sh
10 # for TCP, use this script like:
11 # socat TCP-L:8080,reuseaddr,fork EXEC:"proxy.sh",nofork
13 # 20130622 GR allow hostnames, not only IP addresses
15 if [ -z "$SOCAT" ]; then
16 if type socat
>/dev
/null
2>&1; then
23 if [ $
(echo "x\c") = "x" ]; then E
=""
24 elif [ $
(echo -e "x\c") = "x" ]; then E
="-e"
26 echo "cannot suppress trailing newline on echo" >&2
31 #echo "CR=$($ECHO "$CR\c" |od -c)" >&2
35 # their cats are too stupid to work with unix domain sockets
36 CAT
="$SOCAT -u stdin stdout"
46 -w) n
="$2"; while [ "$n" -gt 0 ]; do SPACES
="$SPACES "; n
=$
((n-1
)); done
48 #-s) STAT="$2"; shift ;;
54 $ECHO "HTTP/1.0${SPACES}500 Bad Request$CR"
58 # read and parse HTTP request
60 #echo "\"$m\" \"$a\" \"$h\"" >&2
61 if [ "$m" != 'CONNECT' ]; then
64 if [[ "$a" == [0-9]+\.
[0-9]+\.
[0-9]+\.
[0-9]+:[0-9]+ ]]; then
66 elif [[ "$a" == [0-9a-zA-Z-.
][0-9a-zA-Z-.
]*:[0-9][0-9]* ]]; then
72 if [[ "$h" == HTTP
/1.
[01][[:space
:]]* ]]; then
78 # read more headers until empty line
79 while [ "$l" != "$CR" ]; do
84 $ECHO "HTTP/1.0${SPACES}200 OK$CR"
88 # perform proxy (relay) function
89 $SOCAT $SOCAT_OPTS - tcp
:$a ||
{
90 $ECHO "HTTP/1.0${SPACES}500 Failed to connect to $a$CR"