wormhole/lib/Socket.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

const EventEmitter = require('events');
const constants = require("./constants");
2021-02-02 10:17:49 +02:00
const parser = require('./Parser');
class Socket extends EventEmitter {
2021-01-26 12:04:53 +02:00
constructor({ initialState='CONNECTING', socket }) {
super();
2021-01-26 12:04:53 +02:00
this._state = initialState;
this._socket = socket;
this._accepted = false;
this._fateDecided = false; // Wether the decision to accept or reject the socket was made
2021-01-26 12:04:53 +02:00
2021-02-04 16:01:14 +02:00
this._buffering = undefined;
2021-01-26 12:04:53 +02:00
this._socket.on('data', (e) => {
if (!this._isConnected()) return;
const packet = this._decodePayload(e.buffer);
2021-02-04 16:01:14 +02:00
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);
}
2021-01-26 12:04:53 +02:00
});
}
}
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();
frame.FIN = 1;
frame.RSVx = 0;
frame.Opcode = 0x01;
frame.MASK = 0;
const length = buffer.byteLength;
if (length > 125) {
frame.PayloadLen = 126;
} else if (length >= 65536) {
frame.PayloadLen = 127;
} else if (length <= 125) {
frame.PayloadLen = length;
}
frame.PayloadLenEx = length;
frame.PayloadData = buffer;
this._socket.write(frame.toBuffer());
};
2021-02-02 10:17:49 +02:00
Socket.prototype._decodePayload = function(payload) {
return parser.parseWebsocketFrame(new DataView(payload));
2021-02-02 10:17:49 +02:00
};
Socket.prototype._setConnectionState = function(state) {
this._state = state;
};
Socket.prototype._setAccepted = function(state) {
if (this._fateDecided) throw new Error('Tried to decide fate (wether socket is accepted or not) more than 1 time');
this._fateDecided = true;
this._accepted = state;
};
Socket.prototype._isConnected = function() {
return (this._fateDecided && this._isConnected && this._accepted === true);
}
2021-01-26 12:04:53 +02:00
module.exports = Socket;