V2bX/node/task.go

154 lines
3.8 KiB
Go
Raw Normal View History

package node
2022-09-14 21:55:11 -04:00
import (
"fmt"
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-15 22:04:39 -04:00
"log"
"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-15 22:04:39 -04:00
log.Printf("[%s] Start monitor node status", c.Tag)
2023-01-12 01:27:06 -05:00
// delay to start nodeInfoMonitor
2023-06-15 22:04:39 -04:00
_ = c.nodeInfoMonitorPeriodic.Start(false)
log.Printf("[%s] Start report node status", c.Tag)
_ = 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,
}
log.Printf("[%s] Start renew cert", c.Tag)
// delay to start renewCert
_ = c.renewCertPeriodic.Start(true)
2023-01-12 01:27:06 -05:00
}
}
}
func (c *Controller) nodeInfoMonitor() (err error) {
2022-09-14 21:55:11 -04:00
// First fetch Node Info
newNodeInfo, err := c.apiClient.GetNodeInfo()
if err != nil {
log.Print(err)
return nil
}
var nodeInfoChanged = false
// If nodeInfo changed
if newNodeInfo != nil {
// Remove old tag
oldTag := c.Tag
2023-05-22 09:01:31 -04:00
err := c.server.DelNode(oldTag)
if err != nil {
log.Print(err)
return nil
2022-09-14 21:55:11 -04:00
}
// Remove Old limiter
limiter.DeleteLimiter(oldTag)
// Add new tag
c.nodeInfo = newNodeInfo
c.Tag = c.buildNodeTag()
2023-05-22 09:01:31 -04:00
err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig)
if err != nil {
log.Print(err)
return nil
}
2023-06-17 15:37:27 -04:00
if c.nodeInfo.Tls || c.nodeInfo.Type == "hysteria" {
err = c.requestCert()
if err != nil {
return fmt.Errorf("request cert error: %s", err)
}
}
nodeInfoChanged = true
2022-09-14 21:55:11 -04:00
}
// 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)
_, 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 {
log.Print(err)
return nil
}
err = l.UpdateRule(newNodeInfo.Rules)
if err != nil {
log.Printf("Update Rule error: %s", err)
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)
}
2022-09-14 21:55:11 -04:00
} 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)
2022-09-14 21:55:11 -04:00
}
err := c.server.DelUsers(deletedEmail, c.Tag)
2022-09-14 21:55:11 -04:00
if err != nil {
log.Print(err)
}
}
if len(added) > 0 {
_, err := c.server.AddUsers(&vCore.AddUsersParams{
2023-05-22 09:01:31 -04:00
Tag: c.Tag,
Config: c.ControllerConfig,
UserInfo: added,
NodeInfo: c.nodeInfo,
})
2022-09-14 21:55:11 -04:00
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)
2022-09-14 21:55:11 -04:00
}
}
log.Printf("[%s: %d] %d user deleted, %d user added", c.nodeInfo.Type, c.nodeInfo.Id,
2022-09-14 21:55:11 -04:00
len(deleted), len(added))
c.userList = newUserInfo
}
return nil
}