From 3cc2a3810f5ef97ea6a6992978479bc388d020bd Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Fri, 21 Oct 2022 11:07:04 +0800 Subject: [PATCH] refactor ipRecorder --- api/iprecoder/interface.go | 7 +++++++ api/iprecoder/recorder.go | 40 ++++++++++++++++++++++++++++++++++++++ api/iprecoder/redis.go | 1 + conf/node.go | 22 ++++++++++++++++----- example/config.yml.example | 8 +++++--- node/controller/inbound.go | 2 ++ node/controller/node.go | 7 +++++++ node/controller/task.go | 32 ++++++------------------------ 8 files changed, 85 insertions(+), 34 deletions(-) create mode 100644 api/iprecoder/interface.go create mode 100644 api/iprecoder/recorder.go create mode 100644 api/iprecoder/redis.go diff --git a/api/iprecoder/interface.go b/api/iprecoder/interface.go new file mode 100644 index 0000000..570c558 --- /dev/null +++ b/api/iprecoder/interface.go @@ -0,0 +1,7 @@ +package iprecoder + +import "github.com/Yuzuki616/V2bX/core/app/dispatcher" + +type IpRecorder interface { + SyncOnlineIp(Ips []dispatcher.UserIpList) ([]dispatcher.UserIpList, error) +} diff --git a/api/iprecoder/recorder.go b/api/iprecoder/recorder.go new file mode 100644 index 0000000..47510a9 --- /dev/null +++ b/api/iprecoder/recorder.go @@ -0,0 +1,40 @@ +package iprecoder + +import ( + "errors" + "github.com/Yuzuki616/V2bX/conf" + "github.com/Yuzuki616/V2bX/core/app/dispatcher" + "github.com/go-resty/resty/v2" + "github.com/goccy/go-json" + "time" +) + +type Recorder struct { + client *resty.Client + *conf.RecorderConfig +} + +func New(c *conf.RecorderConfig) *Recorder { + return &Recorder{ + client: resty.New().SetTimeout(time.Duration(c.Timeout) * time.Second), + RecorderConfig: c, + } +} + +func (r *Recorder) SyncOnlineIp(ips []dispatcher.UserIpList) ([]dispatcher.UserIpList, error) { + rsp, err := r.client.R(). + SetBody(ips). + Post(r.Url + "/api/v1/SyncOnlineIp?token=" + r.Token) + if err != nil { + return nil, err + } + if rsp.StatusCode() != 200 { + return nil, errors.New(rsp.String()) + } + ips = []dispatcher.UserIpList{} + err = json.Unmarshal(rsp.Body(), &ips) + if err != nil { + return nil, err + } + return ips, nil +} diff --git a/api/iprecoder/redis.go b/api/iprecoder/redis.go new file mode 100644 index 0000000..807f254 --- /dev/null +++ b/api/iprecoder/redis.go @@ -0,0 +1 @@ +package iprecoder diff --git a/conf/node.go b/conf/node.go index 52a6128..ce5cf15 100644 --- a/conf/node.go +++ b/conf/node.go @@ -19,12 +19,24 @@ type FallBackConfig struct { ProxyProtocolVer uint64 `yaml:"ProxyProtocolVer"` } +type RecorderConfig struct { + Url string `yaml:"Url"` + Token string `yaml:"Token"` + Timeout int `yaml:"Timeout"` +} + +type RedisConfig struct { + Address string `yaml:"Address"` + Password string `yaml:"Password"` + Db int `yaml:"Db"` +} + type IpReportConfig struct { - Url string `yaml:"Url"` - Token string `yaml:"Token"` - Periodic int `yaml:"Periodic"` - Timeout int `yaml:"Timeout"` - EnableIpSync bool `yaml:"EnableIpSync"` + Periodic int `yaml:"Periodic"` + Type string `yaml:"Type"` + RecorderConfig *RecorderConfig `yaml:"RecorderConfig"` + RedisConfig *RedisConfig `yaml:"RedisConfig"` + EnableIpSync bool `yaml:"EnableIpSync"` } type DynamicSpeedLimitConfig struct { diff --git a/example/config.yml.example b/example/config.yml.example index 0158fc5..a2c6761 100644 --- a/example/config.yml.example +++ b/example/config.yml.example @@ -42,10 +42,12 @@ Nodes: ProxyProtocolVer: 0 # Send PROXY protocol version, 0 for dsable EnableIpRecorder: false # Enable online ip report IpRecorderConfig: - Url: "http://127.0.0.1:123" # Report url - Token: "123" # Report token + Type: "Recorder" + RecorderConfig: + Url: "http://127.0.0.1:123" # Report url + Token: "123" # Report token + Timeout: 10 # Report timeout, sec. Periodic: 60 # Report interval, sec. - Timeout: 10 # Report timeout, sec. EnableIpSync: false # Enable online ip sync EnableDynamicSpeedLimit: false # Enable dynamic speed limit DynamicSpeedLimitConfig: diff --git a/node/controller/inbound.go b/node/controller/inbound.go index 4f78015..cb8be55 100644 --- a/node/controller/inbound.go +++ b/node/controller/inbound.go @@ -155,6 +155,8 @@ func buildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s OcspStapling: 3600}) nodeInfo.V2ray.Inbounds[0].StreamSetting.XTLSSettings = xtlsSettings } + } else if nodeInfo.NodeType == "V2ray" { + nodeInfo.V2ray.Inbounds[0].StreamSetting.Security = "none" } // Support ProxyProtocol for any transport protocol if *nodeInfo.V2ray.Inbounds[0].StreamSetting.Network != "tcp" && diff --git a/node/controller/node.go b/node/controller/node.go index 75449d0..def0f45 100644 --- a/node/controller/node.go +++ b/node/controller/node.go @@ -3,6 +3,7 @@ package controller import ( "errors" "fmt" + "github.com/Yuzuki616/V2bX/api/iprecoder" "github.com/Yuzuki616/V2bX/api/panel" "github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/core" @@ -19,6 +20,7 @@ type Node struct { nodeInfo *panel.NodeInfo Tag string userList []panel.UserInfo + ipRecorder iprecoder.IpRecorder nodeInfoMonitorPeriodic *task.Periodic userReportPeriodic *task.Periodic onlineIpReportPeriodic *task.Periodic @@ -99,6 +101,11 @@ func (c *Node) Start() error { _ = c.userReportPeriodic.Start() }() if c.config.EnableIpRecorder { + switch c.config.IpRecorderConfig.Type { + case "Record": + c.ipRecorder = iprecoder.New(c.config.IpRecorderConfig.RecorderConfig) + case "RedisConfig": + } // report and fetch online ip list task c.onlineIpReportPeriodic = &task.Periodic{ Interval: time.Duration(c.config.IpRecorderConfig.Periodic) * time.Second, diff --git a/node/controller/task.go b/node/controller/task.go index 7ce1e8e..d38fdd4 100644 --- a/node/controller/task.go +++ b/node/controller/task.go @@ -3,10 +3,7 @@ package controller import ( "fmt" "github.com/Yuzuki616/V2bX/api/panel" - "github.com/Yuzuki616/V2bX/core/app/dispatcher" "github.com/Yuzuki616/V2bX/node/controller/legoCmd" - "github.com/go-resty/resty/v2" - "github.com/goccy/go-json" "github.com/xtls/xray-core/common/protocol" "log" "reflect" @@ -254,33 +251,16 @@ func (c *Node) reportOnlineIp() (err error) { log.Print(err) return nil } - rsp, err := resty.New().SetTimeout(time.Duration(c.config.IpRecorderConfig.Timeout) * time.Second). - R(). - SetBody(onlineIp). - Post(c.config.IpRecorderConfig.Url + - "/api/v1/SyncOnlineIp?token=" + - c.config.IpRecorderConfig.Token) + onlineIp, err = c.ipRecorder.SyncOnlineIp(onlineIp) if err != nil { - log.Print(err) + log.Print("Report online ip error: ", err) c.server.ClearOnlineIp(c.Tag) - return nil + } + if c.config.IpRecorderConfig.EnableIpSync { + c.server.UpdateOnlineIp(c.Tag, onlineIp) + log.Printf("[Node: %d] Updated %d online ip", c.nodeInfo.NodeId, len(onlineIp)) } log.Printf("[Node: %d] Report %d online ip", c.nodeInfo.NodeId, len(onlineIp)) - if rsp.StatusCode() == 200 { - onlineIp = []dispatcher.UserIpList{} - err := json.Unmarshal(rsp.Body(), &onlineIp) - if err != nil { - log.Print(err) - c.server.ClearOnlineIp(c.Tag) - return nil - } - if c.config.IpRecorderConfig.EnableIpSync { - c.server.UpdateOnlineIp(c.Tag, onlineIp) - log.Printf("[Node: %d] Updated %d online ip", c.nodeInfo.NodeId, len(onlineIp)) - } - } else { - c.server.ClearOnlineIp(c.Tag) - } return nil }