zhengbaoyang 8 years ago
commit 916f3df870
  1. 33
      dump.go
  2. 4
      gomitmproxy.go
  3. 34
      mitm.go

@ -6,13 +6,14 @@ import (
"compress/flate" "compress/flate"
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"math" "math"
"net/http" "net/http"
"strconv" "strconv"
) )
func httpDump(req *http.Request, resp *http.Response) { func httpDump(reqDump []byte, resp *http.Response) {
defer resp.Body.Close() defer resp.Body.Close()
var respStatusStr string var respStatusStr string
respStatus := resp.StatusCode respStatus := resp.StatusCode
@ -27,13 +28,26 @@ func httpDump(req *http.Request, resp *http.Response) {
case 5: case 5:
respStatusStr = Red("<--" + strconv.Itoa(respStatus)) respStatusStr = Red("<--" + strconv.Itoa(respStatus))
} }
fmt.Println(Green("Request:"))
fmt.Printf("%s %s %s\n", Blue(req.Method), req.RequestURI, respStatusStr)
fmt.Printf("%s %s\n", Blue("RemoteAddr:"), req.RemoteAddr)
fmt.Println(Green("Request:"), respStatusStr)
req, _ := ParseReq(reqDump)
fmt.Printf("%s %s %s\n", Blue(req.Method), req.Host+req.RequestURI, respStatusStr)
fmt.Printf("%s %s\n", Blue("RemoteAddr:"), req.RemoteAddr)
for headerName, headerContext := range req.Header { for headerName, headerContext := range req.Header {
fmt.Printf("%s: %s\n", Blue(headerName), headerContext) fmt.Printf("%s: %s\n", Blue(headerName), headerContext)
} }
if req.Method == "POST" {
fmt.Println(Green("POST Param:"))
err := req.ParseForm()
if err != nil {
logger.Println("parseForm error:", err)
} else {
for k, v := range req.Form {
fmt.Printf("\t%s: %s\n", Blue(k), v)
}
}
}
fmt.Println(Green("Response:")) fmt.Println(Green("Response:"))
for headerName, headerContext := range resp.Header { for headerName, headerContext := range resp.Header {
fmt.Printf("%s: %s\n", Blue(headerName), headerContext) fmt.Printf("%s: %s\n", Blue(headerName), headerContext)
@ -71,3 +85,14 @@ func httpDump(req *http.Request, resp *http.Response) {
fmt.Printf("%s%s%s\n", Black("####################"), Cyan("END"), Black("####################")) fmt.Printf("%s%s%s\n", Black("####################"), Cyan("END"), Black("####################"))
} }
func ParseReq(b []byte) (*http.Request, error) {
// func ReadRequest(b *bufio.Reader) (req *Request, err error) { return readRequest(b, deleteHostHeader) }
fmt.Println(string(b))
fmt.Println("-----------------------")
var buf io.ReadWriter
buf = new(bytes.Buffer)
buf.Write(b)
bufr := bufio.NewReader(buf)
return http.ReadRequest(bufr)
}

@ -31,8 +31,12 @@ func main() {
conf.Log = flag.String("log", "./error.log", "log file path") conf.Log = flag.String("log", "./error.log", "log file path")
conf.Monitor = flag.Bool("m", false, "monitor mode") conf.Monitor = flag.Bool("m", false, "monitor mode")
conf.Tls = flag.Bool("tls", false, "tls connect") conf.Tls = flag.Bool("tls", false, "tls connect")
help := flag.Bool("h", false, "help")
flag.Parse() flag.Parse()
if *help {
flag.PrintDefaults()
}
var err error var err error
logFile, err = os.Create(*conf.Log) logFile, err = os.Create(*conf.Log)
if err != nil { if err != nil {

@ -101,7 +101,17 @@ func (hw *HandlerWrapper) DumpHTTPAndHTTPs(resp http.ResponseWriter, req *http.R
req.Header.Del("Proxy-Connection") req.Header.Del("Proxy-Connection")
req.Header.Set("Connection", "Keep-Alive") req.Header.Set("Connection", "Keep-Alive")
var reqDump []byte
var err error
ch := make(chan bool)
// handle connection // handle connection
go func() {
reqDump, err = httputil.DumpRequestOut(req, true)
ch <- true
}()
if err != nil {
logger.Println("DumpRequest error ", err)
}
connIn, _, err := resp.(http.Hijacker).Hijack() connIn, _, err := resp.(http.Hijacker).Hijack()
if err != nil { if err != nil {
logger.Println("hijack error:", err) logger.Println("hijack error:", err)
@ -173,7 +183,10 @@ func (hw *HandlerWrapper) DumpHTTPAndHTTPs(resp http.ResponseWriter, req *http.R
} }
if *hw.MyConfig.Monitor { if *hw.MyConfig.Monitor {
go httpDump(req, respOut) <-ch
go httpDump(reqDump, respOut)
} else {
<-ch
} }
} }
@ -340,3 +353,22 @@ func connectProxyServer(conn net.Conn, addr string) error {
} }
return nil return nil
} }
/*func ReadNotDrain(r *http.Request) (content []byte, err error) {
content, err = ioutil.ReadAll(r.Body)
r.Body = io.ReadCloser(bytes.NewBuffer(content))
return
}
func ParsePostValues(req *http.Request) (url.Values, error) {
c, err := ReadNotDrain(req)
if err != nil {
return nil, err
}
values, err := url.ParseQuery(string(c))
if err != nil {
return nil, err
}
return values, nil
}
*/

Loading…
Cancel
Save