From 3a4a3ee8e8761c69deca97f0a6bb7c34ccde3ebd Mon Sep 17 00:00:00 2001 From: lluiscab Date: Thu, 23 Aug 2018 17:36:03 +0200 Subject: [PATCH 1/3] Fix: Join specified server if SRV records could not be resolved --- src/client/tcp_dns.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/client/tcp_dns.js b/src/client/tcp_dns.js index 9b615d4..bf8ac5c 100644 --- a/src/client/tcp_dns.js +++ b/src/client/tcp_dns.js @@ -13,13 +13,15 @@ module.exports = function (client, options) { } else if (options.port === 25565 && net.isIP(options.host) === 0 && options.host !== 'localhost') { dns.resolveSrv('_minecraft._tcp.' + options.host, function (err, addresses) { if (err) { - console.log(err) + client.setSocket(net.connect(options.port, options.host)) + return; } if (addresses && addresses.length > 0) { client.setSocket(net.connect(addresses[0].port, addresses[0].name)) - } else { - client.setSocket(net.connect(options.port, options.host)) + return; } + client.emit('error', 'Could not resolve server address'); + }) } else { client.setSocket(net.connect(options.port, options.host)) From 5ebbd3a493069036d93b026a4d319431b99121f0 Mon Sep 17 00:00:00 2001 From: lluiscab Date: Fri, 24 Aug 2018 10:00:47 +0200 Subject: [PATCH 2/3] Fixed hostname resolver and added comments and errors --- src/client/tcp_dns.js | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/client/tcp_dns.js b/src/client/tcp_dns.js index bf8ac5c..c62a66e 100644 --- a/src/client/tcp_dns.js +++ b/src/client/tcp_dns.js @@ -2,30 +2,59 @@ const net = require('net') const dns = require('dns') module.exports = function (client, options) { + + // Default options options.port = options.port || 25565 options.host = options.host || 'localhost' if (!options.connect) { + options.connect = (client) => { + + // Use stream if provided if (options.stream) { client.setSocket(options.stream) client.emit('connect') - } else if (options.port === 25565 && net.isIP(options.host) === 0 && options.host !== 'localhost') { - dns.resolveSrv('_minecraft._tcp.' + options.host, function (err, addresses) { + return; + } + + // If port was not defined (defauls to 25565), host is not an ip neither localhost + if (options.port === 25565 && net.isIP(options.host) === 0 && options.host !== 'localhost') { + + // Try to resolve SRV records for the comain + dns.resolveSrv('_minecraft._tcp.' + options.host, (err, addresses) => { + + // Error resolving domain if (err) { - client.setSocket(net.connect(options.port, options.host)) - return; + + // Could not resolve SRV lookup, connect directly + if(err.code === 'ENODATA') { + client.setSocket(net.connect(options.port, options.host)) + return; + } else { + // Something else happened + return client.emit('error', err) + } + } + + // SRV Lookup resolved conrrectly if (addresses && addresses.length > 0) { client.setSocket(net.connect(addresses[0].port, addresses[0].name)) - return; + } else { + client.emit('error', new Error("Could not resolve hostname")); } - client.emit('error', 'Could not resolve server address'); }) + } else { + // Otherwise, just connect using the provided hostname and port client.setSocket(net.connect(options.port, options.host)) } + + } + } + } From 648cabfa9a8a05c803c6c61cd4293058279adc59 Mon Sep 17 00:00:00 2001 From: lluiscab Date: Fri, 24 Aug 2018 10:03:57 +0200 Subject: [PATCH 3/3] Fixed formatting --- src/client/tcp_dns.js | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/client/tcp_dns.js b/src/client/tcp_dns.js index c62a66e..0ae3bbe 100644 --- a/src/client/tcp_dns.js +++ b/src/client/tcp_dns.js @@ -2,59 +2,46 @@ const net = require('net') const dns = require('dns') module.exports = function (client, options) { - // Default options options.port = options.port || 25565 options.host = options.host || 'localhost' if (!options.connect) { - options.connect = (client) => { - // Use stream if provided if (options.stream) { client.setSocket(options.stream) client.emit('connect') - return; - } - + return + } + // If port was not defined (defauls to 25565), host is not an ip neither localhost if (options.port === 25565 && net.isIP(options.host) === 0 && options.host !== 'localhost') { - // Try to resolve SRV records for the comain dns.resolveSrv('_minecraft._tcp.' + options.host, (err, addresses) => { - // Error resolving domain if (err) { - // Could not resolve SRV lookup, connect directly - if(err.code === 'ENODATA') { - client.setSocket(net.connect(options.port, options.host)) - return; + if (err.code === 'ENODATA') { + client.setSocket(net.connect(options.port, options.host)) + return } else { - // Something else happened - return client.emit('error', err) + // Something else happened + return client.emit('error', err) } - } - + // SRV Lookup resolved conrrectly if (addresses && addresses.length > 0) { client.setSocket(net.connect(addresses[0].port, addresses[0].name)) } else { - client.emit('error', new Error("Could not resolve hostname")); + client.emit('error', new Error('Could not resolve hostname')) } - }) - } else { // Otherwise, just connect using the provided hostname and port client.setSocket(net.connect(options.port, options.host)) } - - } - } - }