socks.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. "github.com/v2ray/v2ray-core/proxy"
  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.Warning("Socks: expected 2 bytes read, but only ", nBytes, " bytes read")
  43. err = transport.ErrCorruptedPacket
  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("Socks: Unknown protocol version ", auth.version)
  57. err = proxy.ErrInvalidProtocolVersion
  58. return
  59. }
  60. auth.nMethods = buffer.Value[1]
  61. if auth.nMethods <= 0 {
  62. log.Warning("Socks: Zero length of authentication methods")
  63. err = proxy.ErrInvalidAuthentication
  64. return
  65. }
  66. if nBytes-2 != int(auth.nMethods) {
  67. log.Warning("Socks: Unmatching number of auth methods, expecting ", auth.nMethods, ", but got ", nBytes)
  68. err = proxy.ErrInvalidAuthentication
  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. _, err = io.ReadFull(reader, buffer.Value[:4])
  163. if err != nil {
  164. return
  165. }
  166. request = &Socks5Request{
  167. Version: buffer.Value[0],
  168. Command: buffer.Value[1],
  169. // buffer[2] is a reserved field
  170. AddrType: buffer.Value[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. _, err = io.ReadFull(reader, buffer.Value[0:1])
  180. if err != nil {
  181. return
  182. }
  183. domainLength := buffer.Value[0]
  184. _, err = io.ReadFull(reader, buffer.Value[:domainLength])
  185. if err != nil {
  186. return
  187. }
  188. request.Domain = string(append([]byte(nil), buffer.Value[:domainLength]...))
  189. case AddrTypeIPv6:
  190. _, err = io.ReadFull(reader, request.IPv6[:])
  191. if err != nil {
  192. return
  193. }
  194. default:
  195. log.Warning("Socks: Unexpected address type ", request.AddrType)
  196. err = transport.ErrCorruptedPacket
  197. return
  198. }
  199. _, err = io.ReadFull(reader, buffer.Value[:2])
  200. if err != nil {
  201. return
  202. }
  203. request.Port = v2net.PortFromBytes(buffer.Value[: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. }