3 # Copyright Gerhard Rieger 2003-2004
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 as:
11 # socat tcp-l:8080,reuseaddr,fork exec:"proxy.sh",nofork
13 if [ -z "$SOCAT" ]; then
14 if type socat
>/dev
/null
2>&1; then
21 if [ $
(echo "x\c") = "x" ]; then E
=""
22 elif [ $
(echo -e "x\c") = "x" ]; then E
="-e"
24 echo "cannot suppress trailing newline on echo" >&2
29 #echo "CR=$($ECHO "$CR\c" |od -c)" >&2
33 # their cats are too stupid to work with unix domain sockets
34 CAT
="$SOCAT -u stdin stdout"
44 -w) n
="$2"; while [ "$n" -gt 0 ]; do SPACES
="$SPACES "; n
=$
((n-1
)); done
46 #-s) STAT="$2"; shift ;;
51 # read and parse HTTP request
53 if echo "$l" |
egrep '^CONNECT +[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+ +HTTP/1.[01]' >/dev
/null
57 $ECHO "HTTP/1.0${SPACES}500 Bad Request$CR"
62 # extract target server name/address
63 s
=`echo $l |awk '{print($2);}'`
65 # read more headers until empty line
66 while [ "$l" != "$CR" ]; do
71 $ECHO "HTTP/1.0${SPACES}200 OK$CR"
75 # perform proxy (relay) function
76 exec $SOCAT $SOCAT_OPTS - tcp
:$s