2023-05-15 21:15:29 -04:00
|
|
|
package node
|
2022-09-14 21:55:11 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-06-07 13:18:56 -04:00
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
vCore "github.com/Yuzuki616/V2bX/core"
|
|
|
|
|
2022-09-14 21:55:11 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
2023-05-15 21:15:29 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/limiter"
|
|
|
|
"github.com/Yuzuki616/V2bX/node/lego"
|
2023-01-12 01:27:06 -05:00
|
|
|
"github.com/xtls/xray-core/common/task"
|
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
|
|
|
|
c.nodeInfoMonitorPeriodic = &task.Periodic{
|
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
|
|
|
|
c.userReportPeriodic = &task.Periodic{
|
2023-06-08 10:46:33 -04:00
|
|
|
Interval: c.nodeInfo.PushInterval,
|
2023-01-12 01:27:06 -05:00
|
|
|
Execute: c.reportUserTraffic,
|
|
|
|
}
|
2023-06-08 10:46:33 -04:00
|
|
|
log.Printf("[%s: %d] Start monitor node status", c.nodeInfo.Type, c.nodeInfo.Id)
|
2023-01-12 01:27:06 -05:00
|
|
|
// delay to start nodeInfoMonitor
|
|
|
|
go func() {
|
2023-06-08 10:46:33 -04:00
|
|
|
time.Sleep(c.nodeInfo.PullInterval)
|
2023-01-12 01:27:06 -05:00
|
|
|
_ = c.nodeInfoMonitorPeriodic.Start()
|
|
|
|
}()
|
2023-06-08 10:46:33 -04:00
|
|
|
log.Printf("[%s: %d] Start report node status", c.nodeInfo.Type, c.nodeInfo.Id)
|
2023-01-12 01:27:06 -05:00
|
|
|
// delay to start userReport
|
|
|
|
go func() {
|
2023-06-08 10:46:33 -04:00
|
|
|
time.Sleep(c.nodeInfo.PullInterval)
|
2023-01-12 01:27:06 -05:00
|
|
|
_ = c.userReportPeriodic.Start()
|
|
|
|
}()
|
2023-06-08 10:46:33 -04:00
|
|
|
if c.nodeInfo.Tls && c.CertConfig.CertMode != "none" &&
|
2023-01-12 01:27:06 -05:00
|
|
|
(c.CertConfig.CertMode == "dns" || c.CertConfig.CertMode == "http") {
|
|
|
|
c.renewCertPeriodic = &task.Periodic{
|
|
|
|
Interval: time.Hour * 24,
|
|
|
|
Execute: c.reportUserTraffic,
|
|
|
|
}
|
2023-06-08 10:46:33 -04:00
|
|
|
log.Printf("[%s: %d] Start renew cert", c.nodeInfo.Type, c.nodeInfo.Id)
|
2023-01-12 01:27:06 -05:00
|
|
|
// delay to start renewCert
|
|
|
|
go func() {
|
|
|
|
_ = c.renewCertPeriodic.Start()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08 10:46:33 -04:00
|
|
|
if newNodeInfo.Tls {
|
|
|
|
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
|
2022-12-18 10:31:06 -05:00
|
|
|
_ = c.nodeInfoMonitorPeriodic.Close()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(c.nodeInfoMonitorPeriodic.Interval)
|
|
|
|
_ = c.nodeInfoMonitorPeriodic.Start()
|
|
|
|
}()
|
|
|
|
}
|
2023-06-08 10:46:33 -04:00
|
|
|
if c.userReportPeriodic.Interval != newNodeInfo.PushInterval &&
|
|
|
|
newNodeInfo.PushInterval != 0 {
|
|
|
|
c.userReportPeriodic.Interval = newNodeInfo.PullInterval
|
2022-12-18 10:31:06 -05:00
|
|
|
_ = c.userReportPeriodic.Close()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(c.userReportPeriodic.Interval)
|
|
|
|
_ = c.userReportPeriodic.Start()
|
|
|
|
}()
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) reportUserTraffic() (err error) {
|
2022-09-14 21:55:11 -04:00
|
|
|
// Get User traffic
|
|
|
|
userTraffic := make([]panel.UserTraffic, 0)
|
|
|
|
for i := range c.userList {
|
2023-06-08 10:46:33 -04:00
|
|
|
up, down := c.server.GetUserTraffic(c.Tag, c.userList[i].Uuid, true)
|
2022-09-14 21:55:11 -04:00
|
|
|
if up > 0 || down > 0 {
|
2023-05-16 21:46:52 -04:00
|
|
|
if c.LimitConfig.EnableDynamicSpeedLimit {
|
2022-09-14 21:55:11 -04:00
|
|
|
c.userList[i].Traffic += up + down
|
|
|
|
}
|
|
|
|
userTraffic = append(userTraffic, panel.UserTraffic{
|
2022-12-18 10:31:06 -05:00
|
|
|
UID: (c.userList)[i].Id,
|
2022-09-14 21:55:11 -04:00
|
|
|
Upload: up,
|
|
|
|
Download: down})
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
if len(userTraffic) > 0 && !c.DisableUploadTraffic {
|
2022-09-14 21:55:11 -04:00
|
|
|
err = c.apiClient.ReportUserTraffic(userTraffic)
|
|
|
|
if err != nil {
|
2022-10-09 20:59:00 -04:00
|
|
|
log.Printf("Report user traffic faild: %s", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
} else {
|
2023-06-08 10:46:33 -04:00
|
|
|
log.Printf("[%s: %d] Report %d online users", c.nodeInfo.Type, c.nodeInfo.Id, len(userTraffic))
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
userTraffic = nil
|
|
|
|
runtime.GC()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) RenewCert() {
|
2023-01-12 01:27:06 -05:00
|
|
|
l, err := lego.New(c.CertConfig)
|
|
|
|
if err != nil {
|
2023-06-08 10:46:33 -04:00
|
|
|
log.Print("new lego error: ", err)
|
2023-01-12 01:27:06 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = l.RenewCert()
|
|
|
|
if err != nil {
|
2023-06-08 10:46:33 -04:00
|
|
|
log.Print("renew cert error: ", err)
|
2023-01-12 01:27:06 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|