-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdp.go
518 lines (455 loc) · 10.9 KB
/
cdp.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package cdp
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"sync"
"time"
"github.com/gorilla/websocket"
)
var (
ErrorOpenRemoteURL = errors.New("调试地址访问失败")
ErrorTabWsDisconnect = errors.New("websocket连接已经断开")
ErrorTimeout = errors.New("超时")
)
// ws消息体
type wsMessage struct {
ID int `json:"id"`
Result json.RawMessage `json:"result"`
Method string `json:"Method"`
Params json.RawMessage `json:"Params"`
}
type RemoteClient struct {
RemoteUrl *url.URL //调试地址
Client *http.Client //http client for brower
TabSize int //最多允许打开的tab标签数量
Tabs []*Tab //tab池
TabChan chan *Tab //tab chan
TabCallers []TabCaller //标签调用集合
lock *sync.Mutex //lock
}
// 标签调用方法
type TabCaller func(tab *Tab)
// params
type Params map[string]interface{}
// 事件回调
type EventCallback func(params Params)
// tab标签
type Tab struct {
Description string `json:"description"`
DevtoolsFrontendUrl string `json:"devtoolsFrontendUrl"`
ID string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Url string `json:"url"`
WebSocketDebuggerUrl string `json:"webSocketDebuggerUrl"`
BorwserClient *RemoteClient `json:"-"`
wsConn *websocket.Conn `json:"-"`
reqID int `json:"-"`
lock *sync.Mutex `json:"-"`
responses map[int]chan json.RawMessage `json:"-"`
eventCallBacks map[string]EventCallback `json:"-"`
}
func (c *RemoteClient) getRequest(method, path string) (*http.Request, error) {
url, err := c.RemoteUrl.Parse(path)
if err != nil {
return nil, err
}
return http.NewRequest(method, url.String(), nil)
}
// NewBrower 创建一个浏览器
func NewBrower(remoteUrl string, maxTabSize int) (*RemoteClient, error) {
var client RemoteClient
u, err := url.Parse(remoteUrl)
if err != nil {
return nil, err
}
http_client := &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{},
}
client.RemoteUrl = u
client.Client = http_client
client.TabSize = maxTabSize
client.lock = &sync.Mutex{}
client.TabChan = make(chan *Tab, maxTabSize)
tabs, err := client.TabList("page")
if err != nil {
return nil, err
}
n := maxTabSize - len(tabs)
if n < 0 {
for i := 0; i > n; i-- {
tab := tabs[0]
client.CloseTab(tab)
tabs = tabs[1:]
}
}
for i := 0; i < n; i++ {
tab, err := client.NewTab("")
if err != nil {
return nil, err
}
tabs = append(tabs, tab)
}
client.Tabs = tabs
for _, tab := range client.Tabs {
tab.BorwserClient = &client
}
err = tabInit(client.Tabs...)
if err == nil {
go client.Run()
}
return &client, err
}
// 初始化tab,建立ws侦听轮询
func tabInit(tabs ...*Tab) error {
for _, tab := range tabs {
conn, _, err := websocket.DefaultDialer.Dial(tab.WebSocketDebuggerUrl, nil)
if err != nil {
return err
}
tab.lock = &sync.Mutex{}
tab.wsConn = conn
tab.responses = make(map[int]chan json.RawMessage)
tab.eventCallBacks = make(map[string]EventCallback)
go tab.WsLoop()
}
return nil
}
func (c *RemoteClient) Run() {
for {
select {
case t := <-c.TabChan:
//清理tab
t.lock.Lock()
t.eventCallBacks = nil
t.responses = nil
t.lock.Unlock()
c.lock.Lock()
c.Tabs = append(c.Tabs, t)
c.lock.Unlock()
default:
if len(c.TabCallers) > 0 && len(c.Tabs) > 0 {
fmt.Printf("tabs: %d callers: %d", len(c.Tabs), len(c.TabCallers))
c.lock.Lock()
tab := c.Tabs[0]
c.Tabs = c.Tabs[1:]
caller := c.TabCallers[0]
c.TabCallers = c.TabCallers[1:]
c.lock.Unlock()
caller(tab)
c.TabChan <- tab
}
}
}
}
func (c *RemoteClient) Reset(t *Tab) {
t.wsConn.Close()
c.CloseTab(t)
tab, err := c.NewTab("")
if err != nil {
fmt.Println(err)
}
c.TabChan <- tab
}
// 添加一个执行任务
func (c *RemoteClient) Do(caller TabCaller) {
c.lock.Lock()
c.TabCallers = append(c.TabCallers, caller)
c.lock.Unlock()
}
// 创建标签
func (c *RemoteClient) NewTab(url string) (*Tab, error) {
path := "/json/new"
if url != "" {
path += "?" + url
}
req, err := c.getRequest("GET", path)
if err != nil {
return nil, err
}
resp, err := c.Client.Do(req)
if err != nil {
return nil, ErrorOpenRemoteURL
}
var tab Tab
if err = decode(resp, &tab); err != nil {
return nil, err
}
return &tab, nil
}
// 关闭标签
func (c *RemoteClient) CloseTab(t *Tab) error {
if t.wsConn != nil {
t.wsConn.Close()
}
path := "/json/close/" + t.ID
req, err := c.getRequest("GET", path)
if err != nil {
return err
}
_, err = c.Client.Do(req)
if err != nil {
return ErrorOpenRemoteURL
}
return nil
}
// 标签列表
func (c *RemoteClient) TabList(filter string) ([]*Tab, error) {
req, err := c.getRequest("GET", "/json/list")
if err != nil {
return nil, err
}
resp, err := c.Client.Do(req)
if err != nil {
return nil, ErrorOpenRemoteURL
}
var tabs []*Tab
if err = decode(resp, &tabs); err != nil {
return nil, err
}
if filter == "" {
return tabs, nil
}
var filtered []*Tab
for _, t := range tabs {
if t.Type == filter {
filtered = append(filtered, t)
}
}
return filtered, nil
}
func decode(resp *http.Response, v interface{}) error {
err := json.NewDecoder(resp.Body).Decode(v)
resp.Body.Close()
return err
}
func unmarshal(payload []byte) (map[string]interface{}, error) {
var response map[string]interface{}
err := json.Unmarshal(payload, &response)
if err != nil {
log.Println("unmarshal", string(payload), len(payload), err)
}
return response, err
}
// tab ws轮询
func (tab *Tab) WsLoop() {
for {
var message wsMessage
err := tab.wsConn.ReadJSON(&message)
if err != nil {
tab.reset()
break
} else if message.Method != "" {
callback, ok := tab.eventCallBacks[message.Method]
if ok {
params, err := unmarshal(message.Params)
if err == nil {
callback(params)
}
}
} else {
ch, ok := tab.responses[message.ID]
if ok {
ch <- message.Result
}
}
}
}
// 重置tab
func (tab *Tab) reset() error {
path := "/json/new"
req, err := tab.BorwserClient.getRequest("GET", path)
if err != nil {
return err
}
resp, err := tab.BorwserClient.Client.Do(req)
if err != nil {
return ErrorOpenRemoteURL
}
if err = decode(resp, &tab); err != nil {
return err
}
//通知任务失败
tab.lock.Lock()
for _, ch := range tab.responses {
ch <- []byte(`{"error":"tab reset"}`)
}
for _, callback := range tab.eventCallBacks {
callback(Params{
"error": "tab reset",
})
}
tab.responses = nil
tab.eventCallBacks = nil
tab.lock.Unlock()
return tabInit(tab)
}
func GetWs(t *Tab) (*websocket.Conn, error) {
if t.wsConn == nil {
d := &websocket.Dialer{}
ws, _, err := d.Dial(t.WebSocketDebuggerUrl, nil)
if err != nil {
return nil, err
}
t.wsConn = ws
return ws, nil
}
return t.wsConn, nil
}
func (p Params) String(k string) string {
val, _ := p[k].(string)
return val
}
func (p Params) Int(k string) int {
val, _ := p[k].(float64)
return int(val)
}
func (p Params) Bool(k string) bool {
val, _ := p[k].(bool)
return val
}
func (p Params) Map(k string) map[string]interface{} {
val, _ := p[k].(map[string]interface{})
return val
}
type EvaluateError struct {
ErrorDetails map[string]interface{}
ExceptionDetails map[string]interface{}
}
func (err EvaluateError) Error() string {
desc := err.ErrorDetails["description"].(string)
if excp := err.ExceptionDetails; excp != nil {
if excp["exception"] != nil {
desc += fmt.Sprintf(" at line %v col %v",
excp["lineNumber"].(float64), excp["columnNumber"].(float64))
}
}
return desc
}
func (t *Tab) Evaluate(expr string) (interface{}, error) {
params := Params{
"expression": expr,
"returnByValue": true,
}
res, err := t.SendRequest("Runtime.evaluate", params)
if err != nil {
return nil, err
}
if res == nil {
return nil, nil
}
result := res["result"].(map[string]interface{})
if subtype, ok := result["subtype"]; ok && subtype.(string) == "error" {
// this is actually an error
exception := res["exceptionDetails"].(map[string]interface{})
return nil, EvaluateError{ErrorDetails: result, ExceptionDetails: exception}
}
return result["value"], nil
}
func (t *Tab) Navigate(url string) error {
_, err := t.SendRequest("Page.navigate", Params{
"url": url,
})
return err
}
func (t *Tab) NetworkEvents(enable bool) error {
method := "Network"
if enable {
method += ".enable"
} else {
method += ".disable"
}
_, err := t.SendRequest(method, nil)
return err
}
func (t *Tab) CallbackEvent(method string, cb EventCallback) {
t.lock.Lock()
t.eventCallBacks[method] = cb
t.lock.Unlock()
}
func (t *Tab) ClearCallbacks() {
t.lock.Lock()
t.eventCallBacks = nil
t.lock.Unlock()
}
// SendRequest sends a request and returns the reply as a a map.
func (t *Tab) SendRequest(method string, params Params) (map[string]interface{}, error) {
rawReply, err := t.sendRawReplyRequest(method, params)
if err != nil || rawReply == nil {
return nil, err
}
res, err := unmarshal(rawReply)
if err != nil {
return nil, err
}
if _, ok := res["error"]; ok {
return nil, errors.New("tab reset")
}
return res, nil
}
// sendRawReplyRequest sends a request and returns the reply bytes.
func (t *Tab) sendRawReplyRequest(method string, params Params) ([]byte, error) {
t.lock.Lock()
responseChan := make(chan json.RawMessage, 1)
reqID := t.reqID
t.responses[reqID] = responseChan
t.reqID++
t.lock.Unlock()
command := Params{
"id": reqID,
"method": method,
"params": params,
}
err := t.wsConn.WriteJSON(command)
if err != nil {
return nil, err
}
reply := <-responseChan
close(responseChan)
t.lock.Lock()
delete(t.responses, reqID)
t.lock.Unlock()
return reply, nil
}
func (t *Tab) GetResponseBody(requestId string) ([]byte, error) {
res, err := t.SendRequest("Network.getResponseBody", Params{
"requestId": requestId,
})
if err != nil {
return nil, err
}
body := res["body"]
if body == nil {
return nil, nil
}
if b, ok := res["base64Encoded"]; ok && b.(bool) {
return base64.StdEncoding.DecodeString(body.(string))
} else {
return []byte(body.(string)), nil
}
}
// wait for page loaded before timeout
func (t *Tab) WaitPageComplete(timeout time.Duration) bool {
ready := make(chan bool)
go func(c chan bool) {
for {
time.Sleep(1 * time.Second)
status, err := t.Evaluate("document.readyState")
if err == nil && (status.(string) == "complete" || status.(string) == "interactive") {
c <- true
}
}
}(ready)
select {
case <-time.After(timeout):
return false
case r := <-ready:
return r
}
}