使用chrome截取WebSocket数据包

先找到一个使用websocket通讯的网站作为测试,比如:
http://www.websocket.org/echo.html

使用chrome的Chrome Developer tools

Chrome Developer tools直接可以在Network下面的WS标签中找到WebSocket的链接,直接点击即可查看。
Chrome Developer tools
可以看更多的详情:
chrome://net-internals/#events

使用fiddler

Fiddler则需要添加自定义的规则,操作如下
打开Fiddler -> Rules -> Customize Rules,会打开 FiddlerScript
然后编辑如下规则:

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
import System.Threading;
// ...
class Handlers
{
// ...
static function Main()
{
// ...
//
// Print Web Socket frame every 2 seconds
//
printSocketTimer =
new System.Threading.Timer(PrintSocketMessage, null, 0, 2000);
}
// Create a first-in, first-out queue
static var socketMessages = new System.Collections.Queue();
static var printSocketTimer = null;
static var requestBodyBuilder = new System.Text.StringBuilder();
static var requestUrlBuilder = new System.Text.StringBuilder();
static var requestPayloadIsJson = false;
static var requestPartCount = 0;
//
// Listen to WebSocketMessage event, and add the socket messages
// to the static queue.
//
static function OnWebSocketMessage(oMsg: WebSocketMessage)
{
Monitor.Enter(socketMessages);
socketMessages.Enqueue(oMsg);
Monitor.Exit(socketMessages);
}
//
// Take socket messages from the static queue, and generate fake
// HTTP requests that will be caught by Fiddler.
//
static function PrintSocketMessage(stateInfo: Object)
{
Monitor.Enter(socketMessages);
while (socketMessages.Count > 0)
{
var oMsg = socketMessages.Dequeue();
ExtractSocketMessage(oMsg);
}
Monitor.Exit(socketMessages);
}
//
// Build web socket message information in JSON format, and send this JSON
// information in a fake HTTP request that will be caught by Fiddler
//
// If a frame is split in multiple messages, following function will combine
// them into one
//
static function ExtractSocketMessage(oMsg: WebSocketMessage)
{
if (oMsg.FrameType != WebSocketFrameTypes.Continuation)
{
var messageID = String.Format(
"{0}.{1}", oMsg.IsOutbound ? "Client" : "Server", oMsg.ID);
var wsSession = GetWsSession(oMsg);
requestUrlBuilder.AppendFormat("{0}.{1}", wsSession, messageID);
requestBodyBuilder.Append("{");
requestBodyBuilder.AppendFormat("\"doneTime\": \"{0}\",",
oMsg.Timers.dtDoneRead.ToString("hh:mm:ss.fff"));
requestBodyBuilder.AppendFormat("\"messageType\": \"{0}\",",
oMsg.FrameType);
requestBodyBuilder.AppendFormat("\"messageID\": \"{0}\",", messageID);
requestBodyBuilder.AppendFormat("\"wsSession\": \"{0}\",", wsSession);
requestBodyBuilder.Append("\"payload\": ");
var payloadString = oMsg.PayloadAsString();
if (oMsg.FrameType == WebSocketFrameTypes.Binary)
{
payloadString = HexToString(payloadString);
}
if (payloadString.StartsWith("{"))
{
requestPayloadIsJson = true;
}
else
{
requestBodyBuilder.Append("\"");
}
requestBodyBuilder.AppendFormat("{0}", payloadString);
}
else
{
var payloadString = HexToString(oMsg.PayloadAsString());
requestBodyBuilder.AppendFormat("{0}", payloadString);
}
requestPartCount++;
if (oMsg.IsFinalFrame)
{
if (!requestPayloadIsJson)
{
requestBodyBuilder.Append("\"");
}
requestBodyBuilder.AppendFormat(", \"requestPartCount\": \"{0}\"",
requestPartCount);
requestBodyBuilder.Append("}");
SendRequest(requestUrlBuilder.ToString(), requestBodyBuilder.ToString());
requestBodyBuilder.Clear();
requestUrlBuilder.Clear();
requestPayloadIsJson = false;
requestPartCount = 0;
}
}
//
// Generate fake HTTP request with JSON data that will be caught by Fiddler
// We can inspect this request in "Inspectors" tab -> "JSON" sub-tab
//
static function SendRequest(urlPath: String, message: String)
{
var request = String.Format(
"POST http://fakewebsocket/{0} HTTP/1.1\n" +
"User-Agent: Fiddler\n" +
"Content-Type: application/json; charset=utf-8\n" +
"Host: fakewebsocket\n" +
"Content-Length: {1}\n\n{2}",
urlPath, message.Length, message);
FiddlerApplication.oProxy.SendRequest(request, null);
}
//
// Unfortunately, WebSocketMessage class does not have a member for
// Web Socket session number. Therefore, we are extracting session number
// from its string output.
//
static function GetWsSession(oMsg: WebSocketMessage)
{
var message = oMsg.ToString();
var index = message.IndexOf(".");
var wsSession = message.Substring(0, index);
return wsSession;
}
//
// Extract Hex to String.
// E.g., 7B-22-48-22-3A-22-54-72-61-6E to {"H":"TransportHub","M":"
//
static function HexToString(sourceHex: String)
{
sourceHex = sourceHex.Replace("-", "");
var sb = new System.Text.StringBuilder();
for (var i = 0; i < sourceHex.Length; i += 2)
{
var hs = sourceHex.Substring(i, 2);
sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));
}
var ascii = sb.ToString();
return ascii;
}
}

fiddler

参考

http://www.codeproject.com/Articles/718660/Debug-Inspect-WebSocket-traffic-with-Fiddler