command_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. package scenarios
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/google/go-cmp/cmp"
  10. "github.com/google/go-cmp/cmp/cmpopts"
  11. "github.com/xtls/xray-core/app/commander"
  12. "github.com/xtls/xray-core/app/policy"
  13. "github.com/xtls/xray-core/app/proxyman"
  14. "github.com/xtls/xray-core/app/proxyman/command"
  15. "github.com/xtls/xray-core/app/router"
  16. "github.com/xtls/xray-core/app/stats"
  17. statscmd "github.com/xtls/xray-core/app/stats/command"
  18. "github.com/xtls/xray-core/common"
  19. "github.com/xtls/xray-core/common/net"
  20. "github.com/xtls/xray-core/common/protocol"
  21. "github.com/xtls/xray-core/common/serial"
  22. "github.com/xtls/xray-core/common/uuid"
  23. core "github.com/xtls/xray-core/core"
  24. "github.com/xtls/xray-core/proxy/dokodemo"
  25. "github.com/xtls/xray-core/proxy/freedom"
  26. "github.com/xtls/xray-core/proxy/vmess"
  27. "github.com/xtls/xray-core/proxy/vmess/inbound"
  28. "github.com/xtls/xray-core/proxy/vmess/outbound"
  29. "github.com/xtls/xray-core/testing/servers/tcp"
  30. "google.golang.org/grpc"
  31. "google.golang.org/grpc/credentials/insecure"
  32. )
  33. func TestCommanderRemoveHandler(t *testing.T) {
  34. tcpServer := tcp.Server{
  35. MsgProcessor: xor,
  36. }
  37. dest, err := tcpServer.Start()
  38. common.Must(err)
  39. defer tcpServer.Close()
  40. clientPort := tcp.PickPort()
  41. cmdPort := tcp.PickPort()
  42. clientConfig := &core.Config{
  43. App: []*serial.TypedMessage{
  44. serial.ToTypedMessage(&commander.Config{
  45. Tag: "api",
  46. Service: []*serial.TypedMessage{
  47. serial.ToTypedMessage(&command.Config{}),
  48. },
  49. }),
  50. serial.ToTypedMessage(&router.Config{
  51. Rule: []*router.RoutingRule{
  52. {
  53. InboundTag: []string{"api"},
  54. TargetTag: &router.RoutingRule_Tag{
  55. Tag: "api",
  56. },
  57. },
  58. },
  59. }),
  60. },
  61. Inbound: []*core.InboundHandlerConfig{
  62. {
  63. Tag: "d",
  64. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  65. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  66. Listen: net.NewIPOrDomain(net.LocalHostIP),
  67. }),
  68. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  69. Address: net.NewIPOrDomain(dest.Address),
  70. Port: uint32(dest.Port),
  71. Networks: []net.Network{net.Network_TCP},
  72. }),
  73. },
  74. {
  75. Tag: "api",
  76. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  77. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}},
  78. Listen: net.NewIPOrDomain(net.LocalHostIP),
  79. }),
  80. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  81. Address: net.NewIPOrDomain(dest.Address),
  82. Port: uint32(dest.Port),
  83. Networks: []net.Network{net.Network_TCP},
  84. }),
  85. },
  86. },
  87. Outbound: []*core.OutboundHandlerConfig{
  88. {
  89. Tag: "default-outbound",
  90. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  91. },
  92. },
  93. }
  94. servers, err := InitializeServerConfigs(clientConfig)
  95. common.Must(err)
  96. defer CloseAllServers(servers)
  97. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
  98. t.Fatal(err)
  99. }
  100. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  101. common.Must(err)
  102. defer cmdConn.Close()
  103. hsClient := command.NewHandlerServiceClient(cmdConn)
  104. resp, err := hsClient.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
  105. Tag: "d",
  106. })
  107. common.Must(err)
  108. if resp == nil {
  109. t.Error("unexpected nil response")
  110. }
  111. {
  112. _, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  113. IP: []byte{127, 0, 0, 1},
  114. Port: int(clientPort),
  115. })
  116. if err == nil {
  117. t.Error("unexpected nil error")
  118. }
  119. }
  120. }
  121. func TestCommanderAddRemoveUser(t *testing.T) {
  122. tcpServer := tcp.Server{
  123. MsgProcessor: xor,
  124. }
  125. dest, err := tcpServer.Start()
  126. common.Must(err)
  127. defer tcpServer.Close()
  128. u1 := protocol.NewID(uuid.New())
  129. u2 := protocol.NewID(uuid.New())
  130. cmdPort := tcp.PickPort()
  131. serverPort := tcp.PickPort()
  132. serverConfig := &core.Config{
  133. App: []*serial.TypedMessage{
  134. serial.ToTypedMessage(&commander.Config{
  135. Tag: "api",
  136. Service: []*serial.TypedMessage{
  137. serial.ToTypedMessage(&command.Config{}),
  138. },
  139. }),
  140. serial.ToTypedMessage(&router.Config{
  141. Rule: []*router.RoutingRule{
  142. {
  143. InboundTag: []string{"api"},
  144. TargetTag: &router.RoutingRule_Tag{
  145. Tag: "api",
  146. },
  147. },
  148. },
  149. }),
  150. serial.ToTypedMessage(&policy.Config{
  151. Level: map[uint32]*policy.Policy{
  152. 0: {
  153. Timeout: &policy.Policy_Timeout{
  154. UplinkOnly: &policy.Second{Value: 0},
  155. DownlinkOnly: &policy.Second{Value: 0},
  156. },
  157. },
  158. },
  159. }),
  160. },
  161. Inbound: []*core.InboundHandlerConfig{
  162. {
  163. Tag: "v",
  164. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  165. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
  166. Listen: net.NewIPOrDomain(net.LocalHostIP),
  167. }),
  168. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  169. User: []*protocol.User{
  170. {
  171. Account: serial.ToTypedMessage(&vmess.Account{
  172. Id: u1.String(),
  173. }),
  174. },
  175. },
  176. }),
  177. },
  178. {
  179. Tag: "api",
  180. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  181. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}},
  182. Listen: net.NewIPOrDomain(net.LocalHostIP),
  183. }),
  184. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  185. Address: net.NewIPOrDomain(dest.Address),
  186. Port: uint32(dest.Port),
  187. Networks: []net.Network{net.Network_TCP},
  188. }),
  189. },
  190. },
  191. Outbound: []*core.OutboundHandlerConfig{
  192. {
  193. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  194. },
  195. },
  196. }
  197. clientPort := tcp.PickPort()
  198. clientConfig := &core.Config{
  199. App: []*serial.TypedMessage{
  200. serial.ToTypedMessage(&policy.Config{
  201. Level: map[uint32]*policy.Policy{
  202. 0: {
  203. Timeout: &policy.Policy_Timeout{
  204. UplinkOnly: &policy.Second{Value: 0},
  205. DownlinkOnly: &policy.Second{Value: 0},
  206. },
  207. },
  208. },
  209. }),
  210. },
  211. Inbound: []*core.InboundHandlerConfig{
  212. {
  213. Tag: "d",
  214. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  215. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  216. Listen: net.NewIPOrDomain(net.LocalHostIP),
  217. }),
  218. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  219. Address: net.NewIPOrDomain(dest.Address),
  220. Port: uint32(dest.Port),
  221. NetworkList: &net.NetworkList{
  222. Network: []net.Network{net.Network_TCP},
  223. },
  224. }),
  225. },
  226. },
  227. Outbound: []*core.OutboundHandlerConfig{
  228. {
  229. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  230. Receiver: []*protocol.ServerEndpoint{
  231. {
  232. Address: net.NewIPOrDomain(net.LocalHostIP),
  233. Port: uint32(serverPort),
  234. User: []*protocol.User{
  235. {
  236. Account: serial.ToTypedMessage(&vmess.Account{
  237. Id: u2.String(),
  238. SecuritySettings: &protocol.SecurityConfig{
  239. Type: protocol.SecurityType_AES128_GCM,
  240. },
  241. }),
  242. },
  243. },
  244. },
  245. },
  246. }),
  247. },
  248. },
  249. }
  250. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  251. common.Must(err)
  252. defer CloseAllServers(servers)
  253. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != io.EOF &&
  254. /*We might wish to drain the connection*/
  255. (err != nil && !strings.HasSuffix(err.Error(), "i/o timeout")) {
  256. t.Fatal("expected error: ", err)
  257. }
  258. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  259. common.Must(err)
  260. defer cmdConn.Close()
  261. hsClient := command.NewHandlerServiceClient(cmdConn)
  262. resp, err := hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
  263. Tag: "v",
  264. Operation: serial.ToTypedMessage(
  265. &command.AddUserOperation{
  266. User: &protocol.User{
  267. Email: "[email protected]",
  268. Account: serial.ToTypedMessage(&vmess.Account{
  269. Id: u2.String(),
  270. }),
  271. },
  272. }),
  273. })
  274. common.Must(err)
  275. if resp == nil {
  276. t.Fatal("nil response")
  277. }
  278. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
  279. t.Fatal(err)
  280. }
  281. resp, err = hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
  282. Tag: "v",
  283. Operation: serial.ToTypedMessage(&command.RemoveUserOperation{Email: "[email protected]"}),
  284. })
  285. common.Must(err)
  286. if resp == nil {
  287. t.Fatal("nil response")
  288. }
  289. }
  290. func TestCommanderStats(t *testing.T) {
  291. tcpServer := tcp.Server{
  292. MsgProcessor: xor,
  293. }
  294. dest, err := tcpServer.Start()
  295. common.Must(err)
  296. defer tcpServer.Close()
  297. userID := protocol.NewID(uuid.New())
  298. serverPort := tcp.PickPort()
  299. cmdPort := tcp.PickPort()
  300. serverConfig := &core.Config{
  301. App: []*serial.TypedMessage{
  302. serial.ToTypedMessage(&stats.Config{}),
  303. serial.ToTypedMessage(&commander.Config{
  304. Tag: "api",
  305. Service: []*serial.TypedMessage{
  306. serial.ToTypedMessage(&statscmd.Config{}),
  307. },
  308. }),
  309. serial.ToTypedMessage(&router.Config{
  310. Rule: []*router.RoutingRule{
  311. {
  312. InboundTag: []string{"api"},
  313. TargetTag: &router.RoutingRule_Tag{
  314. Tag: "api",
  315. },
  316. },
  317. },
  318. }),
  319. serial.ToTypedMessage(&policy.Config{
  320. Level: map[uint32]*policy.Policy{
  321. 0: {
  322. Timeout: &policy.Policy_Timeout{
  323. UplinkOnly: &policy.Second{Value: 0},
  324. DownlinkOnly: &policy.Second{Value: 0},
  325. },
  326. },
  327. 1: {
  328. Stats: &policy.Policy_Stats{
  329. UserUplink: true,
  330. UserDownlink: true,
  331. },
  332. },
  333. },
  334. System: &policy.SystemPolicy{
  335. Stats: &policy.SystemPolicy_Stats{
  336. InboundUplink: true,
  337. },
  338. },
  339. }),
  340. },
  341. Inbound: []*core.InboundHandlerConfig{
  342. {
  343. Tag: "vmess",
  344. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  345. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
  346. Listen: net.NewIPOrDomain(net.LocalHostIP),
  347. }),
  348. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  349. User: []*protocol.User{
  350. {
  351. Level: 1,
  352. Email: "test",
  353. Account: serial.ToTypedMessage(&vmess.Account{
  354. Id: userID.String(),
  355. }),
  356. },
  357. },
  358. }),
  359. },
  360. {
  361. Tag: "api",
  362. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  363. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}},
  364. Listen: net.NewIPOrDomain(net.LocalHostIP),
  365. }),
  366. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  367. Address: net.NewIPOrDomain(dest.Address),
  368. Port: uint32(dest.Port),
  369. NetworkList: &net.NetworkList{
  370. Network: []net.Network{net.Network_TCP},
  371. },
  372. }),
  373. },
  374. },
  375. Outbound: []*core.OutboundHandlerConfig{
  376. {
  377. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  378. },
  379. },
  380. }
  381. clientPort := tcp.PickPort()
  382. clientConfig := &core.Config{
  383. Inbound: []*core.InboundHandlerConfig{
  384. {
  385. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  386. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  387. Listen: net.NewIPOrDomain(net.LocalHostIP),
  388. }),
  389. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  390. Address: net.NewIPOrDomain(dest.Address),
  391. Port: uint32(dest.Port),
  392. NetworkList: &net.NetworkList{
  393. Network: []net.Network{net.Network_TCP},
  394. },
  395. }),
  396. },
  397. },
  398. Outbound: []*core.OutboundHandlerConfig{
  399. {
  400. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  401. Receiver: []*protocol.ServerEndpoint{
  402. {
  403. Address: net.NewIPOrDomain(net.LocalHostIP),
  404. Port: uint32(serverPort),
  405. User: []*protocol.User{
  406. {
  407. Account: serial.ToTypedMessage(&vmess.Account{
  408. Id: userID.String(),
  409. SecuritySettings: &protocol.SecurityConfig{
  410. Type: protocol.SecurityType_AES128_GCM,
  411. },
  412. }),
  413. },
  414. },
  415. },
  416. },
  417. }),
  418. },
  419. },
  420. }
  421. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  422. if err != nil {
  423. t.Fatal("Failed to create all servers", err)
  424. }
  425. defer CloseAllServers(servers)
  426. if err := testTCPConn(clientPort, 10240*1024, time.Second*20)(); err != nil {
  427. t.Fatal(err)
  428. }
  429. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  430. common.Must(err)
  431. defer cmdConn.Close()
  432. const name = "user>>>test>>>traffic>>>uplink"
  433. sClient := statscmd.NewStatsServiceClient(cmdConn)
  434. sresp, err := sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  435. Name: name,
  436. Reset_: true,
  437. })
  438. common.Must(err)
  439. if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
  440. Name: name,
  441. Value: 10240 * 1024,
  442. }, cmpopts.IgnoreUnexported(statscmd.Stat{})); r != "" {
  443. t.Error(r)
  444. }
  445. sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  446. Name: name,
  447. })
  448. common.Must(err)
  449. if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
  450. Name: name,
  451. Value: 0,
  452. }, cmpopts.IgnoreUnexported(statscmd.Stat{})); r != "" {
  453. t.Error(r)
  454. }
  455. sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  456. Name: "inbound>>>vmess>>>traffic>>>uplink",
  457. Reset_: true,
  458. })
  459. common.Must(err)
  460. if sresp.Stat.Value <= 10240*1024 {
  461. t.Error("value < 10240*1024: ", sresp.Stat.Value)
  462. }
  463. }