mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-09 23:40:08 +07:00
chore: Replace regexp with regexp2 for better filtering (#467)
This commit is contained in:
@ -108,7 +108,7 @@ var (
|
||||
|
||||
log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
|
||||
if err := Run(log, conf, []string{filepath.Dir(cfgFile)}); err != nil {
|
||||
logrus.Fatalln(err)
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -7,11 +7,12 @@ package outbound
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/daeuniverse/dae/component/outbound/dialer"
|
||||
"github.com/daeuniverse/dae/pkg/config_parser"
|
||||
"github.com/dlclark/regexp2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -28,12 +29,14 @@ const (
|
||||
)
|
||||
|
||||
type DialerSet struct {
|
||||
log *logrus.Logger
|
||||
dialers []*dialer.Dialer
|
||||
nodeToTagMap map[*dialer.Dialer]string
|
||||
}
|
||||
|
||||
func NewDialerSetFromLinks(option *dialer.GlobalOption, tagToNodeList map[string][]string) *DialerSet {
|
||||
s := &DialerSet{
|
||||
log: option.Log,
|
||||
dialers: make([]*dialer.Dialer, 0),
|
||||
nodeToTagMap: make(map[*dialer.Dialer]string),
|
||||
}
|
||||
@ -41,7 +44,7 @@ func NewDialerSetFromLinks(option *dialer.GlobalOption, tagToNodeList map[string
|
||||
for _, node := range nodes {
|
||||
d, err := dialer.NewFromLink(option, dialer.InstanceOption{DisableCheck: false}, node, subscriptionTag)
|
||||
if err != nil {
|
||||
option.Log.Infof("failed to parse node: %v", err)
|
||||
s.log.Infof("failed to parse node: %v", err)
|
||||
continue
|
||||
}
|
||||
s.dialers = append(s.dialers, d)
|
||||
@ -69,24 +72,29 @@ func (s *DialerSet) filterHit(dialer *dialer.Dialer, filters []*config_parser.Fu
|
||||
switch filter.Name {
|
||||
case FilterInput_Name:
|
||||
// Or
|
||||
loop:
|
||||
for _, param := range filter.Params {
|
||||
switch param.Key {
|
||||
case FilterKey_Name_Regex:
|
||||
matched, _ := regexp.MatchString(param.Val, dialer.Property().Name)
|
||||
regex, err := regexp2.Compile(param.Val, 0)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("bad regexp in filter %v: %w", filter.String(false, true, true), err)
|
||||
}
|
||||
matched, _ := regex.MatchString(dialer.Property().Name)
|
||||
//logrus.Warnln(param.Val, matched, dialer.Name())
|
||||
if matched {
|
||||
subFilterHit = true
|
||||
break
|
||||
break loop
|
||||
}
|
||||
case FilterKey_Name_Keyword:
|
||||
if strings.Contains(dialer.Property().Name, param.Val) {
|
||||
subFilterHit = true
|
||||
break
|
||||
break loop
|
||||
}
|
||||
case "":
|
||||
if dialer.Property().Name == param.Val {
|
||||
subFilterHit = true
|
||||
break
|
||||
break loop
|
||||
}
|
||||
default:
|
||||
return false, fmt.Errorf(`unsupported filter key "%v" in "filter: %v()"`, param.Key, filter.Name)
|
||||
@ -94,20 +102,25 @@ func (s *DialerSet) filterHit(dialer *dialer.Dialer, filters []*config_parser.Fu
|
||||
}
|
||||
case FilterInput_SubscriptionTag:
|
||||
// Or
|
||||
loop2:
|
||||
for _, param := range filter.Params {
|
||||
switch param.Key {
|
||||
case FilterInput_SubscriptionTag_Regex:
|
||||
matched, _ := regexp.MatchString(param.Val, s.nodeToTagMap[dialer])
|
||||
//logrus.Warnln(param.Val, matched, dialer.Name())
|
||||
regex, err := regexp2.Compile(param.Val, 0)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("bad regexp in filter %v: %w", filter.String(false, true, true), err)
|
||||
}
|
||||
matched, _ := regex.MatchString(s.nodeToTagMap[dialer])
|
||||
if matched {
|
||||
subFilterHit = true
|
||||
break
|
||||
break loop2
|
||||
}
|
||||
//logrus.Warnln(param.Val, matched, dialer.Name())
|
||||
case "":
|
||||
// Full
|
||||
if s.nodeToTagMap[dialer] == param.Val {
|
||||
subFilterHit = true
|
||||
break
|
||||
break loop2
|
||||
}
|
||||
default:
|
||||
return false, fmt.Errorf(`unsupported filter key "%v" in "filter: %v()"`, param.Key, filter.Name)
|
||||
|
1
go.mod
1
go.mod
@ -55,6 +55,7 @@ require (
|
||||
github.com/dgryski/go-idea v0.0.0-20170306091226-d2fb45a411fb // indirect
|
||||
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 // indirect
|
||||
github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152 // indirect
|
||||
github.com/dlclark/regexp2 v1.11.0
|
||||
github.com/eknkc/basex v1.0.1 // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -29,6 +29,8 @@ github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 h1:y7y0Oa6UawqTFP
|
||||
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140/go.mod h1:c9O8+fpSOX1DM8cPNSkX/qsBWdkD4yd2dpciOWQjpBw=
|
||||
github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152 h1:ED31mPIxDJnrLt9W9dH5xgd/6KjzEACKHBVGQ33czc0=
|
||||
github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152/go.mod h1:I9fhc/EvSg88cDxmfQ47v35Ssz9rlFunL/KY0A1JAYI=
|
||||
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
|
||||
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||
github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521 h1:fBHFH+Y/GPGFGo7LIrErQc3p2MeAhoIQNgaxPWYsSxk=
|
||||
github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521/go.mod h1:ucvhdsUCE3TH0LoLRb6ShHiJl8e39dGlx6A4g/ujlow=
|
||||
github.com/eknkc/basex v1.0.1 h1:TcyAkqh4oJXgV3WYyL4KEfCMk9W8oJCpmx1bo+jVgKY=
|
||||
|
Reference in New Issue
Block a user