command_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. "google.golang.org/protobuf/testing/protocmp"
  33. )
  34. func TestCommanderListenConfigurationItem(t *testing.T) {
  35. tcpServer := tcp.Server{
  36. MsgProcessor: xor,
  37. }
  38. dest, err := tcpServer.Start()
  39. common.Must(err)
  40. defer tcpServer.Close()
  41. clientPort := tcp.PickPort()
  42. cmdPort := tcp.PickPort()
  43. clientConfig := &core.Config{
  44. App: []*serial.TypedMessage{
  45. serial.ToTypedMessage(&commander.Config{
  46. Tag: "api",
  47. Listen: fmt.Sprintf("127.0.0.1:%d", cmdPort),
  48. Service: []*serial.TypedMessage{
  49. serial.ToTypedMessage(&command.Config{}),
  50. },
  51. }),
  52. },
  53. Inbound: []*core.InboundHandlerConfig{
  54. {
  55. Tag: "d",
  56. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  57. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  58. Listen: net.NewIPOrDomain(net.LocalHostIP),
  59. }),
  60. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  61. Address: net.NewIPOrDomain(dest.Address),
  62. Port: uint32(dest.Port),
  63. Networks: []net.Network{net.Network_TCP},
  64. }),
  65. },
  66. },
  67. Outbound: []*core.OutboundHandlerConfig{
  68. {
  69. Tag: "default-outbound",
  70. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  71. },
  72. },
  73. }
  74. servers, err := InitializeServerConfigs(clientConfig)
  75. common.Must(err)
  76. defer CloseAllServers(servers)
  77. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
  78. t.Fatal(err)
  79. }
  80. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  81. common.Must(err)
  82. defer cmdConn.Close()
  83. hsClient := command.NewHandlerServiceClient(cmdConn)
  84. resp, err := hsClient.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
  85. Tag: "d",
  86. })
  87. common.Must(err)
  88. if resp == nil {
  89. t.Error("unexpected nil response")
  90. }
  91. {
  92. _, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  93. IP: []byte{127, 0, 0, 1},
  94. Port: int(clientPort),
  95. })
  96. if err == nil {
  97. t.Error("unexpected nil error")
  98. }
  99. }
  100. }
  101. func TestCommanderRemoveHandler(t *testing.T) {
  102. tcpServer := tcp.Server{
  103. MsgProcessor: xor,
  104. }
  105. dest, err := tcpServer.Start()
  106. common.Must(err)
  107. defer tcpServer.Close()
  108. clientPort := tcp.PickPort()
  109. cmdPort := tcp.PickPort()
  110. clientConfig := &core.Config{
  111. App: []*serial.TypedMessage{
  112. serial.ToTypedMessage(&commander.Config{
  113. Tag: "api",
  114. Service: []*serial.TypedMessage{
  115. serial.ToTypedMessage(&command.Config{}),
  116. },
  117. }),
  118. serial.ToTypedMessage(&router.Config{
  119. Rule: []*router.RoutingRule{
  120. {
  121. InboundTag: []string{"api"},
  122. TargetTag: &router.RoutingRule_Tag{
  123. Tag: "api",
  124. },
  125. },
  126. },
  127. }),
  128. },
  129. Inbound: []*core.InboundHandlerConfig{
  130. {
  131. Tag: "d",
  132. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  133. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  134. Listen: net.NewIPOrDomain(net.LocalHostIP),
  135. }),
  136. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  137. Address: net.NewIPOrDomain(dest.Address),
  138. Port: uint32(dest.Port),
  139. Networks: []net.Network{net.Network_TCP},
  140. }),
  141. },
  142. {
  143. Tag: "api",
  144. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  145. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}},
  146. Listen: net.NewIPOrDomain(net.LocalHostIP),
  147. }),
  148. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  149. Address: net.NewIPOrDomain(dest.Address),
  150. Port: uint32(dest.Port),
  151. Networks: []net.Network{net.Network_TCP},
  152. }),
  153. },
  154. },
  155. Outbound: []*core.OutboundHandlerConfig{
  156. {
  157. Tag: "default-outbound",
  158. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  159. },
  160. },
  161. }
  162. servers, err := InitializeServerConfigs(clientConfig)
  163. common.Must(err)
  164. defer CloseAllServers(servers)
  165. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
  166. t.Fatal(err)
  167. }
  168. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  169. common.Must(err)
  170. defer cmdConn.Close()
  171. hsClient := command.NewHandlerServiceClient(cmdConn)
  172. resp, err := hsClient.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
  173. Tag: "d",
  174. })
  175. common.Must(err)
  176. if resp == nil {
  177. t.Error("unexpected nil response")
  178. }
  179. {
  180. _, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  181. IP: []byte{127, 0, 0, 1},
  182. Port: int(clientPort),
  183. })
  184. if err == nil {
  185. t.Error("unexpected nil error")
  186. }
  187. }
  188. }
  189. func TestCommanderListHandlers(t *testing.T) {
  190. tcpServer := tcp.Server{
  191. MsgProcessor: xor,
  192. }
  193. dest, err := tcpServer.Start()
  194. common.Must(err)
  195. defer tcpServer.Close()
  196. clientPort := tcp.PickPort()
  197. cmdPort := tcp.PickPort()
  198. clientConfig := &core.Config{
  199. App: []*serial.TypedMessage{
  200. serial.ToTypedMessage(&commander.Config{
  201. Tag: "api",
  202. Service: []*serial.TypedMessage{
  203. serial.ToTypedMessage(&command.Config{}),
  204. },
  205. }),
  206. serial.ToTypedMessage(&router.Config{
  207. Rule: []*router.RoutingRule{
  208. {
  209. InboundTag: []string{"api"},
  210. TargetTag: &router.RoutingRule_Tag{
  211. Tag: "api",
  212. },
  213. },
  214. },
  215. }),
  216. },
  217. Inbound: []*core.InboundHandlerConfig{
  218. {
  219. Tag: "d",
  220. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  221. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  222. Listen: net.NewIPOrDomain(net.LocalHostIP),
  223. }),
  224. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  225. Address: net.NewIPOrDomain(dest.Address),
  226. Port: uint32(dest.Port),
  227. Networks: []net.Network{net.Network_TCP},
  228. }),
  229. },
  230. {
  231. Tag: "api",
  232. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  233. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}},
  234. Listen: net.NewIPOrDomain(net.LocalHostIP),
  235. }),
  236. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  237. Address: net.NewIPOrDomain(dest.Address),
  238. Port: uint32(dest.Port),
  239. Networks: []net.Network{net.Network_TCP},
  240. }),
  241. },
  242. },
  243. Outbound: []*core.OutboundHandlerConfig{
  244. {
  245. Tag: "default-outbound",
  246. SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{}),
  247. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  248. },
  249. },
  250. }
  251. servers, err := InitializeServerConfigs(clientConfig)
  252. common.Must(err)
  253. defer CloseAllServers(servers)
  254. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
  255. t.Fatal(err)
  256. }
  257. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  258. common.Must(err)
  259. defer cmdConn.Close()
  260. hsClient := command.NewHandlerServiceClient(cmdConn)
  261. inboundResp, err := hsClient.ListInbounds(context.Background(), &command.ListInboundsRequest{})
  262. common.Must(err)
  263. if inboundResp == nil {
  264. t.Error("unexpected nil response")
  265. }
  266. if diff := cmp.Diff(
  267. inboundResp.Inbounds,
  268. clientConfig.Inbound,
  269. protocmp.Transform(),
  270. cmpopts.SortSlices(func(a, b *core.InboundHandlerConfig) bool {
  271. return a.Tag < b.Tag
  272. })); diff != "" {
  273. t.Fatalf("inbound response doesn't match config (-want +got):\n%s", diff)
  274. }
  275. outboundResp, err := hsClient.ListOutbounds(context.Background(), &command.ListOutboundsRequest{})
  276. common.Must(err)
  277. if outboundResp == nil {
  278. t.Error("unexpected nil response")
  279. }
  280. if diff := cmp.Diff(
  281. outboundResp.Outbounds,
  282. clientConfig.Outbound,
  283. protocmp.Transform(),
  284. cmpopts.SortSlices(func(a, b *core.InboundHandlerConfig) bool {
  285. return a.Tag < b.Tag
  286. })); diff != "" {
  287. t.Fatalf("outbound response doesn't match config (-want +got):\n%s", diff)
  288. }
  289. }
  290. func TestCommanderAddRemoveUser(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. u1 := protocol.NewID(uuid.New())
  298. u2 := protocol.NewID(uuid.New())
  299. cmdPort := tcp.PickPort()
  300. serverPort := tcp.PickPort()
  301. serverConfig := &core.Config{
  302. App: []*serial.TypedMessage{
  303. serial.ToTypedMessage(&commander.Config{
  304. Tag: "api",
  305. Service: []*serial.TypedMessage{
  306. serial.ToTypedMessage(&command.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. },
  328. }),
  329. },
  330. Inbound: []*core.InboundHandlerConfig{
  331. {
  332. Tag: "v",
  333. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  334. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
  335. Listen: net.NewIPOrDomain(net.LocalHostIP),
  336. }),
  337. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  338. User: []*protocol.User{
  339. {
  340. Account: serial.ToTypedMessage(&vmess.Account{
  341. Id: u1.String(),
  342. }),
  343. },
  344. },
  345. }),
  346. },
  347. {
  348. Tag: "api",
  349. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  350. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}},
  351. Listen: net.NewIPOrDomain(net.LocalHostIP),
  352. }),
  353. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  354. Address: net.NewIPOrDomain(dest.Address),
  355. Port: uint32(dest.Port),
  356. Networks: []net.Network{net.Network_TCP},
  357. }),
  358. },
  359. },
  360. Outbound: []*core.OutboundHandlerConfig{
  361. {
  362. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  363. },
  364. },
  365. }
  366. clientPort := tcp.PickPort()
  367. clientConfig := &core.Config{
  368. App: []*serial.TypedMessage{
  369. serial.ToTypedMessage(&policy.Config{
  370. Level: map[uint32]*policy.Policy{
  371. 0: {
  372. Timeout: &policy.Policy_Timeout{
  373. UplinkOnly: &policy.Second{Value: 0},
  374. DownlinkOnly: &policy.Second{Value: 0},
  375. },
  376. },
  377. },
  378. }),
  379. },
  380. Inbound: []*core.InboundHandlerConfig{
  381. {
  382. Tag: "d",
  383. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  384. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  385. Listen: net.NewIPOrDomain(net.LocalHostIP),
  386. }),
  387. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  388. Address: net.NewIPOrDomain(dest.Address),
  389. Port: uint32(dest.Port),
  390. Networks: []net.Network{net.Network_TCP},
  391. }),
  392. },
  393. },
  394. Outbound: []*core.OutboundHandlerConfig{
  395. {
  396. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  397. Receiver: &protocol.ServerEndpoint{
  398. Address: net.NewIPOrDomain(net.LocalHostIP),
  399. Port: uint32(serverPort),
  400. User: &protocol.User{
  401. Account: serial.ToTypedMessage(&vmess.Account{
  402. Id: u2.String(),
  403. SecuritySettings: &protocol.SecurityConfig{
  404. Type: protocol.SecurityType_AES128_GCM,
  405. },
  406. }),
  407. },
  408. },
  409. }),
  410. },
  411. },
  412. }
  413. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  414. common.Must(err)
  415. defer CloseAllServers(servers)
  416. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != io.EOF &&
  417. /*We might wish to drain the connection*/
  418. (err != nil && !strings.HasSuffix(err.Error(), "i/o timeout")) {
  419. t.Fatal("expected error: ", err)
  420. }
  421. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  422. common.Must(err)
  423. defer cmdConn.Close()
  424. hsClient := command.NewHandlerServiceClient(cmdConn)
  425. resp, err := hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
  426. Tag: "v",
  427. Operation: serial.ToTypedMessage(
  428. &command.AddUserOperation{
  429. User: &protocol.User{
  430. Email: "[email protected]",
  431. Account: serial.ToTypedMessage(&vmess.Account{
  432. Id: u2.String(),
  433. }),
  434. },
  435. }),
  436. })
  437. common.Must(err)
  438. if resp == nil {
  439. t.Fatal("nil response")
  440. }
  441. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
  442. t.Fatal(err)
  443. }
  444. resp, err = hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
  445. Tag: "v",
  446. Operation: serial.ToTypedMessage(&command.RemoveUserOperation{Email: "[email protected]"}),
  447. })
  448. common.Must(err)
  449. if resp == nil {
  450. t.Fatal("nil response")
  451. }
  452. }
  453. func TestCommanderStats(t *testing.T) {
  454. tcpServer := tcp.Server{
  455. MsgProcessor: xor,
  456. }
  457. dest, err := tcpServer.Start()
  458. common.Must(err)
  459. defer tcpServer.Close()
  460. userID := protocol.NewID(uuid.New())
  461. serverPort := tcp.PickPort()
  462. cmdPort := tcp.PickPort()
  463. serverConfig := &core.Config{
  464. App: []*serial.TypedMessage{
  465. serial.ToTypedMessage(&stats.Config{}),
  466. serial.ToTypedMessage(&commander.Config{
  467. Tag: "api",
  468. Service: []*serial.TypedMessage{
  469. serial.ToTypedMessage(&statscmd.Config{}),
  470. },
  471. }),
  472. serial.ToTypedMessage(&router.Config{
  473. Rule: []*router.RoutingRule{
  474. {
  475. InboundTag: []string{"api"},
  476. TargetTag: &router.RoutingRule_Tag{
  477. Tag: "api",
  478. },
  479. },
  480. },
  481. }),
  482. serial.ToTypedMessage(&policy.Config{
  483. Level: map[uint32]*policy.Policy{
  484. 0: {
  485. Timeout: &policy.Policy_Timeout{
  486. UplinkOnly: &policy.Second{Value: 0},
  487. DownlinkOnly: &policy.Second{Value: 0},
  488. },
  489. },
  490. 1: {
  491. Stats: &policy.Policy_Stats{
  492. UserUplink: true,
  493. UserDownlink: true,
  494. },
  495. },
  496. },
  497. System: &policy.SystemPolicy{
  498. Stats: &policy.SystemPolicy_Stats{
  499. InboundUplink: true,
  500. },
  501. },
  502. }),
  503. },
  504. Inbound: []*core.InboundHandlerConfig{
  505. {
  506. Tag: "vmess",
  507. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  508. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
  509. Listen: net.NewIPOrDomain(net.LocalHostIP),
  510. }),
  511. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  512. User: []*protocol.User{
  513. {
  514. Level: 1,
  515. Email: "test",
  516. Account: serial.ToTypedMessage(&vmess.Account{
  517. Id: userID.String(),
  518. }),
  519. },
  520. },
  521. }),
  522. },
  523. {
  524. Tag: "api",
  525. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  526. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}},
  527. Listen: net.NewIPOrDomain(net.LocalHostIP),
  528. }),
  529. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  530. Address: net.NewIPOrDomain(dest.Address),
  531. Port: uint32(dest.Port),
  532. Networks: []net.Network{net.Network_TCP},
  533. }),
  534. },
  535. },
  536. Outbound: []*core.OutboundHandlerConfig{
  537. {
  538. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  539. },
  540. },
  541. }
  542. clientPort := tcp.PickPort()
  543. clientConfig := &core.Config{
  544. Inbound: []*core.InboundHandlerConfig{
  545. {
  546. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  547. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  548. Listen: net.NewIPOrDomain(net.LocalHostIP),
  549. }),
  550. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  551. Address: net.NewIPOrDomain(dest.Address),
  552. Port: uint32(dest.Port),
  553. Networks: []net.Network{net.Network_TCP},
  554. }),
  555. },
  556. },
  557. Outbound: []*core.OutboundHandlerConfig{
  558. {
  559. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  560. Receiver: &protocol.ServerEndpoint{
  561. Address: net.NewIPOrDomain(net.LocalHostIP),
  562. Port: uint32(serverPort),
  563. User: &protocol.User{
  564. Account: serial.ToTypedMessage(&vmess.Account{
  565. Id: userID.String(),
  566. SecuritySettings: &protocol.SecurityConfig{
  567. Type: protocol.SecurityType_AES128_GCM,
  568. },
  569. }),
  570. },
  571. },
  572. }),
  573. },
  574. },
  575. }
  576. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  577. if err != nil {
  578. t.Fatal("Failed to create all servers", err)
  579. }
  580. defer CloseAllServers(servers)
  581. if err := testTCPConn(clientPort, 10240*1024, time.Second*20)(); err != nil {
  582. t.Fatal(err)
  583. }
  584. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  585. common.Must(err)
  586. defer cmdConn.Close()
  587. const name = "user>>>test>>>traffic>>>uplink"
  588. sClient := statscmd.NewStatsServiceClient(cmdConn)
  589. sresp, err := sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  590. Name: name,
  591. Reset_: true,
  592. })
  593. common.Must(err)
  594. if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
  595. Name: name,
  596. Value: 10240 * 1024,
  597. }, cmpopts.IgnoreUnexported(statscmd.Stat{})); r != "" {
  598. t.Error(r)
  599. }
  600. sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  601. Name: name,
  602. })
  603. common.Must(err)
  604. if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
  605. Name: name,
  606. Value: 0,
  607. }, cmpopts.IgnoreUnexported(statscmd.Stat{})); r != "" {
  608. t.Error(r)
  609. }
  610. sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  611. Name: "inbound>>>vmess>>>traffic>>>uplink",
  612. Reset_: true,
  613. })
  614. common.Must(err)
  615. if sresp.Stat.Value <= 10240*1024 {
  616. t.Error("value < 10240*1024: ", sresp.Stat.Value)
  617. }
  618. }