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" "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:

View File

@ -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 {