-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
395 lines (317 loc) · 7.56 KB
/
server.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package exaroton
import (
"bytes"
"encoding/json"
"errors"
)
// Server struct to represent a Exaroton server
type Server struct {
// The server unique ID
ID string `json:"id"`
// The server name
Name string `json:"name"`
// The server full address
Address string `json:"address"`
// The server MOTD
MOTD string `json:"motd"`
// The server status is an integer as described in the documentation.
// https://developers.exaroton.com/#header-server-status
Status int64 `json:"status"`
// The server host adress, only available if the server is online
Host interface{} `json:"host"`
// The server port, only available if the server is online
Port interface{} `json:"port"`
// The server player information
Players Players `json:"players"`
// The server software
Software Software `json:"software"`
// Whether the server is shared.
Shared bool `json:"shared"`
}
// Players struct to represent player list
type Players struct {
// The server max player
Max int64 `json:"max"`
// The active player count
Count int64 `json:"count"`
// The active player list
List []interface{} `json:"list"`
}
// Software struct to represent the server software
type Software struct {
// The software unique ID
// NO USAGE (Internal Usage Only)
ID string `json:"id"`
// The software name
Name string `json:"name"`
// The software version
Version string `json:"version"`
}
// Logs struct to represent the logs content
// Gonna remove this (just realize how bad it is)
type Logs struct {
// The log content
Content string `json:"content"`
}
// ShareLogs struct to represent the mc.logs share link
type ShareLogs struct {
// The log id
ID string `json:"id"`
// The mc.logs url
URL string `json:"url"`
// The raw mc.logs url
Raw string `json:"raw"`
}
// PlayerList struct to represent the playerlist
type PlayerList struct {
// The server unique ID
ID string
// The playerlist type
// Whitelist/Ops/Banned-Players/Banned-IPs
Type string
// The list of players
List []string
}
// Get a list of all servers that the user has access to
func (s *Session) Servers() (servers []*Server, err error) {
body, err := s.Request("GET", EndpointServers, nil)
if err != nil {
return
}
err = json.Unmarshal(body, &servers)
return
}
// Get the server details as a struct
func (s *Session) Server(serverID string) (server *Server, err error) {
body, err := s.Request("GET", EndpointServer(serverID), nil)
if err != nil {
return
}
err = json.Unmarshal(body, &server)
return
}
// Get the content of the server logs
func (server *Server) GetLogs(s *Session) (logs string, err error) {
body, err := s.Request("GET", EndpointLogs(server.ID), nil)
if err != nil {
return
}
data := make(map[string]interface{})
err = json.Unmarshal(body, &data)
if err != nil {
return
}
if data["content"] == nil {
err = errors.New("Server is offline")
return
}
logs = data["content"].(string)
return
}
// Upload the content of the server logs to mclo.gs
func (server *Server) ShareLogs(s *Session) (shareLogs *ShareLogs, err error) {
body, err := s.Request("GET", EndpointShareLogs(server.ID), nil)
if err != nil {
return
}
err = json.Unmarshal(body, &shareLogs)
return
}
// Set the server MOTD
func (server *Server) SetMOTD(s *Session, motd string) (err error) {
data := struct {
Ram string `json:"motd"`
}{motd}
var tempBody []byte
tempBody, err = json.Marshal(data)
if err != nil {
return err
}
_, err = s.Request("POST", EndpointMOTD(server.ID), bytes.NewBuffer(tempBody))
if err != nil {
return err
}
return err
}
// Get the server ram
func (server *Server) GetRam(s *Session) (ram float64, err error) {
body, err := s.Request("GET", EndpointRam(server.ID), nil)
if err != nil {
return
}
data := make(map[string]interface{})
err = json.Unmarshal(body, &data)
if err != nil {
return
}
ram = data["ram"].(float64)
return
}
// Set the ram of the server
func (server *Server) SetRam(s *Session, ram float64) (err error) {
data := struct {
Ram float64 `json:"ram"`
}{ram}
var tempBody []byte
tempBody, err = json.Marshal(data)
if err != nil {
return err
}
_, err = s.Request("POST", EndpointRam(server.ID), bytes.NewBuffer(tempBody))
if err != nil {
return err
}
return err
}
// Start the server
func (server *Server) Start(s *Session) (err error) {
_, err = s.Request("GET", EndpointStart(server.ID), nil)
if err != nil {
return err
}
return err
}
// Stop the server
func (server *Server) Stop(s *Session) (err error) {
_, err = s.Request("GET", EndpointStop(server.ID), nil)
if err != nil {
return err
}
return err
}
// Restart the server
func (server *Server) Restart(s *Session) (err error) {
_, err = s.Request("GET", EndpointRestart(server.ID), nil)
if err != nil {
return err
}
return err
}
// Get the server status
func (server *Server) GetStatus() string {
/*
0 = OFFLINE
1 = ONLINE
2 = STARTING
3 = STOPPING
4 = RESTARTING
5 = SAVING
6 = LOADING
7 = CRASHED
8 = PENDING
10 = PREPARING
*/
switch server.Status {
case 0:
return "OFFLINE"
case 1:
return "ONLINE"
case 2:
return "STARTING"
case 3:
return "STOPPING"
case 4:
return "RESTARTING"
case 5:
return "SAVING"
case 6:
return "LOADING"
case 7:
return "CRASHED"
case 8:
return "PENDING"
case 10:
return "PREPARING"
}
return "UNKNOWN"
}
// Execute a command in the server console
func (server *Server) ExecuteCommand(s *Session, command string) (err error) {
data := struct {
Command string `json:"command"`
}{command}
var tempBody []byte
tempBody, err = json.Marshal(data)
if err != nil {
return err
}
_, err = s.Request("POST", EndpointCommand(server.ID), bytes.NewBuffer(tempBody))
if err != nil {
return err
}
return
}
// Get the server player list by name
func (server *Server) GetPlayerList(s *Session, types ...string) (playerList *PlayerList, err error) {
// This code can be improved
var URL string
var tempList = &PlayerList{
ID: server.ID,
}
if types == nil {
URL = EndpointGetPlayerLists(server.ID)
tempList.Type = "nil"
} else {
switch types[0] {
case "whitelist":
URL = EndpointPlayerLists(server.ID, "whitelist")
tempList.Type = "whitelist"
case "ops":
URL = EndpointPlayerLists(server.ID, "ops")
tempList.Type = "ops"
case "banned-players":
URL = EndpointPlayerLists(server.ID, "banned-players")
tempList.Type = "banned-players"
case "banned-ips":
URL = EndpointPlayerLists(server.ID, "banned-ips")
tempList.Type = "banned-ips"
default:
URL = EndpointGetPlayerLists(server.ID)
tempList.Type = "nil"
}
}
body, err := s.Request("GET", URL, nil)
if err != nil {
return
}
err = json.Unmarshal(body, &tempList.List)
if err != nil {
return
}
playerList = tempList
return
}
// Add a name to the player list
func (playerList *PlayerList) AddEntry(s *Session, entries []string) (err error) {
data := struct {
Entries []string `json:"entries"`
}{entries}
var tempBody []byte
tempBody, err = json.Marshal(data)
if err != nil {
return err
}
var URL = EndpointPlayerLists(playerList.ID, playerList.Type)
_, err = s.Request("PUT", URL, bytes.NewBuffer(tempBody))
if err != nil {
return err
}
return
}
// Remove a name from the player list
func (playerList *PlayerList) RemoveEntry(s *Session, entries []string) (err error) {
data := struct {
Entries []string `json:"entries"`
}{entries}
var tempBody []byte
tempBody, err = json.Marshal(data)
if err != nil {
return err
}
var URL = EndpointPlayerLists(playerList.ID, playerList.Type)
_, err = s.Request("DELETE", URL, bytes.NewBuffer(tempBody))
if err != nil {
return err
}
return
}