node-minecraft-protocol/src/client.js

162 lines
5.3 KiB
JavaScript
Raw Normal View History

var EventEmitter = require('events').EventEmitter
2013-01-04 01:45:57 -05:00
, util = require('util')
, debug = require('./debug')
, serializer = require('./transforms/serializer')
, compression = require('./transforms/compression')
, framing = require('./transforms/framing')
, crypto = require('crypto')
, states = serializer.states
;
var version = require('./version');
2015-08-27 07:36:07 -04:00
var packets = require('minecraft-data')(version.majorVersion).protocol.states;
var readPackets = require("./packets").readPackets;
var packetIndexes = readPackets(packets, states);
2013-01-04 01:45:57 -05:00
module.exports = Client;
2013-01-07 23:36:14 -05:00
function Client(isServer) {
2013-01-04 01:45:57 -05:00
EventEmitter.call(this);
var socket;
2015-05-22 18:25:12 -04:00
this.packetsToParse = {};
this.serializer = serializer.createSerializer({ isServer });
this.compressor = null;
this.framer = framing.createFramer();
this.cipher = null;
this.decipher = null;
this.splitter = framing.createSplitter();
this.decompressor = null;
2015-05-22 18:25:12 -04:00
this.deserializer = serializer.createDeserializer({ isServer, packetsToParse: this.packetsToParse });
this._state = states.HANDSHAKING;
Object.defineProperty(this, "state", {
get: function() {
return this.serializer.protocolState;
},
set: function(newProperty) {
var oldProperty = this.serializer.protocolState;
this.serializer.protocolState = newProperty;
this.deserializer.protocolState = newProperty;
this.emit('state', newProperty, oldProperty);
}
});
Object.defineProperty(this, "compressionThreshold", {
get: () => this.compressor == null ? -2 : this.compressor.compressionThreshold,
set: (threshold) => this.setCompressionThreshold(threshold)
});
2013-01-07 23:36:14 -05:00
this.isServer = !!isServer;
2014-04-10 19:48:06 -04:00
this.on('newListener', function(event, listener) {
var direction = this.isServer ? 'toServer' : 'toClient';
if(packetIndexes.packetStates[direction].hasOwnProperty(event) || event === "packet") {
if(typeof this.packetsToParse[event] === "undefined") this.packetsToParse[event] = 1;
2014-04-10 19:48:06 -04:00
else this.packetsToParse[event] += 1;
}
});
this.on('removeListener', function(event, listener) {
var direction = this.isServer ? 'toServer' : 'toClient';
if(packetIndexes.packetStates[direction].hasOwnProperty(event) || event === "packet") {
2014-04-10 19:48:06 -04:00
this.packetsToParse[event] -= 1;
}
});
2013-01-04 01:45:57 -05:00
}
2014-04-10 19:48:06 -04:00
2013-01-04 01:45:57 -05:00
util.inherits(Client, EventEmitter);
Client.prototype.setSocket = function(socket) {
var ended = false;
// TODO : A lot of other things needs to be done.
var endSocket = () => {
if(ended) return;
ended = true;
this.socket.removeListener('close', endSocket);
this.socket.removeListener('end', endSocket);
this.socket.removeListener('timeout', endSocket);
this.emit('end', this._endReason);
};
2015-05-22 19:43:32 -04:00
var onFatalError = (err) => {
this.emit('error', err);
endSocket();
};
2015-05-22 19:43:32 -04:00
var onError = (err) => this.emit('error', err);
this.socket = socket;
2013-01-04 01:45:57 -05:00
if(this.socket.setNoDelay)
this.socket.setNoDelay(true);
this.socket.on('connect', () => this.emit('connect'));
2015-05-22 19:43:32 -04:00
this.socket.on('error', onFatalError);
this.socket.on('close', endSocket);
this.socket.on('end', endSocket);
this.socket.on('timeout', endSocket);
2015-05-22 19:43:32 -04:00
this.serializer.on('error', onError);
this.deserializer.on('error', onError);
this.framer.on('error', onError);
this.splitter.on('error', onError);
this.socket.pipe(this.splitter).pipe(this.deserializer);
this.serializer.pipe(this.framer).pipe(this.socket);
this.deserializer.on('data', (parsed) => {
this.emit('packet', parsed.data, parsed.metadata);
this.emit(parsed.metadata.name, parsed.data, parsed.metadata);
this.emit('raw.' + parsed.metadata.name, parsed.buffer, parsed.metadata);
this.emit('raw', parsed.buffer, parsed.metadata);
});
2013-01-04 01:45:57 -05:00
};
Client.prototype.end = function(reason) {
this._endReason = reason;
2013-01-04 01:45:57 -05:00
this.socket.end();
};
Client.prototype.setEncryption = function(sharedSecret) {
if (this.cipher != null)
throw new Error("Set encryption twice !");
this.cipher = crypto.createCipheriv('aes-128-cfb8', sharedSecret, sharedSecret);
2015-05-22 19:43:32 -04:00
this.cipher.on('error', (err) => this.emit('error', err));
this.framer.unpipe(this.socket);
this.framer.pipe(this.cipher).pipe(this.socket);
this.decipher = crypto.createDecipheriv('aes-128-cfb8', sharedSecret, sharedSecret);
2015-05-22 19:43:32 -04:00
this.decipher.on('error', (err) => this.emit('error', err));
this.socket.unpipe(this.splitter);
this.socket.pipe(this.decipher).pipe(this.splitter);
}
Client.prototype.setCompressionThreshold = function(threshold) {
if (this.compressor == null) {
this.compressor = compression.createCompressor(threshold);
2015-05-22 19:43:32 -04:00
this.compressor.on('error', (err) => this.emit('error', err));
this.serializer.unpipe(this.framer);
this.serializer.pipe(this.compressor).pipe(this.framer);
this.decompressor = compression.createDecompressor(threshold);
2015-05-22 19:43:32 -04:00
this.decompressor.on('error', (err) => this.emit('error', err));
this.splitter.unpipe(this.deserializer);
this.splitter.pipe(this.decompressor).pipe(this.deserializer);
} else {
this.decompressor.threshold = threshold;
this.compressor.threshold = threshold;
}
}
Client.prototype.write = function(packetName, params) {
debug("writing packet " + this.state + "." + packetName);
debug(params);
this.serializer.write({ packetName, params });
2013-01-04 01:45:57 -05:00
};
2014-04-10 19:48:06 -04:00
Client.prototype.writeRaw = function(buffer) {
2015-05-22 15:22:21 -04:00
if (this.compressor === null)
this.framer.write(buffer);
else
this.compressor.write(buffer);
};