Skip to content

Commit

Permalink
update go-mitmproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
yhy0 committed Apr 12, 2024
1 parent ac28656 commit db2c851
Show file tree
Hide file tree
Showing 23 changed files with 2,297 additions and 1,757 deletions.
16 changes: 8 additions & 8 deletions pkg/mitmproxy/go-mitmproxy/addon/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"strings"
"unicode"

log "github.com/sirupsen/logrus"
"github.com/yhy0/Jie/pkg/mitmproxy/go-mitmproxy/proxy"
)
Expand Down Expand Up @@ -44,7 +44,7 @@ func (d *Dumper) Requestheaders(f *proxy.Flow) {
// call when <-f.Done()
func (d *Dumper) dump(f *proxy.Flow) {
// 参考 httputil.DumpRequest

buf := bytes.NewBuffer(make([]byte, 0))
fmt.Fprintf(buf, "%s %s %s\r\n", f.Request.Method, f.Request.URL.RequestURI(), f.Request.Proto)
fmt.Fprintf(buf, "Host: %s\r\n", f.Request.URL.Host)
Expand All @@ -54,26 +54,26 @@ func (d *Dumper) dump(f *proxy.Flow) {
if f.Request.Raw().Close {
fmt.Fprintf(buf, "Connection: close\r\n")
}

err := f.Request.Header.WriteSubset(buf, nil)
if err != nil {
log.Error(err)
}
buf.WriteString("\r\n")

if d.level == 1 && f.Request.Body != nil && len(f.Request.Body) > 0 && canPrint(f.Request.Body) {
buf.Write(f.Request.Body)
buf.WriteString("\r\n\r\n")
}

if f.Response != nil {
fmt.Fprintf(buf, "%v %v %v\r\n", f.Request.Proto, f.Response.StatusCode, http.StatusText(f.Response.StatusCode))
err = f.Response.Header.WriteSubset(buf, nil)
if err != nil {
log.Error(err)
}
buf.WriteString("\r\n")

if d.level == 1 && f.Response.Body != nil && len(f.Response.Body) > 0 && f.Response.IsTextContentType() {
body, err := f.Response.DecodedBody()
if err == nil && body != nil && len(body) > 0 {
Expand All @@ -82,9 +82,9 @@ func (d *Dumper) dump(f *proxy.Flow) {
}
}
}

buf.WriteString("\r\n\r\n")

_, err = d.out.Write(buf.Bytes())
if err != nil {
log.Error(err)
Expand Down
21 changes: 11 additions & 10 deletions pkg/mitmproxy/go-mitmproxy/addon/maplocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"os"
"path"
"strings"

log "github.com/sirupsen/logrus"
"github.com/yhy0/Jie/pkg/mitmproxy/go-mitmproxy/helper"
"github.com/yhy0/Jie/pkg/mitmproxy/go-mitmproxy/proxy"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func (item *mapLocalItem) response(req *proxy.Request) (string, *proxy.Response)
}
return stat, nil
}

respFile := func(filepath string) *proxy.Response {
file, err := os.Open(filepath)
if err != nil {
Expand All @@ -58,28 +59,28 @@ func (item *mapLocalItem) response(req *proxy.Request) (string, *proxy.Response)
BodyReader: file,
}
}

stat, resp := getStat(item.To.Path)
if resp != nil {
return item.To.Path, resp
}

if !stat.IsDir() {
return item.To.Path, respFile(item.To.Path)
}

// is dir
subPath := req.URL.Path
if item.From.Path != "" && strings.HasSuffix(item.From.Path, "/*") {
subPath = req.URL.Path[len(item.From.Path)-2:]
}
filepath := path.Join(item.To.Path, subPath)

stat, resp = getStat(filepath)
if resp != nil {
return filepath, resp
}

if !stat.IsDir() {
return filepath, respFile(filepath)
} else {
Expand Down Expand Up @@ -130,12 +131,12 @@ func (ml *MapLocal) validate() error {
}

func NewMapLocalFromFile(filename string) (*MapLocal, error) {
mapLocal, err := proxy.NewStructFromFile[MapLocal](filename)
if err != nil {
var mapLocal MapLocal
if err := helper.NewStructFromFile(filename, &mapLocal); err != nil {
return nil, err
}
if err := mapLocal.validate(); err != nil {
return nil, err
}
return mapLocal, nil
return &mapLocal, nil
}
9 changes: 5 additions & 4 deletions pkg/mitmproxy/go-mitmproxy/addon/mapremote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"fmt"
"path"
"strings"

"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/tidwall/match"
"github.com/yhy0/Jie/pkg/mitmproxy/go-mitmproxy/helper"
"github.com/yhy0/Jie/pkg/mitmproxy/go-mitmproxy/proxy"
)

Expand Down Expand Up @@ -122,12 +123,12 @@ func (mr *MapRemote) validate() error {
}

func NewMapRemoteFromFile(filename string) (*MapRemote, error) {
mapRemote, err := proxy.NewStructFromFile[MapRemote](filename)
if err != nil {
var mapRemote MapRemote
if err := helper.NewStructFromFile(filename, &mapRemote); err != nil {
return nil, err
}
if err := mapRemote.validate(); err != nil {
return nil, err
}
return mapRemote, nil
return &mapRemote, nil
}
32 changes: 16 additions & 16 deletions pkg/mitmproxy/go-mitmproxy/addon/mapremote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package addon
import (
"net/url"
"testing"

"github.com/yhy0/Jie/pkg/mitmproxy/go-mitmproxy/proxy"
)

Expand All @@ -16,9 +16,9 @@ func TestMapItemMatch(t *testing.T) {
Path: "/path/to/resource",
},
}

// test match

item := &mapRemoteItem{
From: &mapFrom{
Protocol: "https",
Expand All @@ -33,7 +33,7 @@ func TestMapItemMatch(t *testing.T) {
if !result {
t.Errorf("Expected true, but got false")
}

// empty Protocol and empty Method match
item.From = &mapFrom{
Protocol: "",
Expand All @@ -45,7 +45,7 @@ func TestMapItemMatch(t *testing.T) {
if !result {
t.Errorf("Expected true, but got false")
}

// empty Host match
item.From = &mapFrom{
Protocol: "",
Expand All @@ -57,7 +57,7 @@ func TestMapItemMatch(t *testing.T) {
if !result {
t.Errorf("Expected true, but got false")
}

// all empty match
item.From = &mapFrom{
Protocol: "",
Expand All @@ -69,9 +69,9 @@ func TestMapItemMatch(t *testing.T) {
if !result {
t.Errorf("Expected true, but got false")
}

// test not match

// diff Protocol
item.From = &mapFrom{
Protocol: "http",
Expand All @@ -83,7 +83,7 @@ func TestMapItemMatch(t *testing.T) {
if result {
t.Errorf("Expected true, but got false")
}

// diff Host
item.From = &mapFrom{
Protocol: "https",
Expand All @@ -95,7 +95,7 @@ func TestMapItemMatch(t *testing.T) {
if result {
t.Errorf("Expected true, but got false")
}

// diff Method
item.From = &mapFrom{
Protocol: "https",
Expand All @@ -107,7 +107,7 @@ func TestMapItemMatch(t *testing.T) {
if result {
t.Errorf("Expected true, but got false")
}

// diff Path
item.From = &mapFrom{
Protocol: "http",
Expand All @@ -132,7 +132,7 @@ func TestMapItemReplace(t *testing.T) {
},
}
}

item := &mapRemoteItem{
From: &mapFrom{
Protocol: "https",
Expand All @@ -152,7 +152,7 @@ func TestMapItemReplace(t *testing.T) {
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}

item = &mapRemoteItem{
From: &mapFrom{
Protocol: "https",
Expand All @@ -172,7 +172,7 @@ func TestMapItemReplace(t *testing.T) {
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}

item = &mapRemoteItem{
From: &mapFrom{
Protocol: "https",
Expand All @@ -192,7 +192,7 @@ func TestMapItemReplace(t *testing.T) {
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}

item = &mapRemoteItem{
From: &mapFrom{
Protocol: "https",
Expand All @@ -212,7 +212,7 @@ func TestMapItemReplace(t *testing.T) {
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}

item = &mapRemoteItem{
From: &mapFrom{
Protocol: "https",
Expand Down
Loading

0 comments on commit db2c851

Please sign in to comment.