2022-10-09 23:31:15 -04:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-10-20 23:07:04 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/iprecoder"
|
2022-10-09 23:31:15 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
|
|
|
"github.com/Yuzuki616/V2bX/conf"
|
|
|
|
"github.com/Yuzuki616/V2bX/core"
|
|
|
|
"github.com/xtls/xray-core/common/task"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
server *core.Core
|
|
|
|
clientInfo panel.ClientInfo
|
|
|
|
apiClient panel.Panel
|
|
|
|
nodeInfo *panel.NodeInfo
|
|
|
|
Tag string
|
|
|
|
userList []panel.UserInfo
|
2022-10-20 23:07:04 -04:00
|
|
|
ipRecorder iprecoder.IpRecorder
|
2022-10-09 23:31:15 -04:00
|
|
|
nodeInfoMonitorPeriodic *task.Periodic
|
|
|
|
userReportPeriodic *task.Periodic
|
|
|
|
onlineIpReportPeriodic *task.Periodic
|
|
|
|
DynamicSpeedLimitPeriodic *task.Periodic
|
2022-12-18 10:31:06 -05:00
|
|
|
*conf.ControllerConfig
|
2022-10-09 23:31:15 -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-12-18 10:31:06 -05:00
|
|
|
server: server,
|
|
|
|
ControllerConfig: config,
|
|
|
|
apiClient: api,
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
|
|
|
return controller
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implement the Start() function of the service interface
|
|
|
|
func (c *Node) Start() error {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
// Update user
|
|
|
|
c.userList, err = c.apiClient.GetUserList()
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
err = c.addNewUser(c.userList, newNodeInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
if err := c.server.AddInboundLimiter(c.Tag, c.nodeInfo, c.userList); err != nil {
|
2022-10-09 23:31:15 -04:00
|
|
|
return fmt.Errorf("add inbound limiter failed: %s", err)
|
|
|
|
}
|
|
|
|
// Add Rule Manager
|
2022-12-18 10:31:06 -05:00
|
|
|
if !c.DisableGetRule {
|
|
|
|
if err := c.server.UpdateRule(c.Tag, newNodeInfo.Rules); err != nil {
|
|
|
|
log.Printf("Update rule filed: %s", err)
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// fetch node info task
|
|
|
|
c.nodeInfoMonitorPeriodic = &task.Periodic{
|
2022-12-18 10:31:06 -05:00
|
|
|
Interval: time.Duration(c.nodeInfo.BaseConfig.PullInterval.(int)) * time.Second,
|
2022-10-09 23:31:15 -04:00
|
|
|
Execute: c.nodeInfoMonitor,
|
|
|
|
}
|
|
|
|
// fetch user list task
|
|
|
|
c.userReportPeriodic = &task.Periodic{
|
2022-12-18 10:31:06 -05:00
|
|
|
Interval: time.Duration(c.nodeInfo.BaseConfig.PushInterval.(int)) * time.Second,
|
2022-10-09 23:31:15 -04:00
|
|
|
Execute: c.reportUserTraffic,
|
|
|
|
}
|
|
|
|
log.Printf("[%s: %d] Start monitor node status", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
|
|
|
// delay to start nodeInfoMonitor
|
|
|
|
go func() {
|
2022-12-18 10:31:06 -05:00
|
|
|
time.Sleep(time.Duration(c.nodeInfo.BaseConfig.PullInterval.(int)) * time.Second)
|
2022-10-09 23:31:15 -04:00
|
|
|
_ = c.nodeInfoMonitorPeriodic.Start()
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Printf("[%s: %d] Start report node status", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
|
|
|
// delay to start userReport
|
|
|
|
go func() {
|
2022-12-18 10:31:06 -05:00
|
|
|
time.Sleep(time.Duration(c.nodeInfo.BaseConfig.PushInterval.(int)) * time.Second)
|
2022-10-09 23:31:15 -04:00
|
|
|
_ = c.userReportPeriodic.Start()
|
|
|
|
}()
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.EnableDynamicSpeedLimit {
|
2022-11-08 04:01:12 -05:00
|
|
|
// Check dynamic speed limit task
|
|
|
|
c.DynamicSpeedLimitPeriodic = &task.Periodic{
|
2022-12-18 10:31:06 -05:00
|
|
|
Interval: time.Duration(c.DynamicSpeedLimitConfig.Periodic) * time.Second,
|
2022-11-08 04:01:12 -05:00
|
|
|
Execute: c.dynamicSpeedLimit,
|
|
|
|
}
|
|
|
|
go func() {
|
2022-12-18 10:31:06 -05:00
|
|
|
time.Sleep(time.Duration(c.DynamicSpeedLimitConfig.Periodic) * time.Second)
|
2022-11-08 04:01:12 -05:00
|
|
|
_ = c.DynamicSpeedLimitPeriodic.Start()
|
|
|
|
}()
|
|
|
|
log.Printf("[%s: %d] Start dynamic speed limit", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.EnableIpRecorder {
|
|
|
|
switch c.IpRecorderConfig.Type {
|
2022-11-02 03:09:42 -04:00
|
|
|
case "Recorder":
|
2022-12-18 10:31:06 -05:00
|
|
|
c.ipRecorder = iprecoder.NewRecorder(c.IpRecorderConfig.RecorderConfig)
|
2022-11-02 03:09:42 -04:00
|
|
|
case "Redis":
|
2022-12-18 10:31:06 -05:00
|
|
|
c.ipRecorder = iprecoder.NewRedis(c.IpRecorderConfig.RedisConfig)
|
2022-11-08 04:01:12 -05:00
|
|
|
default:
|
2022-12-18 10:31:06 -05:00
|
|
|
log.Printf("recorder type: %s is not vail, disable recorder", c.IpRecorderConfig.Type)
|
2022-11-08 04:01:12 -05:00
|
|
|
return nil
|
2022-10-20 23:07:04 -04:00
|
|
|
}
|
2022-10-09 23:31:15 -04:00
|
|
|
// report and fetch online ip list task
|
|
|
|
c.onlineIpReportPeriodic = &task.Periodic{
|
2022-12-18 10:31:06 -05:00
|
|
|
Interval: time.Duration(c.IpRecorderConfig.Periodic) * time.Second,
|
2022-10-09 23:31:15 -04:00
|
|
|
Execute: c.reportOnlineIp,
|
|
|
|
}
|
|
|
|
go func() {
|
2022-12-18 10:31:06 -05:00
|
|
|
time.Sleep(time.Duration(c.IpRecorderConfig.Periodic) * time.Second)
|
2022-10-09 23:31:15 -04:00
|
|
|
_ = c.onlineIpReportPeriodic.Start()
|
|
|
|
}()
|
|
|
|
log.Printf("[%s: %d] Start report online ip", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implement the Close() function of the service interface
|
|
|
|
func (c *Node) Close() error {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Node) buildNodeTag() string {
|
2022-12-18 10:31:06 -05:00
|
|
|
return fmt.Sprintf("%s_%s_%d", c.nodeInfo.NodeType, c.ListenIP, c.nodeInfo.NodeId)
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|