From 27292dac0c8844656cd080154f087cf11165931a Mon Sep 17 00:00:00 2001 From: admin <2833154405@qq.com> Date: Wed, 16 Feb 2022 22:18:05 +0800 Subject: [PATCH] Replace the regular implementation of the filter for proxy-providers and proxy-groups with regex2 --- adapter/outboundgroup/common.go | 12 +++++++----- adapter/provider/provider.go | 9 ++++++--- go.mod | 1 + go.sum | 2 ++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/adapter/outboundgroup/common.go b/adapter/outboundgroup/common.go index 3f84ba423..89144c43e 100644 --- a/adapter/outboundgroup/common.go +++ b/adapter/outboundgroup/common.go @@ -2,7 +2,7 @@ package outboundgroup import ( "github.com/Dreamacro/clash/tunnel" - "regexp" + "github.com/dlclark/regexp2" "time" C "github.com/Dreamacro/clash/constant" @@ -23,12 +23,14 @@ func getProvidersProxies(providers []provider.ProxyProvider, touch bool, filter } } - var filterReg *regexp.Regexp = nil - matchedProxies := []C.Proxy{} + var filterReg *regexp2.Regexp = nil + var matchedProxies []C.Proxy if len(filter) > 0 { - filterReg = regexp.MustCompile(filter) + //filterReg = regexp.MustCompile(filter) + filterReg = regexp2.MustCompile(filter, 0) for _, p := range proxies { - if filterReg.MatchString(p.Name()) { + //if filterReg.MatchString(p.Name()) { + if mat, _ := filterReg.FindStringMatch(p.Name()); mat != nil { matchedProxies = append(matchedProxies, p) } } diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 49aceda02..e3da2c3a8 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "regexp" + "github.com/dlclark/regexp2" "runtime" "time" @@ -101,7 +101,8 @@ 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) + filterReg, err := regexp2.Compile(filter, 0) if err != nil { return nil, fmt.Errorf("invalid filter regex: %w", err) } @@ -129,7 +130,9 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, veh proxies := []C.Proxy{} for idx, mapping := range schema.Proxies { - if name, ok := mapping["name"]; ok && len(filter) > 0 && !filterReg.MatchString(name.(string)) { + name, ok := mapping["name"] + mat, _ := filterReg.FindStringMatch(name.(string)) + if ok && len(filter) > 0 && mat == nil { continue } proxy, err := adapter.ParseProxy(mapping) diff --git a/go.mod b/go.mod index 64f5b3290..0adc2ad58 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/Dreamacro/go-shadowsocks2 v0.1.7 + github.com/dlclark/regexp2 v1.4.0 github.com/go-chi/chi/v5 v5.0.7 github.com/go-chi/cors v1.2.0 github.com/go-chi/render v1.0.1 diff --git a/go.sum b/go.sum index e315e823e..3028c0659 100644 --- a/go.sum +++ b/go.sum @@ -118,6 +118,8 @@ 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/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= +github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=