proxy.sh resolves hostnames

This commit is contained in:
Gerhard Rieger 2013-06-22 12:21:25 +02:00
parent 36f2afce7c
commit 0c633dba21
2 changed files with 29 additions and 10 deletions

View file

@ -4,6 +4,8 @@ testing:
test.sh: stderr; option -v (verbose); FDOUT_ERROR description test.sh: stderr; option -v (verbose); FDOUT_ERROR description
improved proxy.sh - it now also takes hostnames
####################### V 1.7.3.1: ####################### V 1.7.3.1:
security: security:

View file

@ -10,6 +10,8 @@
# for TCP, use this script as: # for TCP, use this script as:
# socat tcp-l:8080,reuseaddr,fork exec:"proxy.sh",nofork # socat tcp-l:8080,reuseaddr,fork exec:"proxy.sh",nofork
# 20130622 GR allow hostnames, not only IP addresses
if [ -z "$SOCAT" ]; then if [ -z "$SOCAT" ]; then
if type socat >/dev/null 2>&1; then if type socat >/dev/null 2>&1; then
SOCAT=socat SOCAT=socat
@ -48,19 +50,30 @@ while [ -n "$1" ]; do
shift shift
done done
# read and parse HTTP request badrequest () {
read l
if echo "$l" |egrep '^CONNECT +[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+ +HTTP/1.[01]' >/dev/null
then
: go on below
else
$ECHO "HTTP/1.0${SPACES}500 Bad Request$CR" $ECHO "HTTP/1.0${SPACES}500 Bad Request$CR"
$ECHO "$CR" $ECHO "$CR"
exit }
# read and parse HTTP request
read m a h
#echo "\"$m\" \"$a\" \"$h\"" >&2
if [ "$m" != 'CONNECT' ]; then
badrequest; exit 1
fi
if [[ "$a" == [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+ ]]; then
: go on below
elif [[ "$a" == [0-9a-zA-Z-.][0-9a-zA-Z-.]*:[0-9][0-9]* ]]; then
: go on below
else
badrequest; exit 1
fi fi
# extract target server name/address if [[ "$h" == HTTP/1.[01][[:space:]]* ]]; then
s=`echo $l |awk '{print($2);}'` : go on below
else
badrequest; exit 1
fi
# read more headers until empty line # read more headers until empty line
while [ "$l" != "$CR" ]; do while [ "$l" != "$CR" ]; do
@ -73,4 +86,8 @@ $ECHO "HTTP/1.0${SPACES}200 OK$CR"
$ECHO "$CR" $ECHO "$CR"
# perform proxy (relay) function # perform proxy (relay) function
exec $SOCAT $SOCAT_OPTS - tcp:$s $SOCAT $SOCAT_OPTS - tcp:$a || {
$ECHO "HTTP/1.0${SPACES}500 Failed to connect to $a$CR"
$ECHO $CR
}