Skip to content

Commit

Permalink
fix: regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
Adlyq authored and xishang0128 committed Apr 3, 2024
1 parent 6731bc9 commit 698aecf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
22 changes: 11 additions & 11 deletions rewrite/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"net/textproto"
"strconv"
Expand Down Expand Up @@ -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:
Expand Down
29 changes: 18 additions & 11 deletions rewrite/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

var groups []string
for _, fg := range sub.Groups() {
groups = append(groups, fg.String())
}
for err == nil && sub != nil {
var (
groups []string
sPayload = payload
)
for _, fg := range sub.Groups() {
groups = append(groups, fg.String())
}

l := len(groups)
l := len(groups)

for i := 1; i < l; i++ {
payload = strings.ReplaceAll(payload, "$"+strconv.Itoa(i), groups[i])
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)
}

return strings.ReplaceAll(oldData, groups[0], payload)
return oldData
}

func NewRewriteRule(urlRegx *regexp.Regexp, ruleType C.RewriteType, ruleRegx *regexp.Regexp, rulePayload string) *RewriteRule {
Expand Down

0 comments on commit 698aecf

Please sign in to comment.