-
Notifications
You must be signed in to change notification settings - Fork 0
/
har.go
183 lines (162 loc) · 4.42 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
package har
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
// Har archive.
type Har struct {
File string `json:"-"` // File this was read from.
Log struct {
Version string `json:"version"` // "1.2"
Creator struct {
Name string `json:"name"` // "Firefox"
Version string `json:"version"` // "72.0.2"
} `json:"creator"`
Browser struct {
Name string `json:"name"` // "Firefox"
Version string `json:"version"` // "72.0.2"
} `json:"browser"`
Pages []struct {
StartedDateTime time.Time `json:"startedDateTime"`
ID string `json:"id"` // "page_1"
PageTimings struct {
OnContentLoad int `json:"onContentLoad"`
OnLoad int `json:"onLoad"`
} `json:"pageTimings"`
} `json:"pages"`
Entries []Entry `json:"entries"`
} `json:"log"`
}
type Entry struct {
PageRef string `json:"pageRef"` // "page_1"
StartedDateTime time.Time `json:"startedDateTime"`
Request struct {
BodySize int `json:"bodySize"`
HeadersSize int `json:"headersSize"`
Method string `json:"method"`
URL string `json:"url"` // "http://localhost/..."
HTTPVersion string `json:"httpVersion"` // "HTTP/1.1"
Headers []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"headers"`
Cookies []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"headers"`
QueryString []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"queryString"`
} `json:"request"`
Response struct {
HeadersSize int `json:"headersSize"`
BodySize int `json:"bodySize"`
Status int `json:"status"`
StatusText string `json:"statusText"` // "OK"
HTTPVersion string `json:"httpVersion"` // "HTTP/1.1"
RedirectURL string `json:"redirectURL"`
Headers []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"headers"`
Cookies []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"headers"`
Content struct {
Encoding string `json:"encoding"`
Size int `json:"size"`
Text string `json:"text"`
} `json:"content"`
} `json:"response"`
Cache struct {
// TODO
} `json:"cache"`
Timings struct {
Blocked int `json:"blocked"`
DNS int `json:"dns"`
Connect int `json:"connect"`
SSL int `json:"ssl"`
Send int `json:"send"`
Wait int `json:"wait"`
Receive int `json:"receive"`
} `json:"timings"`
Time int `json:"time"`
SecurityState string `json:"_securityState"` // TODO
ServerIPAddress string `json:"serverIPAddress"` // "::1"
Connection string `json:"connection"` // "80"
}
// FromFile reads a file in to a Har struct.
func FromFile(f string) (*Har, error) {
fp, err := os.Open(f)
if err != nil {
return nil, fmt.Errorf("open: %w", err)
}
data, err := ioutil.ReadAll(fp)
fp.Close()
if err != nil {
return nil, fmt.Errorf("read: %w", err)
}
var h Har
err = json.Unmarshal(data, &h)
if err != nil {
return nil, fmt.Errorf("json: %w", err)
}
h.File = f
return &h, nil
}
// Extract all the files.
func (h *Har) Extract(verbose bool) error {
root := filepath.Base(h.File)
if ext := filepath.Ext(root); ext != "" {
root = root[:len(root)-len(ext)]
}
for _, e := range h.Log.Entries {
path := root + "/" + strings.TrimPrefix(strings.TrimPrefix(e.Request.URL, "http://"), "https://")
if verbose {
fmt.Println(" ", path)
}
for _, h := range e.Response.Headers {
if h.Key == "Content-Disposition" {
for _, v := range strings.Split(h.Value, ";") {
v := strings.TrimSpace(v)
if strings.HasPrefix(v, "filename=") {
v = filepath.Clean(strings.Trim(v, `"`))
v = strings.ReplaceAll(v, "/", "")
path = filepath.Dir(path) + "/" + v
}
}
break
}
}
if strings.HasSuffix(path, "/") {
path += "index.html"
}
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return fmt.Errorf("mkdir: %w", err)
}
var resp []byte
if e.Response.Content.Encoding == "base64" {
resp, err = base64.StdEncoding.DecodeString(e.Response.Content.Text)
if err != nil {
return fmt.Errorf("base64: %w", err)
}
} else {
resp = []byte(e.Response.Content.Text)
}
err = ioutil.WriteFile(path, resp, 0644)
if err != nil {
return fmt.Errorf("write: %w", err)
}
}
fmt.Printf("Extracted %d files\n", len(h.Log.Entries))
return nil
}