policy_test.go 6.6 KB

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