From 82c1753672e2a21d8e770e11666dc659af2d0ab5 Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Mon, 19 Jun 2023 09:24:05 +0800 Subject: [PATCH] fix node change --- node/controller.go | 2 +- node/task.go | 133 +++++++++++++++++++++++---------------------- 2 files changed, 68 insertions(+), 67 deletions(-) diff --git a/node/controller.go b/node/controller.go index cd64eaf..e633832 100644 --- a/node/controller.go +++ b/node/controller.go @@ -83,7 +83,7 @@ func (c *Controller) Start() error { if err != nil { return fmt.Errorf("add users error: %s", err) } - log.Printf("[%s: %d] Added %d new users", c.nodeInfo.Type, c.nodeInfo.Id, added) + log.Printf("[%s] Added %d new users", c.Tag, added) c.initTask() return nil } diff --git a/node/task.go b/node/task.go index 59d3ee2..713c023 100644 --- a/node/task.go +++ b/node/task.go @@ -41,50 +41,44 @@ func (c *Controller) initTask() { } func (c *Controller) nodeInfoMonitor() (err error) { - // First fetch Node Info + // get node info newNodeInfo, err := c.apiClient.GetNodeInfo() if err != nil { - log.Print(err) + log.Printf("[%s] Get node info error: %s", c.Tag, err) + return nil + } + // get user info + newUserInfo, err := c.apiClient.GetUserList() + if err != nil { + log.Printf("[%s] Get user list error: %s", c.Tag, err) return nil } - var nodeInfoChanged = false - // If nodeInfo changed if newNodeInfo != nil { + // nodeInfo changed // Remove old tag - oldTag := c.Tag - err := c.server.DelNode(oldTag) + err = c.server.DelNode(c.Tag) if err != nil { - log.Print(err) + log.Printf("[%s] Del node error: %s", c.Tag, err) return nil } // Remove Old limiter - limiter.DeleteLimiter(oldTag) - // Add new tag - c.nodeInfo = newNodeInfo + limiter.DeleteLimiter(c.Tag) + // Add new Limiter c.Tag = c.buildNodeTag() - err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig) - if err != nil { - log.Print(err) - return nil - } - if c.nodeInfo.Tls || c.nodeInfo.Type == "hysteria" { + l := limiter.AddLimiter(c.Tag, &c.LimitConfig, newUserInfo) + // check cert + if newNodeInfo.Tls || newNodeInfo.Type == "hysteria" { err = c.requestCert() if err != nil { - return fmt.Errorf("request cert error: %s", err) + log.Printf("[%s] Request cert error: %s", c.Tag, err) } } - nodeInfoChanged = true - } - // Update User - newUserInfo, err := c.apiClient.GetUserList() - if err != nil { - log.Print(err) - return nil - } - if nodeInfoChanged { - c.userList = newUserInfo - // Add new Limiter - l := limiter.AddLimiter(c.Tag, &c.LimitConfig, newUserInfo) + // add new node + err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig) + if err != nil { + log.Printf("[%s] Add node error: %s", c.Tag, err) + return nil + } _, err = c.server.AddUsers(&vCore.AddUsersParams{ Tag: c.Tag, Config: c.ControllerConfig, @@ -92,12 +86,12 @@ func (c *Controller) nodeInfoMonitor() (err error) { NodeInfo: newNodeInfo, }) if err != nil { - log.Print(err) + log.Printf("[%s] Add users error: %s", c.Tag, err) return nil } err = l.UpdateRule(newNodeInfo.Rules) if err != nil { - log.Printf("Update Rule error: %s", err) + log.Printf("[%s] Update Rule error: %s", c.Tag, err) } // Check interval if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.PullInterval && @@ -112,42 +106,49 @@ func (c *Controller) nodeInfoMonitor() (err error) { c.userReportPeriodic.Close() _ = c.userReportPeriodic.Start(false) } - } else { - deleted, added := compareUserList(c.userList, newUserInfo) - if len(deleted) > 0 { - deletedEmail := make([]string, len(deleted)) - for i := range deleted { - deletedEmail[i] = fmt.Sprintf("%s|%s|%d", - c.Tag, - (deleted)[i].Uuid, - (deleted)[i].Id) - } - err := c.server.DelUsers(deletedEmail, c.Tag) - if err != nil { - log.Print(err) - } - } - if len(added) > 0 { - _, err := c.server.AddUsers(&vCore.AddUsersParams{ - Tag: c.Tag, - Config: c.ControllerConfig, - UserInfo: added, - NodeInfo: c.nodeInfo, - }) - if err != nil { - log.Print(err) - } - } - if len(added) > 0 || len(deleted) > 0 { - // Update Limiter - err = limiter.UpdateLimiter(c.Tag, added, deleted) - if err != nil { - log.Print("update limiter:", err) - } - } - log.Printf("[%s: %d] %d user deleted, %d user added", c.nodeInfo.Type, c.nodeInfo.Id, - len(deleted), len(added)) + c.nodeInfo = newNodeInfo c.userList = newUserInfo + // exit + return nil } + + // node no changed, check users + deleted, added := compareUserList(c.userList, newUserInfo) + if len(deleted) > 0 { + // have deleted users + deletedEmail := make([]string, len(deleted)) + for i := range deleted { + deletedEmail[i] = fmt.Sprintf("%s|%s|%d", + c.Tag, + (deleted)[i].Uuid, + (deleted)[i].Id) + } + err = c.server.DelUsers(deletedEmail, c.Tag) + if err != nil { + log.Printf("[%s] Del users error: %s", c.Tag, err) + } + } + if len(added) > 0 { + // have added users + _, err = c.server.AddUsers(&vCore.AddUsersParams{ + Tag: c.Tag, + Config: c.ControllerConfig, + UserInfo: added, + NodeInfo: c.nodeInfo, + }) + if err != nil { + log.Printf("[%s] Add users error: %s", c.Tag, err) + } + } + if len(added) > 0 || len(deleted) > 0 { + // update Limiter + err = limiter.UpdateLimiter(c.Tag, added, deleted) + if err != nil { + log.Printf("[%s] Update limiter error: %s", c.Tag, err) + } + } + c.userList = newUserInfo + log.Printf("[%s] %d user deleted, %d user added", c.Tag, + len(deleted), len(added)) return nil }