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.

102 lines
2.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #! /usr/bin/env bash
  2. # source: socks4echo.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 socks4 server with echo function via stdio.
  6. # accepts and answers correct SOCKS4 requests, but then just echoes data.
  7. # it is required for test.sh
  8. # for TCP, use this script as:
  9. # socat tcp-l:1080,reuseaddr,crlf system:"socks4echo.sh"
  10. # older bash and ksh do not have -n option to read command; we try dd then
  11. #if echo a |read -n 1 null >/dev/null 2>&1; then
  12. # HAVE_READ_N=1
  13. #else
  14. # and newer bash (4.3) has some other problem with read -n
  15. HAVE_READ_N=
  16. #fi
  17. if type socat >/dev/null 2>&1; then
  18. SOCAT=socat
  19. else
  20. SOCAT=./socat
  21. fi
  22. case `uname` in
  23. HP-UX|OSF1)
  24. CAT="$SOCAT -u stdin stdout"
  25. ;;
  26. *)
  27. CAT=cat
  28. ;;
  29. esac
  30. if [ $(echo "x\c") = "x" ]; then E=""
  31. elif [ $(echo -e "x\c") = "x" ]; then E="-e"
  32. else
  33. echo "cannot suppress trailing newline on echo" >&2
  34. exit 1
  35. fi
  36. ECHO="echo $E"
  37. if [ $($ECHO "\0101") = "A" ]; then
  38. SOCKSREPLY_FAILED="\0\0133\0\0\0\0\0\0\c"
  39. SOCKSREPLY_OK="\0\0132\0\0\0\0\0\0\c"
  40. else
  41. SOCKSREPLY_FAILED="\0\133\0\0\0\0\0\0\c"
  42. SOCKSREPLY_OK="\0\132\0\0\0\0\0\0\c"
  43. fi
  44. # read and parse SOCKS4 header
  45. if [ "$HAVE_READ_N" ]; then
  46. read -r -n 1 vn # bash 2.0.3 does not support -n
  47. else
  48. vn=$(dd bs=1 count=1 2>/dev/null)
  49. fi
  50. if [ "$vn" != $($ECHO "\04") ]; then
  51. $ECHO "$SOCKSREPLY_FAILED"
  52. echo "invalid socks version requested" >&2
  53. exit
  54. fi
  55. if [ "$HAVE_READ_N" ]; then
  56. read -r -n 1 cd
  57. else
  58. cd=$(dd bs=1 count=1 2>/dev/null)
  59. fi
  60. if [ "$cd" != $($ECHO "\01") ]; then
  61. $ECHO "$SOCKSREPLY_FAILED"
  62. echo "invalid socks operation requested" >&2
  63. exit
  64. fi
  65. if [ "$HAVE_READ_N" ]; then
  66. read -r -n 6 a
  67. else
  68. a=$(dd bs=1 count=6 2>/dev/null)
  69. fi
  70. if [ "$a" != "$($ECHO "}m bL6")" ]; then
  71. $ECHO "$SOCKSREPLY_FAILED"
  72. echo "$0: wrong socks address or port requested" >&2
  73. echo "$0: expected $($ECHO "}m bL6"|od -t x1), received $($ECHO "$a"|od -t x1)" >&2
  74. exit
  75. fi
  76. if [ "$HAVE_READ_N" ]; then
  77. read -r -n 7 u
  78. else
  79. u=$(dd bs=1 count=7 2>/dev/null)
  80. fi
  81. if [ "$u" != "nobody" ]; then
  82. $ECHO "$SOCKSREPLY_FAILED"
  83. echo "wrong socks user requested" >&2
  84. exit
  85. fi
  86. # send ok status
  87. $ECHO "$SOCKSREPLY_OK"
  88. # perform echo function
  89. $CAT