mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-27 21:24:25 -04:00
Fixed hostname resolver and added comments and errors
This commit is contained in:
parent
3a4a3ee8e8
commit
5ebbd3a493
@ -2,30 +2,59 @@ const net = require('net')
|
|||||||
const dns = require('dns')
|
const dns = require('dns')
|
||||||
|
|
||||||
module.exports = function (client, options) {
|
module.exports = function (client, options) {
|
||||||
|
|
||||||
|
// Default options
|
||||||
options.port = options.port || 25565
|
options.port = options.port || 25565
|
||||||
options.host = options.host || 'localhost'
|
options.host = options.host || 'localhost'
|
||||||
|
|
||||||
if (!options.connect) {
|
if (!options.connect) {
|
||||||
|
|
||||||
options.connect = (client) => {
|
options.connect = (client) => {
|
||||||
|
|
||||||
|
// Use stream if provided
|
||||||
if (options.stream) {
|
if (options.stream) {
|
||||||
client.setSocket(options.stream)
|
client.setSocket(options.stream)
|
||||||
client.emit('connect')
|
client.emit('connect')
|
||||||
} else if (options.port === 25565 && net.isIP(options.host) === 0 && options.host !== 'localhost') {
|
return;
|
||||||
dns.resolveSrv('_minecraft._tcp.' + options.host, function (err, addresses) {
|
}
|
||||||
|
|
||||||
|
// 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) {
|
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) {
|
if (addresses && addresses.length > 0) {
|
||||||
client.setSocket(net.connect(addresses[0].port, addresses[0].name))
|
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 {
|
} else {
|
||||||
|
// Otherwise, just connect using the provided hostname and port
|
||||||
client.setSocket(net.connect(options.port, options.host))
|
client.setSocket(net.connect(options.port, options.host))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user