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