socks.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package protocol
  2. import (
  3. "io"
  4. "v2ray.com/core/common/buf"
  5. "v2ray.com/core/common/crypto"
  6. "v2ray.com/core/common/errors"
  7. "v2ray.com/core/common/log"
  8. v2net "v2ray.com/core/common/net"
  9. "v2ray.com/core/proxy"
  10. )
  11. const (
  12. socksVersion = byte(0x05)
  13. socks4Version = byte(0x04)
  14. AuthNotRequired = byte(0x00)
  15. AuthGssApi = byte(0x01)
  16. AuthUserPass = byte(0x02)
  17. AuthNoMatchingMethod = byte(0xFF)
  18. Socks4RequestGranted = byte(90)
  19. Socks4RequestRejected = byte(91)
  20. )
  21. // Authentication request header of Socks5 protocol
  22. type Socks5AuthenticationRequest struct {
  23. version byte
  24. nMethods byte
  25. authMethods [256]byte
  26. }
  27. func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
  28. for i := 0; i < int(request.nMethods); i++ {
  29. if request.authMethods[i] == method {
  30. return true
  31. }
  32. }
  33. return false
  34. }
  35. func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, auth4 Socks4AuthenticationRequest, err error) {
  36. buffer := make([]byte, 256)
  37. nBytes, err := reader.Read(buffer)
  38. if err != nil {
  39. return
  40. }
  41. if nBytes < 2 {
  42. err = errors.New("Socks: Insufficient header.")
  43. return
  44. }
  45. if buffer[0] == socks4Version {
  46. auth4.Version = buffer[0]
  47. auth4.Command = buffer[1]
  48. auth4.Port = v2net.PortFromBytes(buffer[2:4])
  49. copy(auth4.IP[:], buffer[4:8])
  50. err = Socks4Downgrade
  51. return
  52. }
  53. auth.version = buffer[0]
  54. if auth.version != socksVersion {
  55. log.Warning("Socks: Unknown protocol version ", auth.version)
  56. err = proxy.ErrInvalidProtocolVersion
  57. return
  58. }
  59. auth.nMethods = buffer[1]
  60. if auth.nMethods <= 0 {
  61. log.Warning("Socks: Zero length of authentication methods")
  62. err = crypto.ErrAuthenticationFailed
  63. return
  64. }
  65. if nBytes-2 != int(auth.nMethods) {
  66. log.Warning("Socks: Unmatching number of auth methods, expecting ", auth.nMethods, ", but got ", nBytes)
  67. err = crypto.ErrAuthenticationFailed
  68. return
  69. }
  70. copy(auth.authMethods[:], buffer[2:nBytes])
  71. return
  72. }
  73. type Socks5AuthenticationResponse struct {
  74. version byte
  75. authMethod byte
  76. }
  77. func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse {
  78. return &Socks5AuthenticationResponse{
  79. version: socksVersion,
  80. authMethod: authMethod,
  81. }
  82. }
  83. func WriteAuthentication(writer io.Writer, r *Socks5AuthenticationResponse) error {
  84. _, err := writer.Write([]byte{r.version, r.authMethod})
  85. return err
  86. }
  87. type Socks5UserPassRequest struct {
  88. version byte
  89. username string
  90. password string
  91. }
  92. func (request Socks5UserPassRequest) Username() string {
  93. return request.username
  94. }
  95. func (request Socks5UserPassRequest) Password() string {
  96. return request.password
  97. }
  98. func (request Socks5UserPassRequest) AuthDetail() string {
  99. return request.username + ":" + request.password
  100. }
  101. func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) {
  102. buffer := buf.NewLocal(512)
  103. defer buffer.Release()
  104. err = buffer.AppendSupplier(buf.ReadFullFrom(reader, 2))
  105. if err != nil {
  106. return
  107. }
  108. request.version = buffer.Byte(0)
  109. nUsername := int(buffer.Byte(1))
  110. buffer.Clear()
  111. err = buffer.AppendSupplier(buf.ReadFullFrom(reader, nUsername))
  112. if err != nil {
  113. return
  114. }
  115. request.username = buffer.String()
  116. err = buffer.AppendSupplier(buf.ReadFullFrom(reader, 1))
  117. if err != nil {
  118. return
  119. }
  120. nPassword := int(buffer.Byte(0))
  121. err = buffer.AppendSupplier(buf.ReadFullFrom(reader, nPassword))
  122. if err != nil {
  123. return
  124. }
  125. request.password = buffer.String()
  126. return
  127. }
  128. type Socks5UserPassResponse struct {
  129. version byte
  130. status byte
  131. }
  132. func NewSocks5UserPassResponse(status byte) Socks5UserPassResponse {
  133. return Socks5UserPassResponse{
  134. version: socksVersion,
  135. status: status,
  136. }
  137. }
  138. func WriteUserPassResponse(writer io.Writer, response Socks5UserPassResponse) error {
  139. _, err := writer.Write([]byte{response.version, response.status})
  140. return err
  141. }
  142. const (
  143. AddrTypeIPv4 = byte(0x01)
  144. AddrTypeIPv6 = byte(0x04)
  145. AddrTypeDomain = byte(0x03)
  146. CmdConnect = byte(0x01)
  147. CmdBind = byte(0x02)
  148. CmdUdpAssociate = byte(0x03)
  149. )
  150. type Socks5Request struct {
  151. Version byte
  152. Command byte
  153. AddrType byte
  154. IPv4 [4]byte
  155. Domain string
  156. IPv6 [16]byte
  157. Port v2net.Port
  158. }
  159. func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
  160. buffer := buf.NewLocal(512)
  161. defer buffer.Release()
  162. err = buffer.AppendSupplier(buf.ReadFullFrom(reader, 4))
  163. if err != nil {
  164. return
  165. }
  166. request = &Socks5Request{
  167. Version: buffer.Byte(0),
  168. Command: buffer.Byte(1),
  169. // buffer[2] is a reserved field
  170. AddrType: buffer.Byte(3),
  171. }
  172. switch request.AddrType {
  173. case AddrTypeIPv4:
  174. _, err = io.ReadFull(reader, request.IPv4[:])
  175. if err != nil {
  176. return
  177. }
  178. case AddrTypeDomain:
  179. buffer.Clear()
  180. err = buffer.AppendSupplier(buf.ReadFullFrom(reader, 1))
  181. if err != nil {
  182. return
  183. }
  184. domainLength := int(buffer.Byte(0))
  185. err = buffer.AppendSupplier(buf.ReadFullFrom(reader, domainLength))
  186. if err != nil {
  187. return
  188. }
  189. request.Domain = string(buffer.BytesFrom(-domainLength))
  190. case AddrTypeIPv6:
  191. _, err = io.ReadFull(reader, request.IPv6[:])
  192. if err != nil {
  193. return
  194. }
  195. default:
  196. err = errors.Format("Socks: Unexpected address type %d", request.AddrType)
  197. return
  198. }
  199. err = buffer.AppendSupplier(buf.ReadFullFrom(reader, 2))
  200. if err != nil {
  201. return
  202. }
  203. request.Port = v2net.PortFromBytes(buffer.BytesFrom(-2))
  204. return
  205. }
  206. func (request *Socks5Request) Destination() v2net.Destination {
  207. switch request.AddrType {
  208. case AddrTypeIPv4:
  209. return v2net.TCPDestination(v2net.IPAddress(request.IPv4[:]), request.Port)
  210. case AddrTypeIPv6:
  211. return v2net.TCPDestination(v2net.IPAddress(request.IPv6[:]), request.Port)
  212. case AddrTypeDomain:
  213. return v2net.TCPDestination(v2net.ParseAddress(request.Domain), request.Port)
  214. default:
  215. panic("Unknown address type")
  216. }
  217. }
  218. const (
  219. ErrorSuccess = byte(0x00)
  220. ErrorGeneralFailure = byte(0x01)
  221. ErrorConnectionNotAllowed = byte(0x02)
  222. ErrorNetworkUnreachable = byte(0x03)
  223. ErrorHostUnUnreachable = byte(0x04)
  224. ErrorConnectionRefused = byte(0x05)
  225. ErrorTTLExpired = byte(0x06)
  226. ErrorCommandNotSupported = byte(0x07)
  227. ErrorAddressTypeNotSupported = byte(0x08)
  228. )
  229. type Socks5Response struct {
  230. Version byte
  231. Error byte
  232. AddrType byte
  233. IPv4 [4]byte
  234. Domain string
  235. IPv6 [16]byte
  236. Port v2net.Port
  237. }
  238. func NewSocks5Response() *Socks5Response {
  239. return &Socks5Response{
  240. Version: socksVersion,
  241. }
  242. }
  243. func (r *Socks5Response) SetIPv4(ipv4 []byte) {
  244. r.AddrType = AddrTypeIPv4
  245. copy(r.IPv4[:], ipv4)
  246. }
  247. func (r *Socks5Response) SetIPv6(ipv6 []byte) {
  248. r.AddrType = AddrTypeIPv6
  249. copy(r.IPv6[:], ipv6)
  250. }
  251. func (r *Socks5Response) SetDomain(domain string) {
  252. r.AddrType = AddrTypeDomain
  253. r.Domain = domain
  254. }
  255. func (r *Socks5Response) Write(writer io.Writer) {
  256. writer.Write([]byte{r.Version, r.Error, 0x00 /* reserved */, r.AddrType})
  257. switch r.AddrType {
  258. case 0x01:
  259. writer.Write(r.IPv4[:])
  260. case 0x03:
  261. writer.Write([]byte{byte(len(r.Domain))})
  262. writer.Write([]byte(r.Domain))
  263. case 0x04:
  264. writer.Write(r.IPv6[:])
  265. }
  266. writer.Write(r.Port.Bytes(nil))
  267. }