feat: regexp2
This commit is contained in:
parent
2092a481b3
commit
3fd954d185
@ -1,7 +1,7 @@
|
|||||||
package constant
|
package constant
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
regexp "github.com/dlclark/regexp2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var RewriteTypeMapping = map[string]RewriteType{
|
var RewriteTypeMapping = map[string]RewriteType{
|
||||||
|
@ -181,16 +181,21 @@ func matchRewriteRule(url string, isRequest bool) (rr C.Rewrite, sub []string, f
|
|||||||
rewrites := tunnel.Rewrites()
|
rewrites := tunnel.Rewrites()
|
||||||
if isRequest {
|
if isRequest {
|
||||||
found = rewrites.SearchInRequest(func(r C.Rewrite) bool {
|
found = rewrites.SearchInRequest(func(r C.Rewrite) bool {
|
||||||
sub = r.URLRegx().FindStringSubmatch(url)
|
sub, err := r.URLRegx().FindStringMatch(url)
|
||||||
if len(sub) != 0 {
|
if err != nil {
|
||||||
rr = r
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
rr = r
|
||||||
|
var groups []string
|
||||||
|
for _, fg := range sub.Groups() {
|
||||||
|
groups = append(groups, fg.String())
|
||||||
|
}
|
||||||
|
return true
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
found = rewrites.SearchInResponse(func(r C.Rewrite) bool {
|
found = rewrites.SearchInResponse(func(r C.Rewrite) bool {
|
||||||
if r.URLRegx().FindString(url) != "" {
|
if b, err := r.URLRegx().MatchString(url); b && err == nil {
|
||||||
rr = r
|
rr = r
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package rewrites
|
package rewrites
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
regexp "github.com/dlclark/regexp2"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
@ -22,7 +22,7 @@ func ParseRewrite(line string) (C.Rewrite, error) {
|
|||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
urlRegx, err = regexp.Compile(strings.Trim(url, " "))
|
urlRegx, err = regexp.Compile(strings.Trim(url, " "), regexp.None)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ func ParseRewrite(line string) (C.Rewrite, error) {
|
|||||||
rulePayload = rs[0]
|
rulePayload = rs[0]
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
ruleRegx, err = regexp.Compile(rs[0])
|
ruleRegx, err = regexp.Compile(rs[0], regexp.None)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package rewrites
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"regexp"
|
regexp "github.com/dlclark/regexp2"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -61,18 +61,23 @@ func (r *RewriteRule) ReplaceSubPayload(oldData string) string {
|
|||||||
return oldData
|
return oldData
|
||||||
}
|
}
|
||||||
|
|
||||||
sub := r.ruleRegx.FindStringSubmatch(oldData)
|
sub, err := r.ruleRegx.FindStringMatch(oldData)
|
||||||
l := len(sub)
|
if err != nil {
|
||||||
|
|
||||||
if l == 0 {
|
|
||||||
return oldData
|
return oldData
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 1; i < l; i++ {
|
var groups []string
|
||||||
payload = strings.ReplaceAll(payload, "$"+strconv.Itoa(i), sub[i])
|
for _, fg := range sub.Groups() {
|
||||||
|
groups = append(groups, fg.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.ReplaceAll(oldData, sub[0], payload)
|
l := len(groups)
|
||||||
|
|
||||||
|
for i := 1; i < l; i++ {
|
||||||
|
payload = strings.ReplaceAll(payload, "$"+strconv.Itoa(i), groups[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.ReplaceAll(oldData, groups[0], payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRewriteRule(urlRegx *regexp.Regexp, ruleType C.RewriteType, ruleRegx *regexp.Regexp, rulePayload string) *RewriteRule {
|
func NewRewriteRule(urlRegx *regexp.Regexp, ruleType C.RewriteType, ruleRegx *regexp.Regexp, rulePayload string) *RewriteRule {
|
||||||
|
Reference in New Issue
Block a user