V2bX/node/task.go

149 lines
3.9 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-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) {
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-18 21:24:05 -04:00
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)
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-18 23:08:15 -04:00
log.Printf("[%s] Node changed, reload...", c.Tag)
2023-06-18 21:24:05 -04:00
err = c.server.DelNode(c.Tag)
if err != nil {
2023-06-18 21:24:05 -04:00
log.Printf("[%s] Del node error: %s", c.Tag, err)
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-18 21:24:05 -04:00
log.Printf("[%s] Request cert error: %s", c.Tag, err)
}
}
2023-06-18 21:24:05 -04:00
// add new node
err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig)
if err != nil {
log.Printf("[%s] Add node error: %s", c.Tag, err)
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-18 21:24:05 -04:00
log.Printf("[%s] Add users error: %s", c.Tag, err)
2022-09-14 21:55:11 -04:00
return nil
}
err = l.UpdateRule(newNodeInfo.Rules)
if err != nil {
2023-06-18 21:24:05 -04:00
log.Printf("[%s] Update Rule error: %s", c.Tag, 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)
}
2023-06-18 21:24:05 -04:00
c.nodeInfo = newNodeInfo
c.userList = newUserInfo
2023-06-18 23:08:15 -04:00
log.Printf("[%s] Added %d new users", c.Tag, 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 {
log.Printf("[%s] Del users error: %s", c.Tag, err)
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 {
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)
2022-09-14 21:55:11 -04:00
}
}
2023-06-18 21:24:05 -04:00
c.userList = newUserInfo
log.Printf("[%s] %d user deleted, %d user added", c.Tag,
len(deleted), len(added))
2022-09-14 21:55:11 -04:00
return nil
}