You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #! /usr/bin/env bash
  2. # source: proxy.sh
  3. # Copyright Gerhard Rieger and contributors (see file CHANGES)
  4. # Published under the GNU General Public License V.2, see file COPYING
  5. # perform primitive simulation of a proxy server.
  6. # accepts and answers correct HTTP CONNECT requests on stdio, and tries to
  7. # establish the connection to the given server.
  8. # it is required for socats test.sh
  9. # for TCP, use this script as:
  10. # socat tcp-l:8080,reuseaddr,fork exec:"proxy.sh",nofork
  11. # 20130622 GR allow hostnames, not only IP addresses
  12. if [ -z "$SOCAT" ]; then
  13. if type socat >/dev/null 2>&1; then
  14. SOCAT=socat
  15. else
  16. SOCAT="./socat"
  17. fi
  18. fi
  19. if [ $(echo "x\c") = "x" ]; then E=""
  20. elif [ $(echo -e "x\c") = "x" ]; then E="-e"
  21. else
  22. echo "cannot suppress trailing newline on echo" >&2
  23. exit 1
  24. fi
  25. ECHO="echo $E"
  26. CR=$($ECHO "\r")
  27. #echo "CR=$($ECHO "$CR\c" |od -c)" >&2
  28. case `uname` in
  29. HP-UX|OSF1)
  30. # their cats are too stupid to work with unix domain sockets
  31. CAT="$SOCAT -u stdin stdout"
  32. ;;
  33. *)
  34. CAT=cat
  35. ;;
  36. esac
  37. SPACES=" "
  38. while [ -n "$1" ]; do
  39. case "$1" in
  40. -w) n="$2"; while [ "$n" -gt 0 ]; do SPACES="$SPACES "; n=$((n-1)); done
  41. shift ;;
  42. #-s) STAT="$2"; shift ;;
  43. esac
  44. shift
  45. done
  46. badrequest () {
  47. $ECHO "HTTP/1.0${SPACES}500 Bad Request$CR"
  48. $ECHO "$CR"
  49. }
  50. # read and parse HTTP request
  51. read m a h
  52. #echo "\"$m\" \"$a\" \"$h\"" >&2
  53. if [ "$m" != 'CONNECT' ]; then
  54. badrequest; exit 1
  55. fi
  56. if [[ "$a" == [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+ ]]; then
  57. : go on below
  58. elif [[ "$a" == [0-9a-zA-Z-.][0-9a-zA-Z-.]*:[0-9][0-9]* ]]; then
  59. : go on below
  60. else
  61. badrequest; exit 1
  62. fi
  63. if [[ "$h" == HTTP/1.[01][[:space:]]* ]]; then
  64. : go on below
  65. else
  66. badrequest; exit 1
  67. fi
  68. # read more headers until empty line
  69. while [ "$l" != "$CR" ]; do
  70. read l
  71. done
  72. # send status
  73. $ECHO "HTTP/1.0${SPACES}200 OK$CR"
  74. # send empty line
  75. $ECHO "$CR"
  76. # perform proxy (relay) function
  77. $SOCAT $SOCAT_OPTS - tcp:$a || {
  78. $ECHO "HTTP/1.0${SPACES}500 Failed to connect to $a$CR"
  79. $ECHO $CR
  80. }