main
zhengbaoyang 8 years ago
parent 1c14528edc
commit c5e10c274b
  1. 33
      dump.go
  2. 29
      mitm.go

@ -12,7 +12,7 @@ import (
"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,19 +27,33 @@ 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.Println(Green("Request:"), respStatusStr)
fmt.Printf("%s %s %s\n", Blue(req.Method), req.RequestURI, respStatusStr) ParseReq(reqDump)
/*req, _ := ParseReq(reqDump)
for k, v := range req {
fmt.Println(k, ":::::", v)
}*/
/*fmt.Printf("%s %s %s\n", Blue(req.Method), req.RequestURI, respStatusStr)
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" { /*if req.Method == "POST" {
fmt.Println(Green("URLEncoded form")) fmt.Println(Green("URLEncoded form"))
for k, v := range req.Form { err := req.ParseForm()
if err != nil {
logger.Println("parseForm error:", err)
} else {
for k, v := range req.Form {
fmt.Printf("%s: %s\n", Blue(k), v)
}
}
values, _ := ParsePostValues(reqDump)
for k, v := range values {
fmt.Printf("%s: %s\n", Blue(k), v) fmt.Printf("%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)
@ -77,3 +91,8 @@ 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) error {
str := string(b)
}

@ -102,6 +102,10 @@ func (hw *HandlerWrapper) DumpHTTPAndHTTPs(resp http.ResponseWriter, req *http.R
req.Header.Set("Connection", "Keep-Alive") req.Header.Set("Connection", "Keep-Alive")
// handle connection // handle connection
reqDump, err := httputil.DumpRequest(req, 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,17 +177,13 @@ func (hw *HandlerWrapper) DumpHTTPAndHTTPs(resp http.ResponseWriter, req *http.R
} }
if *hw.MyConfig.Monitor { if *hw.MyConfig.Monitor {
go httpDump(req, respOut) go httpDump(reqDump, respOut)
} }
} }
func (hw *HandlerWrapper) ServeHTTP(resp http.ResponseWriter, req *http.Request) { func (hw *HandlerWrapper) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
logger.Println("parseForm error:", err)
}
raddr := *hw.MyConfig.Raddr raddr := *hw.MyConfig.Raddr
if len(raddr) != 0 { if len(raddr) != 0 {
hw.Forward(resp, req, raddr) hw.Forward(resp, req, raddr)
@ -344,3 +344,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