@@ -15,36 +15,40 @@ import (
15
15
"os"
16
16
"path"
17
17
18
- "github.com/gorilla/websocket"
19
- "github.com/fatih/color"
20
18
"errors"
19
+
20
+ "github.com/fatih/color"
21
+ "github.com/gorilla/websocket"
21
22
)
22
23
23
24
var (
24
- flagListen = flag .String ("l" , "localhost:9223" , "listen address" )
25
- flagRemote = flag .String ("r" , "localhost:9222" , "remote address" )
26
- flagNoLog = flag .Bool ("n" , false , "disable logging to file" )
27
- flagLogMask = flag .String ("log" , "logs/cdp-%s.log" , "log file mask" )
28
- flagEllipsis = flag .Bool ("s" , false , "shorten requests and responses" )
29
- flagOnce = flag .Bool ("once" , false , "debug single session" )
25
+ flagListen = flag .String ("l" , "localhost:9223" , "listen address" )
26
+ flagRemote = flag .String ("r" , "localhost:9222" , "remote address" )
27
+ flagNoLog = flag .Bool ("n" , false , "disable logging to file" )
28
+ flagLogMask = flag .String ("log" , "logs/cdp-%s.log" , "log file mask" )
29
+ flagEllipsis = flag .Bool ("s" , false , "shorten requests and responses" )
30
+ flagOnce = flag .Bool ("once" , false , "debug single session" )
31
+ flagShowRequests = flag .Bool ("i" , false , "include request frames as they are sent" )
30
32
)
31
33
32
34
var (
33
- responseColor = color .New (color .FgHiGreen ).SprintfFunc ()
34
- requestColor = color .New (color .FgGreen ).SprintfFunc ()
35
- eventsColor = color .New (color .FgHiRed ).SprintfFunc ()
36
- protocolColor = color .New (color .FgYellow ).SprintfFunc ()
37
- protocolError = color .New (color .FgHiYellow , color .BgRed ).SprintfFunc ()
38
- targetColor = color .New (color .FgHiWhite ).SprintfFunc ()
39
- methodColor = color .New (color .FgHiYellow ).SprintfFunc ()
40
- errorColor = color .New (color .BgRed , color .FgWhite ).SprintfFunc ()
35
+ responseColor = color .New (color .FgHiGreen ).SprintfFunc ()
36
+ requestColor = color .New (color .FgHiBlue ).SprintFunc ()
37
+ requestReplyColor = color .New (color .FgGreen ).SprintfFunc ()
38
+ eventsColor = color .New (color .FgHiRed ).SprintfFunc ()
39
+ protocolColor = color .New (color .FgYellow ).SprintfFunc ()
40
+ protocolError = color .New (color .FgHiYellow , color .BgRed ).SprintfFunc ()
41
+ targetColor = color .New (color .FgHiWhite ).SprintfFunc ()
42
+ methodColor = color .New (color .FgHiYellow ).SprintfFunc ()
43
+ errorColor = color .New (color .BgRed , color .FgWhite ).SprintfFunc ()
41
44
)
42
45
43
46
const (
44
47
incomingBufferSize = 10 * 1024 * 1024
45
48
outgoingBufferSize = 25 * 1024 * 1024
46
49
ellipsisLength = 80
47
50
requestReplyFormat = "%s % 48s(%s) = %s"
51
+ requestFormat = "%s % 48s(%s)"
48
52
eventFormat = "%s % 48s(%s)"
49
53
)
50
54
@@ -140,10 +144,11 @@ func main() {
140
144
}
141
145
142
146
func dumpStream (logger * log.Logger , stream chan * protocolMessage ) {
143
- logger .Printf ("Legend: %s, %s, %s, %s" ,
147
+ logger .Printf ("Legend: %s, %s, %s, %s, %s " ,
144
148
protocolColor ("protocol informations" ),
145
149
eventsColor ("received events" ),
146
- requestColor ("sent requests" ),
150
+ requestColor ("sent request frames" ),
151
+ requestReplyColor ("requests params" ),
147
152
responseColor ("received responses." ),
148
153
)
149
154
@@ -159,6 +164,11 @@ func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
159
164
160
165
if protocolMessage , err := decodeMessage ([]byte (asString (msg .Params ["message" ]))); err == nil {
161
166
targetRequests [protocolMessage .Id ] = protocolMessage
167
+
168
+ if * flagShowRequests {
169
+ logger .Printf (requestFormat , targetColor ("%s" , msg .Params ["targetId" ]), methodColor (protocolMessage .Method ), requestColor ("%s" , serialize (protocolMessage )))
170
+ }
171
+
162
172
} else {
163
173
logger .Printf (protocolColor ("Could not deserialize message: %+v" , err ))
164
174
}
@@ -175,9 +185,9 @@ func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
175
185
delete (targetRequests , protocolMessage .Id )
176
186
177
187
if protocolMessage .IsError () {
178
- logger .Printf (requestReplyFormat , targetColor ("%s" , msg .Params ["targetId" ]), methodColor (request .Method ), requestColor (serialize (request .Params )), errorColor (serialize (protocolMessage .Error )))
188
+ logger .Printf (requestReplyFormat , targetColor ("%s" , msg .Params ["targetId" ]), methodColor (request .Method ), requestReplyColor (serialize (request .Params )), errorColor (serialize (protocolMessage .Error )))
179
189
} else {
180
- logger .Printf (requestReplyFormat , targetColor ("%s" , msg .Params ["targetId" ]), methodColor (request .Method ), requestColor (serialize (request .Params )), responseColor (serialize (protocolMessage .Result )))
190
+ logger .Printf (requestReplyFormat , targetColor ("%s" , msg .Params ["targetId" ]), methodColor (request .Method ), requestReplyColor (serialize (request .Params )), responseColor (serialize (protocolMessage .Result )))
181
191
}
182
192
} else {
183
193
logger .Printf (protocolColor ("Could not find target request with id: %d" , protocolMessage .Id ))
@@ -191,6 +201,10 @@ func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
191
201
} else {
192
202
if msg .IsRequest () {
193
203
requests [msg .Id ] = msg
204
+
205
+ if * flagShowRequests {
206
+ logger .Printf (requestFormat , targetColor (protocolTargetId ), methodColor (msg .Method ), requestColor (serialize (msg .Params )))
207
+ }
194
208
}
195
209
196
210
if msg .IsResponse () {
@@ -199,9 +213,9 @@ func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
199
213
200
214
if request != nil {
201
215
if msg .IsError () {
202
- logger .Printf (requestReplyFormat , targetColor (protocolTargetId ), methodColor (request .Method ), requestColor (serialize (request .Params )), errorColor (serialize (msg .Error )))
216
+ logger .Printf (requestReplyFormat , targetColor (protocolTargetId ), methodColor (request .Method ), requestReplyColor (serialize (request .Params )), errorColor (serialize (msg .Error )))
203
217
} else {
204
- logger .Printf (requestReplyFormat , targetColor (protocolTargetId ), methodColor (request .Method ), requestColor (serialize (request .Params )), responseColor (serialize (msg .Result )))
218
+ logger .Printf (requestReplyFormat , targetColor (protocolTargetId ), methodColor (request .Method ), requestReplyColor (serialize (request .Params )), responseColor (serialize (msg .Result )))
205
219
}
206
220
}
207
221
} else {
@@ -250,7 +264,7 @@ func proxyWS(ctxt context.Context, stream chan *protocolMessage, in, out *websoc
250
264
251
265
func checkVersion () (map [string ]string , error ) {
252
266
cl := & http.Client {}
253
- req , err := http .NewRequest ("GET" , "http://" + * flagRemote + "/json/version" , nil )
267
+ req , err := http .NewRequest ("GET" , "http://" + * flagRemote + "/json/version" , nil )
254
268
if err != nil {
255
269
return nil , err
256
270
}
0 commit comments