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)
ReportUserTraffic(userTraffic []UserTraffic) (err error)
Describe() ClientInfo
GetNodeRule() (ruleList []DetectRule, protocolList []string, err error)
GetNodeRule() (ruleList *DetectRule, err error)
Debug()
}

View File

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

View File

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

View File

@ -1,104 +1,49 @@
package dispatcher
import (
"fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"reflect"
"strconv"
"strings"
"sync"
mapset "github.com/deckarep/golang-set"
)
type Rule struct {
InboundRule *sync.Map // Key: Tag, Value: []api.DetectRule
InboundProtocolRule *sync.Map // Key: Tag, Value: []string
InboundDetectResult *sync.Map // key: Tag, Value: mapset.NewSet []api.DetectResult
Rule *sync.Map // Key: Tag, Value: *panel.DetectRule
}
func NewRule() *Rule {
return &Rule{
InboundRule: new(sync.Map),
InboundProtocolRule: new(sync.Map),
InboundDetectResult: new(sync.Map),
Rule: new(sync.Map),
}
}
func (r *Rule) UpdateRule(tag string, newRuleList []panel.DetectRule) error {
if value, ok := r.InboundRule.LoadOrStore(tag, newRuleList); ok {
oldRuleList := value.([]panel.DetectRule)
func (r *Rule) UpdateRule(tag string, newRuleList *panel.DetectRule) error {
if value, ok := r.Rule.LoadOrStore(tag, newRuleList); ok {
oldRuleList := value.([]panel.DestinationRule)
if !reflect.DeepEqual(oldRuleList, newRuleList) {
r.InboundRule.Store(tag, newRuleList)
r.Rule.Store(tag, newRuleList)
}
}
return nil
}
func (r *Rule) UpdateProtocolRule(tag string, ruleList []string) error {
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) {
func (r *Rule) Detect(tag string, destination string, protocol string) (reject bool) {
reject = false
var hitRuleID = -1
// If we have some rule for this inbound
if value, ok := r.InboundRule.Load(tag); ok {
ruleList := value.([]panel.DetectRule)
for _, r := range ruleList {
if r.Pattern.Match([]byte(destination)) {
hitRuleID = r.ID
if value, ok := r.Rule.Load(tag); ok {
ruleList := value.(*panel.DetectRule)
for i, _ := range ruleList.DestinationRule {
if ruleList.DestinationRule[i].Pattern.Match([]byte(destination)) {
reject = true
break
}
}
// If we hit some rule
if reject && hitRuleID != -1 {
l := strings.Split(email, "|")
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)
if !reject {
for _, v := range ruleList.ProtocolRule {
if v == protocol {
return true
}
}
}
}
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 false
}

View File

@ -47,3 +47,7 @@ func (p *Core) UpdateInboundLimiter(tag string, deleted []panel.UserInfo) error
func (p *Core) DeleteInboundLimiter(tag string) error {
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
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)
} else if len(ruleList) > 0 {
} else if ruleList != nil {
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
log.Print(err)
}
if len(protocolRule) > 0 {
if err := c.server.UpdateProtocolRule(c.Tag, protocolRule); err != nil {
log.Print(err)
}
}
}
}
c.nodeInfoMonitorPeriodic = &task.Periodic{
@ -193,17 +188,12 @@ func (c *Node) nodeInfoMonitor() (err error) {
// Check Rule
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)
} else if len(ruleList) > 0 {
} else if ruleList != nil {
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
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 (
"fmt"
"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"
xCore "github.com/xtls/xray-core/core"
coreConf "github.com/xtls/xray-core/infra/conf"
"os"
"os/signal"
"runtime"
"syscall"
"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) {
serverConfig := &conf.Config{
Stats: &conf.StatsConfig{},
LogConfig: &conf.LogConfig{LogLevel: "debug"},
serverConfig := &coreConf.Config{
Stats: &coreConf.StatsConfig{},
LogConfig: &coreConf.LogConfig{LogLevel: "debug"},
}
policyConfig := &conf.PolicyConfig{}
policyConfig.Levels = map[uint32]*conf.Policy{0: &conf.Policy{
policyConfig := &coreConf.PolicyConfig{}
policyConfig.Levels = map[uint32]*coreConf.Policy{0: &coreConf.Policy{
StatsUserUplink: true,
StatsUserDownlink: true,
}}
@ -37,7 +37,7 @@ func TestController(t *testing.T) {
// serial.ToTypedMessage(&stats.Config{}),
// }}
server, err := core.New(config)
server, err := xCore.New(config)
defer server.Close()
if err != nil {
t.Errorf("failed to create instance: %s", err)
@ -45,35 +45,36 @@ func TestController(t *testing.T) {
if err = server.Start(); err != nil {
t.Errorf("Failed to start instance: %s", err)
}
certConfig := &CertConfig{
certConfig := &conf.CertConfig{
CertMode: "http",
CertDomain: "test.ss.tk",
Provider: "alidns",
Email: "ss@ss.com",
}
controlerconfig := &Config{
controlerconfig := &conf.ControllerConfig{
UpdatePeriodic: 5,
CertConfig: certConfig,
}
apiConfig := &api.Config{
apiConfig := &conf.ApiConfig{
APIHost: "http://127.0.0.1:667",
Key: "123",
NodeID: 41,
NodeType: "V2ray",
}
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")
err = c.Start()
err = node.Start()
if err != nil {
t.Error(err)
}
//Explicitly triggering GC to remove garbage from config loading.
runtime.GC()
{
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
}
}