/
devtool.go
310 lines (256 loc) · 7.11 KB
/
devtool.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
package devtool
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"sync"
"github.com/mafredri/cdp/internal/errors"
)
// DevToolsOption represents a function that sets a DevTools option.
type DevToolsOption func(*DevTools)
// WithClient returns a DevToolsOption that sets the http Client used
// for HTTP GET requests.
func WithClient(client *http.Client) DevToolsOption {
return func(d *DevTools) {
d.client = client
}
}
// DevTools represents a devtools endpoint for managing and querying
// information about targets.
type DevTools struct {
url string
client *http.Client
mu sync.Mutex // Protects following.
lookup bool
}
// New returns a DevTools instance that uses URL.
func New(url string, opts ...DevToolsOption) *DevTools {
devtools := &DevTools{url: url}
for _, o := range opts {
o(devtools)
}
if devtools.client == nil {
devtools.client = &http.Client{}
}
return devtools
}
// Type represents the type of Target.
type Type string
// Type enums.
const (
BackgroundPage Type = "background_page"
Node Type = "node"
Other Type = "other"
Page Type = "page"
ServiceWorker Type = "service_worker"
)
// Target represents a devtools target, e.g. a browser tab.
type Target struct {
Description string `json:"description"`
DevToolsFrontendURL string `json:"devtoolsFrontendUrl"`
ID string `json:"id"`
Title string `json:"title"`
Type Type `json:"type"`
URL string `json:"url"`
WebSocketDebuggerURL string `json:"webSocketDebuggerUrl"`
}
// Create a new Target, usually a page with about:blank as URL.
func (d *DevTools) Create(ctx context.Context) (*Target, error) {
return d.CreateURL(ctx, "")
}
// CreateURL is like Create but opens the provided URL. The URL must be
// valid and begin with "http://" or "https://".
func (d *DevTools) CreateURL(ctx context.Context, openURL string) (*Target, error) {
var escapedQueryURL string
if openURL != "" {
if parsed, err := url.Parse(openURL); err != nil || !parsed.IsAbs() {
return nil, errors.New("devtool: CreateURL: invalid openURL: " + openURL)
}
escapedQueryURL = "?" + url.QueryEscape(openURL)
}
resp, err := d.httpGet(ctx, "/json/new"+escapedQueryURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
// Returned by Headless Chrome that does
// not support the "/json/new" endpoint.
case http.StatusInternalServerError:
err2 := parseError("CreateUrl: StatusInternalServerError", resp.Body)
v, err := d.Version(ctx)
if err != nil {
return nil, err2
}
if v.WebSocketDebuggerURL != "" {
// This version is too new since it has a debugger URL set.
return nil, err2
}
return fallbackHeadlessCreateURL(ctx, d, openURL)
case http.StatusOK:
t := new(Target)
return t, json.NewDecoder(resp.Body).Decode(t)
default:
return nil, parseError("CreateURL", resp.Body)
}
}
// Get the first Target that matches Type.
func (d *DevTools) Get(ctx context.Context, typ Type) (*Target, error) {
list, err := d.List(ctx)
if err != nil {
return nil, err
}
for _, t := range list {
if t.Type == typ {
return t, nil
}
}
return nil, errors.New("devtool: Get: could not find target of type: " + string(typ))
}
// List returns a list with all devtools Targets.
func (d *DevTools) List(ctx context.Context) ([]*Target, error) {
resp, err := d.httpGet(ctx, "/json/list")
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, parseError("List", resp.Body)
}
var t []*Target
return t, json.NewDecoder(resp.Body).Decode(&t)
}
// Activate brings focus to the Target.
func (d *DevTools) Activate(ctx context.Context, t *Target) error {
resp, err := d.httpGet(ctx, "/json/activate/"+t.ID)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return parseError("Activate", resp.Body)
}
return nil
}
// Close the Target.
func (d *DevTools) Close(ctx context.Context, t *Target) error {
resp, err := d.httpGet(ctx, "/json/close/"+t.ID)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return parseError("Close", resp.Body)
}
return nil
}
// Version contains the version information for the DevTools endpoint.
type Version struct {
// Present in Chrome, Edge, Node, etc.
Browser string `json:"Browser"`
Protocol string `json:"Protocol-Version"`
// Present in Chrome, Edge.
UserAgent string `json:"User-Agent"`
V8 string `json:"V8-Version"`
WebKit string `json:"WebKit-Version"`
// Present on Android.
AndroidPackage string `json:"Android-Package"`
// Present in Chrome >= 62. Generic browser websocket URL.
WebSocketDebuggerURL string `json:"webSocketDebuggerUrl"`
}
// Version returns the version information for the DevTools endpoint.
func (d *DevTools) Version(ctx context.Context) (*Version, error) {
resp, err := d.httpGet(ctx, "/json/version")
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, parseError("Version", resp.Body)
}
v := new(Version)
return v, json.NewDecoder(resp.Body).Decode(&v)
}
func (d *DevTools) httpGet(ctx context.Context, path string) (*http.Response, error) {
if ctx == nil {
ctx = context.Background()
}
err := d.resolveHost(ctx)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, d.url+path, nil)
if err != nil {
return nil, err
}
return d.client.Do(req.WithContext(ctx))
}
// resolveHost does a lookup on the hostname in d.url and tries to
// replace it with a valid IP address. Ever since Chrome 66, the
// DevTools endpoint disallows hostnames other than "localhost".
//
// Example error:
// < HTTP/1.1 500 Internal Server Error
// < Content-Length:63
// < Content-Type:text/html
// <
// Host header is specified and is not an IP address or localhost.
func (d *DevTools) resolveHost(ctx context.Context) error {
d.mu.Lock()
defer d.mu.Unlock()
if d.lookup {
return nil
}
d.lookup = true
u, err := url.Parse(d.url)
if err != nil {
return err
}
host := strings.Split(u.Host, ":")
origHost := host[0]
if origHost == "localhost" {
return nil // Nothing to do, localhost is allowed.
}
addrs, err := net.DefaultResolver.LookupHost(ctx, origHost)
if err != nil {
return err
}
newURL := ""
for _, a := range addrs {
host[0] = a
u.Host = strings.Join(host, ":")
try := u.String()
// The selection of "/json/version" here is arbitrary,
// it just needs to exist and not have side-effects.
req, err := http.NewRequest(http.MethodGet, try+"/json/version", nil)
if err != nil {
return err
}
resp, err := d.client.Do(req.WithContext(ctx))
if err != nil {
continue
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
newURL = try
break
}
}
if newURL == "" {
return errors.New("could not resolve IP for " + origHost)
}
d.url = newURL
return nil
}
func parseError(from string, r io.Reader) error {
m, err := ioutil.ReadAll(r)
if err != nil {
return err
}
return errors.New("devtool: " + from + ": " + string(m))
}