Resolve SRV records for minecraft servers

This commit is contained in:
Michael Morrison 2013-07-10 09:52:47 -05:00
parent a0ba0d219c
commit c4f66a72db
2 changed files with 52 additions and 9 deletions

View file

@ -33,16 +33,20 @@ module.exports = Class.extend(EventEmitter,{
this.done({error: err.toString()});
},
prepState: function(state) {
if(this.options.notes)
state.notes = this.options.notes;
if('host' in this.options) state.queryhost = this.options.host;
if('port' in this.options) state.queryport = this.options.port;
},
finish: function(result) {
this.done(result);
},
done: function(result) {
if(this.finished) return;
clearTimeout(this.globalTimeoutTimer);
if(this.options.notes)
result.notes = this.options.notes;
this.prepState(result);
this.reset();
this.finished = true;
@ -73,11 +77,7 @@ module.exports = Class.extend(EventEmitter,{
self.options.address = self.options.host;
c();
} else {
dns.lookup(self.options.host, function(err,address,family) {
if(err) return self.error(err);
self.options.address = address;
c();
});
self.parseDns(self.options.host,c);
}
}, function(c) {
self.run();
@ -85,11 +85,28 @@ module.exports = Class.extend(EventEmitter,{
]);
},
parseDns: function(host,c) {
var self = this;
dns.lookup(host, function(err,address,family) {
if(err) return self.error(err);
self.options.address = address;
c();
});
},
// utils
reader: function(buffer) {
return new Reader(this,buffer);
},
translateState: function(state,trans) {
for(var from in trans) {
var to = trans[from];
if(from in state) {
if(to) state[to] = state[from];
delete state[from];
}
}
},
setTimeout: function(c,t) {
if(this.finished) return 0;
var id = setTimeout(c,t);

View file

@ -1,7 +1,33 @@
var dns = require('dns');
module.exports = require('./gamespy3').extend({
init: function() {
this._super();
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(self.options.host.match(/\d+\.\d+\.\d+\.\d+/)) {
self.options.host = srvhost;
c();
} else {
// resolve yet again
fallback(srvhost);
}
return;
}
return fallback(host);
});
}
});