-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbedrock.go
62 lines (56 loc) · 1.25 KB
/
bedrock.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
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"github.com/sandertv/go-raknet"
"strconv"
"strings"
"time"
)
type BedrockServerInfo struct {
ServerName string
ProtocolVersion string
Version string
Players int
MaxPlayers int
LevelName string
GameMode string
Difficulty string
Rtt time.Duration
}
func PingBedrockServer(address string, timeout time.Duration) (*BedrockServerInfo, error) {
start := time.Now()
var response []byte
var err error
if timeout > 0 {
response, err = raknet.PingTimeout(address, timeout)
} else {
response, err = raknet.Ping(address)
}
rtt := time.Now().Sub(start)
if err != nil {
return nil, fmt.Errorf("failed to query bedrock server %s: %w", address, err)
}
parts := strings.Split(string(response), ";")
info := &BedrockServerInfo{
Rtt: rtt,
ServerName: parts[1],
ProtocolVersion: parts[2],
Version: parts[3],
Players: safeParseInt(parts[4]),
MaxPlayers: safeParseInt(parts[5]),
LevelName: parts[7],
GameMode: parts[8],
}
if len(parts) >= 10 {
info.Difficulty = parts[9]
}
return info, nil
}
func safeParseInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
return -1
} else {
return i
}
}