-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathhandler.go
320 lines (267 loc) · 6.93 KB
/
handler.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
/*
* Copyright 2019 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package parser
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
"time"
"github.com/gobs/args"
"github.com/pkg/errors"
"github.com/raff/godet"
"github.com/sirupsen/logrus"
)
func (t *Task) Start() (err error) {
t.startTime = time.Now().UTC()
if t.cfg.ChromeApp == "" {
var chromeapp string
switch runtime.GOOS {
case "darwin":
for _, c := range []string{
"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
} {
// MacOS apps are actually folders
if _, err := exec.LookPath(c); err == nil {
chromeapp = fmt.Sprintf("%q", c)
break
}
}
case "linux":
for _, c := range []string{
"headless_shell",
"chromium",
"chromium-browser",
"google-chrome-beta",
"google-chrome-unstable",
"google-chrome-stable"} {
if _, err := exec.LookPath(c); err == nil {
chromeapp = c
break
}
}
case "windows":
}
if chromeapp != "" {
if chromeapp == "headless_shell" {
chromeapp += " --no-sandbox"
} else {
chromeapp += " --headless"
}
chromeapp += fmt.Sprintf(" --remote-debugging-port=%d --no-default-browser-check --no-first-run --hide-scrollbars --bwsi --disable-gpu",
t.cfg.DebuggerPort)
if dir, err := ioutil.TempDir("", "gdpr_cookie"); err == nil {
t.userDir = dir
chromeapp += " --user-data-dir="
chromeapp += dir
}
chromeapp += " about:blank"
}
t.cfg.ChromeApp = chromeapp
}
// start debugger
if !t.cfg.Headless {
t.cfg.ChromeApp = strings.Replace(t.cfg.ChromeApp, "--headless", "", -1)
}
if t.cfg.ChromeApp == "" {
err = errors.New("no chrome application available")
return
}
parts := args.GetArgs(t.cfg.ChromeApp)
cmd := exec.Command(parts[0], parts[1:]...)
if err = cmd.Start(); err != nil {
return
}
t.debugger = cmd
// connect debugger
for i := 0; i < 10; i++ {
time.Sleep(500 * time.Millisecond)
t.remote, err = godet.Connect(fmt.Sprintf("localhost:%d", t.cfg.DebuggerPort), t.cfg.Verbose)
if err == nil {
break
}
logrus.WithError(err).Debug("connect to debugger failed")
}
return
}
func (t *Task) Cleanup() {
if t.userDir != "" {
_ = os.RemoveAll(t.userDir)
}
if t.remote != nil {
t.remote.CloseBrowser()
_ = t.remote.Close()
}
if t.debugger != nil {
_ = t.debugger.Process.Signal(syscall.SIGTERM)
_ = t.debugger.Wait()
}
}
func (t *Task) Version() (*godet.Version, error) {
return t.remote.Version()
}
func (t *Task) Parse(site string) (err error) {
siteURL, err := url.Parse(site)
if err != nil {
err = errors.Wrap(err, "parse url failed")
return
}
if siteURL.Scheme == "" {
siteURL.Scheme = "http"
}
site = siteURL.String()
rc := newRecordCollector()
// request callbacks
t.remote.CallbackEvent("Network.requestWillBeSent", func(params godet.Params) {
req := params.Map("request")
var reqURL string
if rawReqURL, ok := req["url"]; !ok {
return
} else if reqURL, ok = rawReqURL.(string); !ok {
return
}
if strings.HasPrefix(reqURL, "data:") {
// data uri is ignored
return
}
rc.addRequest(params)
})
// response callbacks
t.remote.CallbackEvent("Network.responseReceived", func(params godet.Params) {
resp := params.Map("response")
var respURL string
if rawRespURL, ok := resp["url"]; !ok {
return
} else if respURL, ok = rawRespURL.(string); !ok {
return
}
if strings.HasPrefix(respURL, "data:") {
// data uri is ignored
return
}
rc.addResponse(params)
})
pageWait := make(chan struct{}, 1)
tm := time.AfterFunc(t.cfg.Timeout, func() {
logrus.WithField("site", site).Debug("timeout triggered")
select {
case pageWait <- struct{}{}:
default:
}
})
defer tm.Stop()
// page stopped loading event
t.remote.CallbackEvent("Page.frameStoppedLoading", func(params godet.Params) {
logrus.WithField("site", site).Debug("page frame stopped loading")
go func() {
time.Sleep(t.cfg.WaitAfterPageLoad)
select {
case pageWait <- struct{}{}:
default:
}
}()
})
// page load event fired
t.remote.CallbackEvent("Page.loadEventFired", func(params godet.Params) {
logrus.WithField("site", site).Debug("page load fired")
go func() {
time.Sleep(t.cfg.WaitAfterPageLoad)
select {
case pageWait <- struct{}{}:
default:
}
}()
})
// debugger log
if t.cfg.Verbose {
t.remote.CallbackEvent("Log.entryAdded", func(params godet.Params) {
logrus.WithFields(logrus.Fields(params.Map("entry"))).WithField("site", site).Debug("debugger logged")
})
// console log
t.remote.CallbackEvent("Runtime.consoleAPICalled", func(params godet.Params) {
f := logrus.Fields{
"type": params["type"].(string),
}
for _, a := range params["args"].([]interface{}) {
arg := a.(map[string]interface{})
if arg["value"] != nil {
f["value"] = arg["value"]
} else if arg["preview"] != nil {
arg := arg["preview"].(map[string]interface{})
v := arg["description"].(string) + "{"
for i, p := range arg["properties"].([]interface{}) {
if i > 0 {
v += ", "
}
prop := p.(map[string]interface{})
if prop["name"] != nil {
v += fmt.Sprintf("%q: ", prop["name"])
}
v += fmt.Sprintf("%v", prop["value"])
}
v += "}"
f["desc"] = v
} else {
f["type"] = arg["type"].(string)
}
}
logrus.WithFields(f).Debug("debugger console logged")
})
}
_ = t.remote.RuntimeEvents(true)
_ = t.remote.NetworkEvents(true)
_ = t.remote.PageEvents(true)
_ = t.remote.DOMEvents(true)
_ = t.remote.LogEvents(true)
_ = t.remote.EmulationEvents(true)
//_ = remote.EnableRequestInterception(true)
_, err = t.remote.Navigate(site)
if err != nil {
err = errors.Wrap(err, "send request failed")
return
}
<-pageWait
// parse response
var (
cookieCount int
reportRecords []*reportRecord
)
cookieCount, reportRecords, err = t.parseResponse(rc)
if err != nil {
return
}
// assemble with other page info
t.reportData = &reportData{
ScanTime: t.startTime,
ScanURL: site,
CookieCount: cookieCount,
Records: reportRecords,
}
// take snapshot of current page
screenShotImage, err := t.remote.CaptureScreenshot("png", 0, true)
if err == nil {
t.reportData.ScreenShotImage = base64.StdEncoding.EncodeToString(screenShotImage)
} else {
err = nil
}
return
}