-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhar.go
366 lines (327 loc) · 9.58 KB
/
har.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"bytes"
"compress/flate"
"compress/gzip"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"strings"
"time"
)
const (
startingEntrySize = 128
captureContent = true
)
// ResponseWriterProxy is a proxy to intercept http.ResponseWriter method calls
type ResponseWriterProxy struct {
under http.ResponseWriter
response HarResponse
buffer bytes.Buffer
}
// NewResponseWriterProxy creates a new ResponseWriterProxy
func NewResponseWriterProxy(r http.ResponseWriter) *ResponseWriterProxy {
return &ResponseWriterProxy{under: r}
}
// Header from http.ResponseWriter interface
func (rwp *ResponseWriterProxy) Header() http.Header {
return rwp.under.Header()
}
// Wrote from from http.ResponseWriter interface
func (rwp *ResponseWriterProxy) Write(bs []byte) (int, error) {
rwp.buffer.Write(bs)
return rwp.under.Write(bs)
}
// WriteHeader from http.ResponseWriter interface
func (rwp *ResponseWriterProxy) WriteHeader(statusCode int) {
rwp.response.Status = statusCode
rwp.response.Headers = parseStringArrMap(rwp.under.Header())
rwp.under.WriteHeader(statusCode)
}
// GetResponse returns a HarResponse after the response was written by the handler
func (rwp *ResponseWriterProxy) GetResponse() *HarResponse {
var bs []byte
rwp.response.Content = &HarContent{}
encoding := rwp.under.Header()["Content-Encoding"]
if len(encoding) > 0 {
rwp.response.Content.Encoding = encoding[0]
if encoding[0] == "gzip" {
gr, _ := gzip.NewReader(&rwp.buffer)
defer gr.Close()
bs, _ = ioutil.ReadAll(gr)
} else if encoding[0] == "deflate" {
gr := flate.NewReader(&rwp.buffer)
defer gr.Close()
bs, _ = ioutil.ReadAll(gr)
}
}
if bs == nil {
bs = rwp.buffer.Bytes()
}
rwp.response.BodySize = int64(len(bs))
contentType := rwp.under.Header()["Content-Type"]
if contentType == nil {
contentType = []string{"text/plain"}
}
rwp.response.Content.MimeType = contentType[0]
rwp.response.Content.Text = string(bs)
return &rwp.response
}
func fillIPAddress(req *http.Request, harEntry *HarEntry) {
host, _, err := net.SplitHostPort(req.URL.Host)
if err != nil {
host = req.URL.Host
}
if ip := net.ParseIP(host); ip != nil {
harEntry.ServerIPAddress = string(ip)
}
if ipaddr, err := net.LookupIP(host); err == nil {
for _, ip := range ipaddr {
if ip.To4() != nil {
harEntry.ServerIPAddress = ip.String()
return
}
}
}
}
// Har represents the json HAR file format
type Har struct {
HarLog HarLog `json:"log"`
}
// HarCreator is a field of HAR files
type HarCreator struct {
Name string `json:"name"`
Version string `json:"version"`
}
// HarLog is a field of HAR files
type HarLog struct {
Version string `json:"version"`
Creator HarCreator `json:"creator"`
Browser string `json:"browser"`
Pages []HarPage `json:"pages"`
Entries []HarEntry `json:"entries"`
}
func newHarLog() *HarLog {
harLog := &HarLog{
Version: "1.2",
Creator: HarCreator{Name: "TLSProxy", Version: "2.8"},
Browser: "",
Pages: make([]HarPage, 0, 10),
Entries: makeNewEntries(),
}
return harLog
}
func (harLog *HarLog) addEntry(entry ...HarEntry) {
entries := harLog.Entries
m := len(entries)
n := m + len(entry)
if n > cap(entries) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newEntries := make([]HarEntry, (n+1)*2)
copy(newEntries, entries)
entries = newEntries
}
entries = entries[0:n]
copy(entries[m:n], entry)
harLog.Entries = entries
log.Println("Added entry to HAR file", entry[0].Request.URL)
}
func makeNewEntries() []HarEntry {
return make([]HarEntry, 0, startingEntrySize)
}
// HarPage is a field of HAR files
type HarPage struct {
ID string `json:"id"`
StartedDateTime time.Time `json:"startedDateTime"`
Title string `json:"title"`
PageTimings HarPageTimings `json:"pageTimings"`
}
// HarEntry is a field of HAR files
type HarEntry struct {
PageRef string `json:"pageRef"`
StartedDateTime time.Time `json:"startedDateTime"`
Time int64 `json:"time"`
Request *HarRequest `json:"request"`
Response *HarResponse `json:"response"`
Timings HarTimings `json:"timings"`
ServerIPAddress string `json:"serverIpAddress"`
Connection string `json:"connection"`
}
// HarRequest is a field of HAR files
type HarRequest struct {
Method string `json:"method"`
URL string `json:"url"`
HTTPVersion string `json:"httpVersion"`
Cookies []HarCookie `json:"cookies"`
Headers []HarNameValuePair `json:"headers"`
QueryString []HarNameValuePair `json:"queryString"`
PostData *HarPostData `json:"postData"`
BodySize int64 `json:"bodySize"`
HeadersSize int64 `json:"headersSize"`
}
func parseRequest(req *http.Request) *HarRequest {
if req == nil {
return nil
}
harRequest := HarRequest{
Method: req.Method,
URL: req.URL.String(),
HTTPVersion: req.Proto,
Cookies: parseCookies(req.Cookies()),
Headers: parseStringArrMap(req.Header),
QueryString: parseStringArrMap((req.URL.Query())),
BodySize: req.ContentLength,
HeadersSize: calcHeaderSize(req.Header),
}
if captureContent && (req.Method == http.MethodPost || req.Method != http.MethodPut) {
harRequest.PostData = parsePostData(req)
}
return &harRequest
}
func calcHeaderSize(header http.Header) int64 {
headerSize := 0
for headerName, headerValues := range header {
headerSize += len(headerName) + 2
for _, v := range headerValues {
headerSize += len(v)
}
}
return int64(headerSize)
}
func parsePostData(req *http.Request) *HarPostData {
defer func() {
if e := recover(); e != nil {
log.Printf("Error parsing request to %v: %v\n", req.URL, e)
}
}()
harPostData := new(HarPostData)
contentType := req.Header["Content-Type"]
if contentType == nil {
panic("Missing content type in request")
}
harPostData.MimeType = contentType[0]
if len(req.PostForm) > 0 {
index := 0
params := make([]HarPostDataParam, len(req.PostForm))
for k, v := range req.PostForm {
param := HarPostDataParam{
Name: k,
Value: strings.Join(v, ","),
}
params[index] = param
index++
}
harPostData.Params = params
} else {
str, _ := ioutil.ReadAll(req.Body) // read body
req.Body = ioutil.NopCloser(bytes.NewReader(str)) // put it back in place
encoding := req.Header.Get("Content-encoding")
if encoding == "gzip" {
gr, _ := gzip.NewReader(bytes.NewReader(str))
defer gr.Close()
str, _ = ioutil.ReadAll(gr)
} else if encoding == "deflate" {
gr := flate.NewReader(bytes.NewReader(str))
defer gr.Close()
str, _ = ioutil.ReadAll(gr)
}
harPostData.Text = string(str)
}
return harPostData
}
func parseStringArrMap(stringArrMap map[string][]string) []HarNameValuePair {
index := 0
harQueryString := make([]HarNameValuePair, len(stringArrMap))
for k, v := range stringArrMap {
escapedKey, _ := url.QueryUnescape(k)
escapedValues, _ := url.QueryUnescape(strings.Join(v, ","))
harNameValuePair := HarNameValuePair{
Name: escapedKey,
Value: escapedValues,
}
harQueryString[index] = harNameValuePair
index++
}
return harQueryString
}
func parseCookies(cookies []*http.Cookie) []HarCookie {
harCookies := make([]HarCookie, len(cookies))
for i, cookie := range cookies {
harCookie := HarCookie{
Name: cookie.Name,
Domain: cookie.Domain,
Expires: cookie.Expires,
HTTPOnly: cookie.HttpOnly,
Path: cookie.Path,
Secure: cookie.Secure,
Value: cookie.Value,
}
harCookies[i] = harCookie
}
return harCookies
}
// HarResponse is a field of HAR files
type HarResponse struct {
Status int `json:"status"`
StatusText string `json:"statusText"`
HTTPVersion string `json:"httpVersion"`
Cookies []HarCookie `json:"cookies"`
Headers []HarNameValuePair `json:"headers"`
Content *HarContent `json:"content"`
RedirectURL string `json:"redirectUrl"`
BodySize int64 `json:"bodySize"`
HeadersSize int64 `json:"headersSize"`
}
// HarCookie is a field of HAR files
type HarCookie struct {
Name string `json:"name"`
Value string `json:"value"`
Path string `json:"path"`
Domain string `json:"domain"`
Expires time.Time `json:"expires"`
HTTPOnly bool `json:"httpOnly"`
Secure bool `json:"secure"`
}
// HarNameValuePair is a field of HAR files
type HarNameValuePair struct {
Name string `json:"name"`
Value string `json:"value"`
}
// HarPostData is a field of HAR files
type HarPostData struct {
MimeType string `json:"mimeType"`
Params []HarPostDataParam `json:"params"`
Text string `json:"text"`
}
// HarPostDataParam is a field of HAR files
type HarPostDataParam struct {
Name string `json:"name"`
Value string `json:"value"`
FileName string `json:"fileName"`
ContentType string `json:"contentType"`
}
// HarContent is a field of HAR files
type HarContent struct {
Size int64 `json:"size"`
Compression int64 `json:"compression"`
MimeType string `json:"mimeType"`
Text string `json:"text"`
Encoding string `json:"encoding"`
}
// HarPageTimings is a field of HAR files
type HarPageTimings struct {
OnContentLoad int64 `json:"onContentLoad"`
OnLoad int64 `json:"onLoad"`
}
// HarTimings is a field of HAR files
type HarTimings struct {
Blocked int64 `json:"blocked"`
DNS int64 `json:"dns"`
Connect int64 `json:"connect"`
Send int64 `json:"send"`
Wait int64 `json:"wait"`
Receive int64 `json:"receive"`
Ssl int64 `json:"ssl"`
}