-
Notifications
You must be signed in to change notification settings - Fork 19
/
routes.go
480 lines (414 loc) · 11.4 KB
/
routes.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package main
import (
"context"
b64 "encoding/base64"
"encoding/json"
"io"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"golbat/config"
"golbat/decoder"
"golbat/geo"
"golbat/pogo"
)
type ProtoData struct {
Method int
Data []byte
Request []byte
HaveAr *bool
Account string
Level int
Uuid string
ScanContext string
Lat float64
Lon float64
}
type InboundRawData struct {
Base64Data string
Request string
Method int
HaveAr *bool
}
func questsHeldHasARTask(quests_held any) *bool {
const ar_quest_id = int64(pogo.QuestType_QUEST_GEOTARGETED_AR_SCAN)
quests_held_list, ok := quests_held.([]any)
if !ok {
log.Errorf("Raw: unexpected quests_held type in data: %T", quests_held)
return nil
}
for _, quest_id := range quests_held_list {
if quest_id_f, ok := quest_id.(float64); ok {
if int64(quest_id_f) == ar_quest_id {
res := true
return &res
}
continue
}
// quest_id is not float64? Treat the whole thing as unknown.
log.Errorf("Raw: unexpected quest_id type in quests_held: %T", quest_id)
return nil
}
res := false
return &res
}
func Raw(c *gin.Context) {
var w http.ResponseWriter = c.Writer
var r *http.Request = c.Request
authHeader := r.Header.Get("Authorization")
if config.Config.RawBearer != "" {
if authHeader != "Bearer "+config.Config.RawBearer {
statsCollector.IncRawRequests("error", "auth")
log.Errorf("Raw: Incorrect authorisation received (%s)", authHeader)
return
}
}
body, err := io.ReadAll(io.LimitReader(r.Body, 5*1048576))
if err != nil {
statsCollector.IncRawRequests("error", "io_error")
log.Errorf("Raw: Error (1) during HTTP receive %s", err)
return
}
if err := r.Body.Close(); err != nil {
statsCollector.IncRawRequests("error", "io_close_error")
log.Errorf("Raw: Error (2) during HTTP receive %s", err)
return
}
decodeError := false
uuid := ""
account := ""
level := 30
scanContext := ""
var latTarget, lonTarget float64
var globalHaveAr *bool
var protoData []InboundRawData
// Objective is to normalise incoming proto data. Unfortunately each provider seems
// to be just different enough that this ends up being a little bit more of a mess
// than I would like
pogodroidHeader := r.Header.Get("origin")
userAgent := r.Header.Get("User-Agent")
//log.Infof("Raw: Received data from %s", body)
//log.Infof("User agent is %s", userAgent)
if pogodroidHeader != "" {
var raw []map[string]interface{}
if err := json.Unmarshal(body, &raw); err != nil {
decodeError = true
} else {
for _, entry := range raw {
if latTarget == 0 && lonTarget == 0 {
lat := entry["lat"]
lng := entry["lng"]
if lat != nil && lng != nil {
lat_f, _ := lat.(float64)
lng_f, _ := lng.(float64)
if lat_f != 0 && lng_f != 0 {
latTarget = lat_f
lonTarget = lng_f
}
}
}
protoData = append(protoData, InboundRawData{
Base64Data: entry["payload"].(string),
Method: int(entry["type"].(float64)),
HaveAr: func() *bool {
if v := entry["quests_held"]; v != nil {
return questsHeldHasARTask(v)
}
return nil
}(),
})
}
}
uuid = pogodroidHeader
account = "Pogodroid"
} else {
var raw map[string]interface{}
if err := json.Unmarshal(body, &raw); err != nil {
decodeError = true
} else {
if v := raw["have_ar"]; v != nil {
res, ok := v.(bool)
if ok {
globalHaveAr = &res
}
}
if v := raw["uuid"]; v != nil {
uuid, _ = v.(string)
}
if v := raw["username"]; v != nil {
account, _ = v.(string)
}
if v := raw["trainerlvl"]; v != nil {
lvl, ok := v.(float64)
if ok {
level = int(lvl)
}
}
if v := raw["scan_context"]; v != nil {
scanContext, _ = v.(string)
}
if v := raw["lat_target"]; v != nil {
latTarget, _ = v.(float64)
}
if v := raw["lon_target"]; v != nil {
lonTarget, _ = v.(float64)
}
contents, ok := raw["contents"].([]interface{})
if !ok {
decodeError = true
} else {
decodeAlternate := func(data map[string]interface{}, key1, key2 string) interface{} {
if v := data[key1]; v != nil {
return v
}
if v := data[key2]; v != nil {
return v
}
return nil
}
for _, v := range contents {
entry := v.(map[string]interface{})
// Try to decode the payload automatically without requiring any knowledge of the
// provider type
b64data := decodeAlternate(entry, "data", "payload")
method := decodeAlternate(entry, "method", "type")
request := entry["request"]
haveAr := entry["have_ar"]
if method == nil || b64data == nil {
log.Errorf("Error decoding raw")
continue
}
inboundRawData := InboundRawData{
Base64Data: func() string {
if res, ok := b64data.(string); ok {
return res
}
return ""
}(),
Method: func() int {
if res, ok := method.(float64); ok {
return int(res)
}
return 0
}(),
Request: func() string {
if request != nil {
res, ok := request.(string)
if ok {
return res
}
}
return ""
}(),
HaveAr: func() *bool {
if haveAr != nil {
res, ok := haveAr.(bool)
if ok {
return &res
}
}
return nil
}(),
}
protoData = append(protoData, inboundRawData)
}
}
}
}
if decodeError == true {
statsCollector.IncRawRequests("error", "decode")
log.Infof("Raw: Data could not be decoded. From User agent %s - Received data %s", userAgent, body)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
// Process each proto in a packet in sequence, but in a go-routine
go func() {
timeout := 5 * time.Second
if config.Config.Tuning.ExtendedTimeout {
timeout = 30 * time.Second
}
for _, entry := range protoData {
method := entry.Method
payload := entry.Base64Data
request := entry.Request
haveAr := globalHaveAr
if entry.HaveAr != nil {
haveAr = entry.HaveAr
}
protoData := ProtoData{
Account: account,
Level: level,
HaveAr: haveAr,
Uuid: uuid,
Lat: latTarget,
Lon: lonTarget,
ScanContext: scanContext,
}
protoData.Data, _ = b64.StdEncoding.DecodeString(payload)
if request != "" {
protoData.Request, _ = b64.StdEncoding.DecodeString(request)
}
// provide independent cancellation contexts for each proto decode
ctx, cancel := context.WithTimeout(context.Background(), timeout)
decode(ctx, method, &protoData)
cancel()
}
}()
if latTarget != 0 && lonTarget != 0 && uuid != "" {
UpdateDeviceLocation(uuid, latTarget, lonTarget, scanContext)
}
statsCollector.IncRawRequests("ok", "")
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
//if err := json.NewEncoder(w).Encode(t); err != nil {
// panic(err)
//}
}
func AuthRequired() gin.HandlerFunc {
return func(context *gin.Context) {
if config.Config.ApiSecret != "" {
authHeader := context.Request.Header.Get("X-Golbat-Secret")
if authHeader != config.Config.ApiSecret {
log.Errorf("Incorrect authorisation received (%s)", authHeader)
context.String(http.StatusUnauthorized, "Unauthorised")
context.Abort()
return
}
}
context.Next()
}
}
func ClearQuests(c *gin.Context) {
fence, err := geo.NormaliseFenceRequest(c)
if err != nil {
log.Warnf("POST /api/clear-quests/ Error during post area %v", err)
c.Status(http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
log.Debugf("Clear quests %+v", fence)
startTime := time.Now()
decoder.ClearQuestsWithinGeofence(ctx, dbDetails, fence)
log.Infof("Clear quest took %s", time.Since(startTime))
c.JSON(http.StatusAccepted, map[string]interface{}{
"status": "ok",
})
}
func ReloadGeojson(c *gin.Context) {
decoder.ReloadGeofenceAndClearStats()
c.JSON(http.StatusAccepted, map[string]interface{}{
"status": "ok",
})
}
func PokemonScan(c *gin.Context) {
var requestBody decoder.ApiPokemonScan
if err := c.BindJSON(&requestBody); err != nil {
log.Warnf("POST /api/pokemon/scan/ Error during post retrieve %v", err)
c.Status(http.StatusInternalServerError)
return
}
res := decoder.GetPokemonInArea(requestBody)
if res == nil {
c.Status(http.StatusInternalServerError)
return
}
c.JSON(http.StatusAccepted, res)
}
func PokemonScan2(c *gin.Context) {
var requestBody decoder.ApiPokemonScan2
if err := c.BindJSON(&requestBody); err != nil {
log.Warnf("POST /api/pokemon/scan/ Error during post retrieve %v", err)
c.Status(http.StatusInternalServerError)
return
}
res := decoder.GetPokemonInArea2(requestBody)
if res == nil {
c.Status(http.StatusInternalServerError)
return
}
c.JSON(http.StatusAccepted, res)
}
func PokemonOne(c *gin.Context) {
pokemonId, err := strconv.ParseUint(c.Param("pokemon_id"), 10, 64)
if err != nil {
log.Warnf("GET /api/pokemon/:pokemon_id/ Error during get pokemon %v", err)
c.Status(http.StatusInternalServerError)
return
}
res := decoder.GetOnePokemon(uint64(pokemonId))
if res != nil {
c.JSON(http.StatusAccepted, map[string]interface{}{
"lat": res.Lat,
"lon": res.Lon,
})
} else {
c.Status(http.StatusNotFound)
}
}
func PokemonAvailable(c *gin.Context) {
res := decoder.GetAvailablePokemon()
c.JSON(http.StatusAccepted, res)
}
func PokemonSearch(c *gin.Context) {
var requestBody decoder.ApiPokemonSearch
if err := c.BindJSON(&requestBody); err != nil {
log.Warnf("POST /api/search/ Error during post search %v", err)
c.Status(http.StatusInternalServerError)
return
}
res, err := decoder.SearchPokemon(requestBody)
if err != nil {
log.Warnf("POST /api/search/ Error during post search %v", err)
c.Status(http.StatusBadRequest)
return
}
c.JSON(http.StatusAccepted, res)
}
func GetQuestStatus(c *gin.Context) {
fence, err := geo.NormaliseFenceRequest(c)
if err != nil {
log.Warnf("POST /api/quest-status/ Error during post area %v", err)
c.Status(http.StatusInternalServerError)
return
}
questStatus := decoder.GetQuestStatusWithGeofence(dbDetails, fence)
c.JSON(http.StatusOK, &questStatus)
}
// GetHealth provides unrestricted health status for monitoring tools
func GetHealth(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
func GetPokestopPositions(c *gin.Context) {
fence, err := geo.NormaliseFenceRequest(c)
if err != nil {
log.Warnf("POST /api/pokestop-positions/ Error during post area %v %v", err, fence)
c.Status(http.StatusInternalServerError)
return
}
response, err := decoder.GetPokestopPositions(dbDetails, fence)
if err != nil {
log.Warnf("POST /api/pokestop-positions/ Error during post retrieve %v", err)
c.Status(http.StatusInternalServerError)
return
}
c.JSON(http.StatusAccepted, response)
}
func GetPokestop(c *gin.Context) {
fortId := c.Param("fort_id")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
pokestop, err := decoder.GetPokestopRecord(ctx, dbDetails, fortId)
cancel()
if err != nil {
log.Warnf("GET /api/pokestop/id/:fort_id/ Error during post retrieve %v", err)
c.Status(http.StatusInternalServerError)
return
}
c.JSON(http.StatusAccepted, pokestop)
}
func GetDevices(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"devices": GetAllDevices()})
}