2023-05-15 21:15:29 -04:00
|
|
|
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"
|
2023-06-07 13:18:56 -04:00
|
|
|
vCore "github.com/Yuzuki616/V2bX/core"
|
2023-05-15 21:15:29 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/limiter"
|
2023-06-15 22:04:39 -04:00
|
|
|
"log"
|
|
|
|
"time"
|
2022-09-14 21:55:11 -04:00
|
|
|
)
|
|
|
|
|
2023-05-15 21:15:29 -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{
|
2023-06-08 10:46:33 -04:00
|
|
|
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{
|
2023-06-08 10:46:33 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04: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 {
|
2022-12-18 10:31:06 -05:00
|
|
|
// Remove old tag
|
|
|
|
oldTag := c.Tag
|
2023-05-22 09:01:31 -04:00
|
|
|
err := c.server.DelNode(oldTag)
|
2022-12-18 10:31:06 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
2023-05-15 21:15:29 -04:00
|
|
|
// Remove Old limiter
|
|
|
|
limiter.DeleteLimiter(oldTag)
|
2022-12-18 10:31:06 -05:00
|
|
|
// 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)
|
2022-12-18 10:31:06 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-17 15:37:27 -04:00
|
|
|
if c.nodeInfo.Tls || c.nodeInfo.Type == "hysteria" {
|
2023-06-08 10:46:33 -04:00
|
|
|
err = c.requestCert()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("request cert error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
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
|
2023-05-15 21:15:29 -04:00
|
|
|
// Add new Limiter
|
2023-05-16 21:46:52 -04:00
|
|
|
l := limiter.AddLimiter(c.Tag, &c.LimitConfig, newUserInfo)
|
2023-06-07 13:18:56 -04:00
|
|
|
_, err = c.server.AddUsers(&vCore.AddUsersParams{
|
2023-06-08 10:46:33 -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 {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-15 21:15:29 -04:00
|
|
|
err = l.UpdateRule(newNodeInfo.Rules)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Update Rule error: %s", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
// Check interval
|
2023-06-08 10:46:33 -04:00
|
|
|
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)
|
2022-12-18 10:31:06 -05:00
|
|
|
}
|
2023-06-08 10:46:33 -04:00
|
|
|
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-12-18 10:31:06 -05:00
|
|
|
}
|
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 {
|
2022-12-18 10:31:06 -05:00
|
|
|
deletedEmail[i] = fmt.Sprintf("%s|%s|%d",
|
|
|
|
c.Tag,
|
|
|
|
(deleted)[i].Uuid,
|
|
|
|
(deleted)[i].Id)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
2023-06-07 13:18:56 -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 {
|
2023-06-07 13:18:56 -04:00
|
|
|
_, 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
|
2023-05-15 21:15:29 -04:00
|
|
|
err = limiter.UpdateLimiter(c.Tag, added, deleted)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("update limiter:", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
}
|
2023-06-08 10:46:33 -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
|
|
|
|
}
|