-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatus_bedrock.go
128 lines (109 loc) · 3.13 KB
/
status_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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package mcstatus
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
type rawBedrockStatus struct {
Online bool `json:"online"`
Host string `json:"host"`
Port uint16 `json:"port"`
IPAddress string `json:"ip_address"`
EULABlocked bool `json:"eula_blocked"`
RetrievedAt int64 `json:"retrieved_at"`
ExpiresAt int64 `json:"expires_at"`
Version *struct {
Name string `json:"name"`
Protocol int `json:"protocol"`
} `json:"version"`
Players *struct {
Online int `json:"online"`
Max int `json:"max"`
} `json:"players"`
MOTD struct {
Raw string `json:"raw"`
Clean string `json:"clean"`
HTML string `json:"html"`
} `json:"motd"`
Gamemode string `json:"gamemode"`
ServerID string `json:"server_id"`
Edition string `json:"edition"`
}
type BedrockStatusResponse struct {
Online bool `json:"online"`
Host string `json:"host"`
Port uint16 `json:"port"`
IPAddress string `json:"ip_address"`
EULABlocked bool `json:"eula_blocked"`
RetrievedAt time.Time `json:"retrieved_at"`
ExpiresAt time.Time `json:"expires_at"`
Version *struct {
Name string `json:"name"`
Protocol int `json:"protocol"`
} `json:"version,omitempty"`
Players *struct {
Online int `json:"online"`
Max int `json:"max"`
} `json:"players,omitempty"`
MOTD struct {
Raw string `json:"raw"`
Clean string `json:"clean"`
HTML string `json:"html"`
} `json:"motd,omitempty"`
Gamemode string `json:"gamemode,omitempty"`
ServerID string `json:"server_id,omitempty"`
Edition string `json:"edition,omitempty"`
}
type BedrockStatusOptions struct {
Timeout float64
}
func GetBedrockStatus(host string, port uint16, options ...BedrockStatusOptions) (*BedrockStatusResponse, error) {
opts := BedrockStatusOptions{
Timeout: 5.0,
}
if len(options) > 0 {
opts = options[0]
}
params := &url.Values{}
params.Set("timeout", strconv.FormatFloat(opts.Timeout, 'f', 1, 64))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/status/bedrock/%s:%s?%s", baseURL, url.PathEscape(host), url.PathEscape(strconv.FormatUint(uint64(port), 10)), params.Encode()), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("go-mcstatus %s", version))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("mcstatus: unexpected status code: %d", resp.StatusCode)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result rawBedrockStatus
if err = json.Unmarshal(body, &result); err != nil {
return nil, err
}
return &BedrockStatusResponse{
Online: result.Online,
Host: result.Host,
Port: result.Port,
IPAddress: result.IPAddress,
EULABlocked: result.EULABlocked,
RetrievedAt: time.UnixMilli(result.RetrievedAt),
ExpiresAt: time.UnixMilli(result.ExpiresAt),
Version: result.Version,
Players: result.Players,
MOTD: result.MOTD,
Gamemode: result.Gamemode,
ServerID: result.ServerID,
Edition: result.Edition,
}, nil
}