performance optimization

This commit is contained in:
yuzuki999 2022-09-15 09:24:14 +08:00
parent d5d1292c0e
commit 50260fcf79
8 changed files with 66 additions and 149 deletions

View File

@ -5,6 +5,6 @@ type Panel interface {
GetUserList() (userList []UserInfo, err error) GetUserList() (userList []UserInfo, err error)
ReportUserTraffic(userTraffic []UserTraffic) (err error) ReportUserTraffic(userTraffic []UserTraffic) (err error)
Describe() ClientInfo Describe() ClientInfo
GetNodeRule() (ruleList []DetectRule, protocolList []string, err error) GetNodeRule() (ruleList *DetectRule, err error)
Debug() Debug()
} }

View File

@ -14,32 +14,29 @@ import (
) )
type DetectRule struct { type DetectRule struct {
ProtocolRule []string
DestinationRule []DestinationRule
}
type DestinationRule struct {
ID int ID int
Pattern *regexp.Regexp Pattern *regexp.Regexp
} }
type DetectResult struct {
UID int
RuleID int
}
// readLocalRuleList reads the local rule list file // readLocalRuleList reads the local rule list file
func readLocalRuleList(path string) (LocalRuleList []DetectRule) { func readLocalRuleList(path string) (LocalRuleList *DetectRule) {
LocalRuleList = make([]DetectRule, 0) LocalRuleList = &DetectRule{}
if path != "" { if path != "" {
// open the file // open the file
file, err := os.Open(path) file, err := os.Open(path)
//handle errors while opening //handle errors while opening
if err != nil { if err != nil {
log.Printf("Error when opening file: %s", err) log.Printf("Error when opening file: %s", err)
return LocalRuleList return
} }
fileScanner := bufio.NewScanner(file) fileScanner := bufio.NewScanner(file)
// read line by line // read line by line
for fileScanner.Scan() { for fileScanner.Scan() {
LocalRuleList = append(LocalRuleList, DetectRule{ LocalRuleList.DestinationRule = append(LocalRuleList.DestinationRule, DestinationRule{
ID: -1, ID: -1,
Pattern: regexp.MustCompile(fileScanner.Text()), Pattern: regexp.MustCompile(fileScanner.Text()),
}) })
@ -47,11 +44,10 @@ func readLocalRuleList(path string) (LocalRuleList []DetectRule) {
// handle first encountered error while reading // handle first encountered error while reading
if err := fileScanner.Err(); err != nil { if err := fileScanner.Err(); err != nil {
log.Fatalf("Error while reading file: %s", err) log.Fatalf("Error while reading file: %s", err)
return []DetectRule{} return
} }
} }
return
return LocalRuleList
} }
type NodeInfo struct { type NodeInfo struct {
@ -159,32 +155,31 @@ func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
return nodeInfo, nil return nodeInfo, nil
} }
func (c *Client) GetNodeRule() ([]DetectRule, []string, error) { func (c *Client) GetNodeRule() (*DetectRule, error) {
ruleList := c.LocalRuleList ruleList := c.LocalRuleList
if c.NodeType != "V2ray" || c.RemoteRuleCache == nil { if c.NodeType != "V2ray" || c.RemoteRuleCache == nil {
return ruleList, nil, nil return nil, nil
} }
// V2board only support the rule for v2ray // V2board only support the rule for v2ray
// fix: reuse config response // fix: reuse config response
c.access.Lock() c.access.Lock()
defer c.access.Unlock() defer c.access.Unlock()
if len(*c.RemoteRuleCache) >= 2 { if len(c.RemoteRuleCache) >= 2 {
for i, rule := range (*c.RemoteRuleCache)[1].Domain { for i, rule := range (c.RemoteRuleCache)[1].Domain {
ruleListItem := DetectRule{ ruleListItem := DestinationRule{
ID: i, ID: i,
Pattern: regexp.MustCompile(rule), Pattern: regexp.MustCompile(rule),
} }
ruleList = append(ruleList, ruleListItem) ruleList.DestinationRule = append(ruleList.DestinationRule, ruleListItem)
} }
} }
var protocolList []string if len(c.RemoteRuleCache) >= 3 {
if len(*c.RemoteRuleCache) >= 3 { for _, str := range (c.RemoteRuleCache)[2].Protocol {
for _, str := range (*c.RemoteRuleCache)[2].Protocol { ruleList.ProtocolRule = append(ruleList.ProtocolRule, str)
protocolList = append(protocolList, str)
} }
} }
c.RemoteRuleCache = nil c.RemoteRuleCache = nil
return ruleList, protocolList, nil return ruleList, nil
} }
// ParseTrojanNodeResponse parse the response for the given nodeinfor format // ParseTrojanNodeResponse parse the response for the given nodeinfor format
@ -246,7 +241,7 @@ func (c *Client) ParseV2rayNodeResponse(body []byte, notParseNode, parseRule boo
return nil, fmt.Errorf("unmarshal nodeinfo error: %s", err) return nil, fmt.Errorf("unmarshal nodeinfo error: %s", err)
} }
if parseRule { if parseRule {
c.RemoteRuleCache = &[]Rule{} c.RemoteRuleCache = []Rule{}
err := json.Unmarshal(node.V2ray.Routing.Rules, c.RemoteRuleCache) err := json.Unmarshal(node.V2ray.Routing.Rules, c.RemoteRuleCache)
if err != nil { if err != nil {
log.Println(err) log.Println(err)

View File

@ -29,8 +29,8 @@ type Client struct {
EnableXTLS bool EnableXTLS bool
SpeedLimit float64 SpeedLimit float64
DeviceLimit int DeviceLimit int
LocalRuleList []DetectRule LocalRuleList *DetectRule
RemoteRuleCache *[]Rule RemoteRuleCache []Rule
access sync.Mutex access sync.Mutex
NodeInfoRspMd5 [16]byte NodeInfoRspMd5 [16]byte
NodeRuleRspMd5 [16]byte NodeRuleRspMd5 [16]byte

View File

@ -1,104 +1,49 @@
package dispatcher package dispatcher
import ( import (
"fmt"
"github.com/Yuzuki616/V2bX/api/panel" "github.com/Yuzuki616/V2bX/api/panel"
"reflect" "reflect"
"strconv"
"strings"
"sync" "sync"
mapset "github.com/deckarep/golang-set"
) )
type Rule struct { type Rule struct {
InboundRule *sync.Map // Key: Tag, Value: []api.DetectRule Rule *sync.Map // Key: Tag, Value: *panel.DetectRule
InboundProtocolRule *sync.Map // Key: Tag, Value: []string
InboundDetectResult *sync.Map // key: Tag, Value: mapset.NewSet []api.DetectResult
} }
func NewRule() *Rule { func NewRule() *Rule {
return &Rule{ return &Rule{
InboundRule: new(sync.Map), Rule: new(sync.Map),
InboundProtocolRule: new(sync.Map),
InboundDetectResult: new(sync.Map),
} }
} }
func (r *Rule) UpdateRule(tag string, newRuleList []panel.DetectRule) error { func (r *Rule) UpdateRule(tag string, newRuleList *panel.DetectRule) error {
if value, ok := r.InboundRule.LoadOrStore(tag, newRuleList); ok { if value, ok := r.Rule.LoadOrStore(tag, newRuleList); ok {
oldRuleList := value.([]panel.DetectRule) oldRuleList := value.([]panel.DestinationRule)
if !reflect.DeepEqual(oldRuleList, newRuleList) { if !reflect.DeepEqual(oldRuleList, newRuleList) {
r.InboundRule.Store(tag, newRuleList) r.Rule.Store(tag, newRuleList)
} }
} }
return nil return nil
} }
func (r *Rule) UpdateProtocolRule(tag string, ruleList []string) error { func (r *Rule) Detect(tag string, destination string, protocol string) (reject bool) {
if value, ok := r.InboundProtocolRule.LoadOrStore(tag, ruleList); ok {
old := value.([]string)
if !reflect.DeepEqual(old, ruleList) {
r.InboundProtocolRule.Store(tag, ruleList)
}
}
return nil
}
func (r *Rule) GetDetectResult(tag string) ([]panel.DetectResult, error) {
detectResult := make([]panel.DetectResult, 0)
if value, ok := r.InboundDetectResult.LoadAndDelete(tag); ok {
resultSet := value.(mapset.Set)
it := resultSet.Iterator()
for result := range it.C {
detectResult = append(detectResult, result.(panel.DetectResult))
}
}
return detectResult, nil
}
func (r *Rule) Detect(tag string, destination string, email string) (reject bool) {
reject = false reject = false
var hitRuleID = -1
// If we have some rule for this inbound // If we have some rule for this inbound
if value, ok := r.InboundRule.Load(tag); ok { if value, ok := r.Rule.Load(tag); ok {
ruleList := value.([]panel.DetectRule) ruleList := value.(*panel.DetectRule)
for _, r := range ruleList { for i, _ := range ruleList.DestinationRule {
if r.Pattern.Match([]byte(destination)) { if ruleList.DestinationRule[i].Pattern.Match([]byte(destination)) {
hitRuleID = r.ID
reject = true reject = true
break break
} }
} }
// If we hit some rule if !reject {
if reject && hitRuleID != -1 { for _, v := range ruleList.ProtocolRule {
l := strings.Split(email, "|") if v == protocol {
uid, err := strconv.Atoi(l[len(l)-1])
if err != nil {
newError(fmt.Sprintf("Record illegal behavior failed! Cannot find user's uid: %s", email)).AtDebug().WriteToLog()
return reject
}
newSet := mapset.NewSetWith(panel.DetectResult{UID: uid, RuleID: hitRuleID})
// If there are any hit history
if v, ok := r.InboundDetectResult.LoadOrStore(tag, newSet); ok {
resultSet := v.(mapset.Set)
// If this is a new record
if resultSet.Add(panel.DetectResult{UID: uid, RuleID: hitRuleID}) {
r.InboundDetectResult.Store(tag, resultSet)
}
}
}
}
return reject
}
func (r *Rule) ProtocolDetect(tag string, protocol string) bool {
if value, ok := r.InboundProtocolRule.Load(tag); ok {
ruleList := value.([]string)
for _, r := range ruleList {
if r == protocol {
return true return true
} }
} }
} }
return false }
return reject
} }

View File

@ -47,3 +47,7 @@ func (p *Core) UpdateInboundLimiter(tag string, deleted []panel.UserInfo) error
func (p *Core) DeleteInboundLimiter(tag string) error { func (p *Core) DeleteInboundLimiter(tag string) error {
return p.dispatcher.Limiter.DeleteInboundLimiter(tag) return p.dispatcher.Limiter.DeleteInboundLimiter(tag)
} }
func (p *Core) UpdateRule(tag string, newRuleList *panel.DetectRule) error {
return p.dispatcher.RuleManager.UpdateRule(tag, newRuleList)
}

View File

@ -1,18 +0,0 @@
package core
import (
"github.com/Yuzuki616/V2bX/api/panel"
)
func (p *Core) UpdateRule(tag string, newRuleList []panel.DetectRule) error {
return p.dispatcher.RuleManager.UpdateRule(tag, newRuleList)
}
func (p *Core) UpdateProtocolRule(tag string, newRuleList []string) error {
return p.dispatcher.RuleManager.UpdateProtocolRule(tag, newRuleList)
}
func (p *Core) GetDetectResult(tag string) ([]panel.DetectResult, error) {
return p.dispatcher.RuleManager.GetDetectResult(tag)
}

View File

@ -72,17 +72,12 @@ func (c *Node) Start() error {
} }
// Add Rule Manager // Add Rule Manager
if !c.config.DisableGetRule { if !c.config.DisableGetRule {
if ruleList, protocolRule, err := c.apiClient.GetNodeRule(); err != nil { if ruleList, err := c.apiClient.GetNodeRule(); err != nil {
log.Printf("Get rule list filed: %s", err) log.Printf("Get rule list filed: %s", err)
} else if len(ruleList) > 0 { } else if ruleList != nil {
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil { if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
log.Print(err) log.Print(err)
} }
if len(protocolRule) > 0 {
if err := c.server.UpdateProtocolRule(c.Tag, protocolRule); err != nil {
log.Print(err)
}
}
} }
} }
c.nodeInfoMonitorPeriodic = &task.Periodic{ c.nodeInfoMonitorPeriodic = &task.Periodic{
@ -193,17 +188,12 @@ func (c *Node) nodeInfoMonitor() (err error) {
// Check Rule // Check Rule
if !c.config.DisableGetRule { if !c.config.DisableGetRule {
if ruleList, protocolRule, err := c.apiClient.GetNodeRule(); err != nil { if ruleList, err := c.apiClient.GetNodeRule(); err != nil {
log.Printf("Get rule list filed: %s", err) log.Printf("Get rule list filed: %s", err)
} else if len(ruleList) > 0 { } else if ruleList != nil {
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil { if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
log.Print(err) log.Print(err)
} }
if len(protocolRule) > 0 {
if err := c.server.UpdateProtocolRule(c.Tag, protocolRule); err != nil {
log.Print(err)
}
}
} }
} }

View File

@ -3,26 +3,26 @@ package node_test
import ( import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/api/panel" "github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core"
_ "github.com/Yuzuki616/V2bX/core/distro/all"
. "github.com/Yuzuki616/V2bX/node" . "github.com/Yuzuki616/V2bX/node"
xCore "github.com/xtls/xray-core/core"
coreConf "github.com/xtls/xray-core/infra/conf"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
"syscall" "syscall"
"testing" "testing"
"github.com/Yuzuki616/V2bX/api"
_ "github.com/Yuzuki616/V2bX/example/distro/all"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/infra/conf"
) )
func TestController(t *testing.T) { func TestController(t *testing.T) {
serverConfig := &conf.Config{ serverConfig := &coreConf.Config{
Stats: &conf.StatsConfig{}, Stats: &coreConf.StatsConfig{},
LogConfig: &conf.LogConfig{LogLevel: "debug"}, LogConfig: &coreConf.LogConfig{LogLevel: "debug"},
} }
policyConfig := &conf.PolicyConfig{} policyConfig := &coreConf.PolicyConfig{}
policyConfig.Levels = map[uint32]*conf.Policy{0: &conf.Policy{ policyConfig.Levels = map[uint32]*coreConf.Policy{0: &coreConf.Policy{
StatsUserUplink: true, StatsUserUplink: true,
StatsUserDownlink: true, StatsUserDownlink: true,
}} }}
@ -37,7 +37,7 @@ func TestController(t *testing.T) {
// serial.ToTypedMessage(&stats.Config{}), // serial.ToTypedMessage(&stats.Config{}),
// }} // }}
server, err := core.New(config) server, err := xCore.New(config)
defer server.Close() defer server.Close()
if err != nil { if err != nil {
t.Errorf("failed to create instance: %s", err) t.Errorf("failed to create instance: %s", err)
@ -45,35 +45,36 @@ func TestController(t *testing.T) {
if err = server.Start(); err != nil { if err = server.Start(); err != nil {
t.Errorf("Failed to start instance: %s", err) t.Errorf("Failed to start instance: %s", err)
} }
certConfig := &CertConfig{ certConfig := &conf.CertConfig{
CertMode: "http", CertMode: "http",
CertDomain: "test.ss.tk", CertDomain: "test.ss.tk",
Provider: "alidns", Provider: "alidns",
Email: "ss@ss.com", Email: "ss@ss.com",
} }
controlerconfig := &Config{ controlerconfig := &conf.ControllerConfig{
UpdatePeriodic: 5, UpdatePeriodic: 5,
CertConfig: certConfig, CertConfig: certConfig,
} }
apiConfig := &api.Config{ apiConfig := &conf.ApiConfig{
APIHost: "http://127.0.0.1:667", APIHost: "http://127.0.0.1:667",
Key: "123", Key: "123",
NodeID: 41, NodeID: 41,
NodeType: "V2ray", NodeType: "V2ray",
} }
apiclient := panel.New(apiConfig) apiclient := panel.New(apiConfig)
c := New(server, apiclient, controlerconfig) c := &core.Core{Server: server}
c.Start()
node := New(c, apiclient, controlerconfig)
fmt.Println("Sleep 1s") fmt.Println("Sleep 1s")
err = c.Start() err = node.Start()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
//Explicitly triggering GC to remove garbage from config loading. //Explicitly triggering GC to remove garbage from config loading.
runtime.GC() runtime.GC()
{ {
osSignals := make(chan os.Signal, 1) osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM) signal.Notify(osSignals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
<-osSignals <-osSignals
} }
} }