socks.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 := make([]byte, 256)
  36. nBytes, err := reader.Read(buffer)
  37. if err != nil {
  38. return
  39. }
  40. if nBytes < 2 {
  41. log.Warning("Socks: expected 2 bytes read, but only ", nBytes, " bytes read")
  42. err = transport.ErrCorruptedPacket
  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 = proxy.ErrInvalidAuthentication
  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 = proxy.ErrInvalidAuthentication
  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 := alloc.NewSmallBuffer()
  103. defer buffer.Release()
  104. _, err = reader.Read(buffer.Value[0:2])
  105. if err != nil {
  106. return
  107. }
  108. request.version = buffer.Value[0]
  109. nUsername := buffer.Value[1]
  110. nBytes, err := reader.Read(buffer.Value[:nUsername])
  111. if err != nil {
  112. return
  113. }
  114. request.username = string(buffer.Value[:nBytes])
  115. _, err = reader.Read(buffer.Value[0:1])
  116. if err != nil {
  117. return
  118. }
  119. nPassword := buffer.Value[0]
  120. nBytes, err = reader.Read(buffer.Value[:nPassword])
  121. if err != nil {
  122. return
  123. }
  124. request.password = string(buffer.Value[:nBytes])
  125. return
  126. }
  127. type Socks5UserPassResponse struct {
  128. version byte
  129. status byte
  130. }
  131. func NewSocks5UserPassResponse(status byte) Socks5UserPassResponse {
  132. return Socks5UserPassResponse{
  133. version: socksVersion,
  134. status: status,
  135. }
  136. }
  137. func WriteUserPassResponse(writer io.Writer, response Socks5UserPassResponse) error {
  138. _, err := writer.Write([]byte{response.version, response.status})
  139. return err
  140. }
  141. const (
  142. AddrTypeIPv4 = byte(0x01)
  143. AddrTypeIPv6 = byte(0x04)
  144. AddrTypeDomain = byte(0x03)
  145. CmdConnect = byte(0x01)
  146. CmdBind = byte(0x02)
  147. CmdUdpAssociate = byte(0x03)
  148. )
  149. type Socks5Request struct {
  150. Version byte
  151. Command byte
  152. AddrType byte
  153. IPv4 [4]byte
  154. Domain string
  155. IPv6 [16]byte
  156. Port v2net.Port
  157. }
  158. func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
  159. buffer := alloc.NewSmallBuffer()
  160. defer buffer.Release()
  161. _, err = io.ReadFull(reader, buffer.Value[:4])
  162. if err != nil {
  163. return
  164. }
  165. request = &Socks5Request{
  166. Version: buffer.Value[0],
  167. Command: buffer.Value[1],
  168. // buffer[2] is a reserved field
  169. AddrType: buffer.Value[3],
  170. }
  171. switch request.AddrType {
  172. case AddrTypeIPv4:
  173. _, err = io.ReadFull(reader, request.IPv4[:])
  174. if err != nil {
  175. return
  176. }
  177. case AddrTypeDomain:
  178. _, err = io.ReadFull(reader, buffer.Value[0:1])
  179. if err != nil {
  180. return
  181. }
  182. domainLength := buffer.Value[0]
  183. _, err = io.ReadFull(reader, buffer.Value[:domainLength])
  184. if err != nil {
  185. return
  186. }
  187. request.Domain = string(append([]byte(nil), buffer.Value[:domainLength]...))
  188. case AddrTypeIPv6:
  189. _, err = io.ReadFull(reader, request.IPv6[:])
  190. if err != nil {
  191. return
  192. }
  193. default:
  194. log.Warning("Socks: Unexpected address type ", request.AddrType)
  195. err = transport.ErrCorruptedPacket
  196. return
  197. }
  198. _, err = io.ReadFull(reader, buffer.Value[:2])
  199. if err != nil {
  200. return
  201. }
  202. request.Port = v2net.PortFromBytes(buffer.Value[:2])
  203. return
  204. }
  205. func (request *Socks5Request) Destination() v2net.Destination {
  206. switch request.AddrType {
  207. case AddrTypeIPv4:
  208. return v2net.TCPDestination(v2net.IPAddress(request.IPv4[:]), request.Port)
  209. case AddrTypeIPv6:
  210. return v2net.TCPDestination(v2net.IPAddress(request.IPv6[:]), request.Port)
  211. case AddrTypeDomain:
  212. return v2net.TCPDestination(v2net.ParseAddress(request.Domain), request.Port)
  213. default:
  214. panic("Unknown address type")
  215. }
  216. }
  217. const (
  218. ErrorSuccess = byte(0x00)
  219. ErrorGeneralFailure = byte(0x01)
  220. ErrorConnectionNotAllowed = byte(0x02)
  221. ErrorNetworkUnreachable = byte(0x03)
  222. ErrorHostUnUnreachable = byte(0x04)
  223. ErrorConnectionRefused = byte(0x05)
  224. ErrorTTLExpired = byte(0x06)
  225. ErrorCommandNotSupported = byte(0x07)
  226. ErrorAddressTypeNotSupported = byte(0x08)
  227. )
  228. type Socks5Response struct {
  229. Version byte
  230. Error byte
  231. AddrType byte
  232. IPv4 [4]byte
  233. Domain string
  234. IPv6 [16]byte
  235. Port v2net.Port
  236. }
  237. func NewSocks5Response() *Socks5Response {
  238. return &Socks5Response{
  239. Version: socksVersion,
  240. }
  241. }
  242. func (r *Socks5Response) SetIPv4(ipv4 []byte) {
  243. r.AddrType = AddrTypeIPv4
  244. copy(r.IPv4[:], ipv4)
  245. }
  246. func (r *Socks5Response) SetIPv6(ipv6 []byte) {
  247. r.AddrType = AddrTypeIPv6
  248. copy(r.IPv6[:], ipv6)
  249. }
  250. func (r *Socks5Response) SetDomain(domain string) {
  251. r.AddrType = AddrTypeDomain
  252. r.Domain = domain
  253. }
  254. func (r *Socks5Response) Write(writer io.Writer) {
  255. writer.Write([]byte{r.Version, r.Error, 0x00 /* reserved */, r.AddrType})
  256. switch r.AddrType {
  257. case 0x01:
  258. writer.Write(r.IPv4[:])
  259. case 0x03:
  260. writer.Write([]byte{byte(len(r.Domain))})
  261. writer.Write([]byte(r.Domain))
  262. case 0x04:
  263. writer.Write(r.IPv6[:])
  264. }
  265. writer.Write(r.Port.Bytes(nil))
  266. }