forked from UnownHash/Golbat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
519 lines (428 loc) · 16 KB
/
main.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package main
import (
"context"
"fmt"
"github.com/golang-migrate/migrate/v4"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
ginlogrus "github.com/toorop/gin-logrus"
"golbat/config"
db2 "golbat/db"
"golbat/decoder"
"golbat/webhooks"
"google.golang.org/protobuf/proto"
"io/ioutil"
"time"
_ "time/tzdata"
"github.com/gin-gonic/gin"
"github.com/go-sql-driver/mysql"
_ "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/mattn/go-sqlite3"
"golbat/pogo"
)
var db *sqlx.DB
var inMemoryDb *sqlx.DB
var dbDetails db2.DbDetails
func main() {
config.ReadConfig()
logLevel := log.InfoLevel
if config.Config.Logging.Debug == true {
logLevel = log.DebugLevel
}
SetupLogger(logLevel, config.Config.Logging.SaveLogs)
log.Infof("Golbat starting")
// Capture connection properties.
cfg := mysql.Config{
User: config.Config.Database.User, //"root", //os.Getenv("DBUSER"),
Passwd: config.Config.Database.Password, //"transmit", //os.Getenv("DBPASS"),
Net: "tcp",
Addr: config.Config.Database.Addr,
DBName: config.Config.Database.Db,
AllowNativePasswords: true,
}
dbConnectionString := cfg.FormatDSN()
driver := "mysql"
log.Infof("Starting migration")
m, err := migrate.New(
"file://sql",
driver+"://"+dbConnectionString+"&multiStatements=true")
if err != nil {
log.Fatal(err)
return
}
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
log.Fatal(err)
return
}
log.Infof("Opening database for processing")
// Get a database handle.
db, err = sqlx.Open(driver, dbConnectionString)
if err != nil {
log.Fatal(err)
return
}
db.SetMaxOpenConns(50)
db.SetMaxIdleConns(10)
db.SetConnMaxIdleTime(time.Minute)
pingErr := db.Ping()
if pingErr != nil {
log.Fatal(pingErr)
return
}
log.Infoln("Connected to database")
if config.Config.InMemory {
//sql.Register("sqlite3_settings",
// &sqlite3.SQLiteDriver{
// ConnectHook: func(conn *sqlite3.SQLiteConn) error {
// conn.SetLimit(sqlite3.SQLITE_LIMIT_EXPR_DEPTH, 50000)
// return nil
// },
// })
// Initialise in memory db
inMemoryDb, err = sqlx.Open("sqlite3", ":memory:")
if err != nil {
log.Fatal(err)
return
}
inMemoryDb.SetMaxOpenConns(1)
pingErr = inMemoryDb.Ping()
if pingErr != nil {
log.Fatal(pingErr)
return
}
// Create database
content, fileErr := ioutil.ReadFile("sql/sqlite/create.sql")
if fileErr != nil {
log.Fatal(err)
}
inMemoryDb.MustExec(string(content))
dbDetails = db2.DbDetails{
PokemonDb: inMemoryDb,
UsePokemonCache: false,
GeneralDb: db,
}
} else {
dbDetails = db2.DbDetails{
PokemonDb: db,
UsePokemonCache: true,
GeneralDb: db,
}
}
decoder.InitialiseOhbem()
decoder.LoadStatsGeofences()
decoder.LoadNests(dbDetails)
log.Infoln("Golbat started")
webhooks.StartSender()
StartDbUsageStatsLogger(db)
decoder.StartStatsWriter(db)
if config.Config.InMemory {
StartInMemoryCleardown(inMemoryDb)
} else {
if config.Config.Cleanup.Pokemon == true {
StartDatabaseArchiver(db)
}
}
if config.Config.Cleanup.Incidents == true {
StartIncidentExpiry(db)
}
if config.Config.Cleanup.Quests == true {
StartQuestExpiry(db)
}
if config.Config.Cleanup.Stats == true {
StartStatsExpiry(db)
}
gin.SetMode(gin.ReleaseMode)
r := gin.New()
if config.Config.Logging.Debug {
r.Use(ginlogrus.Logger(log.StandardLogger()))
} else {
r.Use(gin.Recovery())
}
r.POST("/raw", Raw)
r.POST("/api/clearQuests", ClearQuests)
r.POST("/api/questStatus", GetQuestStatus)
r.POST("/api/reloadGeojson", ReloadGeojson)
r.GET("/api/reloadGeojson", ReloadGeojson)
r.POST("/api/queryPokemon", QueryPokemon)
r.POST("/api/reload-nests", ReloadNests)
r.GET("/api/reload-nests", ReloadNests)
//router := mux.NewRouter().StrictSlash(true)
//router.HandleFunc("/raw", Raw)
addr := fmt.Sprintf(":%d", config.Config.Port)
//log.Fatal(http.ListenAndServe(addr, router)) // addr is in form :9001
err = r.Run(addr)
if err != nil {
log.Fatal(err)
}
}
func decode(ctx context.Context, method int, protoData *ProtoData) {
if method != int(pogo.ClientAction_CLIENT_ACTION_PROXY_SOCIAL_ACTION) && protoData.Level < 30 {
log.Debugf("Insufficient Level %d Did not process hook type %s", protoData.Level, pogo.Method(method))
return
}
processed := false
start := time.Now()
result := ""
switch pogo.Method(method) {
case pogo.Method_METHOD_FORT_DETAILS:
result = decodeFortDetails(ctx, protoData.Data)
processed = true
case pogo.Method_METHOD_GET_MAP_OBJECTS:
result = decodeGMO(ctx, protoData.Data, protoData.Account)
processed = true
case pogo.Method_METHOD_GYM_GET_INFO:
result = decodeGetGymInfo(ctx, protoData.Data)
processed = true
case pogo.Method_METHOD_ENCOUNTER:
result = decodeEncounter(ctx, protoData.Data, protoData.Account)
processed = true
case pogo.Method_METHOD_DISK_ENCOUNTER:
result = decodeDiskEncounter(ctx, protoData.Data)
processed = true
case pogo.Method_METHOD_FORT_SEARCH:
result = decodeQuest(ctx, protoData.Data, protoData.HaveAr)
processed = true
case pogo.Method_METHOD_GET_PLAYER:
break
case pogo.Method_METHOD_GET_HOLOHOLO_INVENTORY:
break
case pogo.Method_METHOD_CREATE_COMBAT_CHALLENGE:
// ignore
break
case pogo.Method(pogo.ClientAction_CLIENT_ACTION_PROXY_SOCIAL_ACTION):
if protoData.Request != nil {
result = decodeSocialActionWithRequest(protoData.Request, protoData.Data)
processed = true
}
break
case pogo.Method_METHOD_GET_MAP_FORTS:
result = decodeGetMapForts(ctx, protoData.Data)
processed = true
default:
log.Debugf("Did not process hook type %s", pogo.Method(method))
}
if processed == true {
elapsed := time.Since(start)
log.Debugf("%s/%s %s - %s - %s", protoData.Uuid, protoData.Account, pogo.Method(method), elapsed, result)
}
}
func decodeQuest(ctx context.Context, sDec []byte, haveAr *bool) string {
if haveAr == nil {
log.Infoln("Cannot determine AR quest - ignoring")
// We should either assume AR quest, or trace inventory like RDM probably
return "No AR quest info"
}
decodedQuest := &pogo.FortSearchOutProto{}
if err := proto.Unmarshal(sDec, decodedQuest); err != nil {
log.Fatalln("Failed to parse", err)
return "Parse failure"
}
if decodedQuest.Result != pogo.FortSearchOutProto_SUCCESS {
res := fmt.Sprintf(`GymGetInfoOutProto: Ignored non-success value %d:%s`, decodedQuest.Result,
pogo.FortSearchOutProto_Result_name[int32(decodedQuest.Result)])
return res
}
return decoder.UpdatePokestopWithQuest(ctx, dbDetails, decodedQuest, *haveAr)
}
func decodeSocialActionWithRequest(request []byte, payload []byte) string {
var proxyRequestProto pogo.ProxyRequestProto
if err := proto.Unmarshal(request, &proxyRequestProto); err != nil {
log.Fatalln("Failed to parse %s", err)
return fmt.Sprintf("Failed to parse %s", err)
}
var proxyResponseProto pogo.ProxyResponseProto
if err := proto.Unmarshal(payload, &proxyResponseProto); err != nil {
log.Fatalln("Failed to parse %s", err)
return fmt.Sprintf("Failed to parse %s", err)
}
if proxyResponseProto.Status != pogo.ProxyResponseProto_COMPLETED && proxyResponseProto.Status != pogo.ProxyResponseProto_COMPLETED_AND_REASSIGNED {
return fmt.Sprintf("unsuccessful proxyResponseProto response %d %s", int(proxyResponseProto.Status), proxyResponseProto.Status)
}
switch pogo.SocialAction(proxyRequestProto.GetAction()) {
case pogo.SocialAction_SOCIAL_ACTION_LIST_FRIEND_STATUS:
return decodeGetFriendDetails(proxyResponseProto.Payload)
case pogo.SocialAction_SOCIAL_ACTION_SEARCH_PLAYER:
return decodeSearchPlayer(proxyRequestProto, proxyResponseProto.Payload)
}
return fmt.Sprintf("Did not process %s", pogo.SocialAction(proxyRequestProto.GetAction()).String())
}
func decodeGetFriendDetails(payload []byte) string {
var getFriendDetailsOutProto pogo.GetFriendDetailsOutProto
getFriendDetailsError := proto.Unmarshal(payload, &getFriendDetailsOutProto)
if getFriendDetailsError != nil {
log.Fatalln("Failed to parse %s", getFriendDetailsError)
return fmt.Sprintf("Failed to parse %s", getFriendDetailsError)
}
if getFriendDetailsOutProto.GetResult() != pogo.GetFriendDetailsOutProto_SUCCESS || getFriendDetailsOutProto.GetFriend() == nil {
return fmt.Sprintf("unsuccessful get friends details")
}
failures := 0
for _, friend := range getFriendDetailsOutProto.GetFriend() {
player := friend.GetPlayer()
publicData, publicDataErr := decodePlayerPublicProfile(player.GetPublicData())
if publicDataErr != nil {
failures++
continue
}
updatePlayerError := decoder.UpdatePlayerRecordWithPlayerSummary(dbDetails, player, publicData, "", player.GetPlayerId())
if updatePlayerError != nil {
failures++
}
}
return fmt.Sprintf("%d players decoded on %d", len(getFriendDetailsOutProto.GetFriend())-failures, len(getFriendDetailsOutProto.GetFriend()))
}
func decodeSearchPlayer(proxyRequestProto pogo.ProxyRequestProto, payload []byte) string {
var searchPlayerOutProto pogo.SearchPlayerOutProto
searchPlayerOutError := proto.Unmarshal(payload, &searchPlayerOutProto)
if searchPlayerOutError != nil {
log.Fatalln("Failed to parse %s", searchPlayerOutError)
return fmt.Sprintf("Failed to parse %s", searchPlayerOutError)
}
if searchPlayerOutProto.GetResult() != pogo.SearchPlayerOutProto_SUCCESS || searchPlayerOutProto.GetPlayer() == nil {
return fmt.Sprintf("unsuccessful search player response")
}
var searchPlayerProto pogo.SearchPlayerProto
searchPlayerError := proto.Unmarshal(proxyRequestProto.GetPayload(), &searchPlayerProto)
if searchPlayerError != nil || searchPlayerProto.GetFriendCode() == "" {
return fmt.Sprintf("Failed to parse %s", searchPlayerError)
}
player := searchPlayerOutProto.GetPlayer()
publicData, publicDataError := decodePlayerPublicProfile(player.GetPublicData())
if publicDataError != nil {
return fmt.Sprintf("Failed to parse %s", publicDataError)
}
updatePlayerError := decoder.UpdatePlayerRecordWithPlayerSummary(dbDetails, player, publicData, searchPlayerProto.GetFriendCode(), "")
if updatePlayerError != nil {
return fmt.Sprintf("Failed update player %s", updatePlayerError)
}
return fmt.Sprintf("1 player decoded from SearchPlayerProto")
}
func decodePlayerPublicProfile(publicProfile []byte) (*pogo.PlayerPublicProfileProto, error) {
var publicData pogo.PlayerPublicProfileProto
publicDataErr := proto.Unmarshal(publicProfile, &publicData)
return &publicData, publicDataErr
}
func decodeFortDetails(ctx context.Context, sDec []byte) string {
decodedFort := &pogo.FortDetailsOutProto{}
if err := proto.Unmarshal(sDec, decodedFort); err != nil {
log.Fatalln("Failed to parse", err)
return fmt.Sprintf("Failed to parse %s", err)
}
switch decodedFort.FortType {
case pogo.FortType_CHECKPOINT:
return decoder.UpdatePokestopRecordWithFortDetailsOutProto(ctx, dbDetails, decodedFort)
case pogo.FortType_GYM:
return decoder.UpdateGymRecordWithFortDetailsOutProto(ctx, dbDetails, decodedFort)
}
return "Unknown fort type"
}
func decodeGetMapForts(ctx context.Context, sDec []byte) string {
decodedMapForts := &pogo.GetMapFortsOutProto{}
if err := proto.Unmarshal(sDec, decodedMapForts); err != nil {
log.Fatalln("Failed to parse", err)
return fmt.Sprintf("Failed to parse %s", err)
}
if decodedMapForts.Status != pogo.GetMapFortsOutProto_SUCCESS {
res := fmt.Sprintf(`GetMapFortsOutProto: Ignored non-success value %d:%s`, decodedMapForts.Status,
pogo.GetMapFortsOutProto_Status_name[int32(decodedMapForts.Status)])
return res
}
var outputString string
processedForts := 0
for _, fort := range decodedMapForts.Fort {
status, output := decoder.UpdateFortRecordWithGetMapFortsOutProto(ctx, dbDetails, fort)
if status {
processedForts += 1
outputString += output + ", "
}
}
if processedForts > 0 {
return fmt.Sprintf("Updated %d forts: %s", processedForts, outputString)
}
return "No forts updated"
}
func decodeGetGymInfo(ctx context.Context, sDec []byte) string {
decodedGymInfo := &pogo.GymGetInfoOutProto{}
if err := proto.Unmarshal(sDec, decodedGymInfo); err != nil {
log.Fatalln("Failed to parse", err)
return fmt.Sprintf("Failed to parse %s", err)
}
if decodedGymInfo.Result != pogo.GymGetInfoOutProto_SUCCESS {
res := fmt.Sprintf(`GymGetInfoOutProto: Ignored non-success value %d:%s`, decodedGymInfo.Result,
pogo.GymGetInfoOutProto_Result_name[int32(decodedGymInfo.Result)])
return res
}
return decoder.UpdateGymRecordWithGymInfoProto(ctx, dbDetails, decodedGymInfo)
}
func decodeEncounter(ctx context.Context, sDec []byte, username string) string {
decodedEncounterInfo := &pogo.EncounterOutProto{}
if err := proto.Unmarshal(sDec, decodedEncounterInfo); err != nil {
log.Fatalln("Failed to parse", err)
return fmt.Sprintf("Failed to parse %s", err)
}
if decodedEncounterInfo.Status != pogo.EncounterOutProto_ENCOUNTER_SUCCESS {
res := fmt.Sprintf(`GymGetInfoOutProto: Ignored non-success value %d:%s`, decodedEncounterInfo.Status,
pogo.EncounterOutProto_Status_name[int32(decodedEncounterInfo.Status)])
return res
}
return decoder.UpdatePokemonRecordWithEncounterProto(ctx, dbDetails, decodedEncounterInfo, username)
}
func decodeDiskEncounter(ctx context.Context, sDec []byte) string {
decodedEncounterInfo := &pogo.DiskEncounterOutProto{}
if err := proto.Unmarshal(sDec, decodedEncounterInfo); err != nil {
log.Fatalln("Failed to parse", err)
return fmt.Sprintf("Failed to parse %s", err)
}
if decodedEncounterInfo.Result != pogo.DiskEncounterOutProto_SUCCESS {
res := fmt.Sprintf(`DiskEncounterOutProto: Ignored non-success value %d:%s`, decodedEncounterInfo.Result,
pogo.DiskEncounterOutProto_Result_name[int32(decodedEncounterInfo.Result)])
return res
}
return decoder.UpdatePokemonRecordWithDiskEncounterProto(ctx, dbDetails, decodedEncounterInfo)
}
func decodeGMO(ctx context.Context, sDec []byte, username string) string {
decodedGmo := &pogo.GetMapObjectsOutProto{}
if err := proto.Unmarshal(sDec, decodedGmo); err != nil {
log.Fatalln("Failed to parse", err)
}
if decodedGmo.Status != pogo.GetMapObjectsOutProto_SUCCESS {
res := fmt.Sprintf(`GetMapObjectsOutProto: Ignored non-success value %d:%s`, decodedGmo.Status,
pogo.GetMapObjectsOutProto_Status_name[int32(decodedGmo.Status)])
return res
}
var newForts []decoder.RawFortData
var newWildPokemon []decoder.RawWildPokemonData
var newNearbyPokemon []decoder.RawNearbyPokemonData
var newMapPokemon []decoder.RawMapPokemonData
var newClientWeather []decoder.RawClientWeatherData
var newMapCells []uint64
for _, mapCell := range decodedGmo.MapCell {
timestampMs := uint64(mapCell.AsOfTimeMs)
for _, fort := range mapCell.Fort {
newForts = append(newForts, decoder.RawFortData{Cell: mapCell.S2CellId, Data: fort})
if fort.ActivePokemon != nil {
newMapPokemon = append(newMapPokemon, decoder.RawMapPokemonData{Cell: mapCell.S2CellId, Data: fort.ActivePokemon})
}
}
newMapCells = append(newMapCells, mapCell.S2CellId)
for _, mon := range mapCell.WildPokemon {
newWildPokemon = append(newWildPokemon, decoder.RawWildPokemonData{Cell: mapCell.S2CellId, Data: mon, Timestamp: timestampMs})
}
for _, mon := range mapCell.NearbyPokemon {
newNearbyPokemon = append(newNearbyPokemon, decoder.RawNearbyPokemonData{Cell: mapCell.S2CellId, Data: mon})
}
}
for _, clientWeather := range decodedGmo.ClientWeather {
newClientWeather = append(newClientWeather, decoder.RawClientWeatherData{Cell: clientWeather.S2CellId, Data: clientWeather})
}
decoder.UpdateFortBatch(ctx, dbDetails, newForts)
decoder.UpdatePokemonBatch(ctx, dbDetails, newWildPokemon, newNearbyPokemon, newMapPokemon, username)
decoder.UpdateClientWeatherBatch(ctx, dbDetails, newClientWeather)
decoder.UpdateClientMapS2CellBatch(ctx, dbDetails, newMapCells)
if !(len(newMapPokemon) == 0 && len(newNearbyPokemon) == 0 && len(newForts) == 0) {
decoder.ClearRemovedForts(ctx, dbDetails, newMapCells)
}
return fmt.Sprintf("%d cells containing %d forts %d mon %d nearby", len(decodedGmo.MapCell), len(newForts), len(newWildPokemon), len(newNearbyPokemon))
}