-
Notifications
You must be signed in to change notification settings - Fork 31
/
gcd.go
401 lines (342 loc) · 10.4 KB
/
gcd.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
The MIT License (MIT)
Copyright (c) 2020 isaac dawson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package gcd
import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/wirepair/gcd/gcdapi"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
var GCDVERSION = "v1.0.15"
var (
ErrNoTabAvailable = errors.New("no available tab found")
)
// When we get an error reading the body from the debugger api endpoint
type GcdBodyReadErr struct {
Message string
}
func (g *GcdBodyReadErr) Error() string {
return "error reading response body: " + g.Message
}
// Failure to unmarshal the JSON response from debugger API
type GcdDecodingErr struct {
Message string
}
func (g *GcdDecodingErr) Error() string {
return "error decoding inspectable page: " + g.Message
}
type TerminatedHandler func(reason string)
// The Google Chrome Debugger
type Gcd struct {
timeout time.Duration // how much time to wait for debugger port to open up
chromeProcess *os.Process
chromeCmd *exec.Cmd
terminatedHandler TerminatedHandler
port string
host string
addr string
profileDir string
deleteProfile bool
readyCh chan struct{}
apiEndpoint string
flags []string
env []string
chomeApiVersion string
ctx context.Context
}
// Give it a friendly name.
func NewChromeDebugger() *Gcd {
c := &Gcd{}
c.timeout = 15
c.host = "localhost"
c.readyCh = make(chan struct{})
c.terminatedHandler = nil
c.flags = make([]string, 0)
c.env = make([]string, 0)
c.ctx = context.Background()
return c
}
// Pass a handler to be notified when the chrome process exits.
func (c *Gcd) SetTerminationHandler(handler TerminatedHandler) {
c.terminatedHandler = handler
}
// SetTimeout for how long we should wait for debug port to become available.
func (c *Gcd) SetTimeout(timeout time.Duration) {
c.timeout = timeout
}
// Port that the debugger is listening on
func (c *Gcd) Port() string {
return c.port
}
// Host that the debugger is listening on
func (c *Gcd) Host() string {
return c.host
}
// AddFlags Allows caller to add additional startup flags to the chrome process
func (c *Gcd) AddFlags(flags []string) {
c.flags = append(c.flags, flags...)
}
// AddEnvironmentVars for the chrome process, useful for Xvfb etc.
func (c *Gcd) AddEnvironmentVars(vars []string) {
c.env = append(c.env, vars...)
}
func (c *Gcd) DeleteProfileOnExit() {
c.deleteProfile = true
}
// StartProcess the process
// exePath - the path to the executable
// userDir - the user directory to start from so we get a fresh profile
// port - The port to listen on.
func (c *Gcd) StartProcess(exePath, userDir, port string) error {
c.port = port
c.addr = fmt.Sprintf("%s:%s", c.host, c.port)
c.profileDir = userDir
c.apiEndpoint = fmt.Sprintf("http://%s/json", c.addr)
// profile directory
c.flags = append(c.flags, fmt.Sprintf("--user-data-dir=%s", c.profileDir))
// debug port to use
c.flags = append(c.flags, fmt.Sprintf("--remote-debugging-port=%s", port))
// bypass first run check
c.flags = append(c.flags, "--no-first-run")
// bypass default browser check
c.flags = append(c.flags, "--no-default-browser-check")
c.chromeCmd = exec.Command(exePath, c.flags...)
// add custom environment variables.
c.chromeCmd.Env = os.Environ()
c.chromeCmd.Env = append(c.chromeCmd.Env, c.env...)
return c.startProcess()
}
// StartProcessCustom lets you pass in the exec.Cmd to use
func (c *Gcd) StartProcessCustom(cmd *exec.Cmd, userDir, port string) error {
c.port = port
c.addr = fmt.Sprintf("%s:%s", c.host, c.port)
c.profileDir = userDir
c.apiEndpoint = fmt.Sprintf("http://%s/json", c.addr)
c.chromeCmd = cmd
return c.startProcess()
}
// startProcess starts the process and waits for the debugger port to be ready
func (c *Gcd) startProcess() error {
go func() {
err := c.chromeCmd.Start()
if err != nil {
msg := fmt.Sprintf("failed to start chrome: %s", err)
if c.terminatedHandler != nil {
c.terminatedHandler(msg)
} else {
log.Println(msg)
}
}
c.chromeProcess = c.chromeCmd.Process
err = c.chromeCmd.Wait()
c.removeProfileDir()
closeMessage := "exited"
if err != nil {
closeMessage = err.Error()
}
if c.terminatedHandler != nil {
c.terminatedHandler(closeMessage)
}
}()
var err error
go func(err error) {
err = c.probeDebugPort()
}(err)
<-c.readyCh
return err
}
// ExitProcess kills the process
func (c *Gcd) ExitProcess() error {
return c.chromeProcess.Kill()
}
// PID of the started process
func (c *Gcd) PID() int {
return c.chromeProcess.Pid
}
// removeProfileDir if deleteProfile is true
func (c *Gcd) removeProfileDir() {
if c.deleteProfile {
// let chrome shutdown completely before deleting
time.Sleep(1 * time.Second)
if err := os.RemoveAll(c.profileDir); err != nil {
log.Printf("error deleting profile directory: %s\n", err)
}
}
}
// ConnectToInstance connects to a running chrome instance without starting a local process
// Host - The host destination.
// Port - The port to listen on.
func (c *Gcd) ConnectToInstance(host string, port string) error {
c.host = host
c.port = port
c.addr = fmt.Sprintf("%s:%s", c.host, c.port)
c.apiEndpoint = fmt.Sprintf("http://%s/json", c.addr)
var err error
go func(err error) {
err = c.probeDebugPort()
}(err)
<-c.readyCh
return err
}
// GetTargets primary tabs/processes to work with. Each will have their own references
// to the underlying API components (such as Page, Debugger, DOM etc).
func (c *Gcd) GetTargets() ([]*ChromeTarget, error) {
empty := make(map[string]struct{}, 0)
return c.GetNewTargets(empty)
}
// GetNewTargets gets a list of current tabs and creates new chrome targets returning a list
// provided they weren't in the knownIds list. Note it is an error to attempt
// to create a new chrome target from one that already exists.
func (c *Gcd) GetNewTargets(knownIds map[string]struct{}) ([]*ChromeTarget, error) {
connectableTargets, err := c.getConnectableTargets()
if err != nil {
return nil, err
}
chromeTargets := make([]*ChromeTarget, 0)
for _, v := range connectableTargets {
if _, ok := knownIds[v.Id]; !ok {
target, err := openChromeTarget(c.ctx, c.addr, v)
if err != nil {
return nil, err
}
chromeTargets = append(chromeTargets, target)
}
}
return chromeTargets, nil
}
func (c *Gcd) getConnectableTargets() ([]*TargetInfo, error) {
// some times it takes a while to get results, so retry 4x
for i := 0; i < 4; i++ {
resp, err := http.Get(c.apiEndpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, errRead := ioutil.ReadAll(resp.Body)
if errRead != nil {
return nil, &GcdBodyReadErr{Message: errRead.Error()}
}
targets := make([]*TargetInfo, 0)
err = json.Unmarshal(body, &targets)
if err != nil {
fmt.Printf("%s\n", string(body))
return nil, &GcdDecodingErr{Message: err.Error()}
}
connectableTargets := make([]*TargetInfo, 0)
for _, v := range targets {
if v.WebSocketDebuggerUrl != "" {
connectableTargets = append(connectableTargets, v)
}
}
if len(connectableTargets) > 0 {
return connectableTargets, nil
}
time.Sleep(time.Millisecond * 350)
}
return nil, ErrNoTabAvailable
}
// NewTab a new empty tab, returns the chrome target.
func (c *Gcd) NewTab() (*ChromeTarget, error) {
resp, err := http.Get(c.apiEndpoint + "/new")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, errRead := ioutil.ReadAll(resp.Body)
if errRead != nil {
return nil, &GcdBodyReadErr{Message: errRead.Error()}
}
tabTarget := &TargetInfo{}
err = json.Unmarshal(body, &tabTarget)
if err != nil {
return nil, &GcdDecodingErr{Message: err.Error()}
}
return openChromeTarget(c.ctx, c.addr, tabTarget)
}
// GetFirstTab returns the first tab created, to be called when
// first started, otherwise you will get a random tab returned.
func (c *Gcd) GetFirstTab() (*ChromeTarget, error) {
connectableTargets, err := c.getConnectableTargets()
if err != nil {
return nil, err
}
for _, tabTarget := range connectableTargets {
if tabTarget.Type == "page" {
return openChromeTarget(c.ctx, c.addr, tabTarget)
}
}
return nil, ErrNoTabAvailable
}
// GetRevision of chrome
func (c *Gcd) GetRevision() string {
return gcdapi.CHROME_VERSION
}
// CloseTab closes the target tab.
func (c *Gcd) CloseTab(target *ChromeTarget) error {
target.shutdown() // close WS connection first
resp, err := http.Get(fmt.Sprintf("%s/close/%s", c.apiEndpoint, target.Target.Id))
if err != nil {
return err
}
defer resp.Body.Close()
_, errRead := ioutil.ReadAll(resp.Body)
return errRead
}
// ActivateTab (focus) the tab.
func (c *Gcd) ActivateTab(target *ChromeTarget) error {
resp, err := http.Get(fmt.Sprintf("%s/activate/%s", c.apiEndpoint, target.Target.Id))
if err != nil {
return err
}
defer resp.Body.Close()
_, errRead := ioutil.ReadAll(resp.Body)
return errRead
}
// probes the debugger report and signals when it's available.
func (c *Gcd) probeDebugPort() error {
ticker := time.NewTicker(time.Millisecond * 100)
timeoutTicker := time.NewTicker(time.Second * c.timeout)
defer func() {
ticker.Stop()
timeoutTicker.Stop()
}()
for {
select {
case <-ticker.C:
resp, err := http.Get(c.apiEndpoint)
if err != nil {
continue
}
defer resp.Body.Close()
c.readyCh <- struct{}{}
return nil
case <-timeoutTicker.C:
return fmt.Errorf("Unable to contact debugger at %s after %d seconds, gave up", c.apiEndpoint, c.timeout)
}
}
}