dokodemo_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package scenarios
  2. import (
  3. "google.golang.org/protobuf/types/known/anypb"
  4. "testing"
  5. "time"
  6. "golang.org/x/sync/errgroup"
  7. core "github.com/v2fly/v2ray-core/v4"
  8. "github.com/v2fly/v2ray-core/v4/app/log"
  9. "github.com/v2fly/v2ray-core/v4/app/proxyman"
  10. "github.com/v2fly/v2ray-core/v4/common"
  11. clog "github.com/v2fly/v2ray-core/v4/common/log"
  12. "github.com/v2fly/v2ray-core/v4/common/net"
  13. "github.com/v2fly/v2ray-core/v4/common/protocol"
  14. "github.com/v2fly/v2ray-core/v4/common/serial"
  15. "github.com/v2fly/v2ray-core/v4/common/uuid"
  16. "github.com/v2fly/v2ray-core/v4/proxy/dokodemo"
  17. "github.com/v2fly/v2ray-core/v4/proxy/freedom"
  18. "github.com/v2fly/v2ray-core/v4/proxy/vmess"
  19. "github.com/v2fly/v2ray-core/v4/proxy/vmess/inbound"
  20. "github.com/v2fly/v2ray-core/v4/proxy/vmess/outbound"
  21. "github.com/v2fly/v2ray-core/v4/testing/servers/tcp"
  22. "github.com/v2fly/v2ray-core/v4/testing/servers/udp"
  23. )
  24. func TestDokodemoTCP(t *testing.T) {
  25. tcpServer := tcp.Server{
  26. MsgProcessor: xor,
  27. }
  28. dest, err := tcpServer.Start()
  29. common.Must(err)
  30. defer tcpServer.Close()
  31. userID := protocol.NewID(uuid.New())
  32. serverPort := tcp.PickPort()
  33. serverConfig := &core.Config{
  34. App: []*anypb.Any{
  35. serial.ToTypedMessage(&log.Config{
  36. Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
  37. }),
  38. },
  39. Inbound: []*core.InboundHandlerConfig{
  40. {
  41. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  42. PortRange: net.SinglePortRange(serverPort),
  43. Listen: net.NewIPOrDomain(net.LocalHostIP),
  44. }),
  45. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  46. User: []*protocol.User{
  47. {
  48. Account: serial.ToTypedMessage(&vmess.Account{
  49. Id: userID.String(),
  50. }),
  51. },
  52. },
  53. }),
  54. },
  55. },
  56. Outbound: []*core.OutboundHandlerConfig{
  57. {
  58. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  59. },
  60. },
  61. }
  62. clientPort := uint32(tcp.PickPort())
  63. clientPortRange := uint32(5)
  64. clientConfig := &core.Config{
  65. App: []*anypb.Any{
  66. serial.ToTypedMessage(&log.Config{
  67. Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
  68. }),
  69. },
  70. Inbound: []*core.InboundHandlerConfig{
  71. {
  72. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  73. PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange},
  74. Listen: net.NewIPOrDomain(net.LocalHostIP),
  75. }),
  76. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  77. Address: net.NewIPOrDomain(dest.Address),
  78. Port: uint32(dest.Port),
  79. NetworkList: &net.NetworkList{
  80. Network: []net.Network{net.Network_TCP},
  81. },
  82. }),
  83. },
  84. },
  85. Outbound: []*core.OutboundHandlerConfig{
  86. {
  87. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  88. Receiver: []*protocol.ServerEndpoint{
  89. {
  90. Address: net.NewIPOrDomain(net.LocalHostIP),
  91. Port: uint32(serverPort),
  92. User: []*protocol.User{
  93. {
  94. Account: serial.ToTypedMessage(&vmess.Account{
  95. Id: userID.String(),
  96. }),
  97. },
  98. },
  99. },
  100. },
  101. }),
  102. },
  103. },
  104. }
  105. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  106. common.Must(err)
  107. defer CloseAllServers(servers)
  108. for port := clientPort; port <= clientPort+clientPortRange; port++ {
  109. if err := testTCPConn(net.Port(port), 1024, time.Second*2)(); err != nil {
  110. t.Error(err)
  111. }
  112. }
  113. }
  114. func TestDokodemoUDP(t *testing.T) {
  115. udpServer := udp.Server{
  116. MsgProcessor: xor,
  117. }
  118. dest, err := udpServer.Start()
  119. common.Must(err)
  120. defer udpServer.Close()
  121. userID := protocol.NewID(uuid.New())
  122. serverPort := tcp.PickPort()
  123. serverConfig := &core.Config{
  124. Inbound: []*core.InboundHandlerConfig{
  125. {
  126. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  127. PortRange: net.SinglePortRange(serverPort),
  128. Listen: net.NewIPOrDomain(net.LocalHostIP),
  129. }),
  130. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  131. User: []*protocol.User{
  132. {
  133. Account: serial.ToTypedMessage(&vmess.Account{
  134. Id: userID.String(),
  135. }),
  136. },
  137. },
  138. }),
  139. },
  140. },
  141. Outbound: []*core.OutboundHandlerConfig{
  142. {
  143. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  144. },
  145. },
  146. }
  147. clientPort := uint32(udp.PickPort())
  148. clientPortRange := uint32(5)
  149. clientConfig := &core.Config{
  150. Inbound: []*core.InboundHandlerConfig{
  151. {
  152. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  153. PortRange: &net.PortRange{From: clientPort, To: clientPort + clientPortRange},
  154. Listen: net.NewIPOrDomain(net.LocalHostIP),
  155. }),
  156. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  157. Address: net.NewIPOrDomain(dest.Address),
  158. Port: uint32(dest.Port),
  159. NetworkList: &net.NetworkList{
  160. Network: []net.Network{net.Network_UDP},
  161. },
  162. }),
  163. },
  164. },
  165. Outbound: []*core.OutboundHandlerConfig{
  166. {
  167. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  168. Receiver: []*protocol.ServerEndpoint{
  169. {
  170. Address: net.NewIPOrDomain(net.LocalHostIP),
  171. Port: uint32(serverPort),
  172. User: []*protocol.User{
  173. {
  174. Account: serial.ToTypedMessage(&vmess.Account{
  175. Id: userID.String(),
  176. }),
  177. },
  178. },
  179. },
  180. },
  181. }),
  182. },
  183. },
  184. }
  185. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  186. common.Must(err)
  187. defer CloseAllServers(servers)
  188. var errg errgroup.Group
  189. for port := clientPort; port <= clientPort+clientPortRange; port++ {
  190. errg.Go(testUDPConn(net.Port(port), 1024, time.Second*5))
  191. }
  192. if err := errg.Wait(); err != nil {
  193. t.Error(err)
  194. }
  195. }