V2bX/node/node.go

161 lines
4.7 KiB
Go
Raw Normal View History

package node
2022-06-01 13:35:41 -04:00
import (
"errors"
2022-06-01 13:35:41 -04:00
"fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core"
"github.com/xtls/xray-core/common/task"
2022-06-01 13:35:41 -04:00
"log"
"time"
)
type Node struct {
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
}
// New return a Node service with default parameters.
func New(server *core.Core, api panel.Panel, config *conf.ControllerConfig) *Node {
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
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 fmt.Errorf("get node info failed: %s", err)
2022-06-01 13:35:41 -04:00
}
c.nodeInfo = newNodeInfo
c.Tag = c.buildNodeTag()
// Add new tag
err = c.addNewTag(newNodeInfo)
if err != nil {
return fmt.Errorf("add new tag failed: %s", err)
2022-06-01 13:35:41 -04:00
}
// 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 fmt.Errorf("get user list failed: %s", err)
}
if len(c.userList) == 0 {
return errors.New("add users failed: not have any user")
2022-06-01 13:35:41 -04:00
}
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 {
return fmt.Errorf("add inbound limiter failed: %s", err)
2022-06-02 13:22:56 -04:00
}
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 {
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
log.Printf("Update rule filed: %s", err)
2022-06-01 13:35:41 -04:00
}
}
}
// fetch node info task
2022-06-01 13:35:41 -04:00
c.nodeInfoMonitorPeriodic = &task.Periodic{
Interval: time.Duration(c.config.UpdatePeriodic) * time.Second,
Execute: c.nodeInfoMonitor,
}
// fetch user list task
2022-06-01 13:35:41 -04:00
c.userReportPeriodic = &task.Periodic{
Interval: time.Duration(c.config.UpdatePeriodic) * time.Second,
Execute: c.reportUserTraffic,
2022-06-01 13:35:41 -04:00
}
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 {
// report and fetch online ip list task
2022-08-19 10:42:51 -04:00
c.onlineIpReportPeriodic = &task.Periodic{
Interval: time.Duration(c.config.IpRecorderConfig.Periodic) * time.Second,
Execute: c.reportOnlineIp,
2022-08-19 10:42:51 -04:00
}
2022-08-19 10:55:47 -04:00
go func() {
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)
}
if c.config.EnableDynamicSpeedLimit {
// Check dynamic speed limit task
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-01 13:35:41 -04:00
return nil
}
// Close implement the Close() function of the service interface
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)
}
}
if c.onlineIpReportPeriodic != nil {
err := c.onlineIpReportPeriodic.Close()
if err != nil {
log.Panicf("online ip report periodic close failed: %s", err)
}
}
if c.DynamicSpeedLimitPeriodic != nil {
err := c.DynamicSpeedLimitPeriodic.Close()
if err != nil {
log.Panicf("dynamic speed limit periodic close failed: %s", err)
}
}
2022-06-01 13:35:41 -04:00
return nil
}
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)
}