1
0

command_test.go 15 KB

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