home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / netcat-traditional / examples / scripts / webproxy < prev    next >
Encoding:
Text File  |  2008-06-21  |  5.5 KB  |  142 lines

  1. #! /bin/sh
  2. ## Web proxy, following the grand tradition of Web things being handled by
  3. ## gross scripts.  Uses netcat to listen on a high port [default 8000],
  4. ## picks apart requests and sends them on to the right place.  Point this
  5. ## at the browser client machine you'll be coming from [to limit access to
  6. ## only it], and point the browser's concept of an HTTP proxy to the
  7. ## machine running this.  Takes a single argument of the client that will
  8. ## be using it, and rejects connections from elsewhere.  LOGS the queries
  9. ## to a configurable logfile, which can be an interesting read later on!
  10. ## If the argument is "reset", the listener and logfile are cleaned up.
  11. ##
  12. ## This works surprisingly fast and well, for a shell script, although may
  13. ## randomly fail when hammered by a browser that tries to open several
  14. ## connections at once.  Drop the "maximum connections" in your browser if
  15. ## this is a problem.
  16. ##
  17. ## A more degenerate case of this, or preferably a small C program that
  18. ## does the same thing under inetd, could handle a small site's worth of
  19. ## proxy queries.  Given the way browsers are evolving, proxies like this
  20. ## can play an important role in protecting your own privacy.
  21. ##
  22. ## If you grabbed this in ASCII mode, search down for "eew" and make sure
  23. ## the embedded-CR check is intact, or requests might hang.
  24. ##
  25. ## Doesn't handle POST forms.  Who cares, if you're just watching HTTV?
  26. ## Dumbness here has a highly desirable side effect: it only sends the first
  27. ## GET line, since that's all you really ever need to send, and suppresses
  28. ## the other somewhat revealing trash that most browsers insist on sending.
  29. ##
  30. ## To use the proxy, export `http_proxy' in your environment, e.g.
  31. ## `http_proxy=http://localhost:8000'.
  32.  
  33. # set these as you wish: proxy port...
  34. PORT=8000
  35. # logfile spec: a real file or /dev/null if you don't care
  36. LFILE=${0}.log
  37. # optional: where to dump connect info, so you can see if anything went wrong
  38. # CFILE=${0}.conn
  39. # optional extra args to the listener "nc", for instance "-s inside-net-addr"
  40. # XNC=''
  41.  
  42. # functionality switch has to be done fast, so the next listener can start
  43. # prelaunch check: if no current client and no args, bail.
  44. case "${1}${CLIENT}" in
  45.   "")
  46.     echo needs client hostname
  47.     exit 1
  48.   ;;
  49. esac
  50.  
  51. case "${1}" in
  52.   "")
  53. # Make like inetd, and run the next relayer process NOW.  All the redirection
  54. # is necessary so this shell has NO remaining channel open to the net.
  55. # This will hang around for 10 minutes, and exit if no new connections arrive.
  56. # Using -n for speed, avoiding any DNS/port lookups.
  57.     nc -w 600 -n -l -p $PORT -e "$0" $XNC "$CLIENT" < /dev/null > /dev/null \
  58.     2> $CFILE &
  59.   ;;
  60. esac
  61.  
  62. # no client yet and had an arg, this checking can be much slower now
  63. umask 077
  64.  
  65. if test "$1" ; then
  66. # if magic arg, just clean up and then hit our own port to cause server exit
  67.   if test "$1" = "reset" ; then
  68.     rm -f $LFILE
  69.     test -f "$CFILE" && rm -f $CFILE
  70.     nc -w 1 -n 127.0.0.1 $PORT < /dev/null > /dev/null 2>&1
  71.     exit 0
  72.   fi
  73. # find our ass with both hands
  74.   test ! -f "$0" && echo "Oops, cannot find my own corporeal being" && exit 1
  75. # correct launch: set up client access control, passed along thru environment.
  76.   CLIENT="$1"
  77.   export CLIENT
  78.   test "$CFILE" || CFILE=/dev/null
  79.   export CFILE
  80.   touch "$CFILE"
  81. # tell us what happened during the last run, if possible
  82.   if test -f "$CFILE"  ; then
  83.     echo "Last connection results:"
  84.     cat $CFILE
  85.   fi
  86.  
  87. # ping client machine and get its bare IP address
  88.   CLIENT=`nc -z -v -w 8 "$1" 22000 2>&1 | sed 's/.*\[\(..*\)\].*/\1/'`
  89.   test ! "$CLIENT" && echo "Can't find address of $1" && exit 1
  90.  
  91. # if this was an initial launch, be informative about it
  92.   echo "=== Launch: $CLIENT" >> $LFILE
  93.   echo "Proxy running -- will accept connections on $PORT from $CLIENT"
  94.   echo "  Logging queries to $LFILE"
  95.   test -f "$CFILE" && echo "  and connection fuckups to $CFILE"
  96.  
  97. # and run the first listener, showing us output just for the first hit
  98.   nc -v -w 600 -n -l -p $PORT -e "$0" $XNC "$CLIENT" &
  99.   exit 0
  100. fi
  101.  
  102. # Fall here to handle a page.
  103. # GET type://host.name:80/file/path HTTP/1.0
  104. # Additional: trash
  105. # More: trash
  106. # <newline>
  107.  
  108. read x1 x2 x3 x4
  109. echo "=== query: $x1 $x2 $x3 $x4" >> $LFILE
  110. test "$x4" && echo "extra junk after request: $x4" && exit 0
  111. # nuke questionable characters and split up the request
  112. hurl=`echo "$x2" | sed -e "s+.*//++" -e 's+[\`'\''|$;<>{}\\!*()"]++g'`
  113. # echo massaged hurl: $hurl >> $LFILE
  114. hh=`echo "$hurl" | sed -e "s+/.*++" -e "s+:.*++"`
  115. hp=`echo "$hurl" | sed -e "s+.*:++" -e "s+/.*++"`
  116. test "$hp" = "$hh" && hp=80
  117. hf=`echo "$hurl" | sed -e "s+[^/]*++"`
  118. # echo total split: $hh : $hp : $hf >> $LFILE
  119. # suck in and log the entire request, because we're curious
  120. # Fails on multipart stuff like forms; oh well...
  121. if test "$x3" ; then
  122.   while read xx ; do
  123.     echo "${xx}" >> $LFILE
  124.     test "${xx}" || break
  125. # eew, buried returns, gross but necessary for DOS stupidity:
  126.     test "${xx}" = "
  127. " && break
  128.   done
  129. fi
  130. # check for non-GET *after* we log the query...
  131. test "$x1" != "GET" && echo "sorry, this proxy only does GETs" && exit 0
  132. # no, you can *not* phone home, you miserable piece of shit
  133. test "`echo $hh | fgrep -i netscap`" && \
  134.   echo "access to Netscam's servers <b>DENIED.</b>" && exit 0
  135. # Do it.  30 sec net-wait time oughta be *plenty*...
  136. # Some braindead servers have forgotten how to handle the simple-query syntax.
  137. # If necessary, replace below with (echo "$x1 $hf" ; echo '') | nc...
  138. echo "$x1 $hf" | nc -w 30 "$hh" "$hp" 2> /dev/null || \
  139.   echo "oops, can't get to $hh : $hp".
  140. echo "sent \"$x1 $hf\" to $hh : $hp" >> $LFILE
  141. exit 0
  142.  
  143.