Compare commits

..

2 commits

Author SHA1 Message Date
hippoz
e13b10fedf
packet fragmentation that doesnt work 2021-02-04 16:01:14 +02:00
hippoz
04e577acc8
add events and make sending packets easier 2021-02-03 19:55:41 +02:00
5 changed files with 79 additions and 18 deletions

View file

@ -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)

View file

@ -8,7 +8,14 @@ const wormhole = new Wormhole({ urls: [ '/hello' ], httpServer });
wormhole.on('connect', ({ socket, accept, reject }) => { wormhole.on('connect', ({ socket, accept, reject }) => {
accept(); accept();
socket.sendText('hello'); const buf = Buffer.from([0x41, 0x41, 0x41, 0x41]);
socket.send(buf);
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);

View file

@ -56,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.
@ -77,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) {

View file

@ -1,29 +1,69 @@
const EventEmitter = require('events');
const constants = require("./constants"); const constants = require("./constants");
const parser = require('./Parser'); const parser = require('./Parser');
class Socket { class Socket extends EventEmitter {
constructor({ initialState='CONNECTING', socket }) { constructor({ initialState='CONNECTING', socket }) {
super();
this._state = initialState; this._state = initialState;
this._socket = socket; this._socket = socket;
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._socket.on('data', (e) => { this._buffering = undefined;
if (this._state !== constants.states.OPEN) return;
this._decodePayload(e.buffer); this._socket.on('data', (e) => {
if (!this._isConnected()) return;
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) {
this.emit('text', packet);
} else if (packet.Opcode === constants.opcodes.BINARY_FRAME) {
this.emit('binary', packet);
}
}); });
} }
} }
Socket.prototype.sendText = function(text) { Socket.prototype.send = function(payload) {
if (typeof payload === 'string') {
this._sendBuffer(Buffer.from(payload));
} else if (payload instanceof Buffer) {
this._sendBuffer(payload);
} else {
throw new Error(`Unsupported type ${typeof payload}, supported types are: string, Buffer`);
}
};
Socket.prototype._sendBuffer = function(buffer) {
const frame = new parser.WebsocketFrame(); const frame = new parser.WebsocketFrame();
frame.FIN = 1; frame.FIN = 1;
frame.RSVx = 0; frame.RSVx = 0;
frame.Opcode = 0x01; frame.Opcode = 0x01;
frame.MASK = 0; frame.MASK = 0;
const length = text.length; const length = buffer.byteLength;
if (length > 125) { if (length > 125) {
frame.PayloadLen = 126; frame.PayloadLen = 126;
} else if (length >= 65536) { } else if (length >= 65536) {
@ -32,13 +72,13 @@ Socket.prototype.sendText = function(text) {
frame.PayloadLen = length; frame.PayloadLen = length;
} }
frame.PayloadLenEx = length; frame.PayloadLenEx = length;
frame.PayloadData = Buffer.from(text); frame.PayloadData = buffer;
this._socket.write(frame.toBuffer()); this._socket.write(frame.toBuffer());
}; };
Socket.prototype._decodePayload = function(payload) { Socket.prototype._decodePayload = function(payload) {
console.log(parser.parseWebsocketFrame(new DataView(payload))); return parser.parseWebsocketFrame(new DataView(payload));
}; };
Socket.prototype._setConnectionState = function(state) { Socket.prototype._setConnectionState = function(state) {
@ -52,4 +92,8 @@ Socket.prototype._setAccepted = function(state) {
this._accepted = state; this._accepted = state;
}; };
Socket.prototype._isConnected = function() {
return (this._fateDecided && this._isConnected && this._accepted === true);
}
module.exports = Socket; module.exports = Socket;

View file

@ -7,5 +7,10 @@ module.exports = {
OPEN: 'OPEN', OPEN: 'OPEN',
CLOSING: 'CLOSING', CLOSING: 'CLOSING',
CLOSED: 'CLOSED' CLOSED: 'CLOSED'
},
opcodes: {
'CONTINUATION': 0x00,
'TEXT_FRAME': 0x01,
'BINARY_FRAME': 0x02
} }
} };