2023-05-15 21:15:29 -04:00
|
|
|
package node
|
2022-10-09 23:31:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-07-20 09:14:18 -04:00
|
|
|
|
2023-07-29 07:27:15 -04:00
|
|
|
"github.com/InazumaV/V2bX/api/panel"
|
|
|
|
"github.com/InazumaV/V2bX/common/task"
|
|
|
|
"github.com/InazumaV/V2bX/conf"
|
|
|
|
vCore "github.com/InazumaV/V2bX/core"
|
|
|
|
"github.com/InazumaV/V2bX/limiter"
|
2023-06-29 23:07:27 -04:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-10-09 23:31:15 -04:00
|
|
|
)
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
type Controller struct {
|
2023-06-07 13:18:56 -04:00
|
|
|
server vCore.Core
|
2023-05-17 21:11:28 -04:00
|
|
|
apiClient *panel.Client
|
2023-07-21 14:38:07 -04:00
|
|
|
tag string
|
|
|
|
limiter *limiter.Limiter
|
|
|
|
traffic map[string]int64
|
2022-10-09 23:31:15 -04:00
|
|
|
userList []panel.UserInfo
|
2023-07-29 07:17:13 -04:00
|
|
|
info *panel.NodeInfo
|
2023-06-15 22:04:39 -04:00
|
|
|
nodeInfoMonitorPeriodic *task.Task
|
|
|
|
userReportPeriodic *task.Task
|
|
|
|
renewCertPeriodic *task.Task
|
|
|
|
dynamicSpeedLimitPeriodic *task.Task
|
|
|
|
onlineIpReportPeriodic *task.Task
|
2023-07-29 06:47:47 -04:00
|
|
|
*conf.Options
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
// NewController return a Node controller with default parameters.
|
2023-07-29 06:47:47 -04:00
|
|
|
func NewController(server vCore.Core, api *panel.Client, config *conf.Options) *Controller {
|
2023-05-15 21:15:29 -04:00
|
|
|
controller := &Controller{
|
2023-07-29 06:47:47 -04:00
|
|
|
server: server,
|
|
|
|
Options: config,
|
|
|
|
apiClient: api,
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
|
|
|
return controller
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implement the Start() function of the service interface
|
2023-05-15 21:15:29 -04:00
|
|
|
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
|
2023-07-21 14:38:07 -04:00
|
|
|
node, err := c.apiClient.GetNodeInfo()
|
2022-10-09 23:31:15 -04:00
|
|
|
if err != nil {
|
2023-06-08 10:46:33 -04:00
|
|
|
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 {
|
2023-06-08 10:46:33 -04:00
|
|
|
return fmt.Errorf("get user list error: %s", err)
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
|
|
|
if len(c.userList) == 0 {
|
2023-06-08 10:46:33 -04:00
|
|
|
return errors.New("add users error: not have any user")
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
2023-09-12 13:44:03 -04:00
|
|
|
if len(c.Options.Name) == 0 {
|
|
|
|
c.tag = c.buildNodeTag(node)
|
|
|
|
} else {
|
|
|
|
c.tag = c.Options.Name
|
|
|
|
}
|
2023-05-15 21:15:29 -04:00
|
|
|
|
|
|
|
// add limiter
|
2023-07-21 14:38:07 -04:00
|
|
|
l := limiter.AddLimiter(c.tag, &c.LimitConfig, c.userList)
|
2023-05-15 21:15:29 -04:00
|
|
|
// add rule limiter
|
2023-07-21 14:38:07 -04:00
|
|
|
if err = l.UpdateRule(&node.Rules); err != nil {
|
2023-06-18 21:33:20 -04:00
|
|
|
return fmt.Errorf("update rule error: %s", err)
|
2023-06-08 10:46:33 -04:00
|
|
|
}
|
2023-07-21 14:38:07 -04:00
|
|
|
c.limiter = l
|
2023-08-19 08:06:42 -04:00
|
|
|
if node.Security == panel.Tls {
|
2023-06-08 10:46:33 -04:00
|
|
|
err = c.requestCert()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("request cert error: %s", err)
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
|
|
|
}
|
2023-05-15 21:15:29 -04:00
|
|
|
// Add new tag
|
2023-07-29 06:47:47 -04:00
|
|
|
err = c.server.AddNode(c.tag, node, c.Options)
|
2023-05-15 21:15:29 -04:00
|
|
|
if err != nil {
|
2023-06-08 10:46:33 -04:00
|
|
|
return fmt.Errorf("add new node error: %s", err)
|
2023-05-15 21:15:29 -04:00
|
|
|
}
|
2023-06-07 13:18:56 -04:00
|
|
|
added, err := c.server.AddUsers(&vCore.AddUsersParams{
|
2023-07-21 14:38:07 -04:00
|
|
|
Tag: c.tag,
|
2023-08-19 08:06:42 -04:00
|
|
|
Users: c.userList,
|
2023-07-21 14:38:07 -04:00
|
|
|
NodeInfo: node,
|
2023-05-22 22:02:47 -04:00
|
|
|
})
|
2023-05-15 21:15:29 -04:00
|
|
|
if err != nil {
|
2023-06-08 10:46:33 -04:00
|
|
|
return fmt.Errorf("add users error: %s", err)
|
2023-05-15 21:15:29 -04:00
|
|
|
}
|
2023-07-21 14:38:07 -04:00
|
|
|
log.WithField("tag", c.tag).Infof("Added %d new users", added)
|
2023-07-29 07:17:13 -04:00
|
|
|
c.info = node
|
2023-07-21 14:38:07 -04:00
|
|
|
c.startTasks(node)
|
2022-10-09 23:31:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implement the Close() function of the service interface
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) Close() error {
|
2023-07-21 14:38:07 -04:00
|
|
|
limiter.DeleteLimiter(c.tag)
|
2022-10-09 23:31:15 -04:00
|
|
|
if c.nodeInfoMonitorPeriodic != nil {
|
2023-06-15 22:04:39 -04:00
|
|
|
c.nodeInfoMonitorPeriodic.Close()
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
2023-06-15 22:04:39 -04:00
|
|
|
if c.userReportPeriodic != nil {
|
|
|
|
c.userReportPeriodic.Close()
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
2023-01-12 01:27:06 -05:00
|
|
|
if c.renewCertPeriodic != nil {
|
2023-06-15 22:04:39 -04:00
|
|
|
c.renewCertPeriodic.Close()
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
2023-01-12 01:27:06 -05:00
|
|
|
if c.dynamicSpeedLimitPeriodic != nil {
|
2023-06-15 22:04:39 -04:00
|
|
|
c.dynamicSpeedLimitPeriodic.Close()
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|
2023-01-12 01:27:06 -05:00
|
|
|
if c.onlineIpReportPeriodic != nil {
|
2023-06-15 22:04:39 -04:00
|
|
|
c.onlineIpReportPeriodic.Close()
|
2023-01-12 01:27:06 -05:00
|
|
|
}
|
2023-11-24 04:16:44 -05:00
|
|
|
err := c.server.DelNode(c.tag)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("del node error: %s", err)
|
|
|
|
}
|
2022-10-09 23:31:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 14:38:07 -04:00
|
|
|
func (c *Controller) buildNodeTag(node *panel.NodeInfo) string {
|
2023-09-17 14:16:17 -04:00
|
|
|
return fmt.Sprintf("[%s]-%s:%d", c.apiClient.APIHost, node.Type, node.Id)
|
2022-10-09 23:31:15 -04:00
|
|
|
}
|