2013-01-04 01:45:57 -05:00
|
|
|
var net = require('net')
|
|
|
|
, EventEmitter = require('events').EventEmitter
|
|
|
|
, util = require('util')
|
|
|
|
, protocol = require('./protocol')
|
2013-04-13 16:23:31 -04:00
|
|
|
, dns = require('dns')
|
2013-01-04 01:45:57 -05:00
|
|
|
, createPacketBuffer = protocol.createPacketBuffer
|
|
|
|
, parsePacket = protocol.parsePacket
|
2013-12-30 10:05:22 -05:00
|
|
|
, states = protocol.states
|
2013-04-05 23:02:36 -04:00
|
|
|
, debug = protocol.debug
|
2013-11-19 18:06:57 -05:00
|
|
|
;
|
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);
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
this._state = states.HANDSHAKING;
|
|
|
|
Object.defineProperty(this, "state", {
|
|
|
|
get: function() {
|
|
|
|
return this._state;
|
|
|
|
},
|
|
|
|
set: function(newProperty) {
|
|
|
|
var oldProperty = this._state;
|
|
|
|
this._state = newProperty;
|
|
|
|
this.emit('state', newProperty, oldProperty);
|
|
|
|
}
|
|
|
|
});
|
2013-01-07 23:36:14 -05:00
|
|
|
this.isServer = !!isServer;
|
2013-01-04 01:45:57 -05:00
|
|
|
this.socket = null;
|
|
|
|
this.encryptionEnabled = false;
|
|
|
|
this.cipher = null;
|
|
|
|
this.decipher = null;
|
2014-04-10 19:48:06 -04:00
|
|
|
this.packetsToParse = {};
|
|
|
|
this.on('newListener', function(event, listener) {
|
|
|
|
var direction = this.isServer ? 'toServer' : 'toClient';
|
2014-04-10 20:08:17 -04:00
|
|
|
if (protocol.packetStates[direction].hasOwnProperty(event) || event === "packet") {
|
2014-04-10 19:48:06 -04:00
|
|
|
if (typeof this.packetsToParse[event] === "undefined") this.packetsToParse[event] = 1;
|
|
|
|
else this.packetsToParse[event] += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.on('removeListener', function(event, listener) {
|
|
|
|
var direction = this.isServer ? 'toServer' : 'toClient';
|
2014-04-10 20:08:17 -04:00
|
|
|
if (protocol.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);
|
|
|
|
|
2014-04-10 19:48:06 -04:00
|
|
|
// Transform weird "packet" types into string representing their type. Should be mostly retro-compatible
|
|
|
|
Client.prototype.on = function(type, func) {
|
|
|
|
var direction = this.isServer ? 'toServer' : 'toClient';
|
|
|
|
if (Array.isArray(type)) {
|
|
|
|
arguments[0] = protocol.packetNames[type[0]][direction][type[1]];
|
|
|
|
} else if (typeof type === "number") {
|
|
|
|
arguments[0] = protocol.packetNames[this.state][direction][type];
|
|
|
|
}
|
|
|
|
EventEmitter.prototype.on.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.onRaw = function(type, func) {
|
|
|
|
var arg = "raw.";
|
|
|
|
if (Array.isArray(type)) {
|
|
|
|
arg += protocol.packetNames[type[0]][direction][type[1]];
|
|
|
|
} else if (typeof type === "number") {
|
|
|
|
arg += protocol.packetNames[this.state][direction][type];
|
|
|
|
} else {
|
|
|
|
arg += type;
|
|
|
|
}
|
|
|
|
arguments[0] = arg;
|
|
|
|
EventEmitter.prototype.on.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
Client.prototype.setSocket = function(socket) {
|
|
|
|
var self = this;
|
|
|
|
self.socket = socket;
|
|
|
|
var incomingBuffer = new Buffer(0);
|
|
|
|
self.socket.on('data', function(data) {
|
|
|
|
if (self.encryptionEnabled) data = new Buffer(self.decipher.update(data), 'binary');
|
|
|
|
incomingBuffer = Buffer.concat([incomingBuffer, data]);
|
|
|
|
var parsed, packet;
|
|
|
|
while (true) {
|
2014-04-10 19:48:06 -04:00
|
|
|
parsed = parsePacket(incomingBuffer, self.state, self.isServer, self.packetsToParse);
|
2013-01-04 01:45:57 -05:00
|
|
|
if (! parsed) break;
|
2013-01-29 12:39:33 -05:00
|
|
|
if (parsed.error) {
|
|
|
|
this.emit('error', parsed.error);
|
|
|
|
this.end("ProtocolError");
|
|
|
|
return;
|
|
|
|
}
|
2013-01-04 01:45:57 -05:00
|
|
|
packet = parsed.results;
|
|
|
|
incomingBuffer = incomingBuffer.slice(parsed.size);
|
2014-01-14 00:56:56 -05:00
|
|
|
|
|
|
|
var packetName = protocol.packetNames[self.state][self.isServer ? 'toServer' : 'toClient'][packet.id];
|
2014-04-10 19:48:06 -04:00
|
|
|
self.emit(packetName, packet);
|
2014-04-10 20:08:17 -04:00
|
|
|
self.emit('packet', packet);
|
2014-04-10 19:48:06 -04:00
|
|
|
self.emit('raw.' + packetName, parsed.buffer);
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-01-04 21:33:19 -05:00
|
|
|
self.socket.on('connect', function() {
|
|
|
|
self.emit('connect');
|
|
|
|
});
|
2013-02-03 15:36:55 -05:00
|
|
|
|
|
|
|
self.socket.on('error', onError);
|
|
|
|
self.socket.on('close', endSocket);
|
|
|
|
self.socket.on('end', endSocket);
|
|
|
|
self.socket.on('timeout', endSocket);
|
|
|
|
|
|
|
|
function onError(err) {
|
|
|
|
self.emit('error', err);
|
|
|
|
endSocket();
|
|
|
|
}
|
|
|
|
|
2013-02-03 22:13:15 -05:00
|
|
|
var ended = false;
|
2013-02-03 15:36:55 -05:00
|
|
|
function endSocket() {
|
2013-02-03 22:13:15 -05:00
|
|
|
if (ended) return;
|
|
|
|
ended = true;
|
2013-02-03 15:36:55 -05:00
|
|
|
self.socket.removeListener('close', endSocket);
|
|
|
|
self.socket.removeListener('end', endSocket);
|
|
|
|
self.socket.removeListener('timeout', endSocket);
|
|
|
|
self.emit('end', self._endReason);
|
|
|
|
}
|
2013-01-04 01:45:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.connect = function(port, host) {
|
|
|
|
var self = this;
|
2013-04-14 23:09:04 -04:00
|
|
|
if (port == 25565) {
|
|
|
|
dns.resolveSrv("_minecraft._tcp." + host, function(err, addresses) {
|
|
|
|
if (addresses) {
|
|
|
|
self.setSocket(net.connect(addresses[0].port, addresses[0].name));
|
|
|
|
} else {
|
|
|
|
self.setSocket(net.connect(port, host));
|
|
|
|
}
|
2013-12-30 10:05:22 -05:00
|
|
|
});
|
2013-04-14 23:09:04 -04:00
|
|
|
} else {
|
|
|
|
self.setSocket(net.connect(port, host));
|
|
|
|
}
|
2013-01-04 01:45:57 -05:00
|
|
|
};
|
|
|
|
|
2013-01-04 20:55:53 -05:00
|
|
|
Client.prototype.end = function(reason) {
|
|
|
|
this._endReason = reason;
|
2013-01-04 01:45:57 -05:00
|
|
|
this.socket.end();
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.write = function(packetId, params) {
|
2013-12-30 10:05:22 -05:00
|
|
|
if (Array.isArray(packetId)) {
|
|
|
|
if (packetId[0] !== this.state)
|
|
|
|
return false;
|
|
|
|
packetId = packetId[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer = createPacketBuffer(packetId, this.state, params, this.isServer);
|
2013-04-05 23:04:47 -04:00
|
|
|
debug("writing packetId " + packetId + " (0x" + packetId.toString(16) + ")");
|
2013-04-15 00:40:09 -04:00
|
|
|
debug(params);
|
2013-01-04 01:45:57 -05:00
|
|
|
var out = this.encryptionEnabled ? new Buffer(this.cipher.update(buffer), 'binary') : buffer;
|
|
|
|
this.socket.write(out);
|
2013-12-30 10:05:22 -05:00
|
|
|
return true;
|
2013-01-04 01:45:57 -05:00
|
|
|
};
|
2014-04-10 19:48:06 -04:00
|
|
|
|
|
|
|
Client.prototype.writeRaw = function(buffer, shouldEncrypt) {
|
|
|
|
if (shouldEncrypt === null) {
|
|
|
|
shouldEncrypt = true;
|
|
|
|
}
|
|
|
|
var out = (shouldEncrypt && this.encryptionEnabled) ? new Buffer(this.cipher.update(buffer), 'binary') : buffer;
|
|
|
|
this.socket.write(out);
|
|
|
|
};
|