Skip to content

Commit b526e46

Browse files
erikogenvikwendigo
authored andcommitted
Make it work with latest protocol.
Seems like the protocol has changed. We now need to look for "result" to recognize responses. We also now handle messages we don't understand by writing error messages to the log. In addition, we handle request-responses separately per session (since id is specific per session). Also made it easier to read output in non-colour mode by removing extra characters.
1 parent e2f5e88 commit b526e46

File tree

5 files changed

+82
-22
lines changed

5 files changed

+82
-22
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ This image bundles headless Chrome in the latest version so debugger is ready to
3737
show delta time between log entries
3838
-exclude value
3939
exclude requests/responses/events matching pattern (default exclude = )
40+
-force-color
41+
force color output regardless of TTY
4042
-i include request frames as they are sent
4143
-include value
4244
display only requests/responses/events matching pattern (default include = )

logger.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ const (
3636
)
3737

3838
const (
39-
requestReplyFormat = "%-17s %-32s % 48s(%s) = %s\n"
40-
requestFormat = "%-17s %-32s % 48s(%s)\n"
41-
eventFormat = "%-17s %-32s % 48s(%s)\n"
39+
requestReplyFormat = "%-17s %-32s % 48s %s => %s\n"
40+
requestFormat = "%-17s %-32s % 48s %s\n"
41+
eventFormat = "%-17s %-32s % 48s %s\n"
4242
protocolFormat = "%-17s %-32s\n"
4343
timeFormat = "15:04:05.00000000"
4444
deltaFormat = "Δ%8.2fms"

main.go

+71-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/url"
1414
"os"
1515
"path"
16+
"strconv"
1617
"strings"
1718

1819
"errors"
@@ -142,7 +143,7 @@ func main() {
142143
mux.HandleFunc("/devtools/page/", handlerFunc("page"))
143144
mux.HandleFunc("/devtools/browser/", handlerFunc("browser"))
144145

145-
log.Printf("Listening for DevTools connections on: %s", *flagListen)
146+
log.Printf("Proxy is listening for DevTools connections on: %s", *flagListen)
146147

147148
log.Fatal(http.ListenAndServe(*flagListen, mux))
148149
}
@@ -157,8 +158,7 @@ func dumpStream(logger *logrus.Entry, stream chan *protocolMessage) {
157158
)
158159

159160
requests := make(map[uint64]*protocolMessage)
160-
targetRequests := make(map[uint64]*protocolMessage)
161-
sessions := make(map[string]interface{}, 0)
161+
sessions := make(map[string]map[uint64]*protocolMessage)
162162

163163
loop:
164164
for {
@@ -174,9 +174,14 @@ loop:
174174
if msg.HasSessionId() {
175175
var targetLogger *logrus.Entry
176176

177+
targetRequests, exists := sessions[msg.TargetID()]
178+
if !exists {
179+
targetRequests = make(map[uint64]*protocolMessage)
180+
sessions[msg.TargetID()] = targetRequests
181+
}
182+
177183
if *flagDistributeLogs {
178184
logger, err := createLogger(fmt.Sprintf("session-%s", msg.TargetID()))
179-
sessions[msg.TargetID()] = interface{}(nil)
180185

181186
if err != nil {
182187
panic(fmt.Sprintf("could not create logger: %v", err))
@@ -203,7 +208,7 @@ loop:
203208
if *flagShowRequests {
204209
targetLogger.WithFields(logrus.Fields{
205210
fieldType: typeRequest,
206-
fieldMethod: protocolMessage.Method,
211+
fieldMethod: protocolMessage.Method + "-(" + strconv.FormatUint(msg.ID, 10) + ")",
207212
}).Info(serialize(protocolMessage.Params))
208213
}
209214

@@ -212,18 +217,14 @@ loop:
212217
fieldLevel: levelConnection,
213218
}).Errorf("Could not deserialize message: %+v", err)
214219
}
215-
}
216-
217-
if msg.IsEvent() {
220+
} else if msg.IsEvent() {
218221
if protocolMessage, err := decodeProtocolMessage(msg); err == nil {
219222
if protocolMessage.IsEvent() {
220223
targetLogger.WithFields(logrus.Fields{
221224
fieldType: typeEvent,
222225
fieldMethod: protocolMessage.Method,
223226
}).Info(serialize(protocolMessage.Params))
224-
}
225-
226-
if protocolMessage.IsResponse() {
227+
} else if protocolMessage.IsResponse() {
227228
var logMessage string
228229
var logType int
229230
var logRequest string
@@ -246,17 +247,68 @@ loop:
246247
logRequest = errorColor("could not find request with id: %d", protocolMessage.ID)
247248
}
248249

250+
if *flagShowRequests {
251+
logMethod += "*(" + strconv.FormatUint(msg.ID, 10) + ")"
252+
} else {
253+
logMethod += "*"
254+
}
255+
249256
targetLogger.WithFields(logrus.Fields{
250257
fieldType: logType,
251258
fieldMethod: logMethod,
252259
fieldRequest: logRequest,
253260
}).Info(logMessage)
261+
} else {
262+
targetLogger.WithFields(logrus.Fields{
263+
fieldType: typeRequest,
264+
fieldMethod: msg.Method,
265+
}).Info("Could not understand session event: " + msg.raw)
254266
}
255267
} else {
256268
logger.WithFields(logrus.Fields{
257269
fieldLevel: levelConnection,
258270
}).Errorf("Could not deserialize message: %+v", err)
259271
}
272+
} else if msg.IsResponse() {
273+
var logMessage string
274+
var logType int
275+
var logRequest string
276+
var logMethod string
277+
278+
if msg.IsError() {
279+
logMessage = serialize(msg.Error)
280+
logType = typeRequestResponseError
281+
} else {
282+
logMessage = serialize(msg.Result)
283+
logType = typeRequestResponse
284+
}
285+
286+
if request, ok := targetRequests[msg.ID]; ok && request != nil {
287+
delete(targetRequests, msg.ID)
288+
logRequest = serialize(request.Params)
289+
logMethod = request.Method
290+
291+
} else {
292+
logRequest = errorColor("could not find request with id: %d", msg.ID)
293+
}
294+
295+
if *flagShowRequests {
296+
logMethod += "*(" + strconv.FormatUint(msg.ID, 10) + ")"
297+
} else {
298+
logMethod += "*"
299+
}
300+
301+
targetLogger.WithFields(logrus.Fields{
302+
fieldType: logType,
303+
fieldMethod: logMethod,
304+
fieldRequest: logRequest,
305+
}).Info(logMessage)
306+
307+
} else {
308+
targetLogger.WithFields(logrus.Fields{
309+
fieldType: typeRequest,
310+
fieldMethod: msg.Method,
311+
}).Info("Could not understand session message: " + msg.raw)
260312
}
261313

