43 lines
904 B
Go
Raw Normal View History

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