Skip to content

Commit

Permalink
chore: Replace regexp with regexp2 for better filtering (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
xishang0128 authored Apr 2, 2024
1 parent b6c3f69 commit 713d991
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down
33 changes: 23 additions & 10 deletions component/outbound/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -28,20 +29,22 @@ 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),
}
for subscriptionTag, nodes := range tagToNodeList {
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)
Expand Down Expand Up @@ -69,45 +72,55 @@ 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)
}
}
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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 713d991

Please sign in to comment.