-
-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathtcp_dns.js
More file actions
54 lines (49 loc) · 1.82 KB
/
tcp_dns.js
File metadata and controls
54 lines (49 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const net = require('net')
const dns = require('dns')
const { createProxyConnect } = require('./proxy')
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
}
// Helper function to connect (direct or via proxy)
const connectToTarget = (targetHost, targetPort) => {
if (options.proxy) {
const proxyConnect = createProxyConnect(options.proxy, targetHost, targetPort)
proxyConnect(client)
} else {
client.setSocket(net.connect(targetPort, targetHost))
}
}
// 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
connectToTarget(options.host, options.port)
return
}
// SRV Lookup resolved conrrectly
if (addresses && addresses.length > 0) {
connectToTarget(addresses[0].name, addresses[0].port)
} else {
// Otherwise, just connect using the provided hostname and port
connectToTarget(options.host, options.port)
}
})
} else {
// Otherwise, just connect using the provided hostname and port
connectToTarget(options.host, options.port)
}
}
}
}