command_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. Networks: []net.Network{net.Network_TCP},
  289. }),
  290. },
  291. },
  292. Outbound: []*core.OutboundHandlerConfig{
  293. {
  294. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  295. Receiver: []*protocol.ServerEndpoint{
  296. {
  297. Address: net.NewIPOrDomain(net.LocalHostIP),
  298. Port: uint32(serverPort),
  299. User: []*protocol.User{
  300. {
  301. Account: serial.ToTypedMessage(&vmess.Account{
  302. Id: u2.String(),
  303. SecuritySettings: &protocol.SecurityConfig{
  304. Type: protocol.SecurityType_AES128_GCM,
  305. },
  306. }),
  307. },
  308. },
  309. },
  310. },
  311. }),
  312. },
  313. },
  314. }
  315. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  316. common.Must(err)
  317. defer CloseAllServers(servers)
  318. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != io.EOF &&
  319. /*We might wish to drain the connection*/
  320. (err != nil && !strings.HasSuffix(err.Error(), "i/o timeout")) {
  321. t.Fatal("expected error: ", err)
  322. }
  323. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  324. common.Must(err)
  325. defer cmdConn.Close()
  326. hsClient := command.NewHandlerServiceClient(cmdConn)
  327. resp, err := hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
  328. Tag: "v",
  329. Operation: serial.ToTypedMessage(
  330. &command.AddUserOperation{
  331. User: &protocol.User{
  332. Email: "[email protected]",
  333. Account: serial.ToTypedMessage(&vmess.Account{
  334. Id: u2.String(),
  335. }),
  336. },
  337. }),
  338. })
  339. common.Must(err)
  340. if resp == nil {
  341. t.Fatal("nil response")
  342. }
  343. if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
  344. t.Fatal(err)
  345. }
  346. resp, err = hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
  347. Tag: "v",
  348. Operation: serial.ToTypedMessage(&command.RemoveUserOperation{Email: "[email protected]"}),
  349. })
  350. common.Must(err)
  351. if resp == nil {
  352. t.Fatal("nil response")
  353. }
  354. }
  355. func TestCommanderStats(t *testing.T) {
  356. tcpServer := tcp.Server{
  357. MsgProcessor: xor,
  358. }
  359. dest, err := tcpServer.Start()
  360. common.Must(err)
  361. defer tcpServer.Close()
  362. userID := protocol.NewID(uuid.New())
  363. serverPort := tcp.PickPort()
  364. cmdPort := tcp.PickPort()
  365. serverConfig := &core.Config{
  366. App: []*serial.TypedMessage{
  367. serial.ToTypedMessage(&stats.Config{}),
  368. serial.ToTypedMessage(&commander.Config{
  369. Tag: "api",
  370. Service: []*serial.TypedMessage{
  371. serial.ToTypedMessage(&statscmd.Config{}),
  372. },
  373. }),
  374. serial.ToTypedMessage(&router.Config{
  375. Rule: []*router.RoutingRule{
  376. {
  377. InboundTag: []string{"api"},
  378. TargetTag: &router.RoutingRule_Tag{
  379. Tag: "api",
  380. },
  381. },
  382. },
  383. }),
  384. serial.ToTypedMessage(&policy.Config{
  385. Level: map[uint32]*policy.Policy{
  386. 0: {
  387. Timeout: &policy.Policy_Timeout{
  388. UplinkOnly: &policy.Second{Value: 0},
  389. DownlinkOnly: &policy.Second{Value: 0},
  390. },
  391. },
  392. 1: {
  393. Stats: &policy.Policy_Stats{
  394. UserUplink: true,
  395. UserDownlink: true,
  396. },
  397. },
  398. },
  399. System: &policy.SystemPolicy{
  400. Stats: &policy.SystemPolicy_Stats{
  401. InboundUplink: true,
  402. },
  403. },
  404. }),
  405. },
  406. Inbound: []*core.InboundHandlerConfig{
  407. {
  408. Tag: "vmess",
  409. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  410. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
  411. Listen: net.NewIPOrDomain(net.LocalHostIP),
  412. }),
  413. ProxySettings: serial.ToTypedMessage(&inbound.Config{
  414. User: []*protocol.User{
  415. {
  416. Level: 1,
  417. Email: "test",
  418. Account: serial.ToTypedMessage(&vmess.Account{
  419. Id: userID.String(),
  420. }),
  421. },
  422. },
  423. }),
  424. },
  425. {
  426. Tag: "api",
  427. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  428. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(cmdPort)}},
  429. Listen: net.NewIPOrDomain(net.LocalHostIP),
  430. }),
  431. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  432. Address: net.NewIPOrDomain(dest.Address),
  433. Port: uint32(dest.Port),
  434. Networks: []net.Network{net.Network_TCP},
  435. }),
  436. },
  437. },
  438. Outbound: []*core.OutboundHandlerConfig{
  439. {
  440. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  441. },
  442. },
  443. }
  444. clientPort := tcp.PickPort()
  445. clientConfig := &core.Config{
  446. Inbound: []*core.InboundHandlerConfig{
  447. {
  448. ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
  449. PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
  450. Listen: net.NewIPOrDomain(net.LocalHostIP),
  451. }),
  452. ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
  453. Address: net.NewIPOrDomain(dest.Address),
  454. Port: uint32(dest.Port),
  455. Networks: []net.Network{net.Network_TCP},
  456. }),
  457. },
  458. },
  459. Outbound: []*core.OutboundHandlerConfig{
  460. {
  461. ProxySettings: serial.ToTypedMessage(&outbound.Config{
  462. Receiver: []*protocol.ServerEndpoint{
  463. {
  464. Address: net.NewIPOrDomain(net.LocalHostIP),
  465. Port: uint32(serverPort),
  466. User: []*protocol.User{
  467. {
  468. Account: serial.ToTypedMessage(&vmess.Account{
  469. Id: userID.String(),
  470. SecuritySettings: &protocol.SecurityConfig{
  471. Type: protocol.SecurityType_AES128_GCM,
  472. },
  473. }),
  474. },
  475. },
  476. },
  477. },
  478. }),
  479. },
  480. },
  481. }
  482. servers, err := InitializeServerConfigs(serverConfig, clientConfig)
  483. if err != nil {
  484. t.Fatal("Failed to create all servers", err)
  485. }
  486. defer CloseAllServers(servers)
  487. if err := testTCPConn(clientPort, 10240*1024, time.Second*20)(); err != nil {
  488. t.Fatal(err)
  489. }
  490. cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
  491. common.Must(err)
  492. defer cmdConn.Close()
  493. const name = "user>>>test>>>traffic>>>uplink"
  494. sClient := statscmd.NewStatsServiceClient(cmdConn)
  495. sresp, err := sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  496. Name: name,
  497. Reset_: true,
  498. })
  499. common.Must(err)
  500. if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
  501. Name: name,
  502. Value: 10240 * 1024,
  503. }, cmpopts.IgnoreUnexported(statscmd.Stat{})); r != "" {
  504. t.Error(r)
  505. }
  506. sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  507. Name: name,
  508. })
  509. common.Must(err)
  510. if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
  511. Name: name,
  512. Value: 0,
  513. }, cmpopts.IgnoreUnexported(statscmd.Stat{})); r != "" {
  514. t.Error(r)
  515. }
  516. sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
  517. Name: "inbound>>>vmess>>>traffic>>>uplink",
  518. Reset_: true,
  519. })
  520. common.Must(err)
  521. if sresp.Stat.Value <= 10240*1024 {
  522. t.Error("value < 10240*1024: ", sresp.Stat.Value)
  523. }
  524. }