socks.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package protocol
  2. import (
  3. "io"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. "github.com/v2ray/v2ray-core/common/log"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. proxyerrors "github.com/v2ray/v2ray-core/proxy/common/errors"
  8. "github.com/v2ray/v2ray-core/transport"
  9. )
  10. const (
  11. socksVersion = byte(0x05)
  12. socks4Version = byte(0x04)
  13. AuthNotRequired = byte(0x00)
  14. AuthGssApi = byte(0x01)
  15. AuthUserPass = byte(0x02)
  16. AuthNoMatchingMethod = byte(0xFF)
  17. Socks4RequestGranted = byte(90)
  18. Socks4RequestRejected = byte(91)
  19. )
  20. // Authentication request header of Socks5 protocol
  21. type Socks5AuthenticationRequest struct {
  22. version byte
  23. nMethods byte
  24. authMethods [256]byte
  25. }
  26. func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
  27. for i := 0; i < int(request.nMethods); i++ {
  28. if request.authMethods[i] == method {
  29. return true
  30. }
  31. }
  32. return false
  33. }
  34. func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, auth4 Socks4AuthenticationRequest, err error) {
  35. buffer := alloc.NewSmallBuffer()
  36. defer buffer.Release()
  37. nBytes, err := reader.Read(buffer.Value)
  38. if err != nil {
  39. return
  40. }
  41. if nBytes < 2 {
  42. log.Info("Socks expected 2 bytes read, but only %d bytes read", nBytes)
  43. err = transport.CorruptedPacket
  44. return
  45. }
  46. if buffer.Value[0] == socks4Version {
  47. auth4.Version = buffer.Value[0]
  48. auth4.Command = buffer.Value[1]
  49. auth4.Port = v2net.PortFromBytes(buffer.Value[2:4])
  50. copy(auth4.IP[:], buffer.Value[4:8])
  51. err = Socks4Downgrade
  52. return
  53. }
  54. auth.version = buffer.Value[0]
  55. if auth.version != socksVersion {
  56. log.Warning("Unknown protocol version %d", auth.version)
  57. err = proxyerrors.InvalidProtocolVersion
  58. return
  59. }
  60. auth.nMethods = buffer.Value[1]
  61. if auth.nMethods <= 0 {
  62. log.Info("Zero length of authentication methods")
  63. err = transport.CorruptedPacket
  64. return
  65. }
  66. if nBytes-2 != int(auth.nMethods) {
  67. log.Info("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
  68. err = transport.CorruptedPacket
  69. return
  70. }
  71. copy(auth.authMethods[:], buffer.Value[2:nBytes])
  72. return
  73. }
  74. type Socks5AuthenticationResponse struct {
  75. version byte
  76. authMethod byte
  77. }
  78. func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse {
  79. return &Socks5AuthenticationResponse{
  80. version: socksVersion,
  81. authMethod: authMethod,
  82. }
  83. }
  84. func WriteAuthentication(writer io.Writer, r *Socks5AuthenticationResponse) error {
  85. _, err := writer.Write([]byte{r.version, r.authMethod})
  86. return err
  87. }
  88. type Socks5UserPassRequest struct {
  89. version byte
  90. username string
  91. password string
  92. }
  93. func (request Socks5UserPassRequest) Username() string {
  94. return request.username
  95. }
  96. func (request Socks5UserPassRequest) Password() string {
  97. return request.password
  98. }
  99. func (request Socks5UserPassRequest) AuthDetail() string {
  100. return request.username + ":" + request.password
  101. }
  102. func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) {
  103. buffer := alloc.NewSmallBuffer()
  104. defer buffer.Release()
  105. _, err = reader.Read(buffer.Value[0:2])
  106. if err != nil {
  107. return
  108. }
  109. request.version = buffer.Value[0]
  110. nUsername := buffer.Value[1]
  111. nBytes, err := reader.Read(buffer.Value[:nUsername])
  112. if err != nil {
  113. return
  114. }
  115. request.username = string(buffer.Value[:nBytes])
  116. _, err = reader.Read(buffer.Value[0:1])
  117. if err != nil {
  118. return
  119. }
  120. nPassword := buffer.Value[0]
  121. nBytes, err = reader.Read(buffer.Value[:nPassword])
  122. if err != nil {
  123. return
  124. }
  125. request.password = string(buffer.Value[:nBytes])
  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 := alloc.NewSmallBuffer()
  161. defer buffer.Release()
  162. nBytes, err := reader.Read(buffer.Value[:4])
  163. if err != nil {
  164. return
  165. }
  166. if nBytes < 4 {
  167. err = transport.CorruptedPacket
  168. return
  169. }
  170. request = &Socks5Request{
  171. Version: buffer.Value[0],
  172. Command: buffer.Value[1],
  173. // buffer[2] is a reserved field
  174. AddrType: buffer.Value[3],
  175. }
  176. switch request.AddrType {
  177. case AddrTypeIPv4:
  178. nBytes, err = reader.Read(request.IPv4[:])
  179. if err != nil {
  180. return
  181. }
  182. if nBytes != 4 {
  183. err = transport.CorruptedPacket
  184. return
  185. }
  186. case AddrTypeDomain:
  187. nBytes, err = reader.Read(buffer.Value[0:1])
  188. if err != nil {
  189. return
  190. }
  191. domainLength := buffer.Value[0]
  192. nBytes, err = reader.Read(buffer.Value[:domainLength])
  193. if err != nil {
  194. return
  195. }
  196. if nBytes != int(domainLength) {
  197. log.Info("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
  198. err = transport.CorruptedPacket
  199. return
  200. }
  201. request.Domain = string(buffer.Value[:domainLength])
  202. case AddrTypeIPv6:
  203. nBytes, err = reader.Read(request.IPv6[:])
  204. if err != nil {
  205. return
  206. }
  207. if nBytes != 16 {
  208. err = transport.CorruptedPacket
  209. return
  210. }
  211. default:
  212. log.Info("Unexpected address type %d", request.AddrType)
  213. err = transport.CorruptedPacket
  214. return
  215. }
  216. nBytes, err = reader.Read(buffer.Value[:2])
  217. if err != nil {
  218. return
  219. }
  220. if nBytes != 2 {
  221. err = transport.CorruptedPacket
  222. return
  223. }
  224. request.Port = v2net.PortFromBytes(buffer.Value[:2])
  225. return
  226. }
  227. func (request *Socks5Request) Destination() v2net.Destination {
  228. var address v2net.Address
  229. switch request.AddrType {
  230. case AddrTypeIPv4:
  231. address = v2net.IPAddress(request.IPv4[:], request.Port)
  232. case AddrTypeIPv6:
  233. address = v2net.IPAddress(request.IPv6[:], request.Port)
  234. case AddrTypeDomain:
  235. address = v2net.DomainAddress(request.Domain, request.Port)
  236. default:
  237. panic("Unknown address type")
  238. }
  239. return v2net.NewTCPDestination(address)
  240. }
  241. const (
  242. ErrorSuccess = byte(0x00)
  243. ErrorGeneralFailure = byte(0x01)
  244. ErrorConnectionNotAllowed = byte(0x02)
  245. ErrorNetworkUnreachable = byte(0x03)
  246. ErrorHostUnUnreachable = byte(0x04)
  247. ErrorConnectionRefused = byte(0x05)
  248. ErrorTTLExpired = byte(0x06)
  249. ErrorCommandNotSupported = byte(0x07)
  250. ErrorAddressTypeNotSupported = byte(0x08)
  251. )
  252. type Socks5Response struct {
  253. Version byte
  254. Error byte
  255. AddrType byte
  256. IPv4 [4]byte
  257. Domain string
  258. IPv6 [16]byte
  259. Port v2net.Port
  260. }
  261. func NewSocks5Response() *Socks5Response {
  262. return &Socks5Response{
  263. Version: socksVersion,
  264. }
  265. }
  266. func (r *Socks5Response) SetIPv4(ipv4 []byte) {
  267. r.AddrType = AddrTypeIPv4
  268. copy(r.IPv4[:], ipv4)
  269. }
  270. func (r *Socks5Response) SetIPv6(ipv6 []byte) {
  271. r.AddrType = AddrTypeIPv6
  272. copy(r.IPv6[:], ipv6)
  273. }
  274. func (r *Socks5Response) SetDomain(domain string) {
  275. r.AddrType = AddrTypeDomain
  276. r.Domain = domain
  277. }
  278. func (r *Socks5Response) Write(buffer *alloc.Buffer) {
  279. buffer.AppendBytes(r.Version, r.Error, 0x00 /* reserved */, r.AddrType)
  280. switch r.AddrType {
  281. case 0x01:
  282. buffer.Append(r.IPv4[:])
  283. case 0x03:
  284. buffer.AppendBytes(byte(len(r.Domain)))
  285. buffer.Append([]byte(r.Domain))
  286. case 0x04:
  287. buffer.Append(r.IPv6[:])
  288. }
  289. buffer.Append(r.Port.Bytes())
  290. }