V2bX/core/app/dispatcher/rule.go

43 lines
904 B
Go
Raw Normal View History

package dispatcher
2022-06-01 13:35:41 -04:00
import (
"github.com/Yuzuki616/V2bX/api/panel"
2022-06-01 13:35:41 -04:00
"reflect"
"sync"
)
type Rule struct {
2022-09-14 21:24:14 -04:00
Rule *sync.Map // Key: Tag, Value: *panel.DetectRule
2022-06-01 13:35:41 -04:00
}
func NewRule() *Rule {
return &Rule{
2022-09-14 21:24:14 -04:00
Rule: new(sync.Map),
2022-06-01 13:35:41 -04:00
}
}
func (r *Rule) UpdateRule(tag string, newRuleList []panel.DestinationRule) error {
2022-09-14 21:24:14 -04:00
if value, ok := r.Rule.LoadOrStore(tag, newRuleList); ok {
oldRuleList := value.([]panel.DestinationRule)
2022-06-01 13:35:41 -04:00
if !reflect.DeepEqual(oldRuleList, newRuleList) {
2022-09-14 21:24:14 -04:00
r.Rule.Store(tag, newRuleList)
2022-06-12 09:10:20 -04:00
}
}
return nil
}
2022-09-14 21:24:14 -04:00
func (r *Rule) Detect(tag string, destination string, protocol string) (reject bool) {
2022-06-01 13:35:41 -04:00
reject = false
// If we have some rule for this inbound
2022-09-14 21:24:14 -04:00
if value, ok := r.Rule.Load(tag); ok {
ruleList := value.([]panel.DestinationRule)
for i := range ruleList {
if ruleList[i].Pattern.Match([]byte(destination)) {
2022-06-01 13:35:41 -04:00
reject = true
break
}
}
}
return reject
}