-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ping_generic.go
50 lines (42 loc) · 1.54 KB
/
ping_generic.go
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
package minequery
// defaultMinecraftPort is a default port Minecraft server runs on and which
// will be used when server port is left as zero value.
const defaultMinecraftPort = 25565
// pingGeneric accepts version-specific ping function and host/port pair. Then it performs
// (if necessary, see PreferSRVRecord) SRV lookup, and attempts to use the SRV record hostname and port
// (first returned record is used if more than one is returned, see net.LookupSRV documentation)
// to ping, if lookup fails or ping fails, the provided hostname/port pair is used directly.
func (p *Pinger) pingGeneric(pingFn func(string, int) (interface{}, error), host string, port int) (interface{}, error) {
// Use default Minecraft port if port is 0
if port == 0 {
port = defaultMinecraftPort
}
if p.PreferSRVRecord {
// When SRV record is preferred, try resolving it
srvHost, srvPort, err := p.resolveSRV(host)
if err != nil {
if p.UseStrict {
// If UseStrict, SRV lookup error is fatal
return nil, err
}
// If not UseStrict, continue pinging on the desired host/port
} else {
// If SRV lookup is successful, check if there are any records
if srvHost != "" {
status, err := pingFn(srvHost, int(srvPort))
if err != nil {
// If pinging on the SRV record failed and UseStrict is set,
// this is fatal enough to raise an error
if p.UseStrict {
return nil, err
}
} else {
// Success, SRV record ping passed
return status, nil
}
}
}
}
// Otherwise just ping normally
return pingFn(host, port)
}