packet fragmentation that doesnt work
This commit is contained in:
parent
04e577acc8
commit
e13b10fedf
5 changed files with 42 additions and 14 deletions
|
@ -6,12 +6,17 @@
|
||||||
<title>Wormhole test</title>
|
<title>Wormhole test</title>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
let theFunniestStringEver = '';
|
||||||
|
for (let i = 0; i < 1000000; i++) {
|
||||||
|
theFunniestStringEver += 'h';
|
||||||
|
}
|
||||||
|
|
||||||
// Create WebSocket connection.
|
// Create WebSocket connection.
|
||||||
const socket = new WebSocket('ws://localhost:8080/hello');
|
const socket = new WebSocket('ws://localhost:8080/hello');
|
||||||
|
|
||||||
// Connection opened
|
// Connection opened
|
||||||
socket.addEventListener('open', function (event) {
|
socket.addEventListener('open', function (event) {
|
||||||
socket.send('Hello Server!');
|
socket.send(theFunniestStringEver);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.addEventListener('error', console.error)
|
socket.addEventListener('error', console.error)
|
||||||
|
|
|
@ -9,9 +9,13 @@ const wormhole = new Wormhole({ urls: [ '/hello' ], httpServer });
|
||||||
wormhole.on('connect', ({ socket, accept, reject }) => {
|
wormhole.on('connect', ({ socket, accept, reject }) => {
|
||||||
accept();
|
accept();
|
||||||
const buf = Buffer.from([0x41, 0x41, 0x41, 0x41]);
|
const buf = Buffer.from([0x41, 0x41, 0x41, 0x41]);
|
||||||
console.log(buf);
|
|
||||||
socket.send(buf);
|
socket.send(buf);
|
||||||
socket.on('text', console.log);
|
socket.on('text', (e) => {
|
||||||
|
//console.log('Got text frame', e);
|
||||||
|
});
|
||||||
|
socket.on('binary', () => {
|
||||||
|
//console.log('Got binary frame', e);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
httpServer.listen(8080);
|
httpServer.listen(8080);
|
|
@ -46,8 +46,6 @@ WebsocketFrame.prototype.toBuffer = function() {
|
||||||
this.MASK && packet.push(this.MaskingKey);
|
this.MASK && packet.push(this.MaskingKey);
|
||||||
packet.push(this.PayloadData);
|
packet.push(this.PayloadData);
|
||||||
|
|
||||||
console.log(packet);
|
|
||||||
|
|
||||||
return Buffer.concat(packet);
|
return Buffer.concat(packet);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -58,13 +56,13 @@ const parseWebsocketFrame = (data) => {
|
||||||
const frame = new WebsocketFrame();
|
const frame = new WebsocketFrame();
|
||||||
|
|
||||||
// 1 byte - first byte
|
// 1 byte - first byte
|
||||||
frame.FIN = (firstByte & 0x01); // 0x01[0b10000000] - Get most significant bit (FIN)
|
frame.FIN = (firstByte >> 7); // >> 7 - Get most significant bit (FIN)
|
||||||
frame.RSVx = (firstByte & 0x70) // 0x70[0b01110000] - Get the 3 bits after the most significant bit (RSVx)
|
frame.RSVx = (firstByte & 0x70) >> 4; // 0x70[0b01110000] - Get the 3 bits after the most significant bit (RSVx)
|
||||||
frame.Opcode = (firstByte & 0x0F) // 0xF[0b00001111] - Get the last 4 bits (Opcode)
|
frame.Opcode = (firstByte & 0x0F); // 0xF[0b00001111] - Get the last 4 bits (Opcode)
|
||||||
|
|
||||||
// 1 byte - second byte
|
// 1 byte - second byte
|
||||||
frame.MASK = (secondByte & 0x01) // 0x01[0b10000000] - Get most significant bit (MASK)
|
frame.MASK = (secondByte >> 7); // >> 7 - Get most significant bit (MASK)
|
||||||
frame.PayloadLen = (secondByte & 0x7F) // 0x7F[0b01111111] - Get last 7 bits (Payload len)
|
frame.PayloadLen = (secondByte & 0x7F); // 0x7F[0b01111111] - Get last 7 bits (Payload len)
|
||||||
|
|
||||||
let maskingKeyOffset = 2; // By default, theres a 2 byte offset. We will modify this in the cases below.
|
let maskingKeyOffset = 2; // By default, theres a 2 byte offset. We will modify this in the cases below.
|
||||||
|
|
||||||
|
@ -79,11 +77,11 @@ const parseWebsocketFrame = (data) => {
|
||||||
frame.PayloadLenEx = frame.PayloadLen;
|
frame.PayloadLenEx = frame.PayloadLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
const maskingKeyEnd = maskingKeyOffset + 4; // (4 bytes because it is a 32 bit value)
|
let maskingKeyEnd = frame.MASK ? maskingKeyOffset + 4 : maskingKeyOffset; // (4 bytes because it is a 32 bit value)
|
||||||
if (frame.MASK) frame.MaskingKey = data.buffer.slice(maskingKeyOffset, maskingKeyEnd); // Create a new buffer starting at the masking key offset and ending at the masking key end (duh).
|
if (frame.MASK) frame.MaskingKey = data.buffer.slice(maskingKeyOffset, maskingKeyEnd); // Create a new buffer starting at the masking key offset and ending at the masking key end (duh).
|
||||||
|
|
||||||
// TODO: Separate extension data from application data
|
// TODO: Separate extension data from application data
|
||||||
frame.PayloadData = data.buffer.slice(maskingKeyEnd, maskingKeyEnd + frame.PayloadLenEx); // Create a new buffer that starts at the end of the masking key and ends at (the end of the masking key + payload length)
|
frame.PayloadData = data.buffer.slice(maskingKeyEnd, Number(BigInt(maskingKeyEnd) + BigInt(frame.PayloadLenEx))); // Create a new buffer that starts at the end of the masking key and ends at (the end of the masking key + payload length)
|
||||||
|
|
||||||
// TODO: implement unmasked string decoding
|
// TODO: implement unmasked string decoding
|
||||||
if (frame.MASK) {
|
if (frame.MASK) {
|
||||||
|
|
|
@ -12,10 +12,31 @@ class Socket extends EventEmitter {
|
||||||
this._accepted = false;
|
this._accepted = false;
|
||||||
this._fateDecided = false; // Wether the decision to accept or reject the socket was made
|
this._fateDecided = false; // Wether the decision to accept or reject the socket was made
|
||||||
|
|
||||||
|
this._buffering = undefined;
|
||||||
|
|
||||||
this._socket.on('data', (e) => {
|
this._socket.on('data', (e) => {
|
||||||
if (!this._isConnected()) return;
|
if (!this._isConnected()) return;
|
||||||
|
|
||||||
const packet = this._decodePayload(e.buffer);
|
const packet = this._decodePayload(e.buffer);
|
||||||
|
|
||||||
|
console.log(packet);
|
||||||
|
|
||||||
|
if (this._buffering && packet.FIN === 1) {
|
||||||
|
this.emit('binary', Buffer.concat([this._buffering, packet.PayloadData]));
|
||||||
|
this._buffering = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packet.FIN === 0) {
|
||||||
|
if (!this._buffering) {
|
||||||
|
this._buffering = Buffer.from(packet.PayloadData);
|
||||||
|
} else {
|
||||||
|
Buffer.concat([this._buffering, Buffer.from(packet.PayloadData)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Buffering', packet);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (packet.Opcode === constants.opcodes.TEXT_FRAME) {
|
if (packet.Opcode === constants.opcodes.TEXT_FRAME) {
|
||||||
this.emit('text', packet);
|
this.emit('text', packet);
|
||||||
} else if (packet.Opcode === constants.opcodes.BINARY_FRAME) {
|
} else if (packet.Opcode === constants.opcodes.BINARY_FRAME) {
|
||||||
|
@ -43,7 +64,6 @@ Socket.prototype._sendBuffer = function(buffer) {
|
||||||
|
|
||||||
frame.MASK = 0;
|
frame.MASK = 0;
|
||||||
const length = buffer.byteLength;
|
const length = buffer.byteLength;
|
||||||
console.log(length);
|
|
||||||
if (length > 125) {
|
if (length > 125) {
|
||||||
frame.PayloadLen = 126;
|
frame.PayloadLen = 126;
|
||||||
} else if (length >= 65536) {
|
} else if (length >= 65536) {
|
||||||
|
|
|
@ -9,7 +9,8 @@ module.exports = {
|
||||||
CLOSED: 'CLOSED'
|
CLOSED: 'CLOSED'
|
||||||
},
|
},
|
||||||
opcodes: {
|
opcodes: {
|
||||||
|
'CONTINUATION': 0x00,
|
||||||
'TEXT_FRAME': 0x01,
|
'TEXT_FRAME': 0x01,
|
||||||
'BINARY_FRAME': 0x02
|
'BINARY_FRAME': 0x02
|
||||||
}
|
}
|
||||||
}
|
};
|
Loading…
Reference in a new issue