V2bX/node/task.go

186 lines
4.6 KiB
Go
Raw Normal View History

package node
2022-09-14 21:55:11 -04:00
import (
2023-06-15 22:04:39 -04:00
"github.com/Yuzuki616/V2bX/common/task"
vCore "github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/limiter"
2023-06-29 23:07:27 -04:00
log "github.com/sirupsen/logrus"
2023-06-15 22:04:39 -04:00
"time"
2022-09-14 21:55:11 -04:00
)
func (c *Controller) initTask() {
2023-01-12 01:27:06 -05:00
// fetch node info task
2023-06-15 22:04:39 -04:00
c.nodeInfoMonitorPeriodic = &task.Task{
Interval: c.nodeInfo.PullInterval,
2023-01-12 01:27:06 -05:00
Execute: c.nodeInfoMonitor,
}
// fetch user list task
2023-06-15 22:04:39 -04:00
c.userReportPeriodic = &task.Task{
Interval: c.nodeInfo.PushInterval,
2023-06-15 22:04:39 -04:00
Execute: c.reportUserTrafficTask,
2023-01-12 01:27:06 -05:00
}
2023-06-29 23:07:27 -04:00
log.WithField("tag", c.Tag).Info("Start monitor node status")
2023-01-12 01:27:06 -05:00
// delay to start nodeInfoMonitor
2023-06-15 22:04:39 -04:00
_ = c.nodeInfoMonitorPeriodic.Start(false)
2023-06-29 23:07:27 -04:00
log.WithField("tag", c.Tag).Info("Start report node status")
2023-06-15 22:04:39 -04:00
_ = c.userReportPeriodic.Start(false)
if c.nodeInfo.Tls {
switch c.CertConfig.CertMode {
case "reality", "none", "":
default:
c.renewCertPeriodic = &task.Task{
Interval: time.Hour * 24,
Execute: c.reportUserTrafficTask,
}
2023-06-29 23:07:27 -04:00
log.WithField("tag", c.Tag).Info("Start renew cert")
2023-06-15 22:04:39 -04:00
// delay to start renewCert
_ = c.renewCertPeriodic.Start(true)
2023-01-12 01:27:06 -05:00
}
}
}
func (c *Controller) nodeInfoMonitor() (err error) {
2023-06-18 21:24:05 -04:00
// get node info
2022-09-14 21:55:11 -04:00
newNodeInfo, err := c.apiClient.GetNodeInfo()
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Get node info failed")
2023-06-18 21:24:05 -04:00
return nil
}
// get user info
newUserInfo, err := c.apiClient.GetUserList()
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Get user list failed")
2022-09-14 21:55:11 -04:00
return nil
}
if newNodeInfo != nil {
2023-06-18 21:24:05 -04:00
// nodeInfo changed
// Remove old tag
2023-06-29 23:07:27 -04:00
log.WithField("tag", c.Tag).Info("Node changed, reload")
2023-06-18 21:24:05 -04:00
err = c.server.DelNode(c.Tag)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Delete node failed")
return nil
2022-09-14 21:55:11 -04:00
}
// Remove Old limiter
2023-06-18 21:24:05 -04:00
limiter.DeleteLimiter(c.Tag)
// Add new Limiter
c.Tag = c.buildNodeTag()
2023-06-18 21:24:05 -04:00
l := limiter.AddLimiter(c.Tag, &c.LimitConfig, newUserInfo)
// check cert
if newNodeInfo.Tls || newNodeInfo.Type == "hysteria" {
err = c.requestCert()
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Request cert failed")
return nil
}
}
2023-06-18 21:24:05 -04:00
// add new node
err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Add node failed")
2023-06-18 21:24:05 -04:00
return nil
}
_, err = c.server.AddUsers(&vCore.AddUsersParams{
Tag: c.Tag,
Config: c.ControllerConfig,
UserInfo: newUserInfo,
NodeInfo: newNodeInfo,
2023-05-22 09:01:31 -04:00
})
2022-09-14 21:55:11 -04:00
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Add users failed")
2022-09-14 21:55:11 -04:00
return nil
}
err = l.UpdateRule(newNodeInfo.Rules)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Update Rule failed")
return nil
2022-09-14 21:55:11 -04:00
}
// Check interval
if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.PullInterval &&
newNodeInfo.PullInterval != 0 {
c.nodeInfoMonitorPeriodic.Interval = newNodeInfo.PullInterval
2023-06-15 22:04:39 -04:00
c.nodeInfoMonitorPeriodic.Close()
_ = c.nodeInfoMonitorPeriodic.Start(false)
}
if c.userReportPeriodic.Interval != newNodeInfo.PushInterval &&
newNodeInfo.PushInterval != 0 {
c.userReportPeriodic.Interval = newNodeInfo.PullInterval
2023-06-15 22:04:39 -04:00
c.userReportPeriodic.Close()
_ = c.userReportPeriodic.Start(false)
}
2023-06-18 21:24:05 -04:00
c.nodeInfo = newNodeInfo
c.userList = newUserInfo
2023-06-29 23:07:27 -04:00
log.WithField("tag", c.Tag).Infof("Added %d new users", len(newUserInfo))
2023-06-18 21:24:05 -04:00
// exit
return nil
}
// node no changed, check users
deleted, added := compareUserList(c.userList, newUserInfo)
if len(deleted) > 0 {
// have deleted users
2023-06-18 22:31:42 -04:00
err = c.server.DelUsers(deleted, c.Tag)
2023-06-18 21:24:05 -04:00
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Delete users failed")
return nil
2022-09-14 21:55:11 -04:00
}
2023-06-18 21:24:05 -04:00
}
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 {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("Add users failed")
return nil
2023-06-18 21:24:05 -04:00
}
}
if len(added) > 0 || len(deleted) > 0 {
// update Limiter
err = limiter.UpdateLimiter(c.Tag, added, deleted)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
"tag": c.Tag,
"err": err,
}).Error("limiter users failed")
return nil
2022-09-14 21:55:11 -04:00
}
}
2023-06-18 21:24:05 -04:00
c.userList = newUserInfo
2023-07-12 13:19:13 -04:00
if len(added)+len(deleted) != 0 {
log.WithField("tag", c.Tag).
Infof("%d user deleted, %d user added", len(deleted), len(added))
}
2022-09-14 21:55:11 -04:00
return nil
}