mirror of
https://github.com/wyx2685/V2bX.git
synced 2025-01-22 09:58:14 -05:00
remove no need interface, fix interval type for v2board v1.7.4dev
This commit is contained in:
parent
48da83fc3d
commit
8bb0196c78
@ -1,9 +0,0 @@
|
||||
package panel
|
||||
|
||||
type Panel interface {
|
||||
GetNodeInfo() (nodeInfo *NodeInfo, err error)
|
||||
GetUserList() (userList []UserInfo, err error)
|
||||
ReportUserTraffic(userTraffic []UserTraffic) (err error)
|
||||
Describe() ClientInfo
|
||||
Debug()
|
||||
}
|
@ -2,9 +2,11 @@ package panel
|
||||
|
||||
import (
|
||||
"github.com/goccy/go-json"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NodeInfo struct {
|
||||
@ -73,13 +75,24 @@ func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
|
||||
}
|
||||
nodeInfo.Routes = nil
|
||||
if _, ok := nodeInfo.BaseConfig.PullInterval.(int); !ok {
|
||||
i, _ := strconv.Atoi(nodeInfo.BaseConfig.PullInterval.(string))
|
||||
nodeInfo.BaseConfig.PullInterval = i
|
||||
nodeInfo.BaseConfig.PullInterval = intervalToTime(nodeInfo.BaseConfig.PullInterval)
|
||||
}
|
||||
if _, ok := nodeInfo.BaseConfig.PushInterval.(int); !ok {
|
||||
i, _ := strconv.Atoi(nodeInfo.BaseConfig.PushInterval.(string))
|
||||
nodeInfo.BaseConfig.PushInterval = i
|
||||
nodeInfo.BaseConfig.PushInterval = intervalToTime(nodeInfo.BaseConfig.PullInterval)
|
||||
}
|
||||
c.etag = r.Header().Get("Etag")
|
||||
return
|
||||
}
|
||||
|
||||
func intervalToTime(i interface{}) time.Duration {
|
||||
switch reflect.TypeOf(i).Kind() {
|
||||
case reflect.Int:
|
||||
return time.Duration(i.(int)) * time.Second
|
||||
case reflect.String:
|
||||
i, _ := strconv.Atoi(i.(string))
|
||||
return time.Duration(i) * time.Second
|
||||
case reflect.Float64:
|
||||
return time.Duration(i.(float64)) * time.Second
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var client Panel
|
||||
var client *Client
|
||||
|
||||
func init() {
|
||||
c, err := New(&conf.ApiConfig{
|
||||
|
@ -32,7 +32,7 @@ type Client struct {
|
||||
etag string
|
||||
}
|
||||
|
||||
func New(c *conf.ApiConfig) (Panel, error) {
|
||||
func New(c *conf.ApiConfig) (*Client, error) {
|
||||
client := resty.New()
|
||||
client.SetRetryCount(3)
|
||||
if c.Timeout > 0 {
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
var c *ConnLimiter
|
||||
|
||||
func init() {
|
||||
c = NewConnLimiter(1, 1)
|
||||
c = NewConnLimiter(1, 1, true)
|
||||
}
|
||||
|
||||
func TestConnLimiter_AddConnCount(t *testing.T) {
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
type Controller struct {
|
||||
server *core.Core
|
||||
clientInfo panel.ClientInfo
|
||||
apiClient panel.Panel
|
||||
apiClient *panel.Client
|
||||
nodeInfo *panel.NodeInfo
|
||||
Tag string
|
||||
userList []panel.UserInfo
|
||||
@ -29,7 +29,7 @@ type Controller struct {
|
||||
}
|
||||
|
||||
// NewController return a Node controller with default parameters.
|
||||
func NewController(server *core.Core, api panel.Panel, config *conf.ControllerConfig) *Controller {
|
||||
func NewController(server *core.Core, api *panel.Client, config *conf.ControllerConfig) *Controller {
|
||||
controller := &Controller{
|
||||
server: server,
|
||||
ControllerConfig: config,
|
||||
|
34
node/task.go
34
node/task.go
@ -5,7 +5,6 @@ import (
|
||||
"github.com/Yuzuki616/V2bX/api/panel"
|
||||
"github.com/Yuzuki616/V2bX/limiter"
|
||||
"github.com/Yuzuki616/V2bX/node/lego"
|
||||
"github.com/xtls/xray-core/common/protocol"
|
||||
"github.com/xtls/xray-core/common/task"
|
||||
"log"
|
||||
"runtime"
|
||||
@ -99,16 +98,18 @@ func (c *Controller) nodeInfoMonitor() (err error) {
|
||||
log.Printf("Update Rule error: %s", err)
|
||||
}
|
||||
// Check interval
|
||||
if c.nodeInfoMonitorPeriodic.Interval != time.Duration(newNodeInfo.BaseConfig.PullInterval.(int))*time.Second {
|
||||
c.nodeInfoMonitorPeriodic.Interval = time.Duration(newNodeInfo.BaseConfig.PullInterval.(int)) * time.Second
|
||||
if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.BaseConfig.PullInterval.(time.Duration) &&
|
||||
newNodeInfo.BaseConfig.PullInterval.(time.Duration) != 0 {
|
||||
c.nodeInfoMonitorPeriodic.Interval = newNodeInfo.BaseConfig.PullInterval.(time.Duration)
|
||||
_ = c.nodeInfoMonitorPeriodic.Close()
|
||||
go func() {
|
||||
time.Sleep(c.nodeInfoMonitorPeriodic.Interval)
|
||||
_ = c.nodeInfoMonitorPeriodic.Start()
|
||||
}()
|
||||
}
|
||||
if c.userReportPeriodic.Interval != time.Duration(newNodeInfo.BaseConfig.PushInterval.(int))*time.Second {
|
||||
c.userReportPeriodic.Interval = time.Duration(newNodeInfo.BaseConfig.PushInterval.(int)) * time.Second
|
||||
if c.userReportPeriodic.Interval != newNodeInfo.BaseConfig.PushInterval.(time.Duration) &&
|
||||
newNodeInfo.BaseConfig.PushInterval.(time.Duration) != 0 {
|
||||
c.userReportPeriodic.Interval = newNodeInfo.BaseConfig.PullInterval.(time.Duration)
|
||||
_ = c.userReportPeriodic.Close()
|
||||
go func() {
|
||||
time.Sleep(c.userReportPeriodic.Interval)
|
||||
@ -182,29 +183,6 @@ func (c *Controller) addNewNode(newNodeInfo *panel.NodeInfo) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) addNewUser(userInfo []panel.UserInfo, nodeInfo *panel.NodeInfo) (err error) {
|
||||
users := make([]*protocol.User, 0)
|
||||
if nodeInfo.NodeType == "V2ray" {
|
||||
if c.EnableVless {
|
||||
users = c.buildVlessUsers(userInfo)
|
||||
} else {
|
||||
users = c.buildVmessUsers(userInfo)
|
||||
}
|
||||
} else if nodeInfo.NodeType == "Trojan" {
|
||||
users = c.buildTrojanUsers(userInfo)
|
||||
} else if nodeInfo.NodeType == "Shadowsocks" {
|
||||
users = c.buildSSUsers(userInfo, getCipherFromString(nodeInfo.Cipher))
|
||||
} else {
|
||||
return fmt.Errorf("unsupported node type: %s", nodeInfo.NodeType)
|
||||
}
|
||||
err = c.server.AddUsers(users, c.Tag)
|
||||
if err != nil {
|
||||
return fmt.Errorf("add users error: %s", err)
|
||||
}
|
||||
log.Printf("[%s: %d] Added %d new users", c.nodeInfo.NodeType, c.nodeInfo.NodeId, len(userInfo))
|
||||
return nil
|
||||
}
|
||||
|
||||
func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) {
|
||||
tmp := map[string]struct{}{}
|
||||
tmp2 := map[string]struct{}{}
|
||||
|
25
node/user.go
25
node/user.go
@ -11,9 +11,34 @@ import (
|
||||
"github.com/xtls/xray-core/proxy/shadowsocks_2022"
|
||||
"github.com/xtls/xray-core/proxy/trojan"
|
||||
"github.com/xtls/xray-core/proxy/vless"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (c *Controller) addNewUser(userInfo []panel.UserInfo, nodeInfo *panel.NodeInfo) (err error) {
|
||||
users := make([]*protocol.User, 0, len(userInfo))
|
||||
switch nodeInfo.NodeType {
|
||||
case "v2ray":
|
||||
if c.EnableVless {
|
||||
users = c.buildVlessUsers(userInfo)
|
||||
} else {
|
||||
users = c.buildVmessUsers(userInfo)
|
||||
}
|
||||
case "Trojan":
|
||||
users = c.buildTrojanUsers(userInfo)
|
||||
case "Shadowsocks":
|
||||
users = c.buildSSUsers(userInfo, getCipherFromString(nodeInfo.Cipher))
|
||||
default:
|
||||
return fmt.Errorf("unsupported node type: %s", nodeInfo.NodeType)
|
||||
}
|
||||
err = c.server.AddUsers(users, c.Tag)
|
||||
if err != nil {
|
||||
return fmt.Errorf("add users error: %s", err)
|
||||
}
|
||||
log.Printf("[%s: %d] Added %d new users", c.nodeInfo.NodeType, c.nodeInfo.NodeId, len(userInfo))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) buildVmessUsers(userInfo []panel.UserInfo) (users []*protocol.User) {
|
||||
users = make([]*protocol.User, len(userInfo))
|
||||
for i, user := range userInfo {
|
||||
|
Loading…
Reference in New Issue
Block a user