Skip to content

Commit

Permalink
finish map remote addon
Browse files Browse the repository at this point in the history
  • Loading branch information
lqqyt2423 committed Mar 8, 2023
1 parent 6898d89 commit bf674cb
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 0 deletions.
30 changes: 30 additions & 0 deletions addon/mapremote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package addon

import (
"encoding/json"
"fmt"
"os"
"path"
"strings"

"github.com/lqqyt2423/go-mitmproxy/proxy"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/tidwall/match"
)
Expand All @@ -21,6 +23,7 @@ import (
type mapFrom struct {
Protocol string
Host string
Method []string
Path string
}

Expand All @@ -46,6 +49,9 @@ func (item *mapItem) match(req *proxy.Request) bool {
if item.From.Host != "" && item.From.Host != req.URL.Host {
return false
}
if len(item.From.Method) > 0 && !lo.Contains(item.From.Method, req.Method) {
return false
}
if item.From.Path != "" && !match.Match(req.URL.Path, item.From.Path) {
return false
}
Expand Down Expand Up @@ -92,6 +98,27 @@ func (mr *MapRemote) Requestheaders(f *proxy.Flow) {
}
}

func (mr *MapRemote) validate() error {
for i, item := range mr.Items {
if item.From == nil {
return fmt.Errorf("%v no item.From", i)
}
if item.From.Protocol != "" && item.From.Protocol != "http" && item.From.Protocol != "https" {
return fmt.Errorf("%v invalid item.From.Protocol %v", i, item.From.Protocol)
}
if item.To == nil {
return fmt.Errorf("%v no item.To", i)
}
if item.To.Protocol == "" && item.To.Host == "" && item.To.Path == "" {
return fmt.Errorf("%v empty item.To", i)
}
if item.To.Protocol != "" && item.To.Protocol != "http" && item.To.Protocol != "https" {
return fmt.Errorf("%v invalid item.To.Protocol %v", i, item.To.Protocol)
}
}
return nil
}

func NewMapRemoteFromFile(filename string) (*MapRemote, error) {
data, err := os.ReadFile(filename)
if err != nil {
Expand All @@ -101,5 +128,8 @@ func NewMapRemoteFromFile(filename string) (*MapRemote, error) {
if err := json.Unmarshal(data, &mapRemote); err != nil {
return nil, err
}
if err := mapRemote.validate(); err != nil {
return nil, err
}
return &mapRemote, nil
}
235 changes: 235 additions & 0 deletions addon/mapremote_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package addon

import (
"net/url"
"testing"

"github.com/lqqyt2423/go-mitmproxy/proxy"
)

func TestMapItemMatch(t *testing.T) {
req := &proxy.Request{
Method: "GET",
URL: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "/path/to/resource",
},
}

// test match

item := &mapItem{
From: &mapFrom{
Protocol: "https",
Host: "example.com",
Method: []string{"GET", "POST"},
Path: "/path/to/resource",
},
To: nil,
Enable: true,
}
result := item.match(req)
if !result {
t.Errorf("Expected true, but got false")
}

// empty Protocol and empty Method match
item.From = &mapFrom{
Protocol: "",
Host: "example.com",
Method: []string{},
Path: "/path/to/resource",
}
result = item.match(req)
if !result {
t.Errorf("Expected true, but got false")
}

// empty Host match
item.From = &mapFrom{
Protocol: "",
Host: "",
Method: []string{},
Path: "/path/to/*",
}
result = item.match(req)
if !result {
t.Errorf("Expected true, but got false")
}

// all empty match
item.From = &mapFrom{
Protocol: "",
Host: "",
Method: []string{},
Path: "",
}
result = item.match(req)
if !result {
t.Errorf("Expected true, but got false")
}

// test not match

// diff Protocol
item.From = &mapFrom{
Protocol: "http",
Host: "example.com",
Method: []string{},
Path: "/path/to/resource",
}
result = item.match(req)
if result {
t.Errorf("Expected true, but got false")
}

// diff Host
item.From = &mapFrom{
Protocol: "https",
Host: "hello.com",
Method: []string{},
Path: "/path/to/resource",
}
result = item.match(req)
if result {
t.Errorf("Expected true, but got false")
}

// diff Method
item.From = &mapFrom{
Protocol: "https",
Host: "example.com",
Method: []string{"PUT"},
Path: "/path/to/resource",
}
result = item.match(req)
if result {
t.Errorf("Expected true, but got false")
}

// diff Path
item.From = &mapFrom{
Protocol: "http",
Host: "example.com",
Method: []string{},
Path: "/hello/world",
}
result = item.match(req)
if result {
t.Errorf("Expected true, but got false")
}
}

func TestMapItemReplace(t *testing.T) {
rawreq := func() *proxy.Request {
return &proxy.Request{
Method: "GET",
URL: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "/path/to/resource",
},
}
}

item := &mapItem{
From: &mapFrom{
Protocol: "https",
Host: "example.com",
Method: []string{"GET", "POST"},
Path: "/path/to/resource",
},
To: &mapTo{
Protocol: "http",
Host: "hello.com",
Path: "",
},
Enable: true,
}
req := item.replace(rawreq())
should := "http:https://hello.com/path/to/resource"
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}

item = &mapItem{
From: &mapFrom{
Protocol: "https",
Host: "example.com",
Method: []string{"GET", "POST"},
Path: "/path/to/resource",
},
To: &mapTo{
Protocol: "http",
Host: "hello.com",
Path: "/path/to/resource",
},
Enable: true,
}
req = item.replace(rawreq())
should = "http:https://hello.com/path/to/resource"
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}

item = &mapItem{
From: &mapFrom{
Protocol: "https",
Host: "example.com",
Method: []string{"GET", "POST"},
Path: "/path/to/resource",
},
To: &mapTo{
Protocol: "http",
Host: "hello.com",
Path: "/path/to/world",
},
Enable: true,
}
req = item.replace(rawreq())
should = "http:https://hello.com/path/to/world"
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}

item = &mapItem{
From: &mapFrom{
Protocol: "https",
Host: "example.com",
Method: []string{"GET", "POST"},
Path: "/path/to/*",
},
To: &mapTo{
Protocol: "http",
Host: "hello.com",
Path: "",
},
Enable: true,
}
req = item.replace(rawreq())
should = "http:https://hello.com/path/to/resource"
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}

item = &mapItem{
From: &mapFrom{
Protocol: "https",
Host: "example.com",
Method: []string{"GET", "POST"},
Path: "/path/to/*",
},
To: &mapTo{
Protocol: "http",
Host: "hello.com",
Path: "/world",
},
Enable: true,
}
req = item.replace(rawreq())
should = "http:https://hello.com/world/resource"
if req.URL.String() != should {
t.Errorf("Expected %v, but got %v", should, req.URL.String())
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ require (
)

require (
github.com/samber/lo v1.37.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.37.0 h1:XjVcB8g6tgUp8rsPsJ2CvhClfImrpL04YpQHXeHPhRw=
github.com/samber/lo v1.37.0/go.mod h1:9vaz2O4o8oOnK23pd2TrXufcbdbJIa3b6cstBWKpopA=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
Expand All @@ -21,6 +23,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit bf674cb

Please sign in to comment.