V2bX/node/task.go

213 lines
5.4 KiB
Go
Raw Normal View History

package node
2022-09-14 21:55:11 -04:00
import (
2023-07-20 09:14:18 -04:00
"time"
2023-07-21 14:38:07 -04:00
"github.com/Yuzuki616/V2bX/api/panel"
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"
2022-09-14 21:55:11 -04:00
)
2023-07-21 14:38:07 -04:00
func (c *Controller) startTasks(node *panel.NodeInfo) {
2023-01-12 01:27:06 -05:00
// fetch node info task
2023-06-15 22:04:39 -04:00
c.nodeInfoMonitorPeriodic = &task.Task{
2023-07-21 14:38:07 -04:00
Interval: node.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{
2023-07-21 14:38:07 -04:00
Interval: node.PushInterval,
2023-06-15 22:04:39 -04:00
Execute: c.reportUserTrafficTask,
2023-01-12 01:27:06 -05:00
}
2023-07-21 14:38:07 -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-07-21 14:38:07 -04:00
log.WithField("tag", c.tag).Info("Start report node status")
2023-06-15 22:04:39 -04:00
_ = c.userReportPeriodic.Start(false)
2023-07-21 14:38:07 -04:00
if node.Tls {
2023-06-15 22:04:39 -04:00
switch c.CertConfig.CertMode {
case "reality", "none", "":
default:
c.renewCertPeriodic = &task.Task{
Interval: time.Hour * 24,
Execute: c.reportUserTrafficTask,
}
2023-07-21 14:38:07 -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
}
}
2023-07-21 14:38:07 -04:00
if c.LimitConfig.EnableDynamicSpeedLimit {
c.traffic = make(map[string]int64)
c.renewCertPeriodic = &task.Task{
Interval: time.Duration(c.LimitConfig.DynamicSpeedLimitConfig.Periodic) * time.Minute,
Execute: c.reportUserTrafficTask,
}
}
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{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"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{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"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
2023-07-21 14:38:07 -04:00
c.traffic = make(map[string]int64)
// Remove old tag
2023-07-21 14:38:07 -04:00
log.WithField("tag", c.tag).Info("Node changed, reload")
err = c.server.DelNode(c.tag)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"err": err,
}).Error("Delete node failed")
return nil
2022-09-14 21:55:11 -04:00
}
// Remove Old limiter
2023-07-21 14:38:07 -04:00
limiter.DeleteLimiter(c.tag)
2023-06-18 21:24:05 -04:00
// Add new Limiter
2023-07-21 14:38:07 -04:00
c.tag = c.buildNodeTag(newNodeInfo)
l := limiter.AddLimiter(c.tag, &c.LimitConfig, newUserInfo)
2023-06-18 21:24:05 -04:00
// 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{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"err": err,
}).Error("Request cert failed")
return nil
}
}
2023-06-18 21:24:05 -04:00
// add new node
2023-07-21 14:38:07 -04:00
err = c.server.AddNode(c.tag, newNodeInfo, c.ControllerConfig)
2023-06-18 21:24:05 -04:00
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"err": err,
}).Error("Add node failed")
2023-06-18 21:24:05 -04:00
return nil
}
_, err = c.server.AddUsers(&vCore.AddUsersParams{
2023-07-21 14:38:07 -04:00
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{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"err": err,
}).Error("Add users failed")
2022-09-14 21:55:11 -04:00
return nil
}
2023-07-20 09:14:18 -04:00
err = l.UpdateRule(&newNodeInfo.Rules)
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"err": err,
}).Error("Update Rule failed")
return nil
2022-09-14 21:55:11 -04:00
}
2023-07-21 14:38:07 -04:00
c.limiter = l
// 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.userList = newUserInfo
2023-07-21 14:38:07 -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-07-21 14:38:07 -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{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"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{
2023-07-21 14:38:07 -04:00
Tag: c.tag,
2023-06-18 21:24:05 -04:00
Config: c.ControllerConfig,
UserInfo: added,
})
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"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
2023-07-21 14:38:07 -04:00
c.limiter.UpdateUser(c.tag, added, deleted)
2023-06-18 21:24:05 -04:00
if err != nil {
2023-06-29 23:07:27 -04:00
log.WithFields(log.Fields{
2023-07-21 14:38:07 -04:00
"tag": c.tag,
2023-06-29 23:07:27 -04:00
"err": err,
}).Error("limiter users failed")
return nil
2022-09-14 21:55:11 -04:00
}
2023-07-21 14:38:07 -04:00
// clear traffic record
if c.LimitConfig.EnableDynamicSpeedLimit {
for i := range deleted {
delete(c.traffic, deleted[i].Uuid)
}
}
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 {
2023-07-21 14:38:07 -04:00
log.WithField("tag", c.tag).
2023-07-12 13:19:13 -04:00
Infof("%d user deleted, %d user added", len(deleted), len(added))
}
2022-09-14 21:55:11 -04:00
return nil
}
2023-07-21 14:38:07 -04:00
func (c *Controller) SpeedChecker() {
for u, t := range c.traffic {
if t >= c.LimitConfig.DynamicSpeedLimitConfig.Traffic {
err := c.limiter.UpdateDynamicSpeedLimit(c.tag, u,
c.LimitConfig.DynamicSpeedLimitConfig.SpeedLimit,
time.Now().Add(time.Duration(c.LimitConfig.DynamicSpeedLimitConfig.ExpireTime)*time.Minute))
log.WithField("err", err).Error("Update dynamic speed limit failed")
delete(c.traffic, u)
}
}
}