-
Notifications
You must be signed in to change notification settings - Fork 2
/
feslManager.go
863 lines (731 loc) · 26.1 KB
/
feslManager.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
package main
import (
"database/sql"
"encoding/json"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
gs "github.com/ReviveNetwork/GoRevive/GameSpy"
log "github.com/ReviveNetwork/GoRevive/Log"
"github.com/ReviveNetwork/GoRevive/core"
"github.com/go-redis/redis"
)
type FeslManager struct {
name string
db *sql.DB
redis *redis.Client
socket *gs.SocketTLS
eventsChannel chan gs.SocketEvent
batchTicker *time.Ticker
stopTicker chan bool
server bool
}
// New creates and starts a new ClientManager
func (fM *FeslManager) New(name string, port string, certFile string, keyFile string, server bool, db *sql.DB, redis *redis.Client) {
var err error
fM.socket = new(gs.SocketTLS)
fM.db = db
fM.redis = redis
fM.name = name
fM.eventsChannel, err = fM.socket.New(fM.name, port, certFile, keyFile)
fM.stopTicker = make(chan bool, 1)
fM.server = server
if err != nil {
log.Errorln(err)
}
go fM.run()
}
func (fM *FeslManager) run() {
for {
select {
case event := <-fM.eventsChannel:
switch {
case event.Name == "newClient":
fM.newClient(event.Data.(gs.EventNewClientTLS))
case event.Name == "client.command.Hello":
fM.hello(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.NuLogin":
fM.NuLogin(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.NuGetPersonas":
fM.NuGetPersonas(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.NuGetAccount":
fM.NuGetAccount(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.NuLoginPersona":
fM.NuLoginPersona(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.GetStatsForOwners":
fM.GetStatsForOwners(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.GetStats":
fM.GetStats(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.NuLookupUserInfo":
fM.NuLookupUserInfo(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.GetPingSites":
fM.GetPingSites(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.UpdateStats":
fM.UpdateStats(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.GetTelemetryToken":
fM.GetTelemetryToken(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.command.Start":
fM.Start(event.Data.(gs.EventClientTLSCommand))
case event.Name == "client.close":
fM.close(event.Data.(gs.EventClientTLSClose))
case event.Name == "client.command":
fM.LogCommand(event.Data.(gs.EventClientTLSCommand))
log.Debugf("Got event %s.%s: %v", event.Name, event.Data.(gs.EventClientTLSCommand).Command.Message["TXN"], event.Data.(gs.EventClientTLSCommand).Command)
default:
log.Debugf("Got event %s: %v", event.Name, event.Data)
}
}
}
}
func (fM *FeslManager) LogCommand(event gs.EventClientTLSCommand) {
b, err := json.MarshalIndent(event.Command.Message, "", " ")
if err != nil {
panic(err)
}
commandType := "request"
os.MkdirAll("./commands/"+event.Command.Query+"."+event.Command.Message["TXN"]+"", 0777)
err = ioutil.WriteFile("./commands/"+event.Command.Query+"."+event.Command.Message["TXN"]+"/"+commandType, b, 0644)
if err != nil {
panic(err)
}
}
func (fM *FeslManager) logAnswer(msgType string, msgContent map[string]string, msgType2 uint32) {
b, err := json.MarshalIndent(msgContent, "", " ")
if err != nil {
panic(err)
}
commandType := "answer"
os.MkdirAll("./commands/"+msgType+"."+msgContent["TXN"]+"", 0777)
err = ioutil.WriteFile("./commands/"+msgType+"."+msgContent["TXN"]+"/"+commandType, b, 0644)
if err != nil {
panic(err)
}
}
// Not being used right now
func (fM *FeslManager) GetTelemetryToken(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
answerPacket := make(map[string]string)
answerPacket["TXN"] = "GetTelemetryToken"
answerPacket["telemetryToken"] = "MTU5LjE1My4yMzUuMjYsOTk0NixlblVTLF7ZmajcnLfGpKSJk53K/4WQj7LRw9asjLHvxLGhgoaMsrDE3bGWhsyb4e6woYKGjJiw4MCBg4bMsrnKibuDppiWxYKditSp0amvhJmStMiMlrHk4IGzhoyYsO7A4dLM26rTgAo%3d"
answerPacket["enabled"] = "US"
answerPacket["filters"] = ""
answerPacket["disabled"] = ""
event.Client.WriteFESL(event.Command.Query, answerPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, answerPacket, event.Command.PayloadID)
}
func (fM *FeslManager) Status(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
answerPacket := make(map[string]string)
answerPacket["TXN"] = "Status"
answerPacket["id.id"] = "1"
answerPacket["id.partition"] = event.Command.Message["partition.partition"]
answerPacket["sessionState"] = "COMPLETE"
answerPacket["props.{}"] = "3"
answerPacket["props.{resultType}"] = "JOIN"
answerPacket["props.{availableServerCount}"] = "1"
answerPacket["props.{games}.[]"] = "1"
answerPacket["props.{games}.0.lid"] = "1"
answerPacket["props.{games}.0.gid"] = "1"
answerPacket["props.{games}.0.fit"] = "1"
/*
answerPacket["props.{games}.1.lid"] = "2"
answerPacket["props.{games}.1.fit"] = "100"
answerPacket["props.{games}.1.gid"] = "2"
answerPacket["props.{games}.1.avgFit"] = "100"
*/
event.Client.WriteFESL("pnow", answerPacket, 0x80000000)
fM.logAnswer("pnow", answerPacket, 0x80000000)
}
func (fM *FeslManager) Start(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
answerPacket := make(map[string]string)
answerPacket["TXN"] = "Start"
answerPacket["id.id"] = "1"
answerPacket["id.partition"] = event.Command.Message["partition.partition"]
event.Client.WriteFESL(event.Command.Query, answerPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, answerPacket, event.Command.PayloadID)
fM.Status(event)
}
func MysqlRealEscapeString(value string) string {
replace := map[string]string{"\\": "\\\\", "'": `\'`, "\\0": "\\\\0", "\n": "\\n", "\r": "\\r", `"`: `\"`, "\x1a": "\\Z"}
for b, a := range replace {
value = strings.Replace(value, b, a, -1)
}
return value
}
func (fM *FeslManager) UpdateStats(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
answerPacket := event.Command.Message
answerPacket["TXN"] = "UpdateStats"
users, _ := strconv.Atoi(event.Command.Message["u.[]"])
for i := 0; i < users; i++ {
query := ""
owner, ok := event.Command.Message["u."+strconv.Itoa(i)+".o"]
if !ok {
return
}
statsNum, _ := strconv.Atoi(event.Command.Message["u."+strconv.Itoa(i)+".s.[]"])
for j := 0; j < statsNum; j++ {
if event.Command.Message["u."+strconv.Itoa(i)+".s."+strconv.Itoa(j)+".t"] != "" {
query += event.Command.Message["u."+strconv.Itoa(i)+".s."+strconv.Itoa(j)+".k"] + "='" + MysqlRealEscapeString(event.Command.Message["u."+strconv.Itoa(i)+".s."+strconv.Itoa(j)+".t"]) + "', "
} else {
// TODO: Needs to be fixed, v = change, so v = -1 means substract one
query += event.Command.Message["u."+strconv.Itoa(i)+".s."+strconv.Itoa(j)+".k"] + "='" + MysqlRealEscapeString(event.Command.Message["u."+strconv.Itoa(i)+".s."+strconv.Itoa(j)+".v"]) + "', "
}
}
if owner != "0" && owner != event.Client.RedisState.Get("uID") {
sql := "UPDATE `revive_heroes_stats` SET " + query + "pid=" + owner + " WHERE pid = " + owner + ""
_, err := fM.db.Exec(sql)
if err != nil {
log.Errorln(err)
}
} else {
sql := "UPDATE `revive_heroes_accounts` SET " + query + "uid=" + owner + " WHERE uid = " + owner + ""
_, err := fM.db.Exec(sql)
if err != nil {
log.Errorln(err)
}
}
}
event.Client.WriteFESL(event.Command.Query, answerPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, answerPacket, event.Command.PayloadID)
}
func (fM *FeslManager) GetPingSites(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
answerPacket := make(map[string]string)
answerPacket["TXN"] = "GetPingSites"
answerPacket["minPingSitesToPing"] = "0"
answerPacket["pingSites.[]"] = "4"
answerPacket["pingSites.0.addr"] = "127.0.0.1"
answerPacket["pingSites.0.name"] = "gva"
answerPacket["pingSites.0.type"] = "0"
answerPacket["pingSites.1.addr"] = "127.0.0.1"
answerPacket["pingSites.1.name"] = "nrt"
answerPacket["pingSites.1.type"] = "0"
answerPacket["pingSites.2.addr"] = "127.0.0.1"
answerPacket["pingSites.2.name"] = "iad"
answerPacket["pingSites.2.type"] = "0"
answerPacket["pingSites.3.addr"] = "127.0.0.1"
answerPacket["pingSites.3.name"] = "sjc"
answerPacket["pingSites.3.type"] = "0"
event.Client.WriteFESL(event.Command.Query, answerPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, answerPacket, event.Command.PayloadID)
}
func (fM *FeslManager) NuLoginPersona(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
loginPacket := make(map[string]string)
loginPacket["TXN"] = "NuLoginPersona"
loginPacket["lkey"] = event.Client.RedisState.Get("keyHash")
loginPacket["profileId"] = event.Client.RedisState.Get("uID")
loginPacket["userId"] = event.Client.RedisState.Get("uID")
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, loginPacket, event.Command.PayloadID)
}
// Done with redis CLIENT
func (fM *FeslManager) NuLogin(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
if event.Client.RedisState.Get("clientType") == "server" {
// Server login
stmt, err := fM.db.Prepare("SELECT t1.id, t2.username, t2.id FROM revive_heroes_servers t1 LEFT JOIN web_users t2 ON t1.uid=t2.id WHERE t1.secretKey = ?")
defer stmt.Close()
if err != nil {
log.Debugln(err)
return
}
var sID, uID int
var username string
err = stmt.QueryRow(event.Command.Message["encryptedInfo"]).Scan(&sID, &username, &uID)
if err != nil {
loginPacket := make(map[string]string)
loginPacket["TXN"] = "NuLogin"
loginPacket["localizedMessage"] = "\"The password the user specified is incorrect\""
loginPacket["errorContainer.[]"] = "0"
loginPacket["errorCode"] = "122"
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
return
}
saveRedis := make(map[string]interface{})
saveRedis["uID"] = strconv.Itoa(uID)
saveRedis["username"] = username
saveRedis["apikey"] = event.Command.Message["encryptedInfo"]
event.Client.RedisState.SetM(saveRedis)
loginPacket := make(map[string]string)
loginPacket["TXN"] = "NuLogin"
loginPacket["profileId"] = strconv.Itoa(uID)
loginPacket["userId"] = strconv.Itoa(uID)
loginPacket["nuid"] = username
loginPacket["lkey"] = event.Command.Message["encryptedInfo"]
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, loginPacket, event.Command.PayloadID)
return
}
stmt, err := fM.db.Prepare("SELECT t1.uid, t1.sessionid, t1.ip, t2.username, t2.banned, t2.is_admin, t2.is_tester, t2.confirmed_em, t2.key_hash, t2.email, t2.country FROM web_sessions t1 LEFT JOIN web_users t2 ON t1.uid=t2.id WHERE t1.sessionid = ?")
defer stmt.Close()
if err != nil {
log.Debugln(err)
return
}
var uID int
var ip, username, sessionID, keyHash, email, country string
var banned, isAdmin, isTester, confirmedEm bool
err = stmt.QueryRow(event.Command.Message["encryptedInfo"]).Scan(&uID, &sessionID, &ip, &username, &banned, &isAdmin, &isTester, &confirmedEm, &keyHash, &email, &country)
if err != nil {
loginPacket := make(map[string]string)
loginPacket["TXN"] = "NuLogin"
loginPacket["localizedMessage"] = "\"The password the user specified is incorrect\""
loginPacket["errorContainer.[]"] = "0"
loginPacket["errorCode"] = "122"
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
return
}
// Currently only allow admins & testers
if sessionID != event.Command.Message["encryptedInfo"] || !confirmedEm || banned || (!isAdmin && !isTester) {
log.Noteln("User not worthy: " + username)
loginPacket := make(map[string]string)
loginPacket["TXN"] = "NuLogin"
loginPacket["localizedMessage"] = "\"The user is not entitled to access this game\""
loginPacket["errorContainer.[]"] = "0"
loginPacket["errorCode"] = "120"
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
return
}
saveRedis := make(map[string]interface{})
saveRedis["uID"] = strconv.Itoa(uID)
saveRedis["username"] = username
saveRedis["ip"] = ip
saveRedis["sessionID"] = sessionID
saveRedis["keyHash"] = keyHash
saveRedis["email"] = email
saveRedis["country"] = country
event.Client.RedisState.SetM(saveRedis)
loginPacket := make(map[string]string)
loginPacket["TXN"] = "NuLogin"
loginPacket["profileId"] = strconv.Itoa(uID)
loginPacket["userId"] = strconv.Itoa(uID)
loginPacket["nuid"] = username
loginPacket["lkey"] = keyHash
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, loginPacket, event.Command.PayloadID)
}
func (fM *FeslManager) NuLookupUserInfo(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
userNames := []interface{}{}
keys, _ := strconv.Atoi(event.Command.Message["userInfo.[]"])
for i := 0; i < keys; i++ {
userNames = append(userNames, event.Command.Message["userInfo."+strconv.Itoa(i)+".userName"])
}
stmt, err := fM.db.Prepare("SELECT nickname, web_id, pid FROM revive_soldiers WHERE nickname IN (?" + strings.Repeat(",?", len(userNames)-1) + ")")
defer stmt.Close()
if err != nil {
log.Errorln(err)
return
}
rows, err := stmt.Query(userNames...)
if err != nil {
log.Errorln(err)
return
}
personaPacket := make(map[string]string)
personaPacket["TXN"] = "NuLookupUserInfo"
var k = 0
for rows.Next() {
var nickname, webId, pid string
err := rows.Scan(&nickname, &webId, &pid)
if err != nil {
log.Errorln(err)
return
}
personaPacket["userInfo."+strconv.Itoa(k)+".userName"] = nickname
personaPacket["userInfo."+strconv.Itoa(k)+".userId"] = pid
personaPacket["userInfo."+strconv.Itoa(k)+".masterUserId"] = webId
personaPacket["userInfo."+strconv.Itoa(k)+".namespace"] = "MAIN"
k++
}
//personaPacket["user"] = "1"
personaPacket["userInfo.[]"] = strconv.Itoa(k)
event.Client.WriteFESL(event.Command.Query, personaPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, personaPacket, event.Command.PayloadID)
}
func (fM *FeslManager) NuGetPersonas(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
stmt, err := fM.db.Prepare("SELECT nickname, pid FROM revive_soldiers WHERE web_id = ? AND game = ?")
defer stmt.Close()
if err != nil {
return
}
rows, err := stmt.Query(event.Client.RedisState.Get("uID"), "heroes")
if err != nil {
return
}
personaPacket := make(map[string]string)
personaPacket["TXN"] = "NuGetPersonas"
var i = 0
for rows.Next() {
var username string
var pid int
err := rows.Scan(&username, &pid)
if err != nil {
log.Errorln(err)
return
}
personaPacket["personas."+strconv.Itoa(i)] = username
event.Client.RedisState.Set("ownerId."+strconv.Itoa(i+1), strconv.Itoa(pid))
i++
}
event.Client.RedisState.Set("numOfHeroes", strconv.Itoa(i))
personaPacket["personas.[]"] = strconv.Itoa(i)
event.Client.WriteFESL(event.Command.Query, personaPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, personaPacket, event.Command.PayloadID)
}
func (fM *FeslManager) NuGetAccount(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
loginPacket := make(map[string]string)
loginPacket["TXN"] = "NuGetAccount"
loginPacket["heroName"] = event.Client.RedisState.Get("username")
loginPacket["nuid"] = event.Client.RedisState.Get("email")
loginPacket["DOBDay"] = "1"
loginPacket["DOBMonthg"] = "1"
loginPacket["DOBYear"] = "2017"
loginPacket["userId"] = event.Client.RedisState.Get("uID")
loginPacket["globalOptin"] = "0"
loginPacket["thidPartyOptin"] = "0"
loginPacket["language"] = "en"
loginPacket["country"] = event.Client.RedisState.Get("country")
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, loginPacket, event.Command.PayloadID)
}
func (fM *FeslManager) GetStatsForOwners(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
loginPacket := make(map[string]string)
loginPacket["TXN"] = "GetStats"
// Get the owner pids from redis
numOfHeroes := event.Client.RedisState.Get("numOfHeroes")
numOfHeroesInt, err := strconv.Atoi(numOfHeroes)
if err != nil {
return
}
pids := []interface{}{}
for i := 1; i <= numOfHeroesInt; i++ {
pids = append(pids, event.Client.RedisState.Get("ownerId."+strconv.Itoa(i)))
}
// TODO
// Check for mysql injection
var query string
keys, _ := strconv.Atoi(event.Command.Message["keys.[]"])
for i := 0; i < keys; i++ {
query += event.Command.Message["keys."+strconv.Itoa(i)+""] + ", "
}
// Result is your slice string.
rawResult := make([][]byte, keys+1)
result := make([]string, keys+1)
dest := make([]interface{}, keys+1) // A temporary interface{} slice
for i, _ := range rawResult {
dest[i] = &rawResult[i] // Put pointers to each string in the interface slice
}
stmt, err := fM.db.Prepare("SELECT " + query + "pid FROM revive_heroes_stats WHERE pid IN (?" + strings.Repeat(",?", len(pids)-1) + ")")
defer stmt.Close()
if err != nil {
log.Errorln(err)
return
}
rows, err := stmt.Query(pids...)
if err != nil {
log.Errorln(err)
return
}
var k = 0
for rows.Next() {
err := rows.Scan(dest...)
if err != nil {
log.Errorln(err)
return
}
for i, raw := range rawResult {
if raw == nil {
result[i] = "\\N"
} else {
result[i] = string(raw)
}
}
keys, _ := strconv.Atoi(event.Command.Message["keys.[]"])
loginPacket["stats."+strconv.Itoa(k)+".ownerId"] = result[len(result)-1]
loginPacket["stats."+strconv.Itoa(k)+".ownerType"] = "1"
loginPacket["stats."+strconv.Itoa(k)+".stats.[]"] = event.Command.Message["keys.[]"]
for i := 0; i < keys; i++ {
loginPacket["stats."+strconv.Itoa(k)+".stats."+strconv.Itoa(i)+".key"] = event.Command.Message["keys."+strconv.Itoa(i)+""]
loginPacket["stats."+strconv.Itoa(k)+".stats."+strconv.Itoa(i)+".value"] = result[i]
loginPacket["stats."+strconv.Itoa(k)+".stats."+strconv.Itoa(i)+".text"] = result[i]
}
k++
}
loginPacket["stats.[]"] = strconv.Itoa(k)
event.Client.WriteFESL(event.Command.Query, loginPacket, 0xC0000007)
fM.logAnswer(event.Command.Query, loginPacket, event.Command.PayloadID)
}
func (fM *FeslManager) GetStats(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
loginPacket := make(map[string]string)
loginPacket["TXN"] = "GetStats"
owner := event.Command.Message["owner"]
// TODO
// Check for mysql injection
var query string
keys, _ := strconv.Atoi(event.Command.Message["keys.[]"])
for i := 0; i < keys; i++ {
query += event.Command.Message["keys."+strconv.Itoa(i)+""] + ", "
}
// Result is your slice string.
rawResult := make([][]byte, keys+1)
result := make([]string, keys+1)
dest := make([]interface{}, keys+1) // A temporary interface{} slice
for i, _ := range rawResult {
dest[i] = &rawResult[i] // Put pointers to each string in the interface slice
}
// Owner==0 is for accounts-stats.
// Otherwise hero-stats
if owner == "0" || owner == event.Client.RedisState.Get("uID") {
stmt, err := fM.db.Prepare("SELECT " + query + "uid FROM revive_heroes_accounts WHERE uid = ?")
defer stmt.Close()
if err != nil {
log.Errorln(err)
// DEV CODE; REMOVE BEFORE TAKING LIVE!!!!!
// Creates a missing column
var columns []string
keys, _ = strconv.Atoi(event.Command.Message["keys.[]"])
for i := 0; i < keys; i++ {
columns = append(columns, event.Command.Message["keys."+strconv.Itoa(i)+""])
}
for _, column := range columns {
log.Debugln("Checking column " + column)
stmt2, err := fM.db.Prepare("SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = \"revive_heroes_accounts\" AND COLUMN_NAME = \"" + column + "\"")
defer stmt2.Close()
if err != nil {
}
var count int
err = stmt2.QueryRow().Scan(&count)
if err != nil {
}
if count == 0 {
log.Debugln("Creating column " + column)
// If we land here, the column doesn't exist, so create it
_, err := fM.db.Exec("ALTER TABLE `revive_heroes_accounts` ADD COLUMN `" + column + "` TEXT NULL")
if err != nil {
}
}
}
//DEV CODE; REMOVE BEFORE TAKING LIVE!!!!!
return
}
err = stmt.QueryRow(event.Client.RedisState.Get("uID")).Scan(dest...)
if err != nil {
log.Debugln(err)
return
}
if err != nil {
log.Errorln(err)
return
}
for i, raw := range rawResult {
if raw == nil {
result[i] = "\\N"
} else {
result[i] = string(raw)
}
}
loginPacket["ownerId"] = result[len(result)-1]
loginPacket["ownerType"] = "1"
loginPacket["stats.[]"] = event.Command.Message["keys.[]"]
for i := 0; i < keys; i++ {
loginPacket["stats."+strconv.Itoa(i)+".key"] = event.Command.Message["keys."+strconv.Itoa(i)+""]
loginPacket["stats."+strconv.Itoa(i)+".value"] = result[i]
}
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, loginPacket, event.Command.PayloadID)
return
}
// DO the same as above but for hero-stats instead of hero-account
stmt, err := fM.db.Prepare("SELECT " + query + "pid FROM revive_heroes_stats WHERE pid = ?")
defer stmt.Close()
if err != nil {
log.Errorln(err)
// DEV CODE; REMOVE BEFORE TAKING LIVE!!!!!
// Creates a missing column
var columns []string
keys, _ = strconv.Atoi(event.Command.Message["keys.[]"])
for i := 0; i < keys; i++ {
columns = append(columns, event.Command.Message["keys."+strconv.Itoa(i)+""])
}
for _, column := range columns {
log.Debugln("Checking column " + column)
stmt2, err := fM.db.Prepare("SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = \"revive_heroes_stats\" AND COLUMN_NAME = \"" + column + "\"")
defer stmt2.Close()
if err != nil {
}
var count int
err = stmt2.QueryRow().Scan(&count)
if err != nil {
}
if count == 0 {
log.Debugln("Creating column " + column)
// If we land here, the column doesn't exist, so create it
sql := "ALTER TABLE `revive_heroes_stats` ADD COLUMN `" + column + "` TEXT NULL"
_, err := fM.db.Exec(sql)
if err != nil {
log.Errorln(sql)
log.Errorln(err)
}
}
}
//DEV CODE; REMOVE BEFORE TAKING LIVE!!!!!
return
}
err = stmt.QueryRow(owner).Scan(dest...)
if err != nil {
log.Debugln(err)
return
}
if err != nil {
log.Errorln(err)
return
}
for i, raw := range rawResult {
if raw == nil {
result[i] = "\\N"
} else {
result[i] = string(raw)
}
}
loginPacket["ownerId"] = result[len(result)-1]
loginPacket["ownerType"] = "1"
loginPacket["stats.[]"] = event.Command.Message["keys.[]"]
for i := 0; i < keys; i++ {
loginPacket["stats."+strconv.Itoa(i)+".key"] = event.Command.Message["keys."+strconv.Itoa(i)+""]
loginPacket["stats."+strconv.Itoa(i)+".value"] = result[i]
}
event.Client.WriteFESL(event.Command.Query, loginPacket, event.Command.PayloadID)
fM.logAnswer(event.Command.Query, loginPacket, event.Command.PayloadID)
}
func (fM *FeslManager) hello(event gs.EventClientTLSCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
redisState := new(core.RedisState)
redisState.New(fM.redis, event.Command.Message["clientType"]+"-"+event.Client.IpAddr.String())
event.Client.RedisState = redisState
if !fM.server {
getSession := make(map[string]string)
getSession["TXN"] = "GetSessionId"
event.Client.WriteFESL("gsum", getSession, 0)
}
saveRedis := make(map[string]interface{})
saveRedis["SDKVersion"] = event.Command.Message["SDKVersion"]
saveRedis["clientPlatform"] = event.Command.Message["clientPlatform"]
saveRedis["clientString"] = event.Command.Message["clientString"]
saveRedis["clientType"] = event.Command.Message["clientType"]
saveRedis["clientVersion"] = event.Command.Message["clientVersion"]
saveRedis["locale"] = event.Command.Message["locale"]
saveRedis["sku"] = event.Command.Message["sku"]
event.Client.RedisState.SetM(saveRedis)
helloPacket := make(map[string]string)
helloPacket["TXN"] = "Hello"
helloPacket["domainPartition.domain"] = "eagames"
if fM.server {
helloPacket["domainPartition.subDomain"] = "bfwest-server"
} else {
helloPacket["domainPartition.subDomain"] = "bfwest-dedicated"
}
helloPacket["curTime"] = "Jun-15-2017 07:26:12 UTC"
helloPacket["activityTimeoutSecs"] = "0"
helloPacket["messengerIp"] = "messaging.ea.com"
helloPacket["messengerPort"] = "13505"
helloPacket["theaterIp"] = "bfwest-server.theater.ea.com"
if fM.server {
helloPacket["theaterPort"] = "18056"
} else {
helloPacket["theaterPort"] = "18275"
}
event.Client.WriteFESL("fsys", helloPacket, 0xC0000001)
fM.logAnswer("fsys", helloPacket, 0xC0000001)
}
func (fM *FeslManager) newClient(event gs.EventNewClientTLS) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
memCheck := make(map[string]string)
memCheck["TXN"] = "MemCheck"
memCheck["memcheck.[]"] = "0"
memCheck["salt"] = "5"
event.Client.WriteFESL("fsys", memCheck, 0xC0000000)
fM.logAnswer("fsys", memCheck, 0xC0000000)
// Start Heartbeat
event.Client.State.HeartTicker = time.NewTicker(time.Second * 10)
go func() {
for {
if !event.Client.IsActive {
return
}
select {
case <-event.Client.State.HeartTicker.C:
if !event.Client.IsActive {
return
}
pingSite := make(map[string]string)
pingSite["TXN"] = "Ping"
event.Client.WriteFESL("fsys", pingSite, 0)
}
}
}()
log.Noteln("Client connecting")
}
func (fM *FeslManager) close(event gs.EventClientTLSClose) {
log.Noteln("Client closed.")
if event.Client.RedisState != nil {
event.Client.RedisState.Delete()
}
if !event.Client.State.HasLogin {
return
}
}
func (fM *FeslManager) error(event gs.EventClientTLSError) {
log.Noteln("Client threw an error: ", event.Error)
}