-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspector.go
More file actions
77 lines (65 loc) · 2 KB
/
inspector.go
File metadata and controls
77 lines (65 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/thebinary/ported/flow/httpflow"
)
//InspectTransport structure
type InspectTransport struct {
RequestHeaders bool
ResponseHeaders bool
ResponseBody bool
WebChannel chan httpflow.HTTPFlow
}
//DefaultInspectTransport is the default inspect transport instance initialized by init
var DefaultInspectTransport *InspectTransport
func init() {
DefaultInspectTransport = &InspectTransport{}
}
//NewInspectTransport returns a new instance of InspectTranspor
//TODO: optimized transport
func NewInspectTransport(responseHeaders, responseBody, requestHeaders bool) (transport *InspectTransport) {
return &InspectTransport{
ResponseHeaders: responseHeaders,
ResponseBody: responseBody,
RequestHeaders: requestHeaders,
}
}
//RoundTrip is the implementation method for http RoundTripper
func (i *InspectTransport) RoundTrip(request *http.Request) (response *http.Response, err error) {
start := time.Now()
response, err = http.DefaultTransport.RoundTrip(request)
elapsed := time.Since(start)
w := *httpflow.NewHTTPFlow(request, response)
// default loggin
// eg: 2020/07/25 18:30:26 1.1.1.1 4.046181ms GET "/test" HTTP/1.1 200 839 "" "curl/7.54.0"
accessLog := fmt.Sprintf("%s %-12s %s \"%s\" HTTP/%d.%d %d %d \"%s\" \"%s\"",
w.RemoteIP, elapsed,
request.Method, request.URL.Path, request.ProtoMajor, request.ProtoMinor,
response.StatusCode, response.ContentLength,
request.Referer(), request.UserAgent())
log.Println(accessLog)
if i.RequestHeaders {
fmt.Println("")
fmt.Println("---- REQUEST ----")
fmt.Println(w.RequestHeaders)
fmt.Println("-----------------")
fmt.Println("")
}
//TODO: [FIX] handle headerOnly or bodyOnly cases for logging
if i.ResponseHeaders || i.ResponseBody {
fmt.Println("")
fmt.Println("---- RESPONSE ----")
fmt.Println(w.ResponseHeaders)
fmt.Println("------------------")
fmt.Println("")
}
if i.WebChannel != nil {
go func() {
i.WebChannel <- w
}()
}
return response, err
}