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(
this.options.port,
this.options.address,
function() {
var portBuf = new Buffer(2); async.series([
portBuf.writeUInt16BE(self.options.port,0); function(c) {
// build and send handshake and status TCP packet
var addressBuf = new Buffer(self.options.address,'utf8');
var bufs = [ var portBuf = new Buffer(2);
varIntBuffer(4), portBuf.writeUInt16BE(self.options.port,0);
varIntBuffer(addressBuf.length),
addressBuf, var addressBuf = new Buffer(self.options.address,'utf8');
portBuf,
varIntBuffer(1) var bufs = [
]; varIntBuffer(4),
self.sendPacket(0,Buffer.concat(bufs)); varIntBuffer(addressBuf.length),
self.sendPacket(0); addressBuf,
}); portBuf,
socket.setTimeout(10000); varIntBuffer(1)
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)
}); ]);
},
sendPacket: function(id,data) { self.tcpSend(outBuffer, function(data) {
if(!data) data = new Buffer(0); if(data.length < 10) return false;
var idBuffer = varIntBuffer(id); var expected = varint.decode(data);
var out = Buffer.concat([ data = data.slice(varint.decode.bytesRead);
varIntBuffer(data.length+idBuffer.length), if(data.length < expected) return false;
idBuffer, receivedData = data;
data c();
]); });
this.socket.write(out); },
}, function(c) {
allReceived: function(received,state) { // parse response
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 data = receivedData;
var json; var packetId = varint.decode(data);
try { data = data.slice(varint.decode.bytesRead);
json = JSON.parse(str);
delete json.favicon; var strLen = varint.decode(data);
} catch(e) { data = data.slice(varint.decode.bytesRead);
return this.fatal('Invalid JSON');
} var str = data.toString('utf8');
var json;
state.raw.version = json.version.name; try {
state.maxplayers = json.players.max; json = JSON.parse(str);
state.raw.description = json.description.text; delete json.favicon;
for(var i = 0; i < json.players.sample.length; i++) { if(self.debug) console.log(json);
state.players.push({ } catch(e) {
id: json.players.sample[i].id, return self.fatal('Invalid JSON');
name: json.players.sample[i].name }
});
} state.raw.version = json.version.name;
while(state.players.length < json.players.online) { state.maxplayers = json.players.max;
state.players.push({}); state.raw.description = json.description.text;
} if(json.players.sample) {
for(var i = 0; i < json.players.sample.length; i++) {
this.finish(state); 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);
}
]);
} }
}); });