-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.html
365 lines (342 loc) · 15.5 KB
/
websocket.html
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
<!DOCTYPE html>
<!-- started from yaws/websockets_example.yaws -->
<html>
<head>
<title>Path MTU Discovery Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta charset="UTF-8">
<link rel="icon" href="data:,">
<style type='text/css'>
div.hidden { display: none; }
tr.hidden { display: none; }
table, th, td { border: 1px solid; border-collapse: collapse; }
#msgs { margin-left: 25px; margin-right: 25px; background-color: rgb(211, 211, 211); max-width: 500px;}
</style>
</head>
<body>
<h1>Path MTU Discovery Test</h1>
<div id="connect" class='hidden'>
<p>Press start to get started. Test results will be collected.
<input id='cA' class='button' type='submit' name='connect' value='start'/>
</p>
</div>
<noscript><p>To run the test, you need a browser with JavaScript, WebSockets, and the Fetch API</p></noscript>
<div id="results" class='hidden'><h2>Results</h2><table>
<tr><th>Direction</th><th>Tested Maximum Size Segment<th>Client Sent MSS<th>Notes</th>
<tr id="s2c" class='hidden'><td>Server to Client<td id="s2c_mss"> <td id="s2c_init_mss"> <td id="s2c_notes">
<tr id="c2s" class='hidden'><td>Client to Server<td id="c2s_mss"> <td id="c2s_init_mss">(n/a)<td id="c2s_notes">
</table></div>
<div id="msgs"></div>
<h2>What is this?
<p>This test sends packets of varying sizes from the server to your
browser, and then from your browser to the server in order to check if
Path MTU Discovery (PMTUD) is working properly between your browser and this
server; which is hopefully indicative of your experience with PMTUD in
general.</p>
<h2>How does it work?</h2>
<p>The first stage of the test sends varying size data packets from the
server to the browser, up to the Maxium Segment Size (MSS) sent by the
browser in the initial TCP SYN. There are three expected outcomes:
<ul>
<li>The MSS is correct, the browser can receive packets of the indicated
MSS; this server is limited to a MSS of 1460 (MTU 1500). In this case,
you should have no trouble receiving large data packets in general.
<li>The MSS is not correct, but the network where there MSS is
restricted does properly send ICMP Fragmentation Needed reports about
packets that are too large. In this case, you should not have trouble
receiving large data packets from servers (or peers) with properly
configured ICMP handling, but you may have trouble with servers which
don't have working ICMP.
<li>The MSS is not correct, and ICMP Fragmentation Needed reports were
not received. In this case, you likely have trouble receiving large data
packets from many servers. However, some popular servers intentionally
use a lower MSS, and some others use probing techniques: probing may be
immediate or after a delay.
</uL>
<p>The second stage is the opposite; the browser sends varying size of
data packets to the server; packet control is less precise in this
direction, and browsers do not expose the MSS in the TCP SYN-ACK received
from the server. Again, there are three exepcted outcomes:
<ul>
<li>The browser is able to send data of any size without delay. Either
the browser received a correct MSS, or the browser does immediate
probing. Measuring the sizes of incoming packets on the server could
distinguish between these two cases, but that's not possible with normal
sockets. In either case, you should have no trouble sending large data
packets.
<li>The browser is able to send data packets up to a certain size
without delay, but larger streams are delayed until path probing begins
and the data is sent in sizes that are able to transit the network. In
this case, you may notice some connections stall initially, but then
work fine. Repeated connections to the same server may work better, if
the server also sends large packets, receives ICMP Fragmentation Needed,
and runs a host cache that tracks that information; in that case, the
server will send a lower MSS in the SYN-ACK on connections that match
the host cache, and that will likely result in the browser sending
properly sized packets. This (FreeBSD) server has sysctl
net.inet.tcp.hostcache.enable=0 to disable the host cache, as it interferes with
probing.
<li>The browser is able to send data packets up to a certain size
without delay, but larger streams are not received. Servers that
intentionally send a lower MSS will work fine, as long as the chosen MSS
is low enough; servers cannot initiate probing for this case. Often,
this case is visible through packet analysis on the server; smaller
packets from the client may come in fine, but MSS sized gaps often form
and are not filled in; reducing the MSS to affected networks could be
used to mitigate.
</ul>
<p>If the test completes, the summary data is sent to the server for
possible future analysis. If the test doesn't complete, please <a
href="mailto:[email protected]">email me</a></p>
<h2>Why isn't this HTTPS?</h2>
<p>Precise packet size control is required, and that's harder, but not impossible, to do with TLS encapsulation. Maybe later.</p>
<h3>Contact</h3>
<p><a href="mailto:[email protected]">email</a> welcome</p>
<p><a href="https://github.com/russor/pmtud_test">source code</a>
<script type='application/javascript'>
var WS = false;
if (window.WebSocket) WS = WebSocket;
if (!WS && window.MozWebSocket) WS = MozWebSocket;
if (!WS) {
add_log("WebSockets are not supported by this browser, the server to client packet size testing uses WebSockets.");
}
if (!fetch) {
add_log("The Fetch API is not supported by this browser, the client to server packet size testing uses the Fetch API.");
}
if (!AbortSignal || !AbortSignal.timeout) {
add_log("The AbortSignal API is not supported by this browser, timed out requests might not be canceled.");
}
// Get an Element
function $() { return document.getElementById(arguments[0]); }
// Get the value of an Element
function $F() { return document.getElementById(arguments[0]).value; }
function add_log(text) {
var msg=$('msgs');
var spanText = document.createElement('span');
spanText.className='text';
spanText.innerHTML=text;
var lineBreak = document.createElement('br');
msg.appendChild(spanText);
msg.appendChild(lineBreak);
//msg.scrollTop = msg.scrollHeight - msg.clientHeight;
}
var init_mss = 0;
var in_min = 68 - 40;
var in_max = 0;
var in_done = 0;
var later_mss = 0;
var out_done = 0;
var out_min = 0;
var out_max = 9000;
var probe_size = 0;
var packet_offset = 0;
var slug_size = 0;
var late_response = 0;
var late_responses = 0;
var timeouts = 0;
function timeout() { return client._timeout(); }
function next_size(min, max) {
if (max == 0) { return 576 - 40; }
if (min == max) { return min;}
var candidate = Math.max(min + 1, min + Math.trunc((max - min) / 2));
// speed up probing by jumping to probable guesses
if (!in_done) {
if (min < init_mss && max >= init_mss) {
candidate = init_mss;
}
} else {
if (min < in_max && candidate > in_max) {
candidate = in_max + 1;
} else if (min < in_max && max <= in_max) {
candidate = in_max;
} else if (min > in_max && max == 9000) {
candidate = 9000;
}
}
return candidate;
}
var client = {
connect: function(){
this.start = Date.now();
this.rtt = 0;
this._ws=new WS("ws://" + window.location.host + "/ws");
this._ws.binaryType = "arraybuffer";
this._ws.onopen=this._onopen;
this._ws.onmessage=this._onmessage;
this._ws.onclose=this._onclose;
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = undefined;
//$('msgs').innerHTML='';
},
_onopen: function(){
client.rtt = Date.now() - client.start;
$('connect').className='hidden';
client.probe_req();
},
probe_req: function() {
var buf = new ArrayBuffer(3);
var view = new DataView(buf);
probe_size = next_size(in_min, in_max);
view.setInt8(0, 1);
view.setInt16(1, probe_size);
client._send(buf);
this.timeout = setTimeout(timeout, client.rtt * 10);
},
send_probe: function() {
probe_size = next_size(out_min, out_max);
slug_size = probe_size - packet_offset;
if (packet_offset == 0 || slug_size < 0) {
slug_size = 0;
}
late_response = 0;
this.timeout = setTimeout(timeout, client.rtt * 10);
var signal;
if (AbortSignal && AbortSignal.timeout) {
signal = AbortSignal.timeout(client.rtt * 50);
}
fetch("/size?" + "X".repeat(slug_size), {signal: signal, method: "GET"})
.then((response) => {
if (response.ok) {
response.json().then((data) => {
if (late_response) {
++late_responses;
var mss = data.size;
add_log("got late out probe for mss " + mss + " target " + probe_size);
packet_offset = mss - slug_size;
client.send_probe();
} else {
clearTimeout(client.timeout);
var mss = data.size;
packet_offset = mss - slug_size;
add_log("got out probe for mss " + mss);
out_min = mss;
if (out_min >= out_max) {
add_log("finished out probing, maximum mss " + mss);
if (out_max == 9000) {
$('c2s_mss').textContent = "unlimited";
$('c2s_notes').textContent = "OK";
} else if (late_responses == timeouts) {
$('c2s_mss').textContent = mss;
$('c2s_notes').textContent = "not ideal, larger packets delayed";
} else {
$('c2s_mss').textContent = mss;
$('c2s_notes').textContent = "broken, larger packets dropped";
}
fetch("/size?in_mss=" + in_min + "&init_mss=" + init_mss + "&later_mss=" + later_mss +
"&out_mss=" + out_min + "&late_responses=" + late_responses + "&timeouts=" + timeouts);
} else {
client.send_probe();
}
}
});
} else {
add_log("response not ok?");
console.log(response);
}
}).catch((err) => {
client.send_probe();
});
},
_timeout: function() {
if (in_done != 2) {
add_log("in probe timed out " + probe_size + " min " + in_min + " max " + in_max);
in_max = probe_size - 1;
if (in_max >= in_min) {
this._ws.close();
client.connect();
} else {
add_log("can't continue probing");
}
} else {
add_log("out probe timed out " + probe_size + " min " + out_min + " max " + out_max);
late_response = 1;
++timeouts;
out_max = probe_size - 1;
}
},
_send: function(message){
if (this._ws)
this._ws.send(message);
},
chat: function(text) {
if (text != null && text.length>0 )
client._send(text);
},
_onmessage: function(m) {
if (m.data instanceof ArrayBuffer) {
if (m.data.byteLength > 0) {
clearTimeout(client.timeout);
var view = new DataView(m.data);
if (view.getInt8(0) == 1) {
//packet_offset = view.getInt8(1);
var maxseg = view.getInt16(2);
var mss = view.getInt16(4);
add_log("got in probe for mss " + mss + " (max seg " + maxseg + ")");
if (mss > maxseg) { mss = maxseg; }
if (init_mss == 0) {
init_mss = maxseg;
later_mss = maxseg;
}
if (maxseg < later_mss) {
later_mss = maxseg;
}
if (maxseg < in_max || in_max == 0) {
in_max = maxseg;
in_done = 0;
}
in_min = mss;
if (in_min == in_max) {
if (in_done) {
add_log("finished in probing, maximum mss " + mss + " peer mss " + maxseg + " initial peer mss " + init_mss);
if (init_mss == maxseg && maxseg == mss) {
$('s2c_mss').textContent = mss;
$('s2c_notes').textContent = "OK";
} else if (mss == maxseg) {
$('s2c_mss').textContent = mss;
$('s2c_notes').textContent = "fragile, relies on ICMP or server help";
} else {
$('s2c_mss').textContent = mss;
$('s2c_notes').textContent = "broken, ICMPs not received, requires server help";
}
$('s2c_init_mss').textContent = init_mss;
$('results').className='';
$('c2s_mss').textContent = 'PENDING ...';
$('s2c').className='';
$('c2s').className='';
in_done = 2;
client.send_probe();
client._ws.close();
} else if (!in_done) {
in_done = 1;
client.probe_req();
}
} else {
client.probe_req();
}
} else {
console.log(m);
}
}
} if (typeof(m.data) === "string"){
var text = m.data;
var msg=$('msgs');
var spanText = document.createElement('span');
spanText.className='text';
spanText.innerHTML=text;
var lineBreak = document.createElement('br');
msg.appendChild(spanText);
msg.appendChild(lineBreak);
msg.scrollTop = msg.scrollHeight - msg.clientHeight;
}
},
_onclose: function(m) {
this._ws=null;
}
};
$('connect').className='';
$('cA').onclick = function(event) { client.connect(); return false; };
</script>
</body>
</html>