Skip to content

Commit 0757aaf

Browse files
committed
refactor: simplify account handling and add tests for flow overrides
1 parent a7db933 commit 0757aaf

3 files changed

Lines changed: 140 additions & 46 deletions

File tree

backend/xray/config.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ func (i *Inbound) syncUsers(users []*common.User) {
152152
log.Println("error for user", user.GetEmail(), ":", err)
153153
continue
154154
}
155-
newAccount := checkVless(i, *account)
156-
i.clients[user.GetEmail()] = &newAccount
155+
i.clients[user.GetEmail()] = account
157156
}
158157
}
159158

@@ -217,8 +216,7 @@ func (i *Inbound) updateUser(account api.Account) {
217216
i.clients[email] = a
218217

219218
case *api.VlessAccount:
220-
newAccount := checkVless(i, *a)
221-
i.clients[email] = &newAccount
219+
i.clients[email] = a
222220

223221
case *api.TrojanAccount:
224222
i.clients[email] = a
@@ -259,8 +257,7 @@ func (i *Inbound) updateUsers(accounts []api.Account, removeEmails []string) {
259257
case Vless:
260258
for _, account := range accounts {
261259
if a, ok := account.(*api.VlessAccount); ok {
262-
newAccount := checkVless(i, *a)
263-
i.clients[account.GetEmail()] = &newAccount
260+
i.clients[account.GetEmail()] = a
264261
}
265262
}
266263

backend/xray/user.go

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,28 @@ func setupUserAccount(user *common.User) (api.ProxySettings, error) {
4141
return settings, nil
4242
}
4343

44-
func checkVless(inbound *Inbound, account api.VlessAccount) api.VlessAccount {
45-
if account.Flow != "" {
46-
47-
networkType, ok := inbound.StreamSettings["network"]
48-
if !ok || !(networkType == "tcp" || networkType == "raw" || networkType == "kcp") {
49-
account.Flow = ""
50-
return account
51-
}
52-
53-
securityType, ok := inbound.StreamSettings["security"]
54-
if !ok || !(securityType == "tls" || securityType == "reality") {
55-
account.Flow = ""
56-
return account
57-
}
58-
59-
rawMap, ok := inbound.StreamSettings["rawSettings"].(map[string]interface{})
60-
if !ok {
61-
rawMap, ok = inbound.StreamSettings["tcpSettings"].(map[string]interface{})
62-
if !ok {
63-
return account
64-
}
65-
}
66-
67-
headerMap, ok := rawMap["header"].(map[string]interface{})
68-
if !ok {
69-
return account
70-
}
44+
func inboundFlow(inbound *Inbound) string {
45+
if inbound == nil || inbound.Settings == nil {
46+
return ""
47+
}
48+
flow, _ := inbound.Settings["flow"].(string)
49+
return flow
50+
}
7151

72-
headerType, ok := headerMap["Type"].(string)
73-
if !ok {
74-
return account
75-
}
52+
func accountForAPI(inbound *Inbound, account api.Account) api.Account {
53+
vlessAccount, ok := account.(*api.VlessAccount)
54+
if !ok {
55+
return account
56+
}
7657

77-
if headerType == "http" {
78-
account.Flow = ""
79-
return account
80-
}
58+
overrideFlow := inboundFlow(inbound)
59+
if overrideFlow == "" {
60+
return account
8161
}
82-
return account
62+
63+
copy := *vlessAccount
64+
copy.Flow = overrideFlow
65+
return &copy
8366
}
8467

8568
func checkShadowsocks2022(method string, account api.ShadowsocksAccount) api.ShadowsocksAccount {
@@ -95,8 +78,7 @@ func isActiveInbound(inbound *Inbound, inbounds []string, settings api.ProxySett
9578
if settings.Vless == nil {
9679
return nil, false
9780
}
98-
account := checkVless(inbound, *settings.Vless)
99-
return &account, true
81+
return settings.Vless, true
10082

10183
case Vmess:
10284
if settings.Vmess == nil {
@@ -157,7 +139,7 @@ func (x *Xray) SyncUser(ctx context.Context, user *common.User) error {
157139
account, isActive := isActiveInbound(inbound, userInbounds, proxySetting)
158140
if isActive {
159141
inbound.updateUser(account)
160-
err = handler.AddInboundUser(ctx, inbound.Tag, account)
142+
err = handler.AddInboundUser(ctx, inbound.Tag, accountForAPI(inbound, account))
161143
if err != nil {
162144
log.Println(err)
163145
errMessage += "\n" + err.Error()
@@ -204,7 +186,7 @@ func (x *Xray) UpdateUsers(ctx context.Context, users []*common.User) error {
204186

205187
for _, account := range update.accounts {
206188
_ = handler.RemoveInboundUser(ctx, tag, account.GetEmail())
207-
if err := handler.AddInboundUser(ctx, tag, account); err != nil {
189+
if err := handler.AddInboundUser(ctx, tag, accountForAPI(inbound, account)); err != nil {
208190
log.Println(err)
209191
errMessage += "\n" + err.Error()
210192
}

backend/xray/user_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package xray
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/uuid"
7+
8+
"github.com/pasarguard/node/backend/xray/api"
9+
"github.com/pasarguard/node/common"
10+
)
11+
12+
func TestAccountForAPIUsesInboundFlowOverrideWhenPresent(t *testing.T) {
13+
user := &common.User{
14+
Email: "user@example.com",
15+
Proxies: &common.Proxy{
16+
Vless: &common.Vless{
17+
Id: uuid.New().String(),
18+
Flow: "user-flow",
19+
},
20+
},
21+
}
22+
23+
account, err := api.NewVlessAccount(user)
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
inbound := &Inbound{
29+
Settings: map[string]any{
30+
"flow": "inbound-flow",
31+
},
32+
}
33+
34+
apiAccount := accountForAPI(inbound, account)
35+
vlessAccount, ok := apiAccount.(*api.VlessAccount)
36+
if !ok {
37+
t.Fatalf("unexpected account type: %T", apiAccount)
38+
}
39+
if vlessAccount.Flow != "inbound-flow" {
40+
t.Fatalf("unexpected flow: got %q want %q", vlessAccount.Flow, "inbound-flow")
41+
}
42+
if account.Flow != "user-flow" {
43+
t.Fatalf("original account flow was mutated: got %q want %q", account.Flow, "user-flow")
44+
}
45+
}
46+
47+
func TestAccountForAPIUsesUserFlowWhenInboundFlowEmpty(t *testing.T) {
48+
user := &common.User{
49+
Email: "user@example.com",
50+
Proxies: &common.Proxy{
51+
Vless: &common.Vless{
52+
Id: uuid.New().String(),
53+
Flow: "user-flow",
54+
},
55+
},
56+
}
57+
58+
account, err := api.NewVlessAccount(user)
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
63+
inbound := &Inbound{
64+
Settings: map[string]any{
65+
"flow": "",
66+
},
67+
}
68+
69+
apiAccount := accountForAPI(inbound, account)
70+
vlessAccount, ok := apiAccount.(*api.VlessAccount)
71+
if !ok {
72+
t.Fatalf("unexpected account type: %T", apiAccount)
73+
}
74+
if vlessAccount.Flow != "user-flow" {
75+
t.Fatalf("unexpected flow: got %q want %q", vlessAccount.Flow, "user-flow")
76+
}
77+
}
78+
79+
func TestIsActiveInboundKeepsUserFlowForConfigStorage(t *testing.T) {
80+
user := &common.User{
81+
Email: "user@example.com",
82+
Inbounds: []string{"vless-in"},
83+
Proxies: &common.Proxy{
84+
Vless: &common.Vless{
85+
Id: uuid.New().String(),
86+
Flow: "user-flow",
87+
},
88+
},
89+
}
90+
91+
settings, err := setupUserAccount(user)
92+
if err != nil {
93+
t.Fatal(err)
94+
}
95+
96+
inbound := &Inbound{
97+
Tag: "vless-in",
98+
Protocol: Vless,
99+
Settings: map[string]any{
100+
"flow": "inbound-flow",
101+
},
102+
}
103+
104+
account, ok := isActiveInbound(inbound, user.Inbounds, settings)
105+
if !ok {
106+
t.Fatal("expected inbound to be active")
107+
}
108+
vlessAccount, ok := account.(*api.VlessAccount)
109+
if !ok {
110+
t.Fatalf("unexpected account type: %T", account)
111+
}
112+
if vlessAccount.Flow != "user-flow" {
113+
t.Fatalf("unexpected config flow: got %q want %q", vlessAccount.Flow, "user-flow")
114+
}
115+
}

0 commit comments

Comments
 (0)