Switch minecraftping type to use core TCP support, move SRV resolving into core

This commit is contained in:
Michael Morrison 2014-02-01 09:47:16 -06:00
parent 55661ae591
commit a3c14a8d42
2 changed files with 81 additions and 132 deletions

View file

@ -6,29 +6,6 @@ module.exports = require('./protocols/gamespy3').extend({
this.pretty = 'Minecraft';
this.maxAttempts = 2;
this.options.port = 25565;
},
parseDns: function(host,c) {
var self = this;
var _super = this._super;
function fallback(h) { _super.call(self,h,c); }
dns.resolve('_minecraft._tcp.'+host, 'SRV', function(err,addresses) {
if(err) return fallback(host);
if(addresses.length >= 1) {
var line = addresses[0];
self.options.port = line.port;
var srvhost = line.name;
if(srvhost.match(/\d+\.\d+\.\d+\.\d+/)) {
self.options.address = srvhost;
c();
} else {
// resolve yet again
fallback(srvhost);
}
return;
}
return fallback(host);
});
this.srvRecord = '_minecraft._tcp';
}
});

View file

@ -1,6 +1,5 @@
var dns = require('dns'),
net = require('net'),
varint = require('varint');
var varint = require('varint'),
async = require('async');
function varIntBuffer(num) {
return new Buffer(varint.encode(num));
@ -11,45 +10,15 @@ module.exports = require('./protocols/core').extend({
this._super();
this.pretty = 'Minecraft';
this.options.port = 25565;
},
parseDns: function(host,c) {
var self = this;
var _super = this._super;
function fallback(h) { _super.call(self,h,c); }
dns.resolve('_minecraft._tcp.'+host, 'SRV', function(err,addresses) {
if(err) return fallback(host);
if(addresses.length >= 1) {
var line = addresses[0];
self.options.port = line.port;
var srvhost = line.name;
if(srvhost.match(/\d+\.\d+\.\d+\.\d+/)) {
self.options.address = srvhost;
c();
} else {
// resolve yet again
fallback(srvhost);
}
return;
}
return fallback(host);
});
},
reset: function() {
this._super();
if(this.socket) {
this.socket.destroy();
delete this.socket;
}
this.srvRecord = '_minecraft._tcp';
},
run: function(state) {
var self = this;
var receivedData;
var socket = this.socket = net.connect(
this.options.port,
this.options.address,
function() {
async.series([
function(c) {
// build and send handshake and status TCP packet
var portBuf = new Buffer(2);
portBuf.writeUInt16BE(self.options.port,0);
@ -63,65 +32,68 @@ module.exports = require('./protocols/core').extend({
portBuf,
varIntBuffer(1)
];
self.sendPacket(0,Buffer.concat(bufs));
self.sendPacket(0);
});
socket.setTimeout(10000);
socket.setNoDelay(true);
var received = new Buffer(0);
var expectedBytes = 0;
socket.on('data', function(data) {
received = Buffer.concat([received,data]);
if(expectedBytes) {
if(received.length >= expectedBytes) {
self.allReceived(received,state);
}
} else if(received.length > 10) {
expectedBytes = varint.decode(received);
received = received.slice(varint.decode.bytesRead);
}
});
},
sendPacket: function(id,data) {
function buildPacket(id,data) {
if(!data) data = new Buffer(0);
var idBuffer = varIntBuffer(id);
var out = Buffer.concat([
return Buffer.concat([
varIntBuffer(data.length+idBuffer.length),
idBuffer,
data
]);
this.socket.write(out);
}
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);
data = data.slice(varint.decode.bytesRead);
if(data.length < expected) return false;
receivedData = data;
c();
});
},
allReceived: function(received,state) {
var packetId = varint.decode(received);
received = received.slice(varint.decode.bytesRead);
function(c) {
// parse response
var strLen = varint.decode(received);
received = received.slice(varint.decode.bytesRead);
var data = receivedData;
var packetId = varint.decode(data);
data = data.slice(varint.decode.bytesRead);
var str = received.toString('utf8');
var strLen = varint.decode(data);
data = data.slice(varint.decode.bytesRead);
var str = data.toString('utf8');
var json;
try {
json = JSON.parse(str);
delete json.favicon;
if(self.debug) console.log(json);
} catch(e) {
return this.fatal('Invalid JSON');
return self.fatal('Invalid JSON');
}
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({});
}
this.finish(state);
self.finish(state);
}
]);
}
});