This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hoster.go
387 lines (368 loc) · 12.5 KB
/
hoster.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
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"net/http"
"regexp"
"strconv"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/georgysavva/scany/pgxscan"
"github.com/jackc/pgx/v4"
_ "github.com/jackc/pgx/v4/pgxpool"
mapsdatabase "github.com/maxsupermanhd/go-wz/maps-database"
)
var regexMaphash = regexp.MustCompile(`^[a-zA-Z0-9-]*$`)
func hostRequestHandlerPOST(w http.ResponseWriter, r *http.Request) {
if !hostRequestAccountPassesChecks(w, r) {
return
}
err := r.ParseForm()
if err != nil {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Failed to parse from"})
return
}
roomName := parseFormString(r, "roomName", nil)
if roomName == nil {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Invalid roomName"})
return
}
mapHash := parseFormString(r, "mapHash", regexMaphash)
if mapHash == nil {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Invalid mapHash"})
return
}
timeLimit := 90
if v := parseFormInt(r, "timeLimit"); v != nil {
timeLimit = *v
if timeLimit < 15 {
timeLimit = 15
}
if timeLimit > 60*3 {
timeLimit = 60 * 3
}
}
settingsAlliances := 2
if v := parseFormIntWhitelist(r, "settingsAlliances", 0, 1, 2, 3); v != nil {
settingsAlliances = *v
}
settingsScav := 0
if v := parseFormIntWhitelist(r, "settingsScav", 0, 1); v != nil {
settingsScav = *v
}
settingsBase := 2
if v := parseFormIntWhitelist(r, "settingsBase", 1, 2, 3); v != nil {
settingsBase = *v
}
inf, err := mapsdatabase.FetchMapInfo(*mapHash)
if err != nil {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Failed to fetch map info: " + err.Error()})
return
}
if !inf.Player.Units.Eq ||
!inf.Player.Structs.Eq ||
!inf.Player.ResourceExtr.Eq ||
!inf.Player.PwrGen.Eq ||
!inf.Player.RegFact.Eq ||
!inf.Player.VtolFact.Eq ||
!inf.Player.CyborgFact.Eq ||
!inf.Player.ResearchCent.Eq ||
!inf.Player.DefStruct.Eq {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Provided map does not meet balance requirements"})
return
}
userAdminFound := false
for _, v := range r.Form["additionalAdmin"] {
adminId, err := strconv.Atoi(v)
if err != nil {
continue
}
if sessionGetUserID(r) == adminId {
userAdminFound = true
break
}
}
if !userAdminFound {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Map requester must be an admin"})
return
}
var adminHashes []string
err = dbpool.QueryRow(r.Context(),
`select
coalesce(array_agg(encode(sha256(i.pkey), 'hex')), '{}'::text[])
from accounts as a
join identities as i on i.account = a.id
where (a.id = any($1) or a.superadmin = true) and i.pkey is not null;`, r.Form["additionalAdmin"]).Scan(&adminHashes)
if err != nil {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database query error: " + err.Error()})
return
}
ratingCategories := []int{}
switch r.Form.Get("ratingCategories") {
case "ratingNoCategories":
ratingCategories = []int{}
case "ratingRegular":
whitelistedMaps, ok := cfg.GetMapStringAny("whitelistedMaps")
if !ok {
whitelistedMaps = map[string]any{}
}
isWhitelisted := false
for _, v := range whitelistedMaps {
switch vv := v.(type) {
case map[string]any:
h, ok := vv["Hash"].(string)
if !ok {
continue
}
if h == inf.Download.Hash {
isWhitelisted = true
break
}
}
}
if !isWhitelisted {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Map is not whitelisted for rating"})
return
}
ratingCategories = []int{3}
}
toSendPreset := map[string]any{
"adminsPolicy": "whitelist",
"admins": adminHashes,
"allowNonLinkedJoin": parseFormBool(r, "allowNonRegisteredJoin"),
"allowNonLinkedPlay": parseFormBool(r, "allowNonRegisteredPlay"),
"allowNonLinkedChat": parseFormBool(r, "allowNonRegisteredChat"),
"timelimit": timeLimit,
"displayCategory": 3,
"ratingCategories": ratingCategories,
"players": inf.Slots,
"roomName": roomName,
"settingsBase": strconv.Itoa(settingsBase),
"settingsPower": "2",
"settingsAlliance": strconv.Itoa(settingsAlliances),
"settingsScavs": strconv.Itoa(settingsScav),
"maps": map[string]any{
inf.Name: map[string]any{
"hash": inf.Download.Hash,
},
},
}
spew.Dump(toSendPreset)
hosterResponse, err := RequestHosting(toSendPreset)
if err != nil {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Hoster returned error: " + err.Error()})
return
}
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Success, hoster responded: " + hosterResponse})
}
func hostRequestHandlerGET(w http.ResponseWriter, r *http.Request) {
if !hostRequestAccountPassesChecks(w, r) {
return
}
s, _ := RequestStatus()
if !s {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Autohoster backend unavaliable"})
return
}
admins := []*struct {
DisplayName string
ID int
}{}
err := pgxscan.Select(r.Context(), dbpool, &admins, `select distinct on (a.id) a.display_name, a.id
from accounts as a
join identities as i on i.account = a.id
where a.allow_host_request = true and i.pkey is not null
order by a.id`)
if err != nil {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database query error: " + err.Error()})
return
}
whitelistedMaps, ok := cfg.GetMapStringAny("whitelistedMaps")
if !ok {
whitelistedMaps = map[string]any{"not": "set"}
}
basicLayoutLookupRespond("hostrequest", w, r, map[string]any{
"Admins": admins,
"WhitelistedMaps": whitelistedMaps,
})
}
func hostRequestAccountPassesChecks(w http.ResponseWriter, r *http.Request) bool {
if !checkUserAuthorized(r) {
basicLayoutLookupRespond("noauth", w, r, map[string]any{})
return false
}
identCount := 0
err := dbpool.QueryRow(r.Context(), `select count(pkey) from identities where account = $1 and pkey is not null`, sessionGetUserID(r)).Scan(&identCount)
if err != nil {
if err == pgx.ErrNoRows {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Unauthorized?!"})
sessionManager.Destroy(r.Context())
} else {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database query error: " + err.Error()})
}
return false
}
if identCount < 1 {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "You must have at least one linked identity with known public key"})
return false
}
var allow_host_request bool
var no_request_reason string
var last_request time.Time
err = dbpool.QueryRow(r.Context(), `SELECT allow_host_request, no_request_reason, last_request FROM accounts WHERE username = $1`,
sessionGetUsername(r)).Scan(&allow_host_request, &no_request_reason, &last_request)
if err != nil {
if err == pgx.ErrNoRows {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Unauthorized?!"})
sessionManager.Destroy(r.Context())
} else {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database query error: " + err.Error()})
}
return false
}
if !allow_host_request {
basicLayoutLookupRespond("errornorequest", w, r, map[string]any{"ForbiddenReason": no_request_reason})
return false
}
if time.Since(last_request) < 5*time.Minute {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "You can only request one room every so often, please wait before opening next one"})
return false
}
return true
}
func wzlinkCheckHandler(w http.ResponseWriter, r *http.Request) {
if !checkUserAuthorized(r) {
basicLayoutLookupRespond("noauth", w, r, map[string]any{})
return
}
// blockedRegions := strings.Split(cfg.GetDString("", "requireDiscordLink", "regions"), " ")
// if stringOneOf(r.Header.Get("CF-IPCountry"), blockedRegions...) {
// basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Please contact administrator to link your profile."})
// return
// }
var confirmcode string
err := dbpool.QueryRow(r.Context(), `SELECT coalesce(wz_confirm_code, '') FROM accounts WHERE username = $1`, sessionGetUsername(r)).Scan(&confirmcode)
if err != nil {
if err == pgx.ErrNoRows {
w.Header().Set("Refresh", "1; /logout")
return
}
log.Printf("Error fetching wz_confirm_code: %s", err.Error())
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database error: " + err.Error()})
return
}
if confirmcode == "" {
confirmcode = "confirm-" + generateRandomString(18)
_, err := dbpool.Exec(r.Context(), `update accounts set wz_confirm_code = $1 where username = $2`, confirmcode, sessionGetUsername(r))
if err != nil {
log.Printf("Error updating wz_confirm_code: %s", err.Error())
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database error: " + err.Error()})
return
}
basicLayoutLookupRespond("wzlinkcheck", w, r, map[string]any{"LinkStatus": "code", "WzConfirmCode": "/hostmsg " + confirmcode})
return
}
var logname string
var logkey []byte
err = dbpool.QueryRow(context.Background(), `select name, pkey from chatlog where msg = $1 limit 1`,
"/hostmsg "+confirmcode).Scan(&logname, &logkey)
if err != nil {
if err == pgx.ErrNoRows {
basicLayoutLookupRespond("wzlinkcheck", w, r, map[string]any{"LinkStatus": "code", "WzConfirmCode": "/hostmsg " + confirmcode})
return
}
log.Printf("Error selecting chatlog: %s", err.Error())
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database error: " + err.Error()})
return
}
tag, err := dbpool.Exec(context.Background(), `
insert into identities (name, pkey, hash, account)
values ($1, $2, encode(sha256($2), 'hex'), $3)
on conflict (hash) do update set account = $3 where identities.account is null and identities.pkey = $2`,
logname, logkey, sessionGetUserID(r))
if err != nil {
log.Printf("Error inserting identity: %s", err.Error())
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database error: " + err.Error()})
return
}
if tag.Update() && tag.RowsAffected() == 0 {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Tyou attempted to link already claimed identity, this is not allowed."})
return
}
_, err = dbpool.Exec(context.Background(), `update accounts set wz_confirm_code = null, display_name = $1 where username = $2`, logname, sessionGetUsername(r))
if err != nil {
log.Printf("Error clearing confirm code: %s", err.Error())
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database error: " + err.Error()})
return
}
basicLayoutLookupRespond("wzlinkcheck", w, r, map[string]any{"LinkStatus": "done", "PlayerKey": logkey, "PlayerName": logname})
}
func wzlinkHandler(w http.ResponseWriter, r *http.Request) {
if !checkUserAuthorized(r) {
basicLayoutLookupRespond("noauth", w, r, map[string]any{})
return
}
idt := []struct {
ID int
Name string
Pkey []byte
Hash string
Account int
}{}
err := pgxscan.Select(r.Context(), dbpool, &idt, `select id, name, pkey, hash, account from identities where account = $1`, sessionGetUserID(r))
if err != nil && err != pgx.ErrNoRows {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": true, "msg": "Database error: " + err.Error()})
return
}
basicLayoutLookupRespond("wzlink", w, r, map[string]any{
"Identities": idt,
})
}
func RequestStatus() (bool, string) {
req, err := http.NewRequest("GET", cfg.GetDSString("http://localhost:9271/", "backendUrl")+"alive", nil)
if err != nil {
log.Print(err)
return false, err.Error()
}
var netClient = &http.Client{
Timeout: time.Second * 2,
}
resp, err := netClient.Do(req)
if err != nil {
log.Print(err)
return false, err.Error()
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Print(err)
return false, err.Error()
}
bodyString := string(bodyBytes) + "\n"
return true, bodyString
}
func RequestHosting(preset map[string]any) (string, error) {
reqBodyBytes, err := json.Marshal(preset)
if err != nil {
return "", err
}
reqBodyBuf := bytes.NewBuffer(reqBodyBytes)
req, err := http.NewRequest("POST", cfg.GetDSString("http://localhost:9271/", "backendUrl")+"request", reqBodyBuf)
if err != nil {
return "", err
}
var netClient = &http.Client{
Timeout: time.Second * 2,
}
resp, err := netClient.Do(req)
if err != nil {
return "", err
}
rspBodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(rspBodyBytes) + "\n", nil
}