mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
performance optimization
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
18
core/rule.go
18
core/rule.go
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user