Skip to content

Commit

Permalink
fix sync user config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Mar 18, 2020
1 parent ad9a41d commit dad6c23
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
33 changes: 30 additions & 3 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,22 @@ func SyncTask(up *UserPool) {
return
}

// get user config
// init or update user config
initOrUpdateUser(up, proxymanClient, &resp)

// sync user traffic
syncUserTrafficToServer(up, statClient, httpClient)
}

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
Expand All @@ -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)
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit dad6c23

Please # to comment.