V2bX/node/controller.go

131 lines
3.5 KiB
Go
Raw Normal View History

package node
2022-10-09 23:31:15 -04:00
import (
"errors"
"fmt"
"log"
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"
vCore "github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/limiter"
2022-10-09 23:31:15 -04:00
"github.com/xtls/xray-core/common/task"
)
type Controller struct {
server vCore.Core
apiClient *panel.Client
2022-10-09 23:31:15 -04:00
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
2023-01-12 01:27:06 -05:00
renewCertPeriodic *task.Periodic
dynamicSpeedLimitPeriodic *task.Periodic
2022-10-09 23:31:15 -04:00
onlineIpReportPeriodic *task.Periodic
*conf.ControllerConfig
2022-10-09 23:31:15 -04:00
}
// NewController return a Node controller with default parameters.
func NewController(server vCore.Core, api *panel.Client, config *conf.ControllerConfig) *Controller {
controller := &Controller{
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 *Controller) Start() error {
2022-10-09 23:31:15 -04:00
// First fetch Node Info
2022-12-20 13:49:23 -05:00
var err error
c.nodeInfo, err = c.apiClient.GetNodeInfo()
2022-10-09 23:31:15 -04:00
if err != nil {
return fmt.Errorf("get node info error: %s", err)
2022-10-09 23:31:15 -04:00
}
// Update user
c.userList, err = c.apiClient.GetUserList()
if err != nil {
return fmt.Errorf("get user list error: %s", err)
2022-10-09 23:31:15 -04:00
}
if len(c.userList) == 0 {
return errors.New("add users error: not have any user")
2022-10-09 23:31:15 -04:00
}
c.Tag = c.buildNodeTag()
// add limiter
l := limiter.AddLimiter(c.Tag, &c.LimitConfig, c.userList)
// add rule limiter
if !c.DisableGetRule {
if err = l.UpdateRule(c.nodeInfo.Rules); err != nil {
return fmt.Errorf("update rule error: %s", err)
}
}
if c.nodeInfo.Tls {
err = c.requestCert()
if err != nil {
return fmt.Errorf("request cert error: %s", err)
2022-10-09 23:31:15 -04:00
}
}
// Add new tag
2023-05-22 09:01:31 -04:00
err = c.server.AddNode(c.Tag, c.nodeInfo, c.ControllerConfig)
if err != nil {
return fmt.Errorf("add new node error: %s", err)
}
added, err := c.server.AddUsers(&vCore.AddUsersParams{
2023-05-22 22:02:47 -04:00
Tag: c.Tag,
Config: c.ControllerConfig,
UserInfo: c.userList,
NodeInfo: c.nodeInfo,
})
if err != nil {
return fmt.Errorf("add users error: %s", err)
}
log.Printf("[%s: %d] Added %d new users", c.nodeInfo.Type, c.nodeInfo.Id, added)
2023-01-12 01:27:06 -05:00
c.initTask()
2022-10-09 23:31:15 -04:00
return nil
}
// Close implement the Close() function of the service interface
func (c *Controller) Close() error {
limiter.DeleteLimiter(c.Tag)
2022-10-09 23:31:15 -04:00
if c.nodeInfoMonitorPeriodic != nil {
err := c.nodeInfoMonitorPeriodic.Close()
if err != nil {
return fmt.Errorf("node info periodic close error: %s", err)
2022-10-09 23:31:15 -04:00
}
}
if c.nodeInfoMonitorPeriodic != nil {
err := c.userReportPeriodic.Close()
if err != nil {
return fmt.Errorf("user report periodic close error: %s", err)
2022-10-09 23:31:15 -04:00
}
}
2023-01-12 01:27:06 -05:00
if c.renewCertPeriodic != nil {
err := c.renewCertPeriodic.Close()
2022-10-09 23:31:15 -04:00
if err != nil {
return fmt.Errorf("renew cert periodic close error: %s", err)
2022-10-09 23:31:15 -04:00
}
}
2023-01-12 01:27:06 -05:00
if c.dynamicSpeedLimitPeriodic != nil {
err := c.dynamicSpeedLimitPeriodic.Close()
2022-10-09 23:31:15 -04:00
if err != nil {
return fmt.Errorf("dynamic speed limit periodic close error: %s", err)
2022-10-09 23:31:15 -04:00
}
}
2023-01-12 01:27:06 -05:00
if c.onlineIpReportPeriodic != nil {
err := c.onlineIpReportPeriodic.Close()
if err != nil {
return fmt.Errorf("online ip report periodic close error: %s", err)
2023-01-12 01:27:06 -05:00
}
}
2022-10-09 23:31:15 -04:00
return nil
}
func (c *Controller) buildNodeTag() string {
return fmt.Sprintf("%s_%s_%d", c.nodeInfo.Type, c.ListenIP, c.nodeInfo.Id)
2022-10-09 23:31:15 -04:00
}