mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
Trojan Protocol Handler implements UserManager (#344)
* Trojan Protocol Handler implements UserManager * Update validator.go Co-authored-by: RPRX <63339210+rprx@users.noreply.github.com>
This commit is contained in:
parent
c325faef38
commit
24f688cc20
@ -99,11 +99,6 @@ type TrojanServerConfig struct {
|
|||||||
// Build implements Buildable
|
// Build implements Buildable
|
||||||
func (c *TrojanServerConfig) Build() (proto.Message, error) {
|
func (c *TrojanServerConfig) Build() (proto.Message, error) {
|
||||||
config := new(trojan.ServerConfig)
|
config := new(trojan.ServerConfig)
|
||||||
|
|
||||||
if len(c.Clients) == 0 {
|
|
||||||
return nil, newError("No trojan user settings.")
|
|
||||||
}
|
|
||||||
|
|
||||||
config.Users = make([]*protocol.User, len(c.Clients))
|
config.Users = make([]*protocol.User, len(c.Clients))
|
||||||
for idx, rawUser := range c.Clients {
|
for idx, rawUser := range c.Clients {
|
||||||
user := new(protocol.User)
|
user := new(protocol.User)
|
||||||
|
@ -85,6 +85,16 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
|||||||
return server, nil
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddUser implements proxy.UserManager.AddUser().
|
||||||
|
func (s *Server) AddUser(ctx context.Context, u *protocol.MemoryUser) error {
|
||||||
|
return s.validator.Add(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveUser implements proxy.UserManager.RemoveUser().
|
||||||
|
func (s *Server) RemoveUser(ctx context.Context, e string) error {
|
||||||
|
return s.validator.Del(e)
|
||||||
|
}
|
||||||
|
|
||||||
// Network implements proxy.Inbound.Network().
|
// Network implements proxy.Inbound.Network().
|
||||||
func (s *Server) Network() []net.Network {
|
func (s *Server) Network() []net.Network {
|
||||||
return []net.Network{net.Network_TCP}
|
return []net.Network{net.Network_TCP}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package trojan
|
package trojan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"v2ray.com/core/common/protocol"
|
"v2ray.com/core/common/protocol"
|
||||||
@ -8,13 +9,35 @@ import (
|
|||||||
|
|
||||||
// Validator stores valid trojan users
|
// Validator stores valid trojan users
|
||||||
type Validator struct {
|
type Validator struct {
|
||||||
|
// Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
|
||||||
|
email sync.Map
|
||||||
users sync.Map
|
users sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a trojan user
|
// Add a trojan user
|
||||||
func (v *Validator) Add(u *protocol.MemoryUser) error {
|
func (v *Validator) Add(u *protocol.MemoryUser) error {
|
||||||
user := u.Account.(*MemoryAccount)
|
if u.Email != "" {
|
||||||
v.users.Store(hexString(user.Key), u)
|
_, loaded := v.email.LoadOrStore(strings.ToLower(u.Email), u)
|
||||||
|
if loaded {
|
||||||
|
return newError("User ", u.Email, " already exists.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
v.users.Store(hexString(u.Account.(*MemoryAccount).Key), u)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Del a trojan user
|
||||||
|
func (v *Validator) Del(e string) error {
|
||||||
|
if e == "" {
|
||||||
|
return newError("Email must not be empty.")
|
||||||
|
}
|
||||||
|
le := strings.ToLower(e)
|
||||||
|
u, _ := v.email.Load(le)
|
||||||
|
if u == nil {
|
||||||
|
return newError("User ", e, " not found.")
|
||||||
|
}
|
||||||
|
v.email.Delete(le)
|
||||||
|
v.users.Delete(hexString(u.(*protocol.MemoryUser).Account.(*MemoryAccount).Key))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user