remove no need interface, fix interval type for v2board v1.7.4dev

This commit is contained in:
yuzuki999 2023-05-18 09:11:28 +08:00
parent 48da83fc3d
commit 8bb0196c78
8 changed files with 53 additions and 46 deletions

View File

@ -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()
}

View File

@ -2,9 +2,11 @@ package panel
import ( import (
"github.com/goccy/go-json" "github.com/goccy/go-json"
"reflect"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
) )
type NodeInfo struct { type NodeInfo struct {
@ -73,13 +75,24 @@ func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
} }
nodeInfo.Routes = nil nodeInfo.Routes = nil
if _, ok := nodeInfo.BaseConfig.PullInterval.(int); !ok { if _, ok := nodeInfo.BaseConfig.PullInterval.(int); !ok {
i, _ := strconv.Atoi(nodeInfo.BaseConfig.PullInterval.(string)) nodeInfo.BaseConfig.PullInterval = intervalToTime(nodeInfo.BaseConfig.PullInterval)
nodeInfo.BaseConfig.PullInterval = i
} }
if _, ok := nodeInfo.BaseConfig.PushInterval.(int); !ok { if _, ok := nodeInfo.BaseConfig.PushInterval.(int); !ok {
i, _ := strconv.Atoi(nodeInfo.BaseConfig.PushInterval.(string)) nodeInfo.BaseConfig.PushInterval = intervalToTime(nodeInfo.BaseConfig.PullInterval)
nodeInfo.BaseConfig.PushInterval = i
} }
c.etag = r.Header().Get("Etag") c.etag = r.Header().Get("Etag")
return 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
}

View File

@ -6,7 +6,7 @@ import (
"testing" "testing"
) )
var client Panel var client *Client
func init() { func init() {
c, err := New(&conf.ApiConfig{ c, err := New(&conf.ApiConfig{

View File

@ -32,7 +32,7 @@ type Client struct {
etag string etag string
} }
func New(c *conf.ApiConfig) (Panel, error) { func New(c *conf.ApiConfig) (*Client, error) {
client := resty.New() client := resty.New()
client.SetRetryCount(3) client.SetRetryCount(3)
if c.Timeout > 0 { if c.Timeout > 0 {

View File

@ -8,7 +8,7 @@ import (
var c *ConnLimiter var c *ConnLimiter
func init() { func init() {
c = NewConnLimiter(1, 1) c = NewConnLimiter(1, 1, true)
} }
func TestConnLimiter_AddConnCount(t *testing.T) { func TestConnLimiter_AddConnCount(t *testing.T) {

View File

@ -15,7 +15,7 @@ import (
type Controller struct { type Controller struct {
server *core.Core server *core.Core
clientInfo panel.ClientInfo clientInfo panel.ClientInfo
apiClient panel.Panel apiClient *panel.Client
nodeInfo *panel.NodeInfo nodeInfo *panel.NodeInfo
Tag string Tag string
userList []panel.UserInfo userList []panel.UserInfo
@ -29,7 +29,7 @@ type Controller struct {
} }
// NewController return a Node controller with default parameters. // 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{ controller := &Controller{
server: server, server: server,
ControllerConfig: config, ControllerConfig: config,

View File

@ -5,7 +5,6 @@ import (
"github.com/Yuzuki616/V2bX/api/panel" "github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/limiter" "github.com/Yuzuki616/V2bX/limiter"
"github.com/Yuzuki616/V2bX/node/lego" "github.com/Yuzuki616/V2bX/node/lego"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/task" "github.com/xtls/xray-core/common/task"
"log" "log"
"runtime" "runtime"
@ -99,16 +98,18 @@ func (c *Controller) nodeInfoMonitor() (err error) {
log.Printf("Update Rule error: %s", err) log.Printf("Update Rule error: %s", err)
} }
// Check interval // Check interval
if c.nodeInfoMonitorPeriodic.Interval != time.Duration(newNodeInfo.BaseConfig.PullInterval.(int))*time.Second { if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.BaseConfig.PullInterval.(time.Duration) &&
c.nodeInfoMonitorPeriodic.Interval = time.Duration(newNodeInfo.BaseConfig.PullInterval.(int)) * time.Second newNodeInfo.BaseConfig.PullInterval.(time.Duration) != 0 {
c.nodeInfoMonitorPeriodic.Interval = newNodeInfo.BaseConfig.PullInterval.(time.Duration)
_ = c.nodeInfoMonitorPeriodic.Close() _ = c.nodeInfoMonitorPeriodic.Close()
go func() { go func() {
time.Sleep(c.nodeInfoMonitorPeriodic.Interval) time.Sleep(c.nodeInfoMonitorPeriodic.Interval)
_ = c.nodeInfoMonitorPeriodic.Start() _ = c.nodeInfoMonitorPeriodic.Start()
}() }()
} }
if c.userReportPeriodic.Interval != time.Duration(newNodeInfo.BaseConfig.PushInterval.(int))*time.Second { if c.userReportPeriodic.Interval != newNodeInfo.BaseConfig.PushInterval.(time.Duration) &&
c.userReportPeriodic.Interval = time.Duration(newNodeInfo.BaseConfig.PushInterval.(int)) * time.Second newNodeInfo.BaseConfig.PushInterval.(time.Duration) != 0 {
c.userReportPeriodic.Interval = newNodeInfo.BaseConfig.PullInterval.(time.Duration)
_ = c.userReportPeriodic.Close() _ = c.userReportPeriodic.Close()
go func() { go func() {
time.Sleep(c.userReportPeriodic.Interval) time.Sleep(c.userReportPeriodic.Interval)
@ -182,29 +183,6 @@ func (c *Controller) addNewNode(newNodeInfo *panel.NodeInfo) (err error) {
return nil 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) { func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) {
tmp := map[string]struct{}{} tmp := map[string]struct{}{}
tmp2 := map[string]struct{}{} tmp2 := map[string]struct{}{}

View File

@ -11,9 +11,34 @@ import (
"github.com/xtls/xray-core/proxy/shadowsocks_2022" "github.com/xtls/xray-core/proxy/shadowsocks_2022"
"github.com/xtls/xray-core/proxy/trojan" "github.com/xtls/xray-core/proxy/trojan"
"github.com/xtls/xray-core/proxy/vless" "github.com/xtls/xray-core/proxy/vless"
"log"
"strings" "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) { func (c *Controller) buildVmessUsers(userInfo []panel.UserInfo) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo)) users = make([]*protocol.User, len(userInfo))
for i, user := range userInfo { for i, user := range userInfo {