refactor ipRecorder

This commit is contained in:
yuzuki999 2022-10-21 11:07:04 +08:00
parent 068c2fef1f
commit 3cc2a3810f
8 changed files with 85 additions and 34 deletions

View File

@ -0,0 +1,7 @@
package iprecoder
import "github.com/Yuzuki616/V2bX/core/app/dispatcher"
type IpRecorder interface {
SyncOnlineIp(Ips []dispatcher.UserIpList) ([]dispatcher.UserIpList, error)
}

40
api/iprecoder/recorder.go Normal file
View File

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

1
api/iprecoder/redis.go Normal file
View File

@ -0,0 +1 @@
package iprecoder

View File

@ -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 {

View File

@ -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:

View File

@ -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" &&

View File

@ -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,

View File

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