2015-05-14 16:08:49 -04:00
|
|
|
var assert = require('assert')
|
|
|
|
, crypto = require('crypto')
|
|
|
|
, bufferEqual = require('buffer-equal')
|
|
|
|
, protocol = require('./protocol')
|
|
|
|
, Client = require('./client')
|
|
|
|
, dns = require('dns')
|
|
|
|
, net = require('net')
|
|
|
|
, Server = require('./server')
|
|
|
|
, Yggdrasil = require('./yggdrasil.js')
|
|
|
|
, getSession = Yggdrasil.getSession
|
|
|
|
, validateSession = Yggdrasil.validateSession
|
|
|
|
, joinServer = Yggdrasil.joinServer
|
|
|
|
, states = protocol.states
|
|
|
|
, debug = require("./debug")
|
|
|
|
;
|
2015-05-15 21:07:10 -04:00
|
|
|
|
2014-10-21 15:15:43 -04:00
|
|
|
var ursa;
|
|
|
|
try {
|
|
|
|
ursa = require("ursa");
|
|
|
|
} catch(e) {
|
|
|
|
console.log("You are using a pure-javascript implementation of RSA.");
|
|
|
|
console.log("Your performance might be subpar. Please consider installing URSA");
|
2015-03-06 23:50:28 -05:00
|
|
|
ursa = require("ursa-purejs");
|
2014-10-21 15:15:43 -04:00
|
|
|
}
|
2012-12-31 20:33:35 -05:00
|
|
|
|
2013-01-04 21:33:19 -05:00
|
|
|
module.exports = {
|
|
|
|
createClient: createClient,
|
|
|
|
createServer: createServer,
|
|
|
|
Client: Client,
|
|
|
|
Server: Server,
|
2015-03-07 00:00:32 -05:00
|
|
|
ping: require('./ping'),
|
2013-01-05 05:17:27 -05:00
|
|
|
protocol: protocol,
|
2014-01-10 06:55:16 -05:00
|
|
|
yggdrasil: Yggdrasil,
|
2013-01-04 21:33:19 -05:00
|
|
|
};
|
2013-01-04 01:45:57 -05:00
|
|
|
|
|
|
|
function createServer(options) {
|
2013-01-26 22:13:24 -05:00
|
|
|
options = options || {};
|
2013-01-04 01:45:57 -05:00
|
|
|
var port = options.port != null ?
|
2015-05-14 16:08:49 -04:00
|
|
|
options.port :
|
|
|
|
options['server-port'] != null ?
|
|
|
|
options['server-port'] :
|
|
|
|
25565;
|
2013-01-04 01:45:57 -05:00
|
|
|
var host = options.host || '0.0.0.0';
|
2013-01-04 21:33:19 -05:00
|
|
|
var kickTimeout = options.kickTimeout || 10 * 1000;
|
|
|
|
var checkTimeoutInterval = options.checkTimeoutInterval || 4 * 1000;
|
2013-01-04 01:45:57 -05:00
|
|
|
var onlineMode = options['online-mode'] == null ? true : options['online-mode'];
|
2015-03-26 04:50:57 -04:00
|
|
|
// a function receiving the default status object and the client
|
|
|
|
// and returning a modified response object.
|
|
|
|
var beforePing = options.beforePing || null;
|
2013-01-26 20:12:09 -05:00
|
|
|
|
|
|
|
var serverKey = ursa.generatePrivateKey(1024);
|
2013-01-04 01:45:57 -05:00
|
|
|
|
|
|
|
var server = new Server(options);
|
2013-02-10 17:58:02 -05:00
|
|
|
server.motd = options.motd || "A Minecraft server";
|
|
|
|
server.maxPlayers = options['max-players'] || 20;
|
2013-02-10 17:51:47 -05:00
|
|
|
server.playerCount = 0;
|
2013-02-03 18:11:19 -05:00
|
|
|
server.onlineModeExceptions = {};
|
2013-01-04 01:45:57 -05:00
|
|
|
server.on("connection", function(client) {
|
2013-12-30 10:05:22 -05:00
|
|
|
client.once([states.HANDSHAKING, 0x00], onHandshake);
|
|
|
|
client.once([states.LOGIN, 0x00], onLogin);
|
|
|
|
client.once([states.STATUS, 0x00], onPing);
|
2013-01-04 01:45:57 -05:00
|
|
|
client.on('end', onEnd);
|
|
|
|
|
|
|
|
var keepAlive = false;
|
|
|
|
var loggedIn = false;
|
|
|
|
var lastKeepAlive = null;
|
|
|
|
|
2013-01-04 20:55:53 -05:00
|
|
|
var keepAliveTimer = null;
|
|
|
|
var loginKickTimer = setTimeout(kickForNotLoggingIn, kickTimeout);
|
|
|
|
|
2013-01-26 20:12:09 -05:00
|
|
|
var hash;
|
|
|
|
|
2013-01-04 20:55:53 -05:00
|
|
|
function kickForNotLoggingIn() {
|
|
|
|
client.end('LoginTimeout');
|
|
|
|
}
|
2013-01-04 01:45:57 -05:00
|
|
|
|
|
|
|
function keepAliveLoop() {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(!keepAlive)
|
2013-12-30 10:05:22 -05:00
|
|
|
return;
|
2013-01-04 21:53:51 -05:00
|
|
|
|
|
|
|
// check if the last keepAlive was too long ago (kickTimeout)
|
|
|
|
var elapsed = new Date() - lastKeepAlive;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(elapsed > kickTimeout) {
|
2013-01-04 21:53:51 -05:00
|
|
|
client.end('KeepAliveTimeout');
|
|
|
|
return;
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
2013-01-04 21:53:51 -05:00
|
|
|
client.write(0x00, {
|
|
|
|
keepAliveId: Math.floor(Math.random() * 2147483648)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onKeepAlive(packet) {
|
|
|
|
lastKeepAlive = new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
function startKeepAlive() {
|
|
|
|
keepAlive = true;
|
|
|
|
lastKeepAlive = new Date();
|
|
|
|
keepAliveTimer = setInterval(keepAliveLoop, checkTimeoutInterval);
|
|
|
|
client.on(0x00, onKeepAlive);
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function onEnd() {
|
|
|
|
clearInterval(keepAliveTimer);
|
2013-01-04 20:55:53 -05:00
|
|
|
clearTimeout(loginKickTimer);
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function onPing(packet) {
|
2013-12-30 10:05:22 -05:00
|
|
|
var response = {
|
|
|
|
"version": {
|
|
|
|
"name": protocol.minecraftVersion,
|
|
|
|
"protocol": protocol.version
|
|
|
|
},
|
|
|
|
"players": {
|
|
|
|
"max": server.maxPlayers,
|
|
|
|
"online": server.playerCount,
|
|
|
|
"sample": []
|
|
|
|
},
|
|
|
|
"description": {"text": server.motd},
|
|
|
|
"favicon": server.favicon
|
|
|
|
};
|
|
|
|
|
2015-05-14 16:08:49 -04:00
|
|
|
if(beforePing) {
|
2015-03-26 04:50:57 -04:00
|
|
|
response = beforePing(response, client) || response;
|
|
|
|
}
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
client.once([states.STATUS, 0x01], function(packet) {
|
2015-05-22 19:10:58 -04:00
|
|
|
client.write(0x01, {time: packet.time});
|
|
|
|
client.end();
|
2013-01-04 01:45:57 -05:00
|
|
|
});
|
2013-12-30 10:05:22 -05:00
|
|
|
client.write(0x00, {response: JSON.stringify(response)});
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
function onLogin(packet) {
|
2013-01-04 01:45:57 -05:00
|
|
|
client.username = packet.username;
|
2013-02-03 18:53:34 -05:00
|
|
|
var isException = !!server.onlineModeExceptions[client.username.toLowerCase()];
|
2015-05-14 16:08:49 -04:00
|
|
|
var needToVerify = (onlineMode && !isException) || (!onlineMode && isException);
|
|
|
|
if(needToVerify) {
|
2013-12-30 10:05:22 -05:00
|
|
|
var serverId = crypto.randomBytes(4).toString('hex');
|
2013-01-26 20:12:09 -05:00
|
|
|
client.verifyToken = crypto.randomBytes(4);
|
|
|
|
var publicKeyStrArr = serverKey.toPublicPem("utf8").split("\n");
|
|
|
|
var publicKeyStr = "";
|
2015-05-14 16:08:49 -04:00
|
|
|
for(var i = 1; i < publicKeyStrArr.length - 2; i++) {
|
2013-01-26 20:12:09 -05:00
|
|
|
publicKeyStr += publicKeyStrArr[i]
|
|
|
|
}
|
2013-12-30 10:05:22 -05:00
|
|
|
client.publicKey = new Buffer(publicKeyStr, 'base64');
|
2013-01-26 20:12:09 -05:00
|
|
|
hash = crypto.createHash("sha1");
|
|
|
|
hash.update(serverId);
|
2013-12-30 10:05:22 -05:00
|
|
|
client.once([states.LOGIN, 0x01], onEncryptionKeyResponse);
|
|
|
|
client.write(0x01, {
|
2013-01-26 20:12:09 -05:00
|
|
|
serverId: serverId,
|
|
|
|
publicKey: client.publicKey,
|
|
|
|
verifyToken: client.verifyToken
|
|
|
|
});
|
|
|
|
} else {
|
2013-01-26 22:13:24 -05:00
|
|
|
loginClient();
|
2013-01-26 20:12:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
function onHandshake(packet) {
|
2015-03-26 04:50:57 -04:00
|
|
|
client.serverHost = packet.serverHost;
|
|
|
|
client.serverPort = packet.serverPort;
|
|
|
|
client.protocolVersion = packet.protocolVersion;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(packet.nextState == 1) {
|
2013-12-30 10:05:22 -05:00
|
|
|
client.state = states.STATUS;
|
2015-05-14 16:08:49 -04:00
|
|
|
} else if(packet.nextState == 2) {
|
2013-12-30 10:05:22 -05:00
|
|
|
client.state = states.LOGIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 20:12:09 -05:00
|
|
|
function onEncryptionKeyResponse(packet) {
|
2015-04-14 08:38:16 -04:00
|
|
|
try {
|
|
|
|
var verifyToken = serverKey.decrypt(packet.verifyToken, undefined, undefined, ursa.RSA_PKCS1_PADDING);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(!bufferEqual(client.verifyToken, verifyToken)) {
|
2015-04-14 08:38:16 -04:00
|
|
|
client.end('DidNotEncryptVerifyTokenProperly');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var sharedSecret = serverKey.decrypt(packet.sharedSecret, undefined, undefined, ursa.RSA_PKCS1_PADDING);
|
2015-05-14 16:08:49 -04:00
|
|
|
} catch(e) {
|
2013-02-03 18:11:19 -05:00
|
|
|
client.end('DidNotEncryptVerifyTokenProperly');
|
|
|
|
return;
|
|
|
|
}
|
2015-05-21 16:35:03 -04:00
|
|
|
client.setEncryption(sharedSecret);
|
2013-02-03 18:11:19 -05:00
|
|
|
hash.update(sharedSecret);
|
|
|
|
hash.update(client.publicKey);
|
|
|
|
|
2013-02-03 18:53:34 -05:00
|
|
|
var isException = !!server.onlineModeExceptions[client.username.toLowerCase()];
|
2013-12-30 10:05:22 -05:00
|
|
|
var needToVerify = (onlineMode && !isException) || (!onlineMode && isException);
|
2013-02-03 18:11:19 -05:00
|
|
|
var nextStep = needToVerify ? verifyUsername : loginClient;
|
|
|
|
nextStep();
|
|
|
|
|
|
|
|
function verifyUsername() {
|
2013-01-26 20:12:09 -05:00
|
|
|
var digest = mcHexDigest(hash);
|
2013-12-30 10:05:22 -05:00
|
|
|
validateSession(client.username, digest, function(err, uuid) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(err) {
|
2013-12-30 10:05:22 -05:00
|
|
|
client.end("Failed to verify username!");
|
|
|
|
return;
|
2013-02-03 18:11:19 -05:00
|
|
|
}
|
2014-04-11 11:07:33 -04:00
|
|
|
client.uuid = uuid;
|
2014-09-05 06:49:39 -04:00
|
|
|
// Convert to a valid UUID until the session server updates and does
|
|
|
|
// it automatically
|
|
|
|
client.uuid = client.uuid.replace(/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/, "$1-$2-$3-$4-$5");
|
2013-12-30 10:05:22 -05:00
|
|
|
loginClient();
|
2013-01-26 20:12:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loginClient() {
|
2015-01-01 17:20:47 -05:00
|
|
|
var isException = !!server.onlineModeExceptions[client.username.toLowerCase()];
|
2015-05-14 16:08:49 -04:00
|
|
|
if(onlineMode == false || isException) {
|
2014-09-07 21:50:40 -04:00
|
|
|
client.uuid = "0-0-0-0-0";
|
|
|
|
}
|
2015-01-01 17:20:47 -05:00
|
|
|
//client.write('compress', { threshold: 256 }); // Default threshold is 256
|
|
|
|
//client.compressionThreshold = 256;
|
2015-05-22 19:10:58 -04:00
|
|
|
client.write(0x02, {uuid: client.uuid, username: client.username});
|
|
|
|
client.state = states.PLAY;
|
|
|
|
loggedIn = true;
|
|
|
|
startKeepAlive();
|
|
|
|
|
|
|
|
clearTimeout(loginKickTimer);
|
|
|
|
loginKickTimer = null;
|
|
|
|
|
|
|
|
server.playerCount += 1;
|
|
|
|
client.once('end', function() {
|
|
|
|
server.playerCount -= 1;
|
2013-02-10 17:51:47 -05:00
|
|
|
});
|
2015-05-22 19:10:58 -04:00
|
|
|
server.emit('login', client);
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
server.listen(port, host);
|
|
|
|
return server;
|
|
|
|
}
|
2012-12-31 20:33:35 -05:00
|
|
|
|
2015-03-29 02:24:23 -04:00
|
|
|
Client.prototype.connect = function(port, host) {
|
|
|
|
var self = this;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(port == 25565 && net.isIP(host) === 0) {
|
2015-03-29 02:24:23 -04:00
|
|
|
dns.resolveSrv("_minecraft._tcp." + host, function(err, addresses) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(addresses && addresses.length > 0) {
|
|
|
|
self.setSocket(net.connect(addresses[0].port, addresses[0].name));
|
|
|
|
} else {
|
|
|
|
self.setSocket(net.connect(port, host));
|
|
|
|
}
|
2015-03-29 02:24:23 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.setSocket(net.connect(port, host));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 23:39:31 -05:00
|
|
|
function createClient(options) {
|
2013-01-02 04:46:01 -05:00
|
|
|
assert.ok(options, "options is required");
|
2013-01-01 23:39:31 -05:00
|
|
|
var port = options.port || 25565;
|
|
|
|
var host = options.host || 'localhost';
|
2013-12-30 10:05:22 -05:00
|
|
|
var clientToken = options.clientToken || Yggdrasil.generateUUID();
|
|
|
|
var accessToken = options.accessToken || null;
|
|
|
|
|
2013-01-01 23:39:31 -05:00
|
|
|
assert.ok(options.username, "username is required");
|
2013-12-30 10:05:22 -05:00
|
|
|
var haveCredentials = options.password != null || (clientToken != null && accessToken != null);
|
2013-01-04 21:53:51 -05:00
|
|
|
var keepAlive = options.keepAlive == null ? true : options.keepAlive;
|
2013-01-01 23:39:31 -05:00
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
|
2013-01-07 23:36:14 -05:00
|
|
|
var client = new Client(false);
|
2013-01-30 19:56:43 -05:00
|
|
|
client.on('connect', onConnect);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(keepAlive) client.on([states.PLAY, 0x00], onKeepAlive);
|
2013-12-30 10:05:22 -05:00
|
|
|
client.once([states.LOGIN, 0x01], onEncryptionKeyRequest);
|
|
|
|
client.once([states.LOGIN, 0x02], onLogin);
|
2015-01-01 17:20:47 -05:00
|
|
|
client.once("compress", onCompressionRequest);
|
2015-05-15 21:07:10 -04:00
|
|
|
client.on("set_compression", onCompressionRequest);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(haveCredentials) {
|
2013-01-30 19:56:43 -05:00
|
|
|
// make a request to get the case-correct username before connecting.
|
2013-12-30 10:05:22 -05:00
|
|
|
var cb = function(err, session) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(err) {
|
2013-01-30 19:56:43 -05:00
|
|
|
client.emit('error', err);
|
|
|
|
} else {
|
|
|
|
client.session = session;
|
|
|
|
client.username = session.username;
|
2013-12-30 10:05:22 -05:00
|
|
|
accessToken = session.accessToken;
|
2013-01-30 19:56:43 -05:00
|
|
|
client.emit('session');
|
|
|
|
client.connect(port, host);
|
|
|
|
}
|
2013-12-30 10:05:22 -05:00
|
|
|
};
|
2015-01-01 17:20:47 -05:00
|
|
|
|
2015-05-19 09:08:09 -04:00
|
|
|
if(accessToken != null) getSession(options.username, accessToken, clientToken, true, cb);
|
|
|
|
else getSession(options.username, options.password, clientToken, false, cb);
|
2013-01-30 19:56:43 -05:00
|
|
|
} else {
|
|
|
|
// assume the server is in offline mode and just go for it.
|
|
|
|
client.username = options.username;
|
|
|
|
client.connect(port, host);
|
|
|
|
}
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
|
|
|
function onConnect() {
|
2013-12-30 10:05:22 -05:00
|
|
|
client.write(0x00, {
|
2013-01-03 23:14:19 -05:00
|
|
|
protocolVersion: protocol.version,
|
2013-01-01 23:39:31 -05:00
|
|
|
serverHost: host,
|
|
|
|
serverPort: port,
|
2013-12-30 10:05:22 -05:00
|
|
|
nextState: 2
|
2015-05-22 19:10:58 -04:00
|
|
|
});
|
|
|
|
client.state = states.LOGIN;
|
|
|
|
client.write(0x00, {
|
|
|
|
username: client.username
|
2013-01-01 23:39:31 -05:00
|
|
|
});
|
2013-01-30 19:56:43 -05:00
|
|
|
}
|
2013-01-01 23:39:31 -05:00
|
|
|
|
2015-01-01 17:20:47 -05:00
|
|
|
function onCompressionRequest(packet) {
|
|
|
|
client.compressionThreshold = packet.threshold;
|
|
|
|
}
|
|
|
|
|
2013-01-01 23:39:31 -05:00
|
|
|
function onKeepAlive(packet) {
|
2013-01-03 21:42:35 -05:00
|
|
|
client.write(0x00, {
|
2013-01-01 23:39:31 -05:00
|
|
|
keepAliveId: packet.keepAliveId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onEncryptionKeyRequest(packet) {
|
2013-01-30 19:56:43 -05:00
|
|
|
crypto.randomBytes(16, gotSharedSecret);
|
|
|
|
|
|
|
|
function gotSharedSecret(err, sharedSecret) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(err) {
|
2015-02-05 13:02:34 -05:00
|
|
|
debug(err);
|
2013-01-01 23:39:31 -05:00
|
|
|
client.emit('error', err);
|
|
|
|
client.end();
|
2015-02-05 13:02:34 -05:00
|
|
|
return;
|
2013-01-01 23:39:31 -05:00
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
if(haveCredentials) {
|
2013-01-02 02:19:47 -05:00
|
|
|
joinServerRequest(onJoinServerResponse);
|
|
|
|
} else {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(packet.serverId != '-') {
|
2013-04-07 06:02:31 -04:00
|
|
|
debug('This server appears to be an online server and you are providing no password, the authentication will probably fail');
|
|
|
|
}
|
2013-01-02 02:19:47 -05:00
|
|
|
sendEncryptionKeyResponse();
|
|
|
|
}
|
2013-01-01 23:39:31 -05:00
|
|
|
|
2013-01-02 02:19:47 -05:00
|
|
|
function onJoinServerResponse(err) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(err) {
|
2013-01-01 23:39:31 -05:00
|
|
|
client.emit('error', err);
|
|
|
|
client.end();
|
|
|
|
} else {
|
|
|
|
sendEncryptionKeyResponse();
|
|
|
|
}
|
2013-01-02 02:19:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function joinServerRequest(cb) {
|
2013-04-07 06:04:29 -04:00
|
|
|
var hash = crypto.createHash('sha1');
|
|
|
|
hash.update(packet.serverId);
|
2013-01-02 02:19:47 -05:00
|
|
|
hash.update(sharedSecret);
|
|
|
|
hash.update(packet.publicKey);
|
|
|
|
|
|
|
|
var digest = mcHexDigest(hash);
|
2015-03-23 12:20:50 -04:00
|
|
|
joinServer(client.username, digest, accessToken, client.session.selectedProfile.id, cb);
|
2013-01-02 02:19:47 -05:00
|
|
|
}
|
2013-01-01 23:39:31 -05:00
|
|
|
|
|
|
|
function sendEncryptionKeyResponse() {
|
|
|
|
var pubKey = mcPubKeyToURsa(packet.publicKey);
|
2013-01-06 05:20:58 -05:00
|
|
|
var encryptedSharedSecretBuffer = pubKey.encrypt(sharedSecret, undefined, undefined, ursa.RSA_PKCS1_PADDING);
|
|
|
|
var encryptedVerifyTokenBuffer = pubKey.encrypt(packet.verifyToken, undefined, undefined, ursa.RSA_PKCS1_PADDING);
|
2013-12-30 10:05:22 -05:00
|
|
|
client.write(0x01, {
|
2013-01-01 23:39:31 -05:00
|
|
|
sharedSecret: encryptedSharedSecretBuffer,
|
|
|
|
verifyToken: encryptedVerifyTokenBuffer,
|
|
|
|
});
|
2015-05-21 16:35:03 -04:00
|
|
|
client.setEncryption(sharedSecret);
|
2013-01-01 23:39:31 -05:00
|
|
|
}
|
2013-01-30 19:56:43 -05:00
|
|
|
}
|
2013-01-01 23:39:31 -05:00
|
|
|
}
|
2015-02-22 11:27:46 -05:00
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
function onLogin(packet) {
|
|
|
|
client.state = states.PLAY;
|
|
|
|
client.uuid = packet.uuid;
|
|
|
|
client.username = packet.username;
|
2013-01-01 23:39:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 20:33:35 -05:00
|
|
|
|
2013-01-01 23:39:31 -05:00
|
|
|
function mcPubKeyToURsa(mcPubKeyBuffer) {
|
|
|
|
var pem = "-----BEGIN PUBLIC KEY-----\n";
|
|
|
|
var base64PubKey = mcPubKeyBuffer.toString('base64');
|
|
|
|
var maxLineLength = 65;
|
2015-05-14 16:08:49 -04:00
|
|
|
while(base64PubKey.length > 0) {
|
2013-01-01 23:39:31 -05:00
|
|
|
pem += base64PubKey.substring(0, maxLineLength) + "\n";
|
|
|
|
base64PubKey = base64PubKey.substring(maxLineLength);
|
|
|
|
}
|
|
|
|
pem += "-----END PUBLIC KEY-----\n";
|
|
|
|
return ursa.createPublicKey(pem, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
function mcHexDigest(hash) {
|
|
|
|
var buffer = new Buffer(hash.digest(), 'binary');
|
|
|
|
// check for negative hashes
|
|
|
|
var negative = buffer.readInt8(0) < 0;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(negative)
|
2013-12-30 10:05:22 -05:00
|
|
|
performTwosCompliment(buffer);
|
2013-01-01 23:39:31 -05:00
|
|
|
var digest = buffer.toString('hex');
|
|
|
|
// trim leading zeroes
|
|
|
|
digest = digest.replace(/^0+/g, '');
|
2015-05-14 16:08:49 -04:00
|
|
|
if(negative)
|
2013-12-30 10:05:22 -05:00
|
|
|
digest = '-' + digest;
|
2013-01-01 23:39:31 -05:00
|
|
|
return digest;
|
|
|
|
|
|
|
|
function performTwosCompliment(buffer) {
|
|
|
|
var carry = true;
|
|
|
|
var i, newByte, value;
|
2015-05-14 16:08:49 -04:00
|
|
|
for(i = buffer.length - 1; i >= 0; --i) {
|
2013-01-01 23:39:31 -05:00
|
|
|
value = buffer.readUInt8(i);
|
|
|
|
newByte = ~value & 0xff;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(carry) {
|
2013-01-01 23:39:31 -05:00
|
|
|
carry = newByte === 0xff;
|
2013-04-07 05:55:20 -04:00
|
|
|
buffer.writeUInt8((newByte + 1) & 0xff, i);
|
2013-01-01 23:39:31 -05:00
|
|
|
} else {
|
|
|
|
buffer.writeUInt8(newByte, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|