forked from raff/godet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.go
378 lines (306 loc) · 7.15 KB
/
extension.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
package godet
import (
"encoding/json"
"fmt"
"github.com/gobs/httpclient"
"github.com/gorilla/websocket"
"log"
"regexp"
"sync"
)
// EventCallback represents a callback event, associated with a method.
type EventCallback func(params Params)
//var callbacks map[string]EventCallback
var callbacks sync.Map
var eventChan chan wsMessage
var client *httpclient.HttpClient
var remoteDebuggerList []*RemoteDebugger
// RemoteDebugger implements an interface for Chrome DevTools.
type RemoteDebugger struct {
sync.Mutex
http *httpclient.HttpClient
wsConn *websocket.Conn // websocket connection
current string
reqID int
verbose bool
closed chan bool
wg *sync.WaitGroup
requests chan Params
responses map[int]chan json.RawMessage
}
// TargetEvents enables Target events listening.
// Adding by aiwen
func (remote *RemoteDebugger) TargetEvents(enable bool) error {
param := make(Params)
param["discover"] = enable
_, err := remote.SendRequest("Target.setDiscoverTargets", param)
return err
}
// TargetId return the target ID associate with the Connection
func (remote *RemoteDebugger) TargetId() string {
return remote.current
}
func (remote *RemoteDebugger) GetTitle(requestId string) string {
param := Params{
"requestId": requestId,
}
resp, err := remote.SendRequest("Network.getResponseBody", param)
if err != nil || resp == nil {
return ""
}
if body, ok := resp["body"]; ok {
var re = regexp.MustCompile(`(?m)<title>(.*)</title>`)
matches := re.FindStringSubmatch(body.(string))
if len(matches) == 1 {
return ""
}
return matches[1]
}
return ""
}
// 处理收到的事件
func processEvents() {
for ev := range eventChan {
load, ok := callbacks.Load(ev.Method)
if !ok {
continue
}
cb := load.(EventCallback)
//cb := callbacks[ev.Method]
if cb != nil {
var params Params
if err := json.Unmarshal(ev.Params, ¶ms); err != nil {
log.Println("unmarshal", string(ev.Params), len(ev.Params), err)
} else {
cb(params)
}
}
}
}
func (remote *RemoteDebugger) connectWs(tab *Tab) error {
if tab == nil || len(tab.WsURL) == 0 {
tabs, err := remote.TabList("page")
if err != nil {
return err
}
if len(tabs) == 0 {
return ErrorNoActiveTab
}
if tab == nil {
tab = tabs[0]
} else {
for _, t := range tabs {
if tab.ID == t.ID {
tab.WsURL = t.WsURL
break
}
}
}
}
if remote.wsConn != nil {
if tab.ID == remote.current {
// nothing to do
return nil
}
if remote.verbose {
log.Println("disconnecting from current tab, id", remote.current)
}
remote.Lock()
ws := remote.wsConn
remote.wsConn, remote.current = nil, ""
remote.Unlock()
_ = ws.Close()
}
if len(tab.WsURL) == 0 {
return ErrorNoWsURL
}
// check websocket connection
if remote.verbose {
log.Println("connecting to tab", tab.WsURL)
}
d := &websocket.Dialer{
ReadBufferSize: MaxReadBufferSize,
WriteBufferSize: MaxWriteBufferSize,
}
ws, _, err := d.Dial(tab.WsURL, nil)
if err != nil {
if remote.verbose {
log.Println("dial error:", err)
}
return err
}
remote.Lock()
remote.wsConn = ws
remote.current = tab.ID
remote.Unlock()
go remote.sendMessages()
go remote.readMessages(ws)
remote.wg.Add(2)
_ = remote.PageEvents(true)
_ = remote.NetworkEvents(true)
_ = remote.TargetEvents(true)
_ = remote.SetCacheDisabled(true)
return nil
}
// StartCapture 连接当前活跃tab页,并开始接收消息
func StartCapture(port string, verbose bool, options ...ConnectOption) (*RemoteDebugger, error) {
//callbacks = make(map[string]EventCallback)
eventChan = make(chan wsMessage, 1024)
// 创建一个到Chrome调试端口的HTTP连接,使用/json系列api
client = httpclient.NewHttpClient("http://" + port)
for _, setOption := range options {
setOption(client)
}
remote := &RemoteDebugger{
http: client,
requests: make(chan Params, 1),
responses: map[int]chan json.RawMessage{},
closed: make(chan bool),
verbose: verbose,
wg: &sync.WaitGroup{},
}
// remote.http.Verbose = verbose
if verbose {
httpclient.StartLogging(false, true, false)
}
// 连接当前活跃标签页,初始只有一个标签页
if err := remote.connectWs(nil); err != nil {
return nil, err
}
// 添加到Client列表
remoteDebuggerList = append(remoteDebuggerList, remote)
go processEvents()
return remote, nil
}
// StopCapture stop service
func StopCapture() {
fmt.Println(len(remoteDebuggerList))
for _, remote := range remoteDebuggerList {
_ = remote.Close()
}
close(eventChan)
}
// AddEventListener add event handler to the service
func AddEventListener(method string, cb EventCallback) {
//callbacks[method] = cb
callbacks.Store(method, cb)
}
func AddDebugger(targetId string, baseWsUrl string) error {
// param check
if len(baseWsUrl) == 0 || len(targetId) == 0 {
return ErrorNoWsURL
}
// 创建RemoteDebugger Client
remote := &RemoteDebugger{
http: client,
requests: make(chan Params, 1),
responses: map[int]chan json.RawMessage{},
closed: make(chan bool),
verbose: false,
wg: &sync.WaitGroup{},
}
// 添加到Client列表
remoteDebuggerList = append(remoteDebuggerList, remote)
d := &websocket.Dialer{
ReadBufferSize: MaxReadBufferSize,
WriteBufferSize: MaxWriteBufferSize,
}
ws, _, err := d.Dial(baseWsUrl+targetId, nil)
if err != nil {
if remote.verbose {
log.Println("dial error:", err)
}
return err
}
remote.Lock()
remote.wsConn = ws
remote.current = targetId
remote.Unlock()
// 启动接收协程
go remote.sendMessages()
go remote.readMessages(ws)
remote.wg.Add(2)
// 开始接收事件
_ = remote.PageEvents(true)
_ = remote.NetworkEvents(true)
_ = remote.TargetEvents(true)
_ = remote.SetCacheDisabled(true)
return nil
}
// Close the RemoteDebugger connection.
func (remote *RemoteDebugger) Close() (err error) {
remote.Lock()
ws := remote.wsConn
remote.wsConn = nil
if ws != nil { // already closed
err = ws.Close()
close(remote.closed)
close(remote.requests)
}
remote.Unlock()
// 等待发送和接收协程退出
remote.wg.Wait()
if remote.verbose {
httpclient.StopLogging()
}
return
}
//------------------------------------------------------------------------------------------//
// Params is a type alias for the event params structure.
type Params map[string]interface{}
func (p Params) String(k string) string {
if p == nil {
return ""
}
if i, ok := p[k]; ok {
if val, ok := i.(string); ok {
return val
}
}
return ""
}
func (p Params) Int(k string) int {
if p == nil {
return 0
}
if i, ok := p[k]; ok {
if val, ok := i.(int); ok {
return val
}
}
return 0
}
func (p Params) Bool(k string) bool {
if p == nil {
return false
}
if i, ok := p[k]; ok {
if val, ok := i.(bool); ok {
return val
}
}
return false
}
func (p Params) Map(k string) map[string]interface{} {
if p == nil {
return nil
}
if i, ok := p[k]; ok {
if val, ok := i.(map[string]interface{}); ok {
return val
}
}
return nil
}
// Float64 return the float value
func (p Params) Float64(k string) float64 {
if p == nil {
return 0
}
if i, ok := p[k]; ok {
if val, ok := i.(float64); ok {
return val
}
}
return 0
}