From c4f66a72dbec5d7001dbc7f5ff7992ba12d85b90 Mon Sep 17 00:00:00 2001 From: Michael Morrison Date: Wed, 10 Jul 2013 09:52:47 -0500 Subject: [PATCH] Resolve SRV records for minecraft servers --- protocols/core.js | 35 ++++++++++++++++++++++++++--------- protocols/minecraft.js | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/protocols/core.js b/protocols/core.js index 90d250b..467fc5d 100644 --- a/protocols/core.js +++ b/protocols/core.js @@ -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); diff --git a/protocols/minecraft.js b/protocols/minecraft.js index cfc701b..aa52d9f 100644 --- a/protocols/minecraft.js +++ b/protocols/minecraft.js @@ -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); + }); } });