File: socks4echo.sh

package info (click to toggle)
socat 2.0.0~beta9-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 3,740 kB
  • sloc: ansic: 30,875; sh: 11,630; makefile: 149
file content (101 lines) | stat: -rwxr-xr-x 2,266 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /bin/bash
# source: socks4echo.sh

# Copyright Gerhard Rieger 2004-2006
# Published under the GNU General Public License V.2, see file COPYING

# perform primitive simulation of a socks4 server with echo function via stdio.
# accepts and answers correct SOCKS4 requests, but then just echoes data.
# it is required for test.sh
# for TCP, use this script as:
# socat tcp-l:1080,reuseaddr,crlf system:"socks4echo.sh"

# older bash and ksh do not have -n option to read command; we try dd then
if echo a |read -n 1 null >/dev/null 2>&1; then
    HAVE_READ_N=1
else
    HAVE_READ_N=
fi

if type socat >/dev/null 2>&1; then
    SOCAT=socat
else
    SOCAT=./socat
fi

case `uname` in
HP-UX|OSF1)
    CAT="$SOCAT -u stdin stdout"
    ;;
*)
    CAT=cat
    ;;
esac

if   [ $(echo "x\c") = "x" ]; then E=""
elif [ $(echo -e "x\c") = "x" ]; then E="-e"
else
    echo "cannot suppress trailing newline on echo" >&2
    exit 1
fi
ECHO="echo $E"

if [ $($ECHO "\0101") = "A" ]; then
    SOCKSREPLY_FAILED="\0\0133\0\0\0\0\0\0\c"
    SOCKSREPLY_OK="\0\0132\0\0\0\0\0\0\c"
else
    SOCKSREPLY_FAILED="\0\133\0\0\0\0\0\0\c"
    SOCKSREPLY_OK="\0\132\0\0\0\0\0\0\c"
fi

# read and parse SOCKS4 header
if [ "$HAVE_READ_N" ]; then
    read -r -n 1 vn	# bash 2.0.3 does not support -n
else
    vn=$(dd bs=1 count=1 2>/dev/null)
fi
if [ "$vn" != $($ECHO "\04") ]; then
    $ECHO "$SOCKSREPLY_FAILED"
    echo "invalid socks version requested" >&2
    exit
fi

if [ "$HAVE_READ_N" ]; then
    read -r -n 1 cd
else
    cd=$(dd bs=1 count=1 2>/dev/null)
fi
if [ "$cd" != $($ECHO "\01") ]; then
    $ECHO "$SOCKSREPLY_FAILED"
    echo "invalid socks operation requested" >&2
    exit
fi

if [ "$HAVE_READ_N" ]; then
    read -r -n 6 a
else
    a=$(dd bs=1 count=6 2>/dev/null)
fi
if [ "$a" != "$($ECHO "}m bL6")" ]; then
    $ECHO "$SOCKSREPLY_FAILED"
    echo "$0: wrong socks address or port requested" >&2
    echo "$0: expected $($ECHO "}m bL6"|od -t x1), received $($ECHO "$a"|od -t x1)" >&2
    exit
fi

if [ "$HAVE_READ_N" ]; then
    read -r -n 7 u
else
    u=$(dd bs=1 count=7 2>/dev/null)
fi
if [ "$u" != "nobody" ]; then
    $ECHO "$SOCKSREPLY_FAILED"
    echo "wrong socks user requested" >&2
    exit
fi

# send ok status
$ECHO "$SOCKSREPLY_OK"

# perform echo function
$CAT