2022-08-16 01:04:33 -04:00
|
|
|
package dispatcher
|
2022-06-01 13:35:41 -04:00
|
|
|
|
|
|
|
import (
|
2022-08-16 01:04:33 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
2022-06-01 13:35:41 -04:00
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-06-04 05:12:28 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-16 01:04:33 -04:00
|
|
|
func NewRule() *Rule {
|
2022-06-04 05:12:28 -04:00
|
|
|
return &Rule{
|
2022-09-14 21:24:14 -04:00
|
|
|
Rule: new(sync.Map),
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-18 10:31:06 -05: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 {
|
2022-12-18 10:31:06 -05:00
|
|
|
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
|
|
|
|
}
|