socks5_helper.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package scenarios
  2. import (
  3. "net"
  4. routerconfig "github.com/v2ray/v2ray-core/app/router/config/testing"
  5. _ "github.com/v2ray/v2ray-core/app/router/rules"
  6. rulesconfig "github.com/v2ray/v2ray-core/app/router/rules/config/testing"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  9. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  10. _ "github.com/v2ray/v2ray-core/proxy/socks"
  11. socksjson "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  12. _ "github.com/v2ray/v2ray-core/proxy/vmess"
  13. "github.com/v2ray/v2ray-core/proxy/vmess/config"
  14. vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
  15. "github.com/v2ray/v2ray-core/shell/point"
  16. "github.com/v2ray/v2ray-core/shell/point/config/testing/mocks"
  17. )
  18. const (
  19. socks5Version = byte(0x05)
  20. )
  21. func socks5AuthMethodRequest(methods ...byte) []byte {
  22. request := []byte{socks5Version, byte(len(methods))}
  23. request = append(request, methods...)
  24. return request
  25. }
  26. func appendAddress(request []byte, address v2net.Address) []byte {
  27. switch {
  28. case address.IsIPv4():
  29. request = append(request, byte(0x01))
  30. request = append(request, address.IP()...)
  31. case address.IsIPv6():
  32. request = append(request, byte(0x04))
  33. request = append(request, address.IP()...)
  34. case address.IsDomain():
  35. request = append(request, byte(0x03), byte(len(address.Domain())))
  36. request = append(request, []byte(address.Domain())...)
  37. }
  38. request = append(request, address.Port().Bytes()...)
  39. return request
  40. }
  41. func socks5Request(command byte, address v2net.Address) []byte {
  42. request := []byte{socks5Version, command, 0}
  43. request = appendAddress(request, address)
  44. return request
  45. }
  46. func socks5UDPRequest(address v2net.Address, payload []byte) []byte {
  47. request := make([]byte, 0, 1024)
  48. request = append(request, 0, 0, 0)
  49. request = appendAddress(request, address)
  50. request = append(request, payload...)
  51. return request
  52. }
  53. func setUpV2Ray(routing func(v2net.Destination) bool) (v2net.Port, v2net.Port, error) {
  54. id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
  55. if err != nil {
  56. return 0, 0, err
  57. }
  58. id2, err := config.NewID("93ccfc71-b136-4015-ac85-e037bd1ead9e")
  59. if err != nil {
  60. return 0, 0, err
  61. }
  62. users := []*vmessjson.ConfigUser{
  63. &vmessjson.ConfigUser{Id: id1},
  64. &vmessjson.ConfigUser{Id: id2},
  65. }
  66. portB := v2nettesting.PickPort()
  67. configB := mocks.Config{
  68. PortValue: portB,
  69. InboundConfigValue: &mocks.ConnectionConfig{
  70. ProtocolValue: "vmess",
  71. SettingsValue: &vmessjson.Inbound{
  72. AllowedClients: users,
  73. },
  74. },
  75. OutboundConfigValue: &mocks.ConnectionConfig{
  76. ProtocolValue: "freedom",
  77. SettingsValue: nil,
  78. },
  79. }
  80. pointB, err := point.NewPoint(&configB)
  81. if err != nil {
  82. return 0, 0, err
  83. }
  84. err = pointB.Start()
  85. if err != nil {
  86. return 0, 0, err
  87. }
  88. portA := v2nettesting.PickPort()
  89. portA2 := v2nettesting.PickPort()
  90. configA := mocks.Config{
  91. PortValue: portA,
  92. InboundConfigValue: &mocks.ConnectionConfig{
  93. ProtocolValue: "socks",
  94. SettingsValue: &socksjson.SocksConfig{
  95. AuthMethod: "noauth",
  96. UDPEnabled: true,
  97. HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
  98. },
  99. },
  100. OutboundConfigValue: &mocks.ConnectionConfig{
  101. ProtocolValue: "vmess",
  102. SettingsValue: &vmessjson.Outbound{
  103. []*vmessjson.ConfigTarget{
  104. &vmessjson.ConfigTarget{
  105. Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
  106. Users: users,
  107. },
  108. },
  109. },
  110. },
  111. InboundDetoursValue: []*mocks.InboundDetourConfig{
  112. &mocks.InboundDetourConfig{
  113. PortRangeValue: &mocks.PortRange{
  114. FromValue: portA2,
  115. ToValue: portA2,
  116. },
  117. ConnectionConfig: &mocks.ConnectionConfig{
  118. ProtocolValue: "socks",
  119. SettingsValue: &socksjson.SocksConfig{
  120. AuthMethod: "noauth",
  121. UDPEnabled: false,
  122. HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
  123. },
  124. },
  125. },
  126. },
  127. OutboundDetoursValue: []*mocks.OutboundDetourConfig{
  128. &mocks.OutboundDetourConfig{
  129. TagValue: "direct",
  130. ConnectionConfig: &mocks.ConnectionConfig{
  131. ProtocolValue: "freedom",
  132. SettingsValue: nil,
  133. },
  134. },
  135. },
  136. RouterConfigValue: &routerconfig.RouterConfig{
  137. StrategyValue: "rules",
  138. SettingsValue: &rulesconfig.RouterRuleConfig{
  139. RuleList: []*rulesconfig.TestRule{
  140. &rulesconfig.TestRule{
  141. TagValue: "direct",
  142. Function: routing,
  143. },
  144. },
  145. },
  146. },
  147. }
  148. pointA, err := point.NewPoint(&configA)
  149. if err != nil {
  150. return 0, 0, err
  151. }
  152. err = pointA.Start()
  153. if err != nil {
  154. return 0, 0, err
  155. }
  156. return portA, portA2, nil
  157. }