diff --git a/adapter/outboundgroup/parser.go b/adapter/outboundgroup/parser.go index 1bcdb81..1bac3c3 100644 --- a/adapter/outboundgroup/parser.go +++ b/adapter/outboundgroup/parser.go @@ -3,13 +3,14 @@ package outboundgroup import ( "errors" "fmt" - "regexp" "github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/structure" C "github.com/Dreamacro/clash/constant" types "github.com/Dreamacro/clash/constant/provider" + + regexp "github.com/dlclark/regexp2" ) var ( @@ -53,7 +54,7 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide ) if groupOption.Filter != "" { - f, err := regexp.Compile(groupOption.Filter) + f, err := regexp.Compile(groupOption.Filter, regexp.None) if err != nil { return nil, fmt.Errorf("%s: invalid filter regex: %w", groupName, err) } diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 03fd697..c187307 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "regexp" "runtime" "time" @@ -14,6 +13,7 @@ import ( C "github.com/Dreamacro/clash/constant" types "github.com/Dreamacro/clash/constant/provider" + regexp "github.com/dlclark/regexp2" "github.com/samber/lo" "gopkg.in/yaml.v3" ) @@ -101,7 +101,7 @@ func stopProxyProvider(pd *ProxySetProvider) { } func NewProxySetProvider(name string, interval time.Duration, filter string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) { - filterReg, err := regexp.Compile(filter) + filterReg, err := regexp.Compile(filter, regexp.None) if err != nil { return nil, fmt.Errorf("invalid filter regex: %w", err) } @@ -133,8 +133,14 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, veh proxies := []C.Proxy{} for idx, mapping := range schema.Proxies { - if name, ok := mapping["name"].(string); ok && len(filter) > 0 && !filterReg.MatchString(name) { - continue + if name, ok := mapping["name"].(string); ok && len(filter) > 0 { + matched, err := filterReg.MatchString(name) + if err != nil { + return nil, fmt.Errorf("regex filter failed: %w", err) + } + if !matched { + continue + } } proxy, err := adapter.ParseProxy(mapping) if err != nil { @@ -286,7 +292,8 @@ func (fp *FilterableProvider) Proxies() []C.Proxy { return lo.Filter( item.Proxies(), func(item C.Proxy, _ int) bool { - return fp.filterReg.MatchString(item.Name()) + matched, _ := fp.filterReg.MatchString(item.Name()) + return matched }) }) diff --git a/go.mod b/go.mod index a0aa0f5..2a0c2f6 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( github.com/Dreamacro/protobytes v0.0.0-20230324064118-87bc784139cd + github.com/dlclark/regexp2 v1.10.0 github.com/go-chi/chi/v5 v5.0.8 github.com/go-chi/cors v1.2.1 github.com/go-chi/render v1.0.2 diff --git a/go.sum b/go.sum index 9efbf40..ea3ea23 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0= +github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0= github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=