-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
339 lines (273 loc) · 8.58 KB
/
main.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
// This software is direct fork of https://github.com/knq/chromedp/tree/master/cmd/chromedp-proxy
// with couple of features added
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path"
"strings"
"errors"
"github.com/sirupsen/logrus"
)
var (
version string = "unknown"
commit string = "unknown"
date string = "unknown"
builtBy string = "unknown"
)
func main() {
flag.Parse()
if *flagVersion {
fmt.Printf("%s version %s built on %s by %s\n\nConfiguration:\n", os.Args[0], version, date, builtBy)
flag.PrintDefaults()
os.Exit(1)
}
mux := http.NewServeMux()
simpleReverseProxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: *flagRemote})
mux.Handle("/json", simpleReverseProxy)
mux.Handle("/", simpleReverseProxy)
rootLogger, err := createLogger("connection")
if err != nil {
panic(fmt.Sprintf("could not create logger: %s", err))
}
logger := rootLogger.WithFields(logrus.Fields{
fieldLevel: levelConnection,
})
handlerFunc := func(basePath string) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
stream := make(chan *protocolMessage, 1024)
id := strings.ReplaceAll(strings.TrimPrefix(req.URL.Path, "/devtools/"), "/", "-")
var protocolLogger *logrus.Entry
if *flagDistributeLogs {
logger, err := createLogger(id)
if err != nil {
panic(fmt.Sprintf("could not create logger: %s", err))
}
protocolLogger = logger.WithFields(logrus.Fields{
fieldLevel: levelConnection,
fieldInspectorID: id,
})
} else {
protocolLogger = logger.WithFields(logrus.Fields{
fieldInspectorID: id,
})
}
go dumpStream(protocolLogger, stream)
endpoint := "ws://" + *flagRemote + "/devtools/" + basePath + "/" + path.Base(req.URL.Path)
logger.Infof("---------- connection from %s to %s ----------", req.RemoteAddr, req.RequestURI)
logger.Infof("checking protocol versions on: %s", endpoint)
ver, err := checkVersion()
if err != nil {
protocolLogger.Errorf("could not check version: %v", err)
http.Error(res, "could not check version", 500)
return
}
logger.Infof("protocol version: %s", ver["Protocol-Version"])
logger.Infof("versions: Chrome(%s), V8(%s), Webkit(%s)", ver["Browser"], ver["V8-Version"], ver["WebKit-Version"])
logger.Infof("browser user agent: %s", ver["User-Agent"])
logger.Infof("connecting to %s... ", endpoint)
// connecting to ws
out, pres, err := wsDialer.Dial(endpoint, nil)
if err != nil {
msg := fmt.Sprintf("could not connect to %s: %v", endpoint, err)
logger.Error(protocolError(msg))
http.Error(res, msg, 500)
return
}
defer pres.Body.Close()
defer out.Close()
// connect incoming websocket
logger.Infof("upgrading connection on %s...", req.RemoteAddr)
in, err := wsUpgrader.Upgrade(res, req, nil)
if err != nil {
logger.Errorf("could not upgrade websocket from %s: %v", req.RemoteAddr, err)
http.Error(res, "could not upgrade websocket connection", 500)
return
}
defer in.Close()
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
errc := make(chan error, 1)
go proxyWS(ctxt, stream, in, out, errc)
go proxyWS(ctxt, stream, out, in, errc)
<-errc
close(stream)
logger.Infof("---------- closing connection from %s to %s ----------", req.RemoteAddr, req.RequestURI)
if *flagDistributeLogs {
destroyLogger(id)
}
if *flagOnce {
os.Exit(0)
}
}
}
mux.HandleFunc("/devtools/page/", handlerFunc("page"))
mux.HandleFunc("/devtools/browser/", handlerFunc("browser"))
log.Printf("Listening for DevTools connections on: %s", *flagListen)
log.Fatal(http.ListenAndServe(*flagListen, mux))
}
func dumpStream(logger *logrus.Entry, stream chan *protocolMessage) {
logger.Printf("Legend: %s, %s, %s, %s, %s, %s", protocolColor("protocol informations"),
eventsColor("received events"),
requestColor("sent request frames"),
requestReplyColor("requests params"),
responseColor("received responses"),
errorColor("error response."),
)
requests := make(map[uint64]*protocolMessage)
targetRequests := make(map[uint64]*protocolMessage)
sessions := make(map[string]interface{}, 0)
loop:
for {
select {
case msg, ok := <-stream:
if !ok {
for sessionId := range sessions {
_ = destroyLogger(fmt.Sprintf("session-%s", sessionId))
}
break loop
}
if msg.HasSessionId() {
var targetLogger *logrus.Entry
if *flagDistributeLogs {
logger, err := createLogger(fmt.Sprintf("session-%s", msg.TargetID()))
sessions[msg.TargetID()] = interface{}(nil)
if err != nil {
panic(fmt.Sprintf("could not create logger: %v", err))
}
targetLogger = logger.WithFields(logrus.Fields{
fieldLevel: levelTarget,
fieldTargetID: msg.TargetID(),
})
} else {
targetLogger = logger.WithFields(logrus.Fields{
fieldLevel: levelTarget,
fieldTargetID: msg.TargetID(),
})
}
if msg.IsRequest() {
requests[msg.ID] = nil
if protocolMessage, err := decodeProtocolMessage(msg); err == nil {
targetRequests[protocolMessage.ID] = protocolMessage
if *flagShowRequests {
targetLogger.WithFields(logrus.Fields{
fieldType: typeRequest,
fieldMethod: protocolMessage.Method,
}).Info(serialize(protocolMessage.Params))
}
} else {
logger.WithFields(logrus.Fields{
fieldLevel: levelConnection,
}).Errorf("Could not deserialize message: %+v", err)
}
}
if msg.IsEvent() {
if protocolMessage, err := decodeProtocolMessage(msg); err == nil {
if protocolMessage.IsEvent() {
targetLogger.WithFields(logrus.Fields{
fieldType: typeEvent,
fieldMethod: protocolMessage.Method,
}).Info(serialize(protocolMessage.Params))
}
if protocolMessage.IsResponse() {
var logMessage string
var logType int
var logRequest string
var logMethod string
if protocolMessage.IsError() {
logMessage = serialize(protocolMessage.Error)
logType = typeRequestResponseError
} else {
logMessage = serialize(protocolMessage.Result)
logType = typeRequestResponse
}
if request, ok := targetRequests[protocolMessage.ID]; ok && request != nil {
delete(targetRequests, protocolMessage.ID)
logRequest = serialize(request.Params)
logMethod = request.Method
} else {
logRequest = errorColor("could not find request with id: %d", protocolMessage.ID)
}
targetLogger.WithFields(logrus.Fields{
fieldType: logType,
fieldMethod: logMethod,
fieldRequest: logRequest,
}).Info(logMessage)
}
} else {
logger.WithFields(logrus.Fields{
fieldLevel: levelConnection,
}).Errorf("Could not deserialize message: %+v", err)
}
}
} else {
protocolLogger := logger.WithFields(logrus.Fields{
fieldLevel: levelProtocol,
fieldTargetID: protocolTargetID,
})
if msg.IsRequest() {
requests[msg.ID] = msg
if *flagShowRequests {
protocolLogger.WithFields(logrus.Fields{
fieldType: typeRequest,
fieldMethod: msg.Method,
}).Info(serialize(msg.Params))
}
}
if msg.IsResponse() {
var logMessage string
var logType int
var logRequest string
var logMethod string
if msg.IsError() {
logMessage = serialize(msg.Error)
logType = typeRequestResponseError
} else {
logMessage = serialize(msg.Result)
logType = typeRequestResponse
}
if request, ok := requests[msg.ID]; ok && request != nil {
logRequest = serialize(request.Params)
logMethod = request.Method
delete(requests, msg.ID)
protocolLogger.WithFields(logrus.Fields{
fieldType: logType,
fieldMethod: logMethod,
fieldRequest: logRequest,
}).Info(logMessage)
}
}
if msg.IsEvent() {
protocolLogger.WithFields(logrus.Fields{
fieldType: typeEvent,
fieldMethod: msg.Method,
}).Info(serialize(msg.Params))
}
}
}
}
}
func checkVersion() (map[string]string, error) {
cl := &http.Client{}
req, err := http.NewRequest("GET", "http://"+*flagRemote+"/json/version", nil)
if err != nil {
return nil, err
}
res, err := cl.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var v map[string]string
if err := json.NewDecoder(res.Body).Decode(&v); err != nil {
return nil, errors.New("expected json result")
}
return v, nil
}