2014-10-29 08:02:03 +01:00
|
|
|
var varint = require('varint'),
|
|
|
|
async = require('async');
|
|
|
|
|
|
|
|
function varIntBuffer(num) {
|
|
|
|
return new Buffer(varint.encode(num));
|
|
|
|
}
|
|
|
|
function buildPacket(id,data) {
|
|
|
|
if(!data) data = new Buffer(0);
|
|
|
|
var idBuffer = varIntBuffer(id);
|
|
|
|
return Buffer.concat([
|
|
|
|
varIntBuffer(data.length+idBuffer.length),
|
|
|
|
idBuffer,
|
|
|
|
data
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = require('./core').extend({
|
|
|
|
run: function(state) {
|
|
|
|
var self = this;
|
|
|
|
var receivedData;
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
function(c) {
|
|
|
|
// build and send handshake and status TCP packet
|
|
|
|
|
|
|
|
var portBuf = new Buffer(2);
|
|
|
|
portBuf.writeUInt16BE(self.options.port_query,0);
|
|
|
|
|
|
|
|
var addressBuf = new Buffer(self.options.address,'utf8');
|
|
|
|
|
|
|
|
var bufs = [
|
|
|
|
varIntBuffer(4),
|
|
|
|
varIntBuffer(addressBuf.length),
|
|
|
|
addressBuf,
|
|
|
|
portBuf,
|
|
|
|
varIntBuffer(1)
|
|
|
|
];
|
2015-01-18 08:38:45 +01:00
|
|
|
|
2014-10-29 08:02:03 +01:00
|
|
|
var outBuffer = Buffer.concat([
|
|
|
|
buildPacket(0,Buffer.concat(bufs)),
|
|
|
|
buildPacket(0)
|
|
|
|
]);
|
|
|
|
|
|
|
|
self.tcpSend(outBuffer, function(data) {
|
|
|
|
if(data.length < 10) return false;
|
|
|
|
var expected = varint.decode(data);
|
2015-01-18 08:38:45 +01:00
|
|
|
data = data.slice(varint.decode.bytes);
|
2014-10-29 08:02:03 +01:00
|
|
|
if(data.length < expected) return false;
|
|
|
|
receivedData = data;
|
|
|
|
c();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(c) {
|
|
|
|
// parse response
|
|
|
|
|
|
|
|
var data = receivedData;
|
|
|
|
var packetId = varint.decode(data);
|
2015-01-18 08:38:45 +01:00
|
|
|
if(self.debug) console.log("Packet ID: "+packetId);
|
|
|
|
data = data.slice(varint.decode.bytes);
|
|
|
|
|
2014-10-29 08:02:03 +01:00
|
|
|
var strLen = varint.decode(data);
|
2015-01-18 08:38:45 +01:00
|
|
|
if(self.debug) console.log("String Length: "+strLen);
|
|
|
|
data = data.slice(varint.decode.bytes);
|
2014-10-29 08:02:03 +01:00
|
|
|
|
|
|
|
var str = data.toString('utf8');
|
2015-01-18 08:38:45 +01:00
|
|
|
if(self.debug) {
|
|
|
|
console.log(str);
|
|
|
|
}
|
|
|
|
|
2014-10-29 08:02:03 +01:00
|
|
|
var json;
|
|
|
|
try {
|
|
|
|
json = JSON.parse(str);
|
|
|
|
delete json.favicon;
|
|
|
|
} catch(e) {
|
|
|
|
return self.fatal('Invalid JSON');
|
|
|
|
}
|
2015-01-18 08:38:45 +01:00
|
|
|
|
2014-10-29 08:02:03 +01:00
|
|
|
state.raw.version = json.version.name;
|
|
|
|
state.maxplayers = json.players.max;
|
|
|
|
state.raw.description = json.description.text;
|
|
|
|
if(json.players.sample) {
|
|
|
|
for(var i = 0; i < json.players.sample.length; i++) {
|
|
|
|
state.players.push({
|
|
|
|
id: json.players.sample[i].id,
|
|
|
|
name: json.players.sample[i].name
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(state.players.length < json.players.online) {
|
|
|
|
state.players.push({});
|
|
|
|
}
|
2015-01-18 08:38:45 +01:00
|
|
|
|
2014-10-29 08:02:03 +01:00
|
|
|
self.finish(state);
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|