Compare commits

..

No commits in common. "e13b10fedf0f7d126ad2c6199ff83f5ffe463c27" and "3c8434fb8ae17139ac94e65636708d15136391cf" have entirely different histories.

5 changed files with 17 additions and 78 deletions

View file

@ -6,17 +6,12 @@
<title>Wormhole test</title>
<script>
let theFunniestStringEver = '';
for (let i = 0; i < 1000000; i++) {
theFunniestStringEver += 'h';
}
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080/hello');
// Connection opened
socket.addEventListener('open', function (event) {
socket.send(theFunniestStringEver);
socket.send('Hello Server!');
});
socket.addEventListener('error', console.error)

View file

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

View file

@ -56,13 +56,13 @@ const parseWebsocketFrame = (data) => {
const frame = new WebsocketFrame();
// 1 byte - first byte
frame.FIN = (firstByte >> 7); // >> 7 - Get most significant bit (FIN)
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.FIN = (firstByte & 0x01); // 0x01[0b10000000] - Get most significant bit (FIN)
frame.RSVx = (firstByte & 0x70) // 0x70[0b01110000] - Get the 3 bits after the most significant bit (RSVx)
frame.Opcode = (firstByte & 0x0F) // 0xF[0b00001111] - Get the last 4 bits (Opcode)
// 1 byte - second byte
frame.MASK = (secondByte >> 7); // >> 7 - Get most significant bit (MASK)
frame.PayloadLen = (secondByte & 0x7F); // 0x7F[0b01111111] - Get last 7 bits (Payload len)
frame.MASK = (secondByte & 0x01) // 0x01[0b10000000] - Get most significant bit (MASK)
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.
@ -77,11 +77,11 @@ const parseWebsocketFrame = (data) => {
frame.PayloadLenEx = frame.PayloadLen;
}
let maskingKeyEnd = frame.MASK ? maskingKeyOffset + 4 : maskingKeyOffset; // (4 bytes because it is a 32 bit value)
const maskingKeyEnd = maskingKeyOffset + 4; // (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).
// TODO: Separate extension data from application data
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)
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)
// TODO: implement unmasked string decoding
if (frame.MASK) {

View file

@ -1,69 +1,29 @@
const EventEmitter = require('events');
const constants = require("./constants");
const parser = require('./Parser');
class Socket extends EventEmitter {
class Socket {
constructor({ initialState='CONNECTING', socket }) {
super();
this._state = initialState;
this._socket = socket;
this._accepted = false;
this._fateDecided = false; // Wether the decision to accept or reject the socket was made
this._buffering = undefined;
this._socket.on('data', (e) => {
if (!this._isConnected()) return;
if (this._state !== constants.states.OPEN) 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);
}
this._decodePayload(e.buffer);
});
}
}
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) {
Socket.prototype.sendText = function(text) {
const frame = new parser.WebsocketFrame();
frame.FIN = 1;
frame.RSVx = 0;
frame.Opcode = 0x01;
frame.MASK = 0;
const length = buffer.byteLength;
const length = text.length;
if (length > 125) {
frame.PayloadLen = 126;
} else if (length >= 65536) {
@ -72,13 +32,13 @@ Socket.prototype._sendBuffer = function(buffer) {
frame.PayloadLen = length;
}
frame.PayloadLenEx = length;
frame.PayloadData = buffer;
frame.PayloadData = Buffer.from(text);
this._socket.write(frame.toBuffer());
};
Socket.prototype._decodePayload = function(payload) {
return parser.parseWebsocketFrame(new DataView(payload));
console.log(parser.parseWebsocketFrame(new DataView(payload)));
};
Socket.prototype._setConnectionState = function(state) {
@ -92,8 +52,4 @@ Socket.prototype._setAccepted = function(state) {
this._accepted = state;
};
Socket.prototype._isConnected = function() {
return (this._fateDecided && this._isConnected && this._accepted === true);
}
module.exports = Socket;

View file

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