2022-07-28 11:00:05 -04:00
|
|
|
package node
|
2022-06-01 13:35:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-16 01:04:33 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
2022-07-28 11:00:05 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/conf"
|
2022-08-12 06:02:06 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/core"
|
2022-08-16 01:04:33 -04:00
|
|
|
"github.com/xtls/xray-core/common/task"
|
2022-06-01 13:35:41 -04:00
|
|
|
"log"
|
2022-06-04 00:05:46 -04:00
|
|
|
"runtime"
|
2022-06-01 13:35:41 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
type Node struct {
|
2022-09-07 11:02:02 -04:00
|
|
|
server *core.Core
|
|
|
|
config *conf.ControllerConfig
|
|
|
|
clientInfo panel.ClientInfo
|
|
|
|
apiClient panel.Panel
|
|
|
|
nodeInfo *panel.NodeInfo
|
|
|
|
Tag string
|
|
|
|
userList []panel.UserInfo
|
|
|
|
nodeInfoMonitorPeriodic *task.Periodic
|
|
|
|
userReportPeriodic *task.Periodic
|
|
|
|
onlineIpReportPeriodic *task.Periodic
|
|
|
|
DynamicSpeedLimitPeriodic *task.Periodic
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
// New return a Node service with default parameters.
|
2022-08-16 01:04:33 -04:00
|
|
|
func New(server *core.Core, api panel.Panel, config *conf.ControllerConfig) *Node {
|
2022-07-28 11:00:05 -04:00
|
|
|
controller := &Node{
|
2022-06-01 13:35:41 -04:00
|
|
|
server: server,
|
|
|
|
config: config,
|
|
|
|
apiClient: api,
|
|
|
|
}
|
|
|
|
return controller
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implement the Start() function of the service interface
|
2022-07-28 11:00:05 -04:00
|
|
|
func (c *Node) Start() error {
|
2022-06-01 13:35:41 -04:00
|
|
|
c.clientInfo = c.apiClient.Describe()
|
|
|
|
// First fetch Node Info
|
|
|
|
newNodeInfo, err := c.apiClient.GetNodeInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.nodeInfo = newNodeInfo
|
|
|
|
c.Tag = c.buildNodeTag()
|
|
|
|
// Add new tag
|
|
|
|
err = c.addNewTag(newNodeInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Update user
|
2022-09-12 22:39:53 -04:00
|
|
|
c.userList, err = c.apiClient.GetUserList()
|
2022-06-01 13:35:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-12 22:39:53 -04:00
|
|
|
err = c.addNewUser(c.userList, newNodeInfo)
|
2022-06-01 13:35:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-12 22:39:53 -04:00
|
|
|
if err := c.server.AddInboundLimiter(c.Tag, c.nodeInfo); err != nil {
|
2022-06-02 13:22:56 -04:00
|
|
|
log.Print(err)
|
|
|
|
}
|
2022-06-01 13:35:41 -04:00
|
|
|
// Add Rule Manager
|
|
|
|
if !c.config.DisableGetRule {
|
2022-09-14 21:24:14 -04:00
|
|
|
if ruleList, err := c.apiClient.GetNodeRule(); err != nil {
|
2022-06-01 13:35:41 -04:00
|
|
|
log.Printf("Get rule list filed: %s", err)
|
2022-09-14 21:24:14 -04:00
|
|
|
} else if ruleList != nil {
|
2022-08-12 06:02:06 -04:00
|
|
|
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
|
2022-06-01 13:35:41 -04:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.nodeInfoMonitorPeriodic = &task.Periodic{
|
|
|
|
Interval: time.Duration(c.config.UpdatePeriodic) * time.Second,
|
|
|
|
Execute: c.nodeInfoMonitor,
|
|
|
|
}
|
|
|
|
c.userReportPeriodic = &task.Periodic{
|
|
|
|
Interval: time.Duration(c.config.UpdatePeriodic) * time.Second,
|
|
|
|
Execute: c.userInfoMonitor,
|
|
|
|
}
|
|
|
|
log.Printf("[%s: %d] Start monitor node status", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
|
|
|
// delay to start nodeInfoMonitor
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Duration(c.config.UpdatePeriodic) * time.Second)
|
|
|
|
_ = c.nodeInfoMonitorPeriodic.Start()
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Printf("[%s: %d] Start report node status", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
|
|
|
// delay to start userReport
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Duration(c.config.UpdatePeriodic) * time.Second)
|
|
|
|
_ = c.userReportPeriodic.Start()
|
|
|
|
}()
|
2022-08-19 10:42:51 -04:00
|
|
|
if c.config.EnableIpRecorder {
|
|
|
|
c.onlineIpReportPeriodic = &task.Periodic{
|
|
|
|
Interval: time.Duration(c.config.IpRecorderConfig.Periodic) * time.Second,
|
|
|
|
Execute: c.onlineIpReport,
|
|
|
|
}
|
2022-08-19 10:55:47 -04:00
|
|
|
go func() {
|
2022-09-07 11:02:02 -04:00
|
|
|
time.Sleep(time.Duration(c.config.IpRecorderConfig.Periodic) * time.Second)
|
2022-08-19 10:55:47 -04:00
|
|
|
_ = c.onlineIpReportPeriodic.Start()
|
|
|
|
}()
|
|
|
|
log.Printf("[%s: %d] Start report online ip", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
2022-09-07 11:02:02 -04:00
|
|
|
if c.config.EnableDynamicSpeedLimit {
|
|
|
|
c.DynamicSpeedLimitPeriodic = &task.Periodic{
|
|
|
|
Interval: time.Duration(c.config.DynamicSpeedLimitConfig.Periodic) * time.Second,
|
|
|
|
Execute: c.DynamicSpeedLimit,
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Duration(c.config.DynamicSpeedLimitConfig.Periodic) * time.Second)
|
|
|
|
_ = c.DynamicSpeedLimitPeriodic.Start()
|
|
|
|
}()
|
|
|
|
log.Printf("[%s: %d] Start dynamic speed limit", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
|
|
|
}
|
2022-06-05 01:19:39 -04:00
|
|
|
runtime.GC()
|
2022-06-01 13:35:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implement the Close() function of the service interface
|
2022-07-28 11:00:05 -04:00
|
|
|
func (c *Node) Close() error {
|
2022-06-01 13:35:41 -04:00
|
|
|
if c.nodeInfoMonitorPeriodic != nil {
|
|
|
|
err := c.nodeInfoMonitorPeriodic.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("node info periodic close failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.nodeInfoMonitorPeriodic != nil {
|
|
|
|
err := c.userReportPeriodic.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("user report periodic close failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
if c.onlineIpReportPeriodic != nil {
|
|
|
|
err := c.onlineIpReportPeriodic.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("online ip report periodic close failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2022-06-01 13:35:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
func (c *Node) buildNodeTag() string {
|
2022-06-01 13:35:41 -04:00
|
|
|
return fmt.Sprintf("%s_%s_%d", c.nodeInfo.NodeType, c.config.ListenIP, c.nodeInfo.NodeId)
|
|
|
|
}
|