V2bX/node/task.go

242 lines
5.9 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-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/api/panel"
"github.com/InazumaV/V2bX/common/task"
vCore "github.com/InazumaV/V2bX/core"
"github.com/InazumaV/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-08-19 08:06:42 -04:00
if node.Security == panel.Tls {
2023-06-15 22:04:39 -04:00
switch c.CertConfig.CertMode {
2023-08-19 08:06:42 -04:00
case "none", "", "file", "self":
2023-06-15 22:04:39 -04:00
default:
c.renewCertPeriodic = &task.Task{
Interval: time.Hour * 24,
2023-07-27 19:06:45 -04:00
Execute: c.renewCertTask,
2023-06-15 22:04:39 -04:00
}
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)
2023-07-24 04:58:26 -04:00
c.dynamicSpeedLimitPeriodic = &task.Task{
Interval: time.Duration(c.LimitConfig.DynamicSpeedLimitConfig.Periodic) * time.Second,
Execute: c.SpeedChecker,
2023-07-21 14:38:07 -04:00
}
2023-07-27 13:07:56 -04:00
log.Printf("[%s: %d] Start dynamic speed limit", c.apiClient.NodeType, c.apiClient.NodeId)
2023-07-21 14:38:07 -04:00
}
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
2023-08-19 08:06:42 -04:00
newN, err := c.apiClient.GetNodeInfo()
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("Get node info failed")
2023-06-18 21:24:05 -04:00
return nil
}
// get user info
2023-08-19 08:06:42 -04:00
newU, err := c.apiClient.GetUserList()
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("Get user list failed")
2022-09-14 21:55:11 -04:00
return nil
}
2024-08-29 17:48:41 -04:00
// get user alive
newA, err := c.apiClient.GetUserAlive()
if err != nil {
2024-12-01 18:38:23 -05:00
log.WithFields(log.Fields{
"tag": c.tag,
"err": err,
}).Error("Get alive list failed")
return nil
2024-08-29 17:48:41 -04:00
}
2023-08-19 08:06:42 -04:00
if newN != nil {
c.info = newN
2023-06-18 21:24:05 -04:00
// nodeInfo changed
2023-08-19 08:06:42 -04:00
if newU != nil {
c.userList = newU
2023-07-21 14:45:08 -04:00
}
2023-07-21 14:38:07 -04:00
c.traffic = make(map[string]int64)
2023-09-12 13:44:03 -04:00
// Remove old node
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
}
2023-09-12 13:44:03 -04:00
// Update limiter
if len(c.Options.Name) == 0 {
c.tag = c.buildNodeTag(newN)
// Remove Old limiter
limiter.DeleteLimiter(c.tag)
// Add new Limiter
2024-08-29 17:48:41 -04:00
l := limiter.AddLimiter(c.tag, &c.LimitConfig, c.userList, newA)
2023-09-12 13:44:03 -04:00
c.limiter = l
}
2024-12-01 18:38:23 -05:00
// update alive list
if newA != nil {
c.limiter.AliveList = newA
}
2023-09-12 13:44:03 -04:00
// Update rule
err = c.limiter.UpdateRule(&newN.Rules)
if err != nil {
log.WithFields(log.Fields{
"tag": c.tag,
"err": err,
}).Error("Update Rule failed")
return nil
}
2023-06-18 21:24:05 -04:00
// check cert
2023-08-19 08:06:42 -04:00
if newN.Security == panel.Tls {
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-08-19 08:06:42 -04:00
err = c.server.AddNode(c.tag, newN, c.Options)
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,
2023-08-19 08:06:42 -04:00
Users: c.userList,
NodeInfo: newN,
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
}
// Check interval
2023-08-19 08:06:42 -04:00
if c.nodeInfoMonitorPeriodic.Interval != newN.PullInterval &&
newN.PullInterval != 0 {
c.nodeInfoMonitorPeriodic.Interval = newN.PullInterval
2023-06-15 22:04:39 -04:00
c.nodeInfoMonitorPeriodic.Close()
_ = c.nodeInfoMonitorPeriodic.Start(false)
}
2023-08-19 08:06:42 -04:00
if c.userReportPeriodic.Interval != newN.PushInterval &&
newN.PushInterval != 0 {
c.userReportPeriodic.Interval = newN.PullInterval
2023-06-15 22:04:39 -04:00
c.userReportPeriodic.Close()
_ = c.userReportPeriodic.Start(false)
}
2023-07-21 14:45:08 -04:00
log.WithField("tag", c.tag).Infof("Added %d new users", len(c.userList))
2023-06-18 21:24:05 -04:00
// exit
return nil
}
2024-08-29 17:48:41 -04:00
// update alive list
if newA != nil {
c.limiter.AliveList = newA
}
2023-06-18 21:24:05 -04:00
// node no changed, check users
2023-08-19 08:06:42 -04:00
if len(newU) == 0 {
2023-07-21 14:45:08 -04:00
return nil
}
2023-08-19 08:06:42 -04:00
deleted, added := compareUserList(c.userList, newU)
2023-06-18 21:24:05 -04:00
if len(deleted) > 0 {
// have deleted users
2025-01-10 02:33:05 -05:00
err = c.server.DelUsers(deleted, c.tag, c.info)
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-07-29 06:56:05 -04:00
NodeInfo: c.info,
2023-08-19 08:06:42 -04:00
Users: added,
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 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-08-19 08:06:42 -04:00
c.userList = newU
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
2023-07-24 04:58:26 -04:00
func (c *Controller) SpeedChecker() error {
2023-07-21 14:38:07 -04:00
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)
}
}
2023-07-24 04:58:26 -04:00
return nil
2023-07-21 14:38:07 -04:00
}