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.pretty = 'Minecraft';
this.maxAttempts = 2; this.maxAttempts = 2;
this.options.port = 25565; this.options.port = 25565;
}, this.srvRecord = '_minecraft._tcp';
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);
});
} }
}); });

View file

@ -1,6 +1,5 @@
var dns = require('dns'), var varint = require('varint'),
net = require('net'), async = require('async');
varint = require('varint');
function varIntBuffer(num) { function varIntBuffer(num) {
return new Buffer(varint.encode(num)); return new Buffer(varint.encode(num));
@ -11,117 +10,90 @@ module.exports = require('./protocols/core').extend({
this._super(); this._super();
this.pretty = 'Minecraft'; this.pretty = 'Minecraft';
this.options.port = 25565; this.options.port = 25565;
}, this.srvRecord = '_minecraft._tcp';
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;
}
}, },
run: function(state) { run: function(state) {
var self = this; var self = this;
var receivedData;
var socket = this.socket = net.connect( async.series([
this.options.port, function(c) {
this.options.address, // build and send handshake and status TCP packet
function() {
var portBuf = new Buffer(2); var portBuf = new Buffer(2);
portBuf.writeUInt16BE(self.options.port,0); portBuf.writeUInt16BE(self.options.port,0);
var addressBuf = new Buffer(self.options.address,'utf8'); var addressBuf = new Buffer(self.options.address,'utf8');
var bufs = [ var bufs = [
varIntBuffer(4), varIntBuffer(4),
varIntBuffer(addressBuf.length), varIntBuffer(addressBuf.length),
addressBuf, addressBuf,
portBuf, portBuf,
varIntBuffer(1) varIntBuffer(1)
]; ];
self.sendPacket(0,Buffer.concat(bufs));
self.sendPacket(0);
});
socket.setTimeout(10000);
socket.setNoDelay(true);
var received = new Buffer(0); function buildPacket(id,data) {
var expectedBytes = 0; if(!data) data = new Buffer(0);
socket.on('data', function(data) { var idBuffer = varIntBuffer(id);
received = Buffer.concat([received,data]); return Buffer.concat([
if(expectedBytes) { varIntBuffer(data.length+idBuffer.length),
if(received.length >= expectedBytes) { idBuffer,
self.allReceived(received,state); data
]);
} }
} else if(received.length > 10) {
expectedBytes = varint.decode(received); var outBuffer = Buffer.concat([
received = received.slice(varint.decode.bytesRead); 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();
});
},
function(c) {
// parse response
var data = receivedData;
var packetId = varint.decode(data);
data = data.slice(varint.decode.bytesRead);
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 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({});
}
self.finish(state);
} }
});
},
sendPacket: function(id,data) {
if(!data) data = new Buffer(0);
var idBuffer = varIntBuffer(id);
var out = Buffer.concat([
varIntBuffer(data.length+idBuffer.length),
idBuffer,
data
]); ]);
this.socket.write(out);
},
allReceived: function(received,state) {
var packetId = varint.decode(received);
received = received.slice(varint.decode.bytesRead);
var strLen = varint.decode(received);
received = received.slice(varint.decode.bytesRead);
var str = received.toString('utf8');
var json;
try {
json = JSON.parse(str);
delete json.favicon;
} catch(e) {
return this.fatal('Invalid JSON');
}
state.raw.version = json.version.name;
state.maxplayers = json.players.max;
state.raw.description = json.description.text;
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);
} }
}); });