fix node change

This commit is contained in:
yuzuki999 2023-06-19 09:24:05 +08:00
parent 2232bf46e5
commit 82c1753672
2 changed files with 68 additions and 67 deletions

View File

@ -83,7 +83,7 @@ func (c *Controller) Start() error {
if err != nil { if err != nil {
return fmt.Errorf("add users error: %s", err) 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() c.initTask()
return nil return nil
} }

View File

@ -41,50 +41,44 @@ func (c *Controller) initTask() {
} }
func (c *Controller) nodeInfoMonitor() (err error) { func (c *Controller) nodeInfoMonitor() (err error) {
// First fetch Node Info // get node info
newNodeInfo, err := c.apiClient.GetNodeInfo() newNodeInfo, err := c.apiClient.GetNodeInfo()
if err != nil { 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 return nil
} }
var nodeInfoChanged = false
// If nodeInfo changed
if newNodeInfo != nil { if newNodeInfo != nil {
// nodeInfo changed
// Remove old tag // Remove old tag
oldTag := c.Tag err = c.server.DelNode(c.Tag)
err := c.server.DelNode(oldTag)
if err != nil { if err != nil {
log.Print(err) log.Printf("[%s] Del node error: %s", c.Tag, err)
return nil return nil
} }
// Remove Old limiter // Remove Old limiter
limiter.DeleteLimiter(oldTag) limiter.DeleteLimiter(c.Tag)
// Add new tag // Add new Limiter
c.nodeInfo = newNodeInfo
c.Tag = c.buildNodeTag() c.Tag = c.buildNodeTag()
err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig) l := limiter.AddLimiter(c.Tag, &c.LimitConfig, newUserInfo)
if err != nil { // check cert
log.Print(err) if newNodeInfo.Tls || newNodeInfo.Type == "hysteria" {
return nil
}
if c.nodeInfo.Tls || c.nodeInfo.Type == "hysteria" {
err = c.requestCert() err = c.requestCert()
if err != nil { if err != nil {
return fmt.Errorf("request cert error: %s", err) log.Printf("[%s] Request cert error: %s", c.Tag, err)
} }
} }
nodeInfoChanged = true // add new node
} err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig)
// Update User if err != nil {
newUserInfo, err := c.apiClient.GetUserList() log.Printf("[%s] Add node error: %s", c.Tag, err)
if err != nil { return nil
log.Print(err) }
return nil
}
if nodeInfoChanged {
c.userList = newUserInfo
// Add new Limiter
l := limiter.AddLimiter(c.Tag, &c.LimitConfig, newUserInfo)
_, err = c.server.AddUsers(&vCore.AddUsersParams{ _, err = c.server.AddUsers(&vCore.AddUsersParams{
Tag: c.Tag, Tag: c.Tag,
Config: c.ControllerConfig, Config: c.ControllerConfig,
@ -92,12 +86,12 @@ func (c *Controller) nodeInfoMonitor() (err error) {
NodeInfo: newNodeInfo, NodeInfo: newNodeInfo,
}) })
if err != nil { if err != nil {
log.Print(err) log.Printf("[%s] Add users error: %s", c.Tag, err)
return nil return nil
} }
err = l.UpdateRule(newNodeInfo.Rules) err = l.UpdateRule(newNodeInfo.Rules)
if err != nil { if err != nil {
log.Printf("Update Rule error: %s", err) log.Printf("[%s] Update Rule error: %s", c.Tag, err)
} }
// Check interval // Check interval
if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.PullInterval && if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.PullInterval &&
@ -112,42 +106,49 @@ func (c *Controller) nodeInfoMonitor() (err error) {
c.userReportPeriodic.Close() c.userReportPeriodic.Close()
_ = c.userReportPeriodic.Start(false) _ = c.userReportPeriodic.Start(false)
} }
} else { c.nodeInfo = newNodeInfo
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.userList = newUserInfo 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 return nil
} }