Skip to content

Commit

Permalink
add map remote addon
Browse files Browse the repository at this point in the history
  • Loading branch information
lqqyt2423 committed Mar 7, 2023
1 parent b152023 commit 6898d89
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
54 changes: 47 additions & 7 deletions addon/mapremote.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package addon

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

"github.com/lqqyt2423/go-mitmproxy/proxy"
log "github.com/sirupsen/logrus"
"github.com/tidwall/match"
)

// Path map rule:
Expand All @@ -24,10 +31,9 @@ type mapTo struct {
}

type mapItem struct {
From *mapFrom
To *mapTo
PerserveHost bool
Enable bool
From *mapFrom
To *mapTo
Enable bool
}

func (item *mapItem) match(req *proxy.Request) bool {
Expand All @@ -37,12 +43,30 @@ func (item *mapItem) match(req *proxy.Request) bool {
if item.From.Protocol != "" && item.From.Protocol != req.URL.Scheme {
return false
}
// todo
return false
if item.From.Host != "" && item.From.Host != req.URL.Host {
return false
}
if item.From.Path != "" && !match.Match(req.URL.Path, item.From.Path) {
return false
}
return true
}

func (item *mapItem) replace(req *proxy.Request) *proxy.Request {
// todo
if item.To.Protocol != "" {
req.URL.Scheme = item.To.Protocol
}
if item.To.Host != "" {
req.URL.Host = item.To.Host
}
if item.To.Path != "" {
if item.From.Path != "" && strings.HasSuffix(item.From.Path, "/*") {
subPath := req.URL.Path[len(item.From.Path)-2:]
req.URL.Path = path.Join("/", item.To.Path, subPath)
} else {
req.URL.Path = path.Join("/", item.To.Path)
}
}
return req
}

Expand All @@ -58,8 +82,24 @@ func (mr *MapRemote) Requestheaders(f *proxy.Flow) {
}
for _, item := range mr.Items {
if item.match(f.Request) {
aurl := f.Request.URL.String()
f.Request = item.replace(f.Request)
f.UseSeparateClient = true
burl := f.Request.URL.String()
log.Infof("map remote %v to %v", aurl, burl)
return
}
}
}

func NewMapRemoteFromFile(filename string) (*MapRemote, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var mapRemote MapRemote
if err := json.Unmarshal(data, &mapRemote); err != nil {
return nil, err
}
return &mapRemote, nil
}
1 change: 1 addition & 0 deletions cmd/go-mitmproxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func loadConfigFromCli() *Config {
flag.StringVar(&config.Dump, "dump", "", "dump filename")
flag.IntVar(&config.DumpLevel, "dump_level", 0, "dump level: 0 - header, 1 - header + body")
flag.StringVar(&config.MapperDir, "mapper_dir", "", "mapper files dirpath")
flag.StringVar(&config.MapRemote, "map_remote", "", "map remote config filename")
flag.StringVar(&config.filename, "f", "", "read config from the filename")
flag.Parse()

Expand Down
10 changes: 10 additions & 0 deletions cmd/go-mitmproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
Dump string // dump filename
DumpLevel int // dump level: 0 - header, 1 - header + body
MapperDir string // mapper files dirpath
MapRemote string // map remote config filename

filename string // read config from the filename
}
Expand Down Expand Up @@ -80,6 +81,15 @@ func main() {
p.AddAddon(&proxy.LogAddon{})
p.AddAddon(web.NewWebAddon(config.WebAddr))

if config.MapRemote != "" {
mapRemote, err := addon.NewMapRemoteFromFile(config.MapRemote)
if err != nil {
log.Warnf("load map remote error: %v", err)
} else {
p.AddAddon(mapRemote)
}
}

if config.Dump != "" {
dumper := addon.NewDumperWithFilename(config.Dump, config.DumpLevel)
p.AddAddon(dumper)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
)

require (
github.com/tidwall/match v1.1.1 // indirect
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
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/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
6 changes: 5 additions & 1 deletion proxy/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ func (c *ServerConn) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
m["id"] = c.Id
m["address"] = c.Address
m["peername"] = c.Conn.RemoteAddr().String()
peername := ""
if c.Conn != nil {
peername = c.Conn.RemoteAddr().String()
}
m["peername"] = peername
return json.Marshal(m)
}

Expand Down

0 comments on commit 6898d89

Please sign in to comment.