policy_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package scenarios
  2. import (
  3. "google.golang.org/protobuf/types/known/anypb"
  4. "io"
  5. "testing"
  6. "time"
  7. "golang.org/x/sync/errgroup"
  8. core "github.com/v2fly/v2ray-core/v4"
  9. "github.com/v2fly/v2ray-core/v4/app/log"
  10. "github.com/v2fly/v2ray-core/v4/app/policy"
  11. "github.com/v2fly/v2ray-core/v4/app/proxyman"
  12. "github.com/v2fly/v2ray-core/v4/common"
  13. clog "github.com/v2fly/v2ray-core/v4/common/log"
  14. "github.com/v2fly/v2ray-core/v4/common/net"
  15. "github.com/v2fly/v2ray-core/v4/common/protocol"
  16. "github.com/v2fly/v2ray-core/v4/common/serial"
  17. "github.com/v2fly/v2ray-core/v4/common/uuid"
  18. "github.com/v2fly/v2ray-core/v4/proxy/dokodemo"
  19. "github.com/v2fly/v2ray-core/v4/proxy/freedom"
  20. "github.com/v2fly/v2ray-core/v4/proxy/vmess"
  21. "github.com/v2fly/v2ray-core/v4/proxy/vmess/inbound"
  22. "github.com/v2fly/v2ray-core/v4/proxy/vmess/outbound"
  23. "github.com/v2fly/v2ray-core/v4/testing/servers/tcp"
  24. )
  25. func startQuickClosingTCPServer() (net.Listener, error) {
  26. listener, err := net.Listen("tcp", "127.0.0.1:0")
  27. if err != nil {
  28. return nil, err
  29. }
  30. go func() {
  31. for {
  32. conn, err := listener.Accept()
  33. if err != nil {
  34. break
  35. }
  36. b := make([]byte, 1024)
  37. conn.Read(b)
  38. conn.Close()
  39. }
  40. }()
  41. return listener, nil
  42. }
  43. func TestVMessClosing(t *testing.T) {
  44. tcpServer, err := startQuickClosingTCPServer()
  45. common.Must(err)
  46. defer tcpServer.Close()
  47. dest := net.DestinationFromAddr(tcpServer.Addr())
  48. userID := protocol.NewID(uuid.New())
  49. serverPort := tcp.PickPort()
  50. serverConfig := &core.Config{
  51. App: []*anypb.Any{
  52. serial.ToTypedMessage(&policy.Config{
  53. Level: map[uint32]*policy.Policy{
  54. 0: {
  55. Timeout: &policy.Policy_Timeout{
  56. UplinkOnly: &policy.Second{Value: 0},
  57. DownlinkOnly: &policy.Second{Value: 0},
  58. },
  59. },
  60. },
  61. }),
  62. },
  63. Inbound: []*core.InboundHandlerConfig{
  64. {
  65. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  66. PortRange: net.SinglePortRange(serverPort),
  67. Listen: net.NewIPOrDomain(net.LocalHostIP),
  68. }),
  69. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  70. User: []*protocol.User{
  71. {
  72. Account: serial.ToTypedMessage(&vmess.Account{
  73. Id: userID.String(),
  74. AlterId: 64,
  75. }),
  76. },
  77. },
  78. }),
  79. },
  80. },
  81. Outbound: []*core.OutboundHandlerConfig{
  82. {
  83. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  84. },
  85. },
  86. }
  87. clientPort := tcp.PickPort()
  88. clientConfig := &core.Config{
  89. App: []*anypb.Any{
  90. serial.ToTypedMessage(&policy.Config{
  91. Level: map[uint32]*policy.Policy{
  92. 0: {
  93. Timeout: &policy.Policy_Timeout{
  94. UplinkOnly: &policy.Second{Value: 0},
  95. DownlinkOnly: &policy.Second{Value: 0},
  96. },
  97. },
  98. },
  99. }),
  100. },
  101. Inbound: []*core.InboundHandlerConfig{
  102. {
  103. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  104. PortRange: net.SinglePortRange(clientPort),
  105. Listen: net.NewIPOrDomain(net.LocalHostIP),
  106. }),
  107. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  108. Address: net.NewIPOrDomain(dest.Address),
  109. Port: uint32(dest.Port),
  110. NetworkList: &net.NetworkList{
  111. Network: []net.Network{net.Network_TCP},
  112. },
  113. }),
  114. },
  115. },
  116. Outbound: []*core.OutboundHandlerConfig{
  117. {
  118. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  119. Receiver: []*protocol.ServerEndpoint{
  120. {
  121. Address: net.NewIPOrDomain(net.LocalHostIP),
  122. Port: uint32(serverPort),
  123. User: []*protocol.User{
  124. {
  125. Account: serial.ToTypedMessage(&vmess.Account{
  126. Id: userID.String(),
  127. AlterId: 64,
  128. SecuritySettings: &protocol.SecurityConfig{
  129. Type: protocol.SecurityType_AES128_GCM,
  130. },
  131. }),
  132. },
  133. },
  134. },
  135. },
  136. }),
  137. },
  138. },
  139. }
  140. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  141. common.Must(err)
  142. defer CloseAllServers(servers)
  143. if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != io.EOF {
  144. t.Error(err)
  145. }
  146. }
  147. func TestZeroBuffer(t *testing.T) {
  148. tcpServer := tcp.Server{
  149. MsgProcessor: xor,
  150. }
  151. dest, err := tcpServer.Start()
  152. common.Must(err)
  153. defer tcpServer.Close()
  154. userID := protocol.NewID(uuid.New())
  155. serverPort := tcp.PickPort()
  156. serverConfig := &core.Config{
  157. App: []*anypb.Any{
  158. serial.ToTypedMessage(&policy.Config{
  159. Level: map[uint32]*policy.Policy{
  160. 0: {
  161. Timeout: &policy.Policy_Timeout{
  162. UplinkOnly: &policy.Second{Value: 0},
  163. DownlinkOnly: &policy.Second{Value: 0},
  164. },
  165. Buffer: &policy.Policy_Buffer{
  166. Connection: 0,
  167. },
  168. },
  169. },
  170. }),
  171. },
  172. Inbound: []*core.InboundHandlerConfig{
  173. {
  174. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  175. PortRange: net.SinglePortRange(serverPort),
  176. Listen: net.NewIPOrDomain(net.LocalHostIP),
  177. }),
  178. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  179. User: []*protocol.User{
  180. {
  181. Account: serial.ToTypedMessage(&vmess.Account{
  182. Id: userID.String(),
  183. AlterId: 64,
  184. }),
  185. },
  186. },
  187. }),
  188. },
  189. },
  190. Outbound: []*core.OutboundHandlerConfig{
  191. {
  192. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  193. },
  194. },
  195. }
  196. clientPort := tcp.PickPort()
  197. clientConfig := &core.Config{
  198. App: []*anypb.Any{
  199. serial.ToTypedMessage(&log.Config{
  200. ErrorLogLevel: clog.Severity_Debug,
  201. ErrorLogType: log.LogType_Console,
  202. }),
  203. },
  204. Inbound: []*core.InboundHandlerConfig{
  205. {
  206. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  207. PortRange: net.SinglePortRange(clientPort),
  208. Listen: net.NewIPOrDomain(net.LocalHostIP),
  209. }),
  210. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  211. Address: net.NewIPOrDomain(dest.Address),
  212. Port: uint32(dest.Port),
  213. NetworkList: &net.NetworkList{
  214. Network: []net.Network{net.Network_TCP},
  215. },
  216. }),
  217. },
  218. },
  219. Outbound: []*core.OutboundHandlerConfig{
  220. {
  221. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  222. Receiver: []*protocol.ServerEndpoint{
  223. {
  224. Address: net.NewIPOrDomain(net.LocalHostIP),
  225. Port: uint32(serverPort),
  226. User: []*protocol.User{
  227. {
  228. Account: serial.ToTypedMessage(&vmess.Account{
  229. Id: userID.String(),
  230. AlterId: 64,
  231. SecuritySettings: &protocol.SecurityConfig{
  232. Type: protocol.SecurityType_AES128_GCM,
  233. },
  234. }),
  235. },
  236. },
  237. },
  238. },
  239. }),
  240. },
  241. },
  242. }
  243. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  244. common.Must(err)
  245. defer CloseAllServers(servers)
  246. var errg errgroup.Group
  247. for i := 0; i < 10; i++ {
  248. errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
  249. }
  250. if err := errg.Wait(); err != nil {
  251. t.Error(err)
  252. }
  253. }