-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve.go
More file actions
100 lines (84 loc) · 2.29 KB
/
resolve.go
File metadata and controls
100 lines (84 loc) · 2.29 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package mcproto
import (
"context"
"net"
"strconv"
"strings"
)
const (
defaultMinecraftPort = "25565"
minecraftSRVService = "minecraft"
minecraftSRVProtocol = "tcp"
)
func ResolveServerAddress(ctx context.Context, address string) string {
var resolver net.Resolver
// Split host & port, port being optional
host, port := splitHostPort(address)
// If no port is given, use the default Minecraft port.
if port == "" {
port = defaultMinecraftPort
}
// If no port is given or the given port is the default,
// do a DNS SRV record lookup.
if port == defaultMinecraftPort {
// Do DNS SRV record lookup on given hostname
_, srvRecords, err := resolver.LookupSRV(ctx, minecraftSRVService, minecraftSRVProtocol, host)
if err == nil && len(srvRecords) > 0 {
// Override host & port with details from the first SRV record returned
record := srvRecords[0]
host = record.Target
port = strconv.Itoa(int(record.Port))
}
}
// Join host & port for connecting to the server but also for returning
// the resolved server address.
//
// Note: If the host was resolved via an SRV record, it will have a
// trailing period. This is kept so the returned address can be used for
// a handshake packet, which the vanilla client also sends with a trailing period.
return net.JoinHostPort(host, port)
}
// Like net.SplitHostPort but with the port being optional
// Handles IPv6, IPv4 and hostnames
func splitHostPort(address string) (host string, port string) {
// Ipv6 with port
if address[0] == '[' {
endIdx := strings.LastIndexByte(address, ']')
host = address[1:endIdx]
port = address[endIdx+2:]
return
}
lastColon := -1
i := len(address)
for i--; i >= 0; i-- {
if address[i] == ':' {
if lastColon != -1 {
// We found multiple colons, so ipv6 (without port)
host = address
return
}
lastColon = i
}
if address[i] == '.' {
// We found a dot, so we've found an ipv4 address or hostname
if lastColon != -1 {
// We found a port
host = address[0:lastColon]
port = address[lastColon+1:]
} else {
// We did not find a port
host = address
}
return
}
}
// Ipv4 or hostname without port
if lastColon == -1 {
host = address
return
}
// ipv4 or hostname with port
host = address[0:lastColon]
port = address[lastColon+1:]
return
}