262314
} else {
@@ -271,12 +323,10 @@ loop:
271323
if *flagShowRequests {
272324
protocolLogger.WithFields(logrus.Fields{
273325
fieldType: typeRequest,
274-
fieldMethod: msg.Method,
326+
fieldMethod: msg.Method + "-(" + strconv.FormatUint(msg.ID, 10) + ")",
275327
}).Info(serialize(msg.Params))
276328
}
277-
}
278-
279-
if msg.IsResponse() {
329+
} else if msg.IsResponse() {
280330

281331
var logMessage string
282332
var logType int
@@ -303,13 +353,16 @@ loop:
303353
fieldRequest: logRequest,
304354
}).Info(logMessage)
305355
}
306-
}
307-
308-
if msg.IsEvent() {
356+
} else if msg.IsEvent() {
309357
protocolLogger.WithFields(logrus.Fields{
310358
fieldType: typeEvent,
311359
fieldMethod: msg.Method,
312360
}).Info(serialize(msg.Params))
361+
} else {
362+
protocolLogger.WithFields(logrus.Fields{
363+
fieldType: typeRequest,
364+
fieldMethod: msg.Method,
365+
}).Info("Could not understand message: " + msg.raw)
313366
}
314367
}
315368
}

protocol.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package main
33
import "fmt"
44

55
type protocolMessage struct {
6+
/**
7+
The raw message as string.
8+
*/
9+
raw string
610
ID uint64 `json:"id"`
711
Result map[string]interface{} `json:"result"`
812
Error struct {
@@ -32,7 +36,7 @@ func (p *protocolMessage) IsError() bool {
3236
}
3337

3438
func (p *protocolMessage) IsResponse() bool {
35-
return p.Method == "" && p.ID > 0
39+
return p.Result != nil && p.ID > 0
3640
}
3741

3842
func (p *protocolMessage) IsRequest() bool {

utils.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func decodeMessage(bytes []byte) (*protocolMessage, error) {
5252
return nil, err
5353
}
5454

55+
msg.raw = string(bytes[:])
5556
return &msg, nil
5657
}
5758

0 commit comments

Comments
 (0)