mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
fix: regexp
This commit is contained in:
parent
1f7a883bfc
commit
a0fd6cfeea
|
@ -6,7 +6,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -150,19 +149,20 @@ func (*RewriteHandler) HandleResponse(session *mitm.Session) *http.Response {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := mitm.DecodeLatin1(bytes.NewReader(b))
|
//body, err := mitm.DecodeLatin1(bytes.NewReader(b))
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
return nil
|
// return nil
|
||||||
}
|
//}
|
||||||
|
|
||||||
newBody := rule.ReplaceSubPayload(body)
|
newBody := rule.ReplaceSubPayload(string(b))
|
||||||
|
|
||||||
modifiedBody, err := mitm.EncodeLatin1(newBody)
|
//modifiedBody, err := mitm.EncodeLatin1(newBody)
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
return nil
|
// return nil
|
||||||
}
|
//}
|
||||||
|
modifiedBody := []byte(newBody)
|
||||||
|
|
||||||
response.Body = ioutil.NopCloser(bytes.NewReader(modifiedBody))
|
response.Body = io.NopCloser(bytes.NewReader(modifiedBody))
|
||||||
response.Header.Del("Content-Encoding")
|
response.Header.Del("Content-Encoding")
|
||||||
response.ContentLength = int64(len(modifiedBody))
|
response.ContentLength = int64(len(modifiedBody))
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -61,27 +61,34 @@ func (r *RewriteRule) ReplaceURLPayload(matchSub []string) string {
|
||||||
|
|
||||||
func (r *RewriteRule) ReplaceSubPayload(oldData string) string {
|
func (r *RewriteRule) ReplaceSubPayload(oldData string) string {
|
||||||
payload := r.rulePayload
|
payload := r.rulePayload
|
||||||
|
|
||||||
if r.ruleRegx == nil {
|
if r.ruleRegx == nil {
|
||||||
return oldData
|
return oldData
|
||||||
}
|
}
|
||||||
|
|
||||||
sub, err := r.ruleRegx.FindStringMatch(oldData)
|
sub, err := r.ruleRegx.FindStringMatch(oldData)
|
||||||
if err != nil {
|
|
||||||
return oldData
|
for err == nil && sub != nil {
|
||||||
|
var (
|
||||||
|
groups []string
|
||||||
|
sPayload = payload
|
||||||
|
)
|
||||||
|
for _, fg := range sub.Groups() {
|
||||||
|
groups = append(groups, fg.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
l := len(groups)
|
||||||
|
|
||||||
|
for i := 1; i < l; i++ {
|
||||||
|
sPayload = strings.Replace(payload, "$"+strconv.Itoa(i), groups[i], 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
oldData = strings.Replace(oldData, groups[0], sPayload, 1)
|
||||||
|
|
||||||
|
sub, err = r.ruleRegx.FindNextMatch(sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
var groups []string
|
return oldData
|
||||||
for _, fg := range sub.Groups() {
|
|
||||||
groups = append(groups, fg.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user