client_flow_isolation_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/database"
  6. "github.com/mhsanaei/3x-ui/v3/database/model"
  7. )
  8. func TestClientWithInboundFlow_GatesByInboundCapability(t *testing.T) {
  9. const vision = "xtls-rprx-vision"
  10. cases := []struct {
  11. name string
  12. protocol model.Protocol
  13. streamSettings string
  14. wantFlow string
  15. }{
  16. {"vless tcp reality keeps flow", model.VLESS, `{"network":"tcp","security":"reality"}`, vision},
  17. {"vless tcp tls keeps flow", model.VLESS, `{"network":"tcp","security":"tls"}`, vision},
  18. {"vless ws tls clears flow", model.VLESS, `{"network":"ws","security":"tls"}`, ""},
  19. {"vless grpc tls clears flow", model.VLESS, `{"network":"grpc","security":"tls"}`, ""},
  20. {"vless tcp none clears flow", model.VLESS, `{"network":"tcp","security":"none"}`, ""},
  21. {"vmess tcp tls clears flow", model.VMESS, `{"network":"tcp","security":"tls"}`, ""},
  22. {"empty stream clears flow", model.VLESS, "", ""},
  23. }
  24. for _, tc := range cases {
  25. t.Run(tc.name, func(t *testing.T) {
  26. ib := &model.Inbound{Protocol: tc.protocol, StreamSettings: tc.streamSettings}
  27. got := clientWithInboundFlow(model.Client{Email: "[email protected]", Flow: vision}, ib)
  28. if got.Flow != tc.wantFlow {
  29. t.Errorf("Flow = %q, want %q", got.Flow, tc.wantFlow)
  30. }
  31. })
  32. }
  33. }
  34. func TestFlowIsolation_VisionDoesNotLeakToWsInbound(t *testing.T) {
  35. dbDir := t.TempDir()
  36. t.Setenv("XUI_DB_FOLDER", dbDir)
  37. if err := database.InitDB(filepath.Join(dbDir, "3x-ui.db")); err != nil {
  38. t.Fatalf("InitDB: %v", err)
  39. }
  40. t.Cleanup(func() { _ = database.CloseDB() })
  41. db := database.GetDB()
  42. wsTls := &model.Inbound{Tag: "vless-ws", Enable: true, Port: 30001, Protocol: model.VLESS, StreamSettings: `{"network":"ws","security":"tls"}`}
  43. if err := db.Create(wsTls).Error; err != nil {
  44. t.Fatalf("create ws+tls inbound: %v", err)
  45. }
  46. reality := &model.Inbound{Tag: "vless-reality", Enable: true, Port: 30002, Protocol: model.VLESS, StreamSettings: `{"network":"tcp","security":"reality"}`}
  47. if err := db.Create(reality).Error; err != nil {
  48. t.Fatalf("create reality inbound: %v", err)
  49. }
  50. svc := ClientService{}
  51. const email = "[email protected]"
  52. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c003"
  53. const vision = "xtls-rprx-vision"
  54. source := model.Client{Email: email, ID: uid, Enable: true, Flow: vision}
  55. for _, ib := range []*model.Inbound{wsTls, reality} {
  56. gated := clientWithInboundFlow(source, ib)
  57. if err := svc.SyncInbound(nil, ib.Id, []model.Client{gated}); err != nil {
  58. t.Fatalf("SyncInbound(%s): %v", ib.Tag, err)
  59. }
  60. }
  61. realityList, err := svc.ListForInbound(nil, reality.Id)
  62. if err != nil {
  63. t.Fatalf("ListForInbound(reality): %v", err)
  64. }
  65. if len(realityList) != 1 || realityList[0].Flow != vision {
  66. t.Errorf("Reality inbound should keep flow=%q, got %#v", vision, realityList)
  67. }
  68. wsList, err := svc.ListForInbound(nil, wsTls.Id)
  69. if err != nil {
  70. t.Fatalf("ListForInbound(ws): %v", err)
  71. }
  72. if len(wsList) != 1 || wsList[0].Flow != "" {
  73. t.Errorf("WS+TLS inbound must not inherit Vision flow (#4628), got %#v", wsList)
  74. }
  75. }