dump level

addon-dailer
lqqyt2423 4 years ago
parent 10260d840b
commit d82eb3d541

@ -14,6 +14,6 @@
- [x] websocket - [x] websocket
- [x] 插件机制 - [x] 插件机制
- [x] 命令行参数控制 dump 至文件 - [x] 命令行参数控制 dump 至文件
- [ ] dump level - [x] dump level
- [ ] support get method with body - [ ] support get method with body
- [ ] http2 - [ ] http2

@ -7,21 +7,28 @@ import (
"net/http" "net/http"
"os" "os"
"strings" "strings"
"unicode"
"github.com/lqqyt2423/go-mitmproxy/flow" "github.com/lqqyt2423/go-mitmproxy/flow"
) )
type Dumper struct { type Dumper struct {
Base Base
level int // 0: header 1: header + body
Out io.Writer Out io.Writer
} }
func NewDumperWithFile(file string) *Dumper { func NewDumper(file string, level int) *Dumper {
out, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) out, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return &Dumper{Out: out}
if level != 0 && level != 1 {
level = 0
}
return &Dumper{Out: out, level: level}
} }
func (d *Dumper) Requestheaders(f *flow.Flow) { func (d *Dumper) Requestheaders(f *flow.Flow) {
@ -48,6 +55,11 @@ func (d *Dumper) Requestheaders(f *flow.Flow) {
} }
buf.WriteString("\r\n") 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 { if f.Response != nil {
fmt.Fprintf(buf, "%v %v %v\r\n", f.Request.Proto, f.Response.StatusCode, http.StatusText(f.Response.StatusCode)) 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) err = f.Response.Header.WriteSubset(buf, nil)
@ -55,7 +67,14 @@ func (d *Dumper) Requestheaders(f *flow.Flow) {
log.Error(err) log.Error(err)
} }
buf.WriteString("\r\n") buf.WriteString("\r\n")
if d.level == 1 && f.Response.Body != nil && len(f.Response.Body) > 0 && CanPrint(f.Response.Body) {
buf.Write(f.Response.Body)
buf.WriteString("\r\n\r\n")
} }
}
buf.WriteString("\r\n\r\n")
_, err = d.Out.Write(buf.Bytes()) _, err = d.Out.Write(buf.Bytes())
if err != nil { if err != nil {
@ -63,3 +82,12 @@ func (d *Dumper) Requestheaders(f *flow.Flow) {
} }
}() }()
} }
func CanPrint(content []byte) bool {
for _, c := range string(content) {
if !unicode.IsPrint(c) && !unicode.IsSpace(c) {
return false
}
}
return true
}

@ -12,6 +12,7 @@ import (
type Config struct { type Config struct {
addr string addr string
dump string // dump filename dump string // dump filename
dumpLevel int // dump level
} }
func loadConfig() *Config { func loadConfig() *Config {
@ -19,6 +20,7 @@ func loadConfig() *Config {
flag.StringVar(&config.addr, "addr", ":9080", "proxy listen addr") flag.StringVar(&config.addr, "addr", ":9080", "proxy listen addr")
flag.StringVar(&config.dump, "dump", "", "dump filename") flag.StringVar(&config.dump, "dump", "", "dump filename")
flag.IntVar(&config.dumpLevel, "dump_level", 0, "dump level: 0 - header, 1 - header + body")
flag.Parse() flag.Parse()
return config return config
@ -45,7 +47,7 @@ func main() {
} }
if config.dump != "" { if config.dump != "" {
dumper := addon.NewDumperWithFile(config.dump) dumper := addon.NewDumper(config.dump, config.dumpLevel)
p.AddAddon(dumper) p.AddAddon(dumper)
} }

Loading…
Cancel
Save