From dad6c233e89ac799b88f2a6083d3b507cb572d57 Mon Sep 17 00:00:00 2001 From: Ehco1996 Date: Wed, 18 Mar 2020 08:23:52 +0800 Subject: [PATCH] fix sync user config --- cli/main.go | 2 +- tasks.go | 33 ++++++++++++++++++++++++++++++--- user.go | 12 ++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/cli/main.go b/cli/main.go index 562038a..4a15704 100644 --- a/cli/main.go +++ b/cli/main.go @@ -17,7 +17,7 @@ func main() { app := cli.NewApp() app.Name = "v2scar" app.Usage = "sidecar for V2ray" - app.Version = "0.0.10" + app.Version = "0.0.11" app.Author = "Ehco1996" app.Flags = []cli.Flag{ diff --git a/tasks.go b/tasks.go index 0c1a70a..9aefc02 100644 --- a/tasks.go +++ b/tasks.go @@ -63,7 +63,7 @@ func SyncTask(up *UserPool) { return } - // get user config + // init or update user config initOrUpdateUser(up, proxymanClient, &resp) // sync user traffic @@ -71,8 +71,14 @@ func SyncTask(up *UserPool) { } func initOrUpdateUser(up *UserPool, c v2proxyman.HandlerServiceClient, sr *syncResp) { + // Enable line numbers in logging + log.SetFlags(log.LstdFlags | log.Lshortfile) log.Println("[INFO] Call initOrUpdateUser") + + syncUserMap := make(map[string]bool) + for _, cfg := range sr.Configs { + syncUserMap[cfg.Email] = true user, _ := up.GetUserByEmail(cfg.Email) if user == nil { // New User @@ -89,14 +95,35 @@ func initOrUpdateUser(up *UserPool, c v2proxyman.HandlerServiceClient, sr *syncR // update enable status user.setEnable(cfg.Enable) } - if user.Enable && !user.running { - // Start Not Running user + + // change user uuid + if user.UUID != cfg.UUID { + log.Printf("[INFO] user: %s 更换了uuid old: %s new: %s", user.Email, user.UUID, cfg.UUID) + RemoveInboundUser(c, sr.Tag, user) + user.setUUID(cfg.UUID) AddInboundUser(c, sr.Tag, user) } + + // remove not enable user if !user.Enable && user.running { // Close Not Enable user RemoveInboundUser(c, sr.Tag, user) } + + // start not runing user + if user.Enable && !user.running { + // Start Not Running user + AddInboundUser(c, sr.Tag, user) + } + + } + } + + // remote user not in server + for _, user := range up.GetAllUsers() { + if _, ok := syncUserMap[user.Email]; !ok { + RemoveInboundUser(c, sr.Tag, user) + up.RemoveUserByEmail(user.Email) } } } diff --git a/user.go b/user.go index 05ed177..ba6a82e 100644 --- a/user.go +++ b/user.go @@ -54,6 +54,11 @@ func (u *User) setRunning(status bool) { u.running = status } +func (u *User) setUUID(uuid string) { + // NOTE not thread safe! + u.UUID = uuid +} + // UserPool user pool type UserPool struct { access sync.RWMutex @@ -94,6 +99,13 @@ func (up *UserPool) GetUserByEmail(email string) (*User, error) { } } +// RemoveUserByEmail get user by email +func (up *UserPool) RemoveUserByEmail(email string) { + up.access.Lock() + defer up.access.Unlock() + delete(up.users, email) +} + // GetAllUsers GetAllUsers func (up *UserPool) GetAllUsers() []*User { up.access.Lock()