chore: Replace regexp with regexp2 for better filtering (#467)

This commit is contained in:
汐殇
2024-04-02 12:35:11 +08:00
committed by GitHub
parent b6c3f69bf3
commit 713d991f2b
4 changed files with 27 additions and 11 deletions

View File

@ -108,7 +108,7 @@ var (
log.Infof("Include config files: [%v]", strings.Join(includes, ", ")) log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
if err := Run(log, conf, []string{filepath.Dir(cfgFile)}); err != nil { if err := Run(log, conf, []string{filepath.Dir(cfgFile)}); err != nil {
logrus.Fatalln(err) log.Fatalln(err)
} }
}, },
} }

View File

@ -7,11 +7,12 @@ package outbound
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"github.com/daeuniverse/dae/component/outbound/dialer" "github.com/daeuniverse/dae/component/outbound/dialer"
"github.com/daeuniverse/dae/pkg/config_parser" "github.com/daeuniverse/dae/pkg/config_parser"
"github.com/dlclark/regexp2"
"github.com/sirupsen/logrus"
) )
const ( const (
@ -28,12 +29,14 @@ const (
) )
type DialerSet struct { type DialerSet struct {
log *logrus.Logger
dialers []*dialer.Dialer dialers []*dialer.Dialer
nodeToTagMap map[*dialer.Dialer]string nodeToTagMap map[*dialer.Dialer]string
} }
func NewDialerSetFromLinks(option *dialer.GlobalOption, tagToNodeList map[string][]string) *DialerSet { func NewDialerSetFromLinks(option *dialer.GlobalOption, tagToNodeList map[string][]string) *DialerSet {
s := &DialerSet{ s := &DialerSet{
log: option.Log,
dialers: make([]*dialer.Dialer, 0), dialers: make([]*dialer.Dialer, 0),
nodeToTagMap: make(map[*dialer.Dialer]string), nodeToTagMap: make(map[*dialer.Dialer]string),
} }
@ -41,7 +44,7 @@ func NewDialerSetFromLinks(option *dialer.GlobalOption, tagToNodeList map[string
for _, node := range nodes { for _, node := range nodes {
d, err := dialer.NewFromLink(option, dialer.InstanceOption{DisableCheck: false}, node, subscriptionTag) d, err := dialer.NewFromLink(option, dialer.InstanceOption{DisableCheck: false}, node, subscriptionTag)
if err != nil { if err != nil {
option.Log.Infof("failed to parse node: %v", err) s.log.Infof("failed to parse node: %v", err)
continue continue
} }
s.dialers = append(s.dialers, d) s.dialers = append(s.dialers, d)
@ -69,24 +72,29 @@ func (s *DialerSet) filterHit(dialer *dialer.Dialer, filters []*config_parser.Fu
switch filter.Name { switch filter.Name {
case FilterInput_Name: case FilterInput_Name:
// Or // Or
loop:
for _, param := range filter.Params { for _, param := range filter.Params {
switch param.Key { switch param.Key {
case FilterKey_Name_Regex: 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()) //logrus.Warnln(param.Val, matched, dialer.Name())
if matched { if matched {
subFilterHit = true subFilterHit = true
break break loop
} }
case FilterKey_Name_Keyword: case FilterKey_Name_Keyword:
if strings.Contains(dialer.Property().Name, param.Val) { if strings.Contains(dialer.Property().Name, param.Val) {
subFilterHit = true subFilterHit = true
break break loop
} }
case "": case "":
if dialer.Property().Name == param.Val { if dialer.Property().Name == param.Val {
subFilterHit = true subFilterHit = true
break break loop
} }
default: default:
return false, fmt.Errorf(`unsupported filter key "%v" in "filter: %v()"`, param.Key, filter.Name) 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: case FilterInput_SubscriptionTag:
// Or // Or
loop2:
for _, param := range filter.Params { for _, param := range filter.Params {
switch param.Key { switch param.Key {
case FilterInput_SubscriptionTag_Regex: case FilterInput_SubscriptionTag_Regex:
matched, _ := regexp.MatchString(param.Val, s.nodeToTagMap[dialer]) regex, err := regexp2.Compile(param.Val, 0)
//logrus.Warnln(param.Val, matched, dialer.Name()) 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 { if matched {
subFilterHit = true subFilterHit = true
break break loop2
} }
//logrus.Warnln(param.Val, matched, dialer.Name())
case "": case "":
// Full // Full
if s.nodeToTagMap[dialer] == param.Val { if s.nodeToTagMap[dialer] == param.Val {
subFilterHit = true subFilterHit = true
break break loop2
} }
default: default:
return false, fmt.Errorf(`unsupported filter key "%v" in "filter: %v()"`, param.Key, filter.Name) return false, fmt.Errorf(`unsupported filter key "%v" in "filter: %v()"`, param.Key, filter.Name)

1
go.mod
View File

@ -55,6 +55,7 @@ require (
github.com/dgryski/go-idea v0.0.0-20170306091226-d2fb45a411fb // indirect 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-metro v0.0.0-20211217172704-adc40b04c140 // indirect
github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152 // 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/eknkc/basex v1.0.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect github.com/google/uuid v1.3.0 // indirect

2
go.sum
View File

@ -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-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 h1:ED31mPIxDJnrLt9W9dH5xgd/6KjzEACKHBVGQ33czc0=
github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152/go.mod h1:I9fhc/EvSg88cDxmfQ47v35Ssz9rlFunL/KY0A1JAYI= 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 h1:fBHFH+Y/GPGFGo7LIrErQc3p2MeAhoIQNgaxPWYsSxk=
github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521/go.mod h1:ucvhdsUCE3TH0LoLRb6ShHiJl8e39dGlx6A4g/ujlow= github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521/go.mod h1:ucvhdsUCE3TH0LoLRb6ShHiJl8e39dGlx6A4g/ujlow=
github.com/eknkc/basex v1.0.1 h1:TcyAkqh4oJXgV3WYyL4KEfCMk9W8oJCpmx1bo+jVgKY= github.com/eknkc/basex v1.0.1 h1:TcyAkqh4oJXgV3WYyL4KEfCMk9W8oJCpmx1bo+jVgKY=