2 # source: socks4a-echo.sh
5 # Copyright Gerhard Rieger and contributors (see file CHANGES)
6 # Published under the GNU General Public License V.2, see file COPYING
8 # perform primitive simulation of a socks4a server with echo function via stdio.
9 # accepts and answers correct SOCKS4a requests, but then just echoes data.
10 # it is required for test.sh
11 # for TCP, use this script as:
12 # socat tcp-l:1080,reuseaddr,crlf system:"socks4a-echo.sh"
14 # older bash and ksh do not have -n option to read command; we try dd then
15 #if echo a |read -n 1 null >/dev/null 2>&1; then
18 # and newer bash (4.3) has some other problem with read -n
22 if type socat
>/dev
/null
2>&1; then
30 CAT
="$SOCAT -u stdin stdout"
37 if [ $
(echo "x\c") = "x" ]; then E
=""
38 elif [ $
(echo -e "x\c") = "x" ]; then E
="-e"
40 echo "cannot suppress trailing newline on echo" >&2
45 if [ $
($ECHO "\0101") = "A" ]; then
46 SOCKSREPLY_FAILED
="\0\0133\0\0\0\0\0\0\c"
47 SOCKSREPLY_OK
="\0\0132\0\0\0\0\0\0\c"
49 SOCKSREPLY_FAILED
="\0\133\0\0\0\0\0\0\c"
50 SOCKSREPLY_OK
="\0\132\0\0\0\0\0\0\c"
53 # read and parse SOCKS4a header
54 if [ "$HAVE_READ_N" ]; then
55 read -r -n 1 vn
# bash 2.0.3 does not support -n
57 vn
=$
(dd bs
=1 count
=1 2>/dev
/null
)
59 if [ "$vn" != $
($ECHO "\04") ]; then
60 $ECHO "$SOCKSREPLY_FAILED"
61 echo "invalid socks version requested" >&2
65 if [ "$HAVE_READ_N" ]; then
68 cd=$
(dd bs
=1 count
=1 2>/dev
/null
)
70 if [ "$cd" != $
($ECHO "\01") ]; then
71 $ECHO "$SOCKSREPLY_FAILED"
72 echo "invalid socks operation requested" >&2
76 a
=$
(dd bs
=1 count
=6 2>/dev
/null
)
78 #echo "$a" |od -c >/dev/tty
79 #$ECHO "$a" |od -c >/dev/tty
82 if [ "$a" != "$($ECHO "}m\
0\
0\
0\
01")" ]; then
84 $ECHO "$SOCKSREPLY_FAILED"
85 echo "wrong socks address or port requested" >&2
89 if [ "$HAVE_READ_N" ]; then
92 u
=$
(dd bs
=1 count
=7 2>/dev
/null
)
94 if [ "$u" != "nobody" ]; then
95 $ECHO "$SOCKSREPLY_FAILED"
96 echo "wrong socks user requested" >&2
100 if [ "$HAVE_READ_N" ]; then
103 h
=$
(dd bs
=1 count
=10 2>/dev
/null
)
105 if [ "$h" != "localhost" ]; then
106 $ECHO "$SOCKSREPLY_FAILED"
107 echo "wrong socks address requested" >&2
112 $ECHO "$SOCKSREPLY_OK"
114 # perform echo function