-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserveme.go
262 lines (220 loc) · 6.11 KB
/
serveme.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
package servemetf
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"time"
)
var client = &http.Client{Timeout: 10 * time.Second}
type Reservation struct {
// https://github.com/Arie/serveme/blob/f9051d618d237ca085a248fa1d29132cf5159845/app/models/reservation.rb#L223-L242
Status string `json:"status"`
StartsAt string `json:"starts_at"`
EndsAt string `json:"ends_at"`
ServerID int `json:"server_id,omitempty"`
RCON string `json:"rcon,omitempty"`
Password string `json:"password,omitempty"`
FirstMap string `json:"first_map,omitempty"`
WhitelistID int `json:"whitelist_id,omitempty"`
ID int `json:"id,omitempty"`
LogSecret string `json:"logsecret,omitempty"`
Ended bool `json:"ended"`
ZipFileURL string `json:"zipfile_url"`
Server struct {
Name string `json:"name"`
IPAndPort string `json:"ip_and_port"`
} `json:"server"`
Errors map[string]interface{} `json:"errors,omitempty"` // errors in response
}
type Server struct {
ID int `json:"id"`
Name string `json:"name"`
Flag string `json:"flag"`
IPAndPort string `json:"ip_and_port"`
}
type File struct {
ID int `json:"id"`
File string `json:"file"`
}
type Response struct {
Reservation Reservation `json:"reservation"`
Servers []Server `json:"servers"`
ServerConfigs []File `json:"server_configs"`
Whitelists []File `json:"whitelists"`
Actions map[string]string `json:"actions"`
}
type Context struct {
Host string
APIKey string
}
const (
TimeFormat = "2006-01-02T15:04:05.999-07:00"
)
var (
ErrAlreadyReserved = errors.New("serveme: you have already reserved a server")
ErrNotFound = errors.New("serveme: server not found")
)
func (c Context) newRequest(data interface{}, method, url string) *http.Request {
json, _ := json.Marshal(data)
req, _ := http.NewRequest(method, url, bytes.NewBuffer(json))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "*/*")
return req
}
// ID is reservation id
func (c Context) Status(id int, steamID string) (string, error) {
u := url.URL{
Scheme: "https",
Host: c.Host,
Path: "api/reservations/" + strconv.FormatUint(uint64(id), 10),
}
values := u.Query()
values.Set("api_key", c.APIKey)
values.Set("steam_uid", steamID)
u.RawQuery = values.Encode()
req := c.newRequest(nil, "GET", u.String())
resp, err := client.Do(req)
if err != nil {
return "", err
}
var jsonresp Response
err = json.NewDecoder(resp.Body).Decode(&jsonresp)
if err != nil {
return "", err
}
return jsonresp.Reservation.Status, nil
}
func (c Context) GetReservationTime(steamID string) (starts time.Time, ends time.Time, err error) {
u := url.URL{
Scheme: "https",
Host: c.Host,
Path: "api/reservations/new",
}
values := u.Query()
values.Set("api_key", c.APIKey)
values.Set("steam_uid", steamID)
u.RawQuery = values.Encode()
req := c.newRequest(nil, "GET", u.String())
resp, err := client.Do(req)
if err != nil {
return
}
var jsonresp Response
err = json.NewDecoder(resp.Body).Decode(&jsonresp)
if err != nil {
return
}
starts, err = time.Parse(TimeFormat, jsonresp.Reservation.StartsAt)
ends, err = time.Parse(TimeFormat, jsonresp.Reservation.EndsAt)
return
}
func (c Context) FindServers(starts, ends time.Time, steamID string) (Response, error) {
u := url.URL{
Scheme: "https",
Host: c.Host,
Path: "api/reservations/find_servers",
}
values := u.Query()
values.Set("api_key", c.APIKey)
values.Set("steam_uid", steamID)
u.RawQuery = values.Encode()
reservation := Reservation{
StartsAt: starts.Format(TimeFormat),
EndsAt: ends.Format(TimeFormat),
}
req := c.newRequest(struct {
Reservation Reservation `json:"reservation"`
}{reservation}, "POST", u.String())
resp, err := client.Do(req)
if err != nil {
return Response{}, err
}
var jsonresp Response
err = json.NewDecoder(resp.Body).Decode(&jsonresp)
return jsonresp, err
}
func (c Context) Create(reservation Reservation, steamID string) (Response, error) {
u := url.URL{
Scheme: "https",
Host: c.Host,
Path: "api/reservations",
}
values := u.Query()
values.Set("api_key", c.APIKey)
values.Set("steam_uid", steamID)
u.RawQuery = values.Encode()
var response Response
req := c.newRequest(reservation, "POST", u.String())
resp, err := client.Do(req)
if err != nil {
return response, err
}
if resp.StatusCode == 400 {
return response, ErrAlreadyReserved
} else if resp.StatusCode == 404 {
return response, ErrNotFound
}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return response, err
}
return response, nil
}
func (c Context) Delete(id int, steamID string) error {
u := url.URL{
Scheme: "https",
Host: c.Host,
Path: "api/reservations/" + strconv.Itoa(id),
}
values := u.Query()
values.Set("api_key", c.APIKey)
values.Set("steam_uid", steamID)
u.RawQuery = values.Encode()
req := c.newRequest(nil, "DELETE", u.String())
_, err := client.Do(req)
return err
}
func (c Context) Ended(id int, steamID string) (bool, error) {
u := url.URL{
Scheme: "https",
Host: c.Host,
Path: "api/reservations/" + strconv.Itoa(id),
}
values := u.Query()
values.Set("api_key", c.APIKey)
values.Set("steam_uid", steamID)
u.RawQuery = values.Encode()
req := c.newRequest(nil, "GET", u.String())
resp, err := client.Do(req)
if err != nil {
return false, err
}
var jsonresp Response
err = json.NewDecoder(resp.Body).Decode(&jsonresp)
if err != nil {
return false, err
}
return jsonresp.Reservation.Ended || jsonresp.Reservation.Status == "Ended", nil
}
func (c Context) GetZipFileURL(id int, steamID string) (string, error) {
u := url.URL{
Scheme: "https",
Host: c.Host,
Path: "api/reservations/" + strconv.Itoa(id),
}
values := u.Query()
values.Set("api_key", c.APIKey)
values.Set("steam_uid", steamID)
u.RawQuery = values.Encode()
req := c.newRequest(nil, "GET", u.String())
resp, err := client.Do(req)
if err != nil {
return "", err
}
var jsonresp Response
err = json.NewDecoder(resp.Body).Decode(&jsonresp)
return jsonresp.Reservation.ZipFileURL, err
}