Skip to content

Commit 452b719

Browse files
nasralbekLjhAUMEM
andauthored
Hysteria inbound: Support routing's vlessRoute as well (#6375)
#6375 (comment) --------- Co-authored-by: LjhAUMEM <llnu14702@gmail.com>
1 parent 345c76f commit 452b719

3 files changed

Lines changed: 60 additions & 84 deletions

File tree

proxy/hysteria/account/config.go

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@ package account
33
import (
44
"sync"
55

6-
"github.com/xtls/xray-core/common/errors"
6+
"github.com/xtls/xray-core/common/net"
77
"github.com/xtls/xray-core/common/protocol"
8+
"github.com/xtls/xray-core/common/uuid"
89

910
"google.golang.org/protobuf/proto"
1011
)
1112

1213
func (a *Account) AsAccount() (protocol.Account, error) {
14+
var VR net.Port
15+
if id, err := uuid.ParseString(a.Auth); err == nil {
16+
VR = net.PortFromBytes(id[6:8])
17+
}
1318
return &MemoryAccount{
1419
Auth: a.Auth,
20+
VR: VR,
1521
}, nil
1622
}
1723

1824
type MemoryAccount struct {
1925
Auth string
26+
VR net.Port
2027
}
2128

22-
func (a *MemoryAccount) Equals(another protocol.Account) bool {
23-
if account, ok := another.(*MemoryAccount); ok {
24-
return a.Auth == account.Auth
29+
func (a *MemoryAccount) Equals(other protocol.Account) bool {
30+
if b, ok := other.(*MemoryAccount); ok {
31+
return a.Auth == b.Auth
2532
}
2633
return false
2734
}
@@ -33,97 +40,63 @@ func (a *MemoryAccount) ToProto() proto.Message {
3340
}
3441

3542
type Validator struct {
36-
emails map[string]struct{}
37-
users map[string]*protocol.MemoryUser
38-
39-
mutex sync.Mutex
43+
users sync.Map
4044
}
4145

4246
func NewValidator() *Validator {
43-
return &Validator{
44-
emails: make(map[string]struct{}),
45-
users: make(map[string]*protocol.MemoryUser),
46-
}
47+
return &Validator{}
4748
}
4849

49-
func (v *Validator) Add(u *protocol.MemoryUser) error {
50-
v.mutex.Lock()
51-
defer v.mutex.Unlock()
52-
53-
if u.Email != "" {
54-
if _, ok := v.emails[u.Email]; ok {
55-
return errors.New("User ", u.Email, " already exists.")
56-
}
57-
v.emails[u.Email] = struct{}{}
58-
}
59-
v.users[u.Account.(*MemoryAccount).Auth] = u
60-
50+
func (v *Validator) Add(user *protocol.MemoryUser) error {
51+
v.users.Store(user.Account.(*MemoryAccount).Auth, user)
6152
return nil
6253
}
6354

64-
func (v *Validator) Del(email string) error {
65-
if email == "" {
66-
return errors.New("Email must not be empty.")
55+
func (v *Validator) DelByEmail(email string) error {
56+
if user := v.GetByEmail(email); user != nil {
57+
v.users.Delete(user.Account.(*MemoryAccount).Auth)
6758
}
68-
69-
v.mutex.Lock()
70-
defer v.mutex.Unlock()
71-
72-
if _, ok := v.emails[email]; !ok {
73-
return errors.New("User ", email, " not found.")
74-
}
75-
delete(v.emails, email)
76-
for key, user := range v.users {
77-
if user.Email == email {
78-
delete(v.users, key)
79-
break
80-
}
81-
}
82-
8359
return nil
8460
}
8561

8662
func (v *Validator) Get(auth string) *protocol.MemoryUser {
87-
v.mutex.Lock()
88-
defer v.mutex.Unlock()
89-
90-
return v.users[auth]
91-
}
92-
93-
func (v *Validator) GetByEmail(email string) *protocol.MemoryUser {
94-
if email == "" {
95-
return nil
96-
}
97-
98-
v.mutex.Lock()
99-
defer v.mutex.Unlock()
100-
101-
if _, ok := v.emails[email]; ok {
102-
for _, user := range v.users {
103-
if user.Email == email {
104-
return user
105-
}
106-
}
63+
if value, ok := v.users.Load(auth); ok {
64+
return value.(*protocol.MemoryUser)
10765
}
108-
10966
return nil
11067
}
11168

112-
func (v *Validator) GetAll() []*protocol.MemoryUser {
113-
v.mutex.Lock()
114-
defer v.mutex.Unlock()
115-
116-
users := make([]*protocol.MemoryUser, 0, len(v.users))
117-
for _, user := range v.users {
118-
users = append(users, user)
119-
}
69+
func (v *Validator) GetByEmail(email string) (user *protocol.MemoryUser) {
70+
v.users.Range(func(key, value any) bool {
71+
if value.(*protocol.MemoryUser).Email == email {
72+
user = value.(*protocol.MemoryUser)
73+
return false
74+
}
75+
return true
76+
})
77+
return
78+
}
12079

121-
return users
80+
func (v *Validator) GetAll() (users []*protocol.MemoryUser) {
81+
v.users.Range(func(key, value any) bool {
82+
users = append(users, value.(*protocol.MemoryUser))
83+
return true
84+
})
85+
return
12286
}
12387

124-
func (v *Validator) GetCount() int64 {
125-
v.mutex.Lock()
126-
defer v.mutex.Unlock()
88+
func (v *Validator) GetCount() (count int64) {
89+
v.users.Range(func(key, value any) bool {
90+
count++
91+
return true
92+
})
93+
return
94+
}
12795

128-
return int64(len(v.users))
96+
func (v *Validator) NotEmpty() (not_empty bool) {
97+
v.users.Range(func(key, value any) bool {
98+
not_empty = true
99+
return false
100+
})
101+
return
129102
}

proxy/hysteria/server.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ func (s *Server) HysteriaInboundValidator() *account.Validator {
5959
return s.validator
6060
}
6161

62-
func (s *Server) AddUser(ctx context.Context, u *protocol.MemoryUser) error {
63-
return s.validator.Add(u)
62+
func (s *Server) AddUser(ctx context.Context, user *protocol.MemoryUser) error {
63+
return s.validator.Add(user)
6464
}
6565

66-
func (s *Server) RemoveUser(ctx context.Context, e string) error {
67-
return s.validator.Del(e)
66+
func (s *Server) RemoveUser(ctx context.Context, email string) error {
67+
return s.validator.DelByEmail(email)
6868
}
6969

7070
func (s *Server) GetUser(ctx context.Context, email string) *protocol.MemoryUser {
@@ -91,9 +91,12 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
9191

9292
iConn := stat.TryUnwrapStatsConn(conn)
9393

94-
type User interface{ User() *protocol.MemoryUser }
95-
if v, ok := iConn.(User); ok && v.User() != nil {
96-
inbound.User = v.User()
94+
if v, ok := iConn.(interface{ User() *protocol.MemoryUser }); ok {
95+
user := v.User()
96+
if user != nil {
97+
inbound.User = user
98+
inbound.VlessRoute = user.Account.(*account.MemoryAccount).VR
99+
}
97100
}
98101

99102
if _, ok := iConn.(*hysteria.InterConn); ok {

transport/internet/hysteria/hub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (h *httpHandler) AuthHTTP(w http.ResponseWriter, r *http.Request) bool {
5858

5959
var user *protocol.MemoryUser
6060
var ok bool
61-
if h.validator != nil && h.validator.GetCount() > 0 {
61+
if h.validator != nil && h.validator.NotEmpty() {
6262
user = h.validator.Get(auth)
6363
} else if h.config.Auth != "" {
6464
ok = auth == h.config.Auth

0 commit comments

Comments
 (0)