2023-05-15 21:15:29 -04:00
|
|
|
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"
|
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-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,
|
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-01-12 01:27:06 -05:00
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04: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:45:08 -04:00
|
|
|
if newUserInfo != nil {
|
|
|
|
c.userList = newUserInfo
|
|
|
|
}
|
2023-07-21 14:38:07 -04:00
|
|
|
c.traffic = make(map[string]int64)
|
2022-12-18 10:31:06 -05:00
|
|
|
// 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)
|
2022-12-18 10:31:06 -05: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 node failed")
|
2022-12-18 10:31:06 -05:00
|
|
|
return nil
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
2023-05-15 21:15:29 -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)
|
2023-07-21 14:45:08 -04:00
|
|
|
l := limiter.AddLimiter(c.tag, &c.LimitConfig, c.userList)
|
2023-06-18 21:24:05 -04:00
|
|
|
// check cert
|
|
|
|
if newNodeInfo.Tls || newNodeInfo.Type == "hysteria" {
|
2023-06-08 10:46:33 -04:00
|
|
|
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-08 10:46:33 -04:00
|
|
|
}
|
|
|
|
}
|
2023-06-18 21:24:05 -04:00
|
|
|
// add new node
|
2023-07-29 06:47:47 -04:00
|
|
|
err = c.server.AddNode(c.tag, newNodeInfo, 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
|
|
|
|
}
|
2023-06-07 13:18:56 -04:00
|
|
|
_, err = c.server.AddUsers(&vCore.AddUsersParams{
|
2023-07-21 14:38:07 -04:00
|
|
|
Tag: c.tag,
|
2023-07-29 06:47:47 -04:00
|
|
|
Config: c.Options,
|
2023-07-21 14:45:08 -04:00
|
|
|
UserInfo: c.userList,
|
2023-06-08 10:46:33 -04:00
|
|
|
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)
|
2023-05-15 21:15:29 -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("Update Rule failed")
|
|
|
|
return nil
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
2023-07-21 14:38:07 -04:00
|
|
|
c.limiter = l
|
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
|
|
|
}
|
2023-07-21 14:45:08 -04:00
|
|
|
log.WithField("tag", c.tag).Infof("Added %d new users", len(c.userList))
|
2023-07-29 06:56:05 -04:00
|
|
|
c.info = newNodeInfo
|
2023-06-18 21:24:05 -04:00
|
|
|
// exit
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// node no changed, check users
|
2023-07-21 14:45:08 -04:00
|
|
|
if len(newUserInfo) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-18 21:24:05 -04:00
|
|
|
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-07-29 06:47:47 -04:00
|
|
|
Config: c.Options,
|
2023-07-29 06:56:05 -04:00
|
|
|
NodeInfo: c.info,
|
2023-06-18 21:24:05 -04:00
|
|
|
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
|
|
|
|
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
|
|
|
}
|