socks.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package protocol
  2. import (
  3. "encoding/binary"
  4. "io"
  5. "github.com/v2ray/v2ray-core/common/alloc"
  6. "github.com/v2ray/v2ray-core/common/log"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. "github.com/v2ray/v2ray-core/proxy"
  9. "github.com/v2ray/v2ray-core/transport"
  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 := alloc.NewSmallBuffer()
  37. defer buffer.Release()
  38. nBytes, err := reader.Read(buffer.Value)
  39. if err != nil {
  40. return
  41. }
  42. if nBytes < 2 {
  43. log.Info("Socks expected 2 bytes read, but only %d bytes read", nBytes)
  44. err = transport.CorruptedPacket
  45. return
  46. }
  47. if buffer.Value[0] == socks4Version {
  48. auth4.Version = buffer.Value[0]
  49. auth4.Command = buffer.Value[1]
  50. auth4.Port = binary.BigEndian.Uint16(buffer.Value[2:4])
  51. copy(auth4.IP[:], buffer.Value[4:8])
  52. err = Socks4Downgrade
  53. return
  54. }
  55. auth.version = buffer.Value[0]
  56. if auth.version != socksVersion {
  57. log.Warning("Unknown protocol version %d", auth.version)
  58. err = proxy.InvalidProtocolVersion
  59. return
  60. }
  61. auth.nMethods = buffer.Value[1]
  62. if auth.nMethods <= 0 {
  63. log.Info("Zero length of authentication methods")
  64. err = transport.CorruptedPacket
  65. return
  66. }
  67. if nBytes-2 != int(auth.nMethods) {
  68. log.Info("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
  69. err = transport.CorruptedPacket
  70. return
  71. }
  72. copy(auth.authMethods[:], buffer.Value[2:nBytes])
  73. return
  74. }
  75. type Socks5AuthenticationResponse struct {
  76. version byte
  77. authMethod byte
  78. }
  79. func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse {
  80. return &Socks5AuthenticationResponse{
  81. version: socksVersion,
  82. authMethod: authMethod,
  83. }
  84. }
  85. func WriteAuthentication(writer io.Writer, r *Socks5AuthenticationResponse) error {
  86. _, err := writer.Write([]byte{r.version, r.authMethod})
  87. return err
  88. }
  89. type Socks5UserPassRequest struct {
  90. version byte
  91. username string
  92. password string
  93. }
  94. func (request Socks5UserPassRequest) Username() string {
  95. return request.username
  96. }
  97. func (request Socks5UserPassRequest) Password() string {
  98. return request.password
  99. }
  100. func (request Socks5UserPassRequest) AuthDetail() string {
  101. return request.username + ":" + request.password
  102. }
  103. func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) {
  104. buffer := alloc.NewSmallBuffer()
  105. defer buffer.Release()
  106. _, err = reader.Read(buffer.Value[0:2])
  107. if err != nil {
  108. return
  109. }
  110. request.version = buffer.Value[0]
  111. nUsername := buffer.Value[1]
  112. nBytes, err := reader.Read(buffer.Value[:nUsername])
  113. if err != nil {
  114. return
  115. }
  116. request.username = string(buffer.Value[:nBytes])
  117. _, err = reader.Read(buffer.Value[0:1])
  118. if err != nil {
  119. return
  120. }
  121. nPassword := buffer.Value[0]
  122. nBytes, err = reader.Read(buffer.Value[:nPassword])
  123. if err != nil {
  124. return
  125. }
  126. request.password = string(buffer.Value[:nBytes])
  127. return
  128. }
  129. type Socks5UserPassResponse struct {
  130. version byte
  131. status byte
  132. }
  133. func NewSocks5UserPassResponse(status byte) Socks5UserPassResponse {
  134. return Socks5UserPassResponse{
  135. version: socksVersion,
  136. status: status,
  137. }
  138. }
  139. func WriteUserPassResponse(writer io.Writer, response Socks5UserPassResponse) error {
  140. _, err := writer.Write([]byte{response.version, response.status})
  141. return err
  142. }
  143. const (
  144. AddrTypeIPv4 = byte(0x01)
  145. AddrTypeIPv6 = byte(0x04)
  146. AddrTypeDomain = byte(0x03)
  147. CmdConnect = byte(0x01)
  148. CmdBind = byte(0x02)
  149. CmdUdpAssociate = byte(0x03)
  150. )
  151. type Socks5Request struct {
  152. Version byte
  153. Command byte
  154. AddrType byte
  155. IPv4 [4]byte
  156. Domain string
  157. IPv6 [16]byte
  158. Port uint16
  159. }
  160. func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
  161. buffer := alloc.NewSmallBuffer()
  162. defer buffer.Release()
  163. nBytes, err := reader.Read(buffer.Value[:4])
  164. if err != nil {
  165. return
  166. }
  167. if nBytes < 4 {
  168. err = transport.CorruptedPacket
  169. return
  170. }
  171. request = &Socks5Request{
  172. Version: buffer.Value[0],
  173. Command: buffer.Value[1],
  174. // buffer[2] is a reserved field
  175. AddrType: buffer.Value[3],
  176. }
  177. switch request.AddrType {
  178. case AddrTypeIPv4:
  179. nBytes, err = reader.Read(request.IPv4[:])
  180. if err != nil {
  181. return
  182. }
  183. if nBytes != 4 {
  184. err = transport.CorruptedPacket
  185. return
  186. }
  187. case AddrTypeDomain:
  188. nBytes, err = reader.Read(buffer.Value[0:1])
  189. if err != nil {
  190. return
  191. }
  192. domainLength := buffer.Value[0]
  193. nBytes, err = reader.Read(buffer.Value[:domainLength])
  194. if err != nil {
  195. return
  196. }
  197. if nBytes != int(domainLength) {
  198. log.Info("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
  199. err = transport.CorruptedPacket
  200. return
  201. }
  202. request.Domain = string(buffer.Value[:domainLength])
  203. case AddrTypeIPv6:
  204. nBytes, err = reader.Read(request.IPv6[:])
  205. if err != nil {
  206. return
  207. }
  208. if nBytes != 16 {
  209. err = transport.CorruptedPacket
  210. return
  211. }
  212. default:
  213. log.Info("Unexpected address type %d", request.AddrType)
  214. err = transport.CorruptedPacket
  215. return
  216. }
  217. nBytes, err = reader.Read(buffer.Value[:2])
  218. if err != nil {
  219. return
  220. }
  221. if nBytes != 2 {
  222. err = transport.CorruptedPacket
  223. return
  224. }
  225. request.Port = binary.BigEndian.Uint16(buffer.Value[:2])
  226. return
  227. }
  228. func (request *Socks5Request) Destination() v2net.Destination {
  229. var address v2net.Address
  230. switch request.AddrType {
  231. case AddrTypeIPv4:
  232. address = v2net.IPAddress(request.IPv4[:], request.Port)
  233. case AddrTypeIPv6:
  234. address = v2net.IPAddress(request.IPv6[:], request.Port)
  235. case AddrTypeDomain:
  236. address = v2net.DomainAddress(request.Domain, request.Port)
  237. default:
  238. panic("Unknown address type")
  239. }
  240. return v2net.NewTCPDestination(address)
  241. }
  242. const (
  243. ErrorSuccess = byte(0x00)
  244. ErrorGeneralFailure = byte(0x01)
  245. ErrorConnectionNotAllowed = byte(0x02)
  246. ErrorNetworkUnreachable = byte(0x03)
  247. ErrorHostUnUnreachable = byte(0x04)
  248. ErrorConnectionRefused = byte(0x05)
  249. ErrorTTLExpired = byte(0x06)
  250. ErrorCommandNotSupported = byte(0x07)
  251. ErrorAddressTypeNotSupported = byte(0x08)
  252. )
  253. type Socks5Response struct {
  254. Version byte
  255. Error byte
  256. AddrType byte
  257. IPv4 [4]byte
  258. Domain string
  259. IPv6 [16]byte
  260. Port uint16
  261. }
  262. func NewSocks5Response() *Socks5Response {
  263. return &Socks5Response{
  264. Version: socksVersion,
  265. }
  266. }
  267. func (r *Socks5Response) SetIPv4(ipv4 []byte) {
  268. r.AddrType = AddrTypeIPv4
  269. copy(r.IPv4[:], ipv4)
  270. }
  271. func (r *Socks5Response) SetIPv6(ipv6 []byte) {
  272. r.AddrType = AddrTypeIPv6
  273. copy(r.IPv6[:], ipv6)
  274. }
  275. func (r *Socks5Response) SetDomain(domain string) {
  276. r.AddrType = AddrTypeDomain
  277. r.Domain = domain
  278. }
  279. func (r *Socks5Response) Write(buffer *alloc.Buffer) {
  280. buffer.AppendBytes(r.Version, r.Error, 0x00 /* reserved */, r.AddrType)
  281. switch r.AddrType {
  282. case 0x01:
  283. buffer.Append(r.IPv4[:])
  284. case 0x03:
  285. buffer.AppendBytes(byte(len(r.Domain)))
  286. buffer.Append([]byte(r.Domain))
  287. case 0x04:
  288. buffer.Append(r.IPv6[:])
  289. }
  290. buffer.AppendBytes(byte(r.Port>>8), byte(r.Port))
  291. }