A SOCKS (SOCKS4, SOCKS4A and SOCKS5) Proxy Package for Go
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.

151 lines
6.8 KiB

  1. SOCKS: A protocol for TCP proxy across firewalls
  2. Ying-Da Lee
  3. yingda@best.com or yingda@esd.sgi.com
  4. SOCKS was originally developed by David Koblas and subsequently modified
  5. and extended by me to its current running version -- version 4. It is a
  6. protocol that relays TCP sessions at a firewall host to allow application
  7. users transparent access across the firewall. Because the protocol is
  8. independent of application protocols, it can be (and has been) used for
  9. many different services, such as telnet, ftp, finger, whois, gopher, WWW,
  10. etc. Access control can be applied at the beginning of each TCP session;
  11. thereafter the server simply relays the data between the client and the
  12. application server, incurring minimum processing overhead. Since SOCKS
  13. never has to know anything about the application protocol, it should also
  14. be easy for it to accommodate applications which use encryption to protect
  15. their traffic from nosey snoopers.
  16. Two operations are defined: CONNECT and BIND.
  17. 1) CONNECT
  18. The client connects to the SOCKS server and sends a CONNECT request when
  19. it wants to establish a connection to an application server. The client
  20. includes in the request packet the IP address and the port number of the
  21. destination host, and userid, in the following format.
  22. +----+----+----+----+----+----+----+----+----+----+....+----+
  23. | VN | CD | DSTPORT | DSTIP | USERID |NULL|
  24. +----+----+----+----+----+----+----+----+----+----+....+----+
  25. # of bytes: 1 1 2 4 variable 1
  26. VN is the SOCKS protocol version number and should be 4. CD is the
  27. SOCKS command code and should be 1 for CONNECT request. NULL is a byte
  28. of all zero bits.
  29. The SOCKS server checks to see whether such a request should be granted
  30. based on any combination of source IP address, destination IP address,
  31. destination port number, the userid, and information it may obtain by
  32. consulting IDENT, cf. RFC 1413. If the request is granted, the SOCKS
  33. server makes a connection to the specified port of the destination host.
  34. A reply packet is sent to the client when this connection is established,
  35. or when the request is rejected or the operation fails.
  36. +----+----+----+----+----+----+----+----+
  37. | VN | CD | DSTPORT | DSTIP |
  38. +----+----+----+----+----+----+----+----+
  39. # of bytes: 1 1 2 4
  40. VN is the version of the reply code and should be 0. CD is the result
  41. code with one of the following values:
  42. 90: request granted
  43. 91: request rejected or failed
  44. 92: request rejected becasue SOCKS server cannot connect to
  45. identd on the client
  46. 93: request rejected because the client program and identd
  47. report different user-ids
  48. The remaining fields are ignored.
  49. The SOCKS server closes its connection immediately after notifying
  50. the client of a failed or rejected request. For a successful request,
  51. the SOCKS server gets ready to relay traffic on both directions. This
  52. enables the client to do I/O on its connection as if it were directly
  53. connected to the application server.
  54. 2) BIND
  55. The client connects to the SOCKS server and sends a BIND request when
  56. it wants to prepare for an inbound connection from an application server.
  57. This should only happen after a primary connection to the application
  58. server has been established with a CONNECT. Typically, this is part of
  59. the sequence of actions:
  60. -bind(): obtain a socket
  61. -getsockname(): get the IP address and port number of the socket
  62. -listen(): ready to accept call from the application server
  63. -use the primary connection to inform the application server of
  64. the IP address and the port number that it should connect to.
  65. -accept(): accept a connection from the application server
  66. The purpose of SOCKS BIND operation is to support such a sequence
  67. but using a socket on the SOCKS server rather than on the client.
  68. The client includes in the request packet the IP address of the
  69. application server, the destination port used in the primary connection,
  70. and the userid.
  71. +----+----+----+----+----+----+----+----+----+----+....+----+
  72. | VN | CD | DSTPORT | DSTIP | USERID |NULL|
  73. +----+----+----+----+----+----+----+----+----+----+....+----+
  74. # of bytes: 1 1 2 4 variable 1
  75. VN is again 4 for the SOCKS protocol version number. CD must be 2 to
  76. indicate BIND request.
  77. The SOCKS server uses the client information to decide whether the
  78. request is to be granted. The reply it sends back to the client has
  79. the same format as the reply for CONNECT request, i.e.,
  80. +----+----+----+----+----+----+----+----+
  81. | VN | CD | DSTPORT | DSTIP |
  82. +----+----+----+----+----+----+----+----+
  83. # of bytes: 1 1 2 4
  84. VN is the version of the reply code and should be 0. CD is the result
  85. code with one of the following values:
  86. 90: request granted
  87. 91: request rejected or failed
  88. 92: request rejected becasue SOCKS server cannot connect to
  89. identd on the client
  90. 93: request rejected because the client program and identd
  91. report different user-ids.
  92. However, for a granted request (CD is 90), the DSTPORT and DSTIP fields
  93. are meaningful. In that case, the SOCKS server obtains a socket to wait
  94. for an incoming connection and sends the port number and the IP address
  95. of that socket to the client in DSTPORT and DSTIP, respectively. If the
  96. DSTIP in the reply is 0 (the value of constant INADDR_ANY), then the
  97. client should replace it with the IP address of the SOCKS server to which
  98. the cleint is connected. (This happens if the SOCKS server is not a
  99. multi-homed host.) In the typical scenario, these two numbers are
  100. made available to the application client prgram via the result of the
  101. subsequent getsockname() call. The application protocol must provide a
  102. way for these two pieces of information to be sent from the client to
  103. the application server so that it can initiate the connection, which
  104. connects it to the SOCKS server rather than directly to the application
  105. client as it normally would.
  106. The SOCKS server sends a second reply packet to the client when the
  107. anticipated connection from the application server is established.
  108. The SOCKS server checks the IP address of the originating host against
  109. the value of DSTIP specified in the client's BIND request. If a mismatch
  110. is found, the CD field in the second reply is set to 91 and the SOCKS
  111. server closes both connections. If the two match, CD in the second
  112. reply is set to 90 and the SOCKS server gets ready to relay the traffic
  113. on its two connections. From then on the client does I/O on its connection
  114. to the SOCKS server as if it were directly connected to the application
  115. server.
  116. For both CONNECT and BIND operations, the server sets a time limit
  117. (2 minutes in current CSTC implementation) for the establishment of its
  118. connection with the application server. If the connection is still not
  119. establiched when the time limit expires, the server closes its connection
  120. to the client and gives up.