fix: regexp

This commit is contained in:
Adlyq 2023-09-04 13:31:18 +08:00
parent 1f7a883bfc
commit a0fd6cfeea
2 changed files with 32 additions and 25 deletions

View File

@ -6,7 +6,6 @@ import (
"errors"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"net/textproto"
"strconv"
@ -150,19 +149,20 @@ func (*RewriteHandler) HandleResponse(session *mitm.Session) *http.Response {
return nil
}
body, err := mitm.DecodeLatin1(bytes.NewReader(b))
if err != nil {
return nil
}
//body, err := mitm.DecodeLatin1(bytes.NewReader(b))
//if err != nil {
// return nil
//}
newBody := rule.ReplaceSubPayload(body)
newBody := rule.ReplaceSubPayload(string(b))
modifiedBody, err := mitm.EncodeLatin1(newBody)
if err != nil {
return nil
}
//modifiedBody, err := mitm.EncodeLatin1(newBody)
//if err != 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.ContentLength = int64(len(modifiedBody))
default:

View File

@ -61,27 +61,34 @@ func (r *RewriteRule) ReplaceURLPayload(matchSub []string) string {
func (r *RewriteRule) ReplaceSubPayload(oldData string) string {
payload := r.rulePayload
if r.ruleRegx == nil {
return 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
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)
return oldData
}
func NewRewriteRule(urlRegx *regexp.Regexp, ruleType C.RewriteType, ruleRegx *regexp.Regexp, rulePayload string) *RewriteRule {