policy_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. Networks: []net.Network{net.Network_TCP},
  109. }),
  110. },
  111. },
  112. Outbound: []*core.OutboundHandlerConfig{
  113. {
  114. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  115. Receiver: &protocol.ServerEndpoint{
  116. Address: net.NewIPOrDomain(net.LocalHostIP),
  117. Port: uint32(serverPort),
  118. User: &protocol.User{
  119. Account: serial.ToTypedMessage(&vmess.Account{
  120. Id: userID.String(),
  121. SecuritySettings: &protocol.SecurityConfig{
  122. Type: protocol.SecurityType_AES128_GCM,
  123. },
  124. }),
  125. },
  126. },
  127. }),
  128. },
  129. },
  130. }
  131. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  132. common.Must(err)
  133. defer CloseAllServers(servers)
  134. if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != io.EOF {
  135. t.Error(err)
  136. }
  137. }
  138. func TestZeroBuffer(t *testing.T) {
  139. tcpServer := tcp.Server{
  140. MsgProcessor: xor,
  141. }
  142. dest, err := tcpServer.Start()
  143. common.Must(err)
  144. defer tcpServer.Close()
  145. userID := protocol.NewID(uuid.New())
  146. serverPort := tcp.PickPort()
  147. serverConfig := &core.Config{
  148. App: []*serial.TypedMessage{
  149. serial.ToTypedMessage(&policy.Config{
  150. Level: map[uint32]*policy.Policy{
  151. 0: {
  152. Timeout: &policy.Policy_Timeout{
  153. UplinkOnly: &policy.Second{Value: 0},
  154. DownlinkOnly: &policy.Second{Value: 0},
  155. },
  156. Buffer: &policy.Policy_Buffer{
  157. Connection: 0,
  158. },
  159. },
  160. },
  161. }),
  162. },
  163. Inbound: []*core.InboundHandlerConfig{
  164. {
  165. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  166. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
  167. Listen: net.NewIPOrDomain(net.LocalHostIP),
  168. }),
  169. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  170. User: []*protocol.User{
  171. {
  172. Account: serial.ToTypedMessage(&vmess.Account{
  173. Id: userID.String(),
  174. }),
  175. },
  176. },
  177. }),
  178. },
  179. },
  180. Outbound: []*core.OutboundHandlerConfig{
  181. {
  182. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  183. },
  184. },
  185. }
  186. clientPort := tcp.PickPort()
  187. clientConfig := &core.Config{
  188. App: []*serial.TypedMessage{
  189. serial.ToTypedMessage(&log.Config{
  190. ErrorLogLevel: clog.Severity_Debug,
  191. ErrorLogType: log.LogType_Console,
  192. }),
  193. },
  194. Inbound: []*core.InboundHandlerConfig{
  195. {
  196. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  197. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  198. Listen: net.NewIPOrDomain(net.LocalHostIP),
  199. }),
  200. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  201. Address: net.NewIPOrDomain(dest.Address),
  202. Port: uint32(dest.Port),
  203. Networks: []net.Network{net.Network_TCP},
  204. }),
  205. },
  206. },
  207. Outbound: []*core.OutboundHandlerConfig{
  208. {
  209. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  210. Receiver: &protocol.ServerEndpoint{
  211. Address: net.NewIPOrDomain(net.LocalHostIP),
  212. Port: uint32(serverPort),
  213. User: &protocol.User{
  214. Account: serial.ToTypedMessage(&vmess.Account{
  215. Id: userID.String(),
  216. SecuritySettings: &protocol.SecurityConfig{
  217. Type: protocol.SecurityType_AES128_GCM,
  218. },
  219. }),
  220. },
  221. },
  222. }),
  223. },
  224. },
  225. }
  226. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  227. common.Must(err)
  228. defer CloseAllServers(servers)
  229. var errg errgroup.Group
  230. for range 3 {
  231. errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*20))
  232. }
  233. if err := errg.Wait(); err != nil {
  234. t.Error(err)
  235. }
  236. }