-
Notifications
You must be signed in to change notification settings - Fork 1
/
clientManager.go
638 lines (544 loc) · 18.6 KB
/
clientManager.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
package main
import (
"database/sql"
"database/sql/driver"
"encoding/hex"
"errors"
"net"
"strconv"
"strings"
"time"
gs "github.com/ReviveNetwork/GoRevive/GameSpy"
log "github.com/ReviveNetwork/GoRevive/Log"
"github.com/ReviveNetwork/GoRevive/core"
)
type LogEnum string
const (
LOG_LOGIN LogEnum = "login"
LOG_LOGIN_BANNED LogEnum = "login_banned"
LOG_LOGIN_UNCONFIRMED LogEnum = "login_unconfirmed"
LOG_LOGIN_FAILED LogEnum = "login_failed"
LOG_LOGOUT LogEnum = "logout"
LOG_CLOSE LogEnum = "close"
LOG_CLOSE_ERROR LogEnum = "close_error"
)
func (s *LogEnum) Scan(src interface{}) error {
if src == nil {
return errors.New("This field cannot be NULL")
}
if stringStatus, ok := src.([]byte); ok {
*s = LogEnum(string(stringStatus[:]))
return nil
}
return errors.New("Cannot convert enum to string")
}
func (s LogEnum) Value() (driver.Value, error) {
return []byte(s), nil
}
type ClientManager struct {
name string
socket *gs.Socket
eventsChannel chan gs.SocketEvent
db *sql.DB
loggingDB *sql.DB
iDB *core.InfluxDB
batchTicker *time.Ticker
stopTicker chan bool
}
// New creates and starts a new ClientManager
func (cM *ClientManager) New(name string, db *sql.DB, loggingDB *sql.DB, iDB *core.InfluxDB) {
var err error
cM.socket = new(gs.Socket)
cM.name = name
cM.db = db
cM.loggingDB = loggingDB
cM.iDB = iDB
cM.eventsChannel, err = cM.socket.New(cM.name, "29900", false)
cM.stopTicker = make(chan bool, 1)
if err != nil {
log.Errorln(err)
}
// Collect metrics every 10 seconds
cM.batchTicker = time.NewTicker(time.Second * 1)
go func() {
for range cM.batchTicker.C {
cM.collectMetrics()
}
}()
go cM.run()
}
func (cM *ClientManager) collectMetrics() {
// Create a point and add to batch
tags := map[string]string{"clients": "clients-total", "server": "ClientManager", "version": Version}
fields := map[string]interface{}{
"clients": len(cM.socket.Clients),
}
cM.iDB.AddMetric("clients_total", tags, fields)
}
func (cM *ClientManager) run() {
for {
select {
case event := <-cM.eventsChannel:
switch {
case event.Name == "newClient":
go cM.newClient(event.Data.(gs.EventNewClient))
case event.Name == "client.command.login":
go cM.login(event.Data.(gs.EventClientCommand))
case event.Name == "client.command.getprofile":
go cM.getProfile(event.Data.(gs.EventClientCommand))
case event.Name == "client.command.updatepro":
go cM.updatePro(event.Data.(gs.EventClientCommand))
case event.Name == "client.command.logout":
go cM.logout(event.Data.(gs.EventClientCommand))
case event.Name == "client.command.newuser":
go cM.newUser(event.Data.(gs.EventClientCommand))
/*
case event.Name == "client.command.status":
go cM.status(event.Data.(gs.EventClientCommand))
case event.Name == "client.command.bm":
go cM.bm(event.Data.(gs.EventClientCommand))
case event.Name == "client.command.addbuddy":
go cM.addBuddy(event.Data.(gs.EventClientCommand))
case event.Name == "client.command.delbuddy":
go cM.delBuddy(event.Data.(gs.EventClientCommand))
case event.Name == "client.command.authadd":
go cM.authAdd(event.Data.(gs.EventClientCommand))
*/
case event.Name == "client.close":
go cM.close(event.Data.(gs.EventClientClose))
default:
log.Debugln(event)
}
/*default:
runtime.Gosched()*/
}
}
}
func (cM *ClientManager) insertLog(uid int, pid int, ip string, username string, log_type LogEnum) {
stmt, err := cM.loggingDB.Prepare("INSERT INTO logs_gamespy_auth (uid, pid, ip, username, log_type, shard) VALUES (?, ?, INET_ATON(?), ?, ?, ?)")
defer stmt.Close()
if err != nil {
log.Errorln("Error with logging DB: ", err)
return
}
_, err = stmt.Exec(uid, pid, ip, username, log_type, "0-0")
if err != nil {
log.Errorln("Error with logging DB: ", err)
return
}
}
func (cM *ClientManager) heartBeat(event gs.EventNewClient) {
if !event.Client.IsActive {
event.Client.State.HeartTicker.Stop()
return
}
event.Client.Write("revive")
}
func (cM *ClientManager) newClient(event gs.EventNewClient) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
log.Noteln("Client connecting")
event.Client.State.ServerChallenge = hex.EncodeToString([]byte(gs.BF2RandomUnsafe(5)))
event.Client.Write("\\lc\\1\\challenge\\" + event.Client.State.ServerChallenge + "\\id\\1\\final\\")
// 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
}
cM.heartBeat(event)
case <-cM.stopTicker:
return
/*default:
if !event.Client.IsActive {
return
}*/
//runtime.Gosched()
}
}
}()
}
func (cM *ClientManager) login(event gs.EventClientCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
challenge, okChallenge := event.Command.Message["challenge"]
response, okResponse := event.Command.Message["response"]
uniqueNick, okUniqueNick := event.Command.Message["uniquenick"]
gamename, okGamename := event.Command.Message["gamename"]
if !okChallenge || !okResponse || !okUniqueNick || !okGamename {
err := event.Client.WriteError("0", "Login query missing a variable.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
if gamename != "battlefield2" && gamename != "stella" {
err := event.Client.WriteError("0", "Game not supported.")
if err != nil {
log.Noteln("Client left during writing error")
}
}
event.Client.State.ClientChallenge = challenge
event.Client.State.ClientResponse = response
event.Client.State.GameName = gamename
stmt, err := cM.db.Prepare("SELECT t1.web_id, t1.pid, t2.username, t2.password, t2.game_country, t2.email, t2.banned, t2.confirmed_em FROM revive_soldiers t1 LEFT JOIN web_users t2 ON t1.web_id=t2.id WHERE t1.nickname = ? AND game = ?")
defer stmt.Close()
if err != nil {
err := event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
var password string
err = stmt.QueryRow(uniqueNick, gamename).Scan(&event.Client.State.BattlelogID, &event.Client.State.PlyPid, &event.Client.State.Username, &password, &event.Client.State.PlyCountry, &event.Client.State.PlyEmail, &event.Client.State.Banned, &event.Client.State.Confirmed)
if err != nil {
err := event.Client.WriteError("256", "The username provided is not registered.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
event.Client.State.PlyName = event.Client.State.Username
responseVerify := password + strings.Repeat(" ", 48) + uniqueNick + event.Client.State.ClientChallenge + event.Client.State.ServerChallenge + password
responseVerify = gs.Hash(responseVerify)
if event.Client.State.ClientResponse != responseVerify {
log.Noteln("Login Failure", gamename, event.Client.IpAddr, event.Client.State.PlyName, "Password: "+password)
cM.insertLog(event.Client.State.BattlelogID, event.Client.State.PlyPid, event.Client.IpAddr.(*net.TCPAddr).IP.String(), event.Client.State.Username, LOG_LOGIN_FAILED)
err := event.Client.WriteError("256", "Incorrect password. Visit www.battlelog.co if you forgot your password.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
// Generate session key I fucking hate this thing
var len = len(event.Client.State.PlyName)
var nameIndex = 0
var session rune
runeName := []rune(event.Client.State.PlyName)
for {
len = len - 1
if len < 0 {
break
}
tmpSession := session >> 8
session = gs.CrcLookup[((runeName[nameIndex]^session)&0xff)%256] ^ (tmpSession)
nameIndex = nameIndex + 1
}
log.Noteln("Login Success", event.Client.IpAddr, event.Client.State.PlyName)
responseVerify2 := password + strings.Repeat(" ", 48) + uniqueNick + event.Client.State.ServerChallenge + event.Client.State.ClientChallenge + password
responseVerify2 = gs.Hash(responseVerify2)
err = event.Client.Write("\\lc\\2\\sesskey\\" + strconv.Itoa(int(session)) + "\\proof\\" + responseVerify2 + "\\userid\\" + strconv.Itoa(event.Client.State.PlyPid) + "\\profileid\\" + strconv.Itoa(event.Client.State.PlyPid) + "\\uniquenick\\" + event.Client.State.PlyName + "\\lt\\" + gs.BF2RandomUnsafe(22) + "__\\id\\1\\final\\")
if err != nil {
log.Noteln("Client left during writing error")
}
if !event.Client.State.Confirmed {
confirmedTimer := time.NewTimer(time.Second)
go func() {
<-confirmedTimer.C
cM.insertLog(event.Client.State.BattlelogID, event.Client.State.PlyPid, event.Client.IpAddr.(*net.TCPAddr).IP.String(), event.Client.State.Username, LOG_LOGIN_UNCONFIRMED)
err := event.Client.WriteError("256", "Sorry! You must confirm your Revive BF2 account before you can play Battlefield 2. Check your email for a confirmation link. If you did not recieve it, please login at https://battlelog.co to request a new one.")
if err != nil {
log.Noteln("Client left during writing error")
}
}()
return
}
if event.Client.State.Banned {
bannedTimer := time.NewTimer(time.Second)
go func() {
<-bannedTimer.C
cM.insertLog(event.Client.State.BattlelogID, event.Client.State.PlyPid, event.Client.IpAddr.(*net.TCPAddr).IP.String(), event.Client.State.Username, LOG_LOGIN_BANNED)
err := event.Client.WriteError("256", "Sorry! Your account has been banned from Revive Networks. Please visit battlelog.co to appeal.")
if err != nil {
log.Noteln("Client left during writing error")
}
}()
return
}
// Login Successful
cM.insertLog(event.Client.State.BattlelogID, event.Client.State.PlyPid, event.Client.IpAddr.(*net.TCPAddr).IP.String(), event.Client.State.Username, LOG_LOGIN)
stmt, err = cM.db.Prepare("UPDATE revive_soldiers SET online = 1, ip = INET_ATON(?) WHERE pid=? AND game=?")
defer stmt.Close()
if err != nil {
err := event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
_, err = stmt.Exec(event.Client.IpAddr.(*net.TCPAddr).IP.String(), event.Client.State.PlyPid, "battlefield2")
if err != nil {
err := event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
event.Client.State.HasLogin = true
if gamename == "stella" {
cM.sendMessages(event, gamename)
cM.sendFriends(event, gamename)
}
}
func (cM *ClientManager) sendFriends(event gs.EventClientCommand, gamename string) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
stmt, err := cM.db.Prepare("SELECT fid, confirmed FROM revive_friends WHERE uid = ?")
defer stmt.Close()
if err != nil {
event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
return
}
rows, err := stmt.Query(event.Client.State.BattlelogID)
if err != nil {
// No friends :(
return
}
var friendObj string
var fids []int
for rows.Next() {
var fid int
var confirmed bool
err := rows.Scan(&fid)
if err != nil {
event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
return
}
if confirmed {
fids = append(fids, fid)
}
}
for _, fid := range fids {
stmt, err := cM.db.Prepare("SELECT pid, online, status, status_msg FROM revive_soldiers WHERE pid = ? AND game = ? LIMIT 1")
defer stmt.Close()
if err != nil {
err := event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
var pid int
var online bool
var status string
var statusMsg string
var onlineStr string
var msg string
err = stmt.QueryRow(fid, gamename).Scan(&pid, &online, &status, &statusMsg)
if err != nil {
log.Noteln("Error requesting friend with fid " + strconv.Itoa(fid) + "and game " + gamename)
return
}
if online {
onlineStr = "1"
} else {
onlineStr = "0"
}
if status == "Offline" {
msg = "|s|" + onlineStr + "|ss|" + status
} else {
msg = "|s|" + onlineStr + "|ss|" + status + "|ls" + statusMsg + "|ip|0|p|0"
}
friendObj += "\\bm\\100"
friendObj += "\\f\\" + strconv.Itoa(pid) + "\\msg\\" + msg
friendObj += "\\final\\"
}
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
event.Client.Write(friendObj)
}
func (cM *ClientManager) sendMessages(event gs.EventClientCommand, gamename string) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
stmt, err := cM.db.Prepare("SELECT id, msg_type, from_pid, sentDate, msg FROM revive_messages WHERE to_uid = ? AND `read` = 0")
defer stmt.Close()
if err != nil {
event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
return
}
rows, err := stmt.Query(event.Client.State.BattlelogID)
if err != nil {
// No friends :(
return
}
var msgObj string
for rows.Next() {
var id int
var msgType int
var fromPid int
var sentDate int
var msg string
err := rows.Scan(&id, &msgType, &fromPid, &sentDate, &msg)
if err != nil {
event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
return
}
stmt, err = cM.db.Prepare("'UPDATE revive_messages SET `read` = 1 WHERE id=?")
defer stmt.Close()
if err != nil {
err := event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
_, err = stmt.Exec(id)
if err != nil {
err := event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
if msgType == 2 {
msg = msg + "|signed|" + gs.Hash(strconv.Itoa(fromPid)+strconv.Itoa(event.Client.State.PlyPid))
}
msgObj += "\\bm\\" + strconv.Itoa(msgType) + "\\f\\" + strconv.Itoa(fromPid) + "\\date\\" + strconv.Itoa(sentDate) + "\\msg\\" + msg + "\\final\\"
}
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
event.Client.Write(msgObj)
}
func (cM *ClientManager) getProfile(event gs.EventClientCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
profileID, okProfileID := event.Command.Message["profileid"]
id, okID := event.Command.Message["id"]
if !okProfileID || !okID {
event.Client.WriteError("256", "Missing arguments")
}
stmt, err := cM.db.Prepare("SELECT pid, nickname, web_id FROM revive_soldiers WHERE pid = ? AND game= ?")
defer stmt.Close()
if err != nil {
err := event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
var pid int
var webID int
var nickname string
err = stmt.QueryRow(profileID, event.Client.State.GameName).Scan(&pid, &nickname, &webID)
if err != nil {
err := event.Client.WriteError("256", "The username provided is not registered.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
stmt.Close()
stmt, err = cM.db.Prepare("SELECT email FROM web_users WHERE id = ?")
defer stmt.Close()
if err != nil {
err := event.Client.WriteError("0", "The login service is having an issue reaching the database. Please try again in a few minutes.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
var email string
err = stmt.QueryRow(webID).Scan(&email)
if err != nil {
err := event.Client.WriteError("256", "The username provided is not registered.")
if err != nil {
log.Noteln("Client left during writing error")
}
return
}
log.Noteln("GetProfile", event.Client.IpAddr, event.Client.State.PlyName)
event.Client.Write("\\pi\\\\profileid\\" + strconv.Itoa(pid) +
"\\nick\\" + nickname +
"\\userid\\" + strconv.Itoa(pid) +
"\\email\\" + email +
"\\sig\\" + gs.BF2RandomUnsafe(32) +
"\\uniquenick\\" + nickname +
"\\pid\\0\\firstname\\\\lastname\\" +
"\\countrycode\\" + event.Client.State.PlyCountry +
"\\birthday\\16844722\\lon\\0.000000\\lat\\0.000000\\loc\\" +
"\\id\\" + id +
"\\\\final\\")
}
func (cM *ClientManager) updatePro(event gs.EventClientCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
countryCode, okCountryCode := event.Command.Message["countrycode"]
if !okCountryCode {
event.Client.WriteError("0", "Invalid query! No country code specified.")
return
}
log.Noteln("UpdateProfile", event.Client.IpAddr, event.Client.State.PlyName)
stmt, err := cM.db.Prepare("UPDATE web_users SET game_country=? WHERE id=?")
defer stmt.Close()
if err != nil {
log.Errorln("Error with DB: ", err)
return
}
_, err = stmt.Exec(countryCode, event.Client.State.BattlelogID)
if err != nil {
log.Errorln("Error with DB: ", err)
return
}
}
func (cM *ClientManager) logout(event gs.EventClientCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
event.Client.State.LoggedOut = true
cM.insertLog(event.Client.State.BattlelogID, event.Client.State.PlyPid, event.Client.IpAddr.(*net.TCPAddr).IP.String(), event.Client.State.Username, LOG_LOGOUT)
event.Client.Close()
}
func (cM *ClientManager) newUser(event gs.EventClientCommand) {
if !event.Client.IsActive {
log.Noteln("Client left")
return
}
event.Client.WriteError("516", "In-game registration is not available. Please visit battlelog.co to register.")
}
func (cM *ClientManager) close(event gs.EventClientClose) {
event.Client.State.HeartTicker.Stop()
cM.stopTicker <- true
log.Noteln("Client closed.")
if !event.Client.State.HasLogin {
return
}
cM.insertLog(event.Client.State.BattlelogID, event.Client.State.PlyPid, event.Client.IpAddr.(*net.TCPAddr).IP.String(), event.Client.State.Username, LOG_CLOSE)
stmt, err := cM.db.Prepare("UPDATE revive_soldiers SET online = 0 WHERE pid=? AND game=?")
defer stmt.Close()
if err != nil {
log.Errorln("Error with DB: ", err)
return
}
_, err = stmt.Exec(event.Client.State.PlyPid, "battlefield2")
if err != nil {
log.Errorln("Error with DB: ", err)
return
}
}
func (cM *ClientManager) error(event gs.EventClientError) {
log.Noteln("Client threw an error: ", event.Error)
}