-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvcserver.cpp
More file actions
1532 lines (1413 loc) · 39.6 KB
/
vcserver.cpp
File metadata and controls
1532 lines (1413 loc) · 39.6 KB
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Game server for Visual Concepts Dreamcast games.
Copyright (C) 2025 Flyinghead
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
extern "C" {
#include "blowfish.h"
}
#include "vcserver.h"
#include "discord.h"
#include "log.h"
#include <dcserver/shared_this.hpp>
#include <dcserver/asio.hpp>
#include <dcserver/status.hpp>
#include <signal.h>
#include <asio.hpp>
#include <stdio.h>
#include <string>
#include <array>
#include <vector>
#include <map>
#include <functional>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <unordered_map>
// server IP: 146.185.135.179
// 92 b9 87 b3
// OOGA BOOGA
// auth server
// request: 1f 00 92 13 02 00 04 2b 00 00 00 06 00 96 59 59 f9 f2 9e 8b e7 06 00 e1 0f a0 9c 6a cf bf 68
// len fixed wsb2k l (var64) len user name encrypted len password encrypted
// -> 5 varies...key num?
// encrypted data is always multiple of 8 bytes (len is real len after decrypt)
// response: 1e 00 95 13 00 00 0c 00 50 6c 61 79 65 72 5f 30 31 34 38 32 08 00 00 00 00 00 00 00 00 08
// len len P l a y e r _ 0 1 4 8 2 len ??
//
// record server (port 12201 for ooga)
// request: (RecordRetrieve)
// 25 00 len (37)
// 5a 1b 04 14 00 00 00 0c 00 b0 23 b8 09 cf 00 af 48 ad 7c 1f 39 5f c8 49 6d 06 00 f8 42 9f d3 8f 65 8e e0
// response:
// 18 00 len (24)
// 5b 1b 00 00 10 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
// len? 00 for new user
// EOT
// (after game?)2
// req: (RecordUpdate)
// 37 00
// 5e 1b 04 14 00 00 00 0c 00 b0 23 b8 09 cf 00 af 48 e6 0b de b6 e4 d2 60 98 06 00 42 62 5b ae 4d a8 df 75 10 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00
// resp:
// 18 00
// 5b 1b 01 00 10 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00
// EOT
//
// region server (port 15203)
// req:
// 04 00
// a0 0f
// resp:
// 17 00 len (23)
// a1 0f 81 0a 00 53 68 75 6f 47 61 72 64 65 6e 02 00 00 00 00 00
// len S h u o G a r d e n
// EOT
// req:
// 10 00 len (16)
// a2 0f 0a 00 53 68 75 6f 47 61 72 64 65 6e
// len S h u o G a r d e n
// resp:
// 1d 00 len (29)
// a3 0f 00 00 81 08 00 53 68 75 6d 61 6e 69 61 92 b9 87 b3 3b 64 02 00 00 00 00 00
// len S h u m a n i a IP address. port.
// 15204
// EOT
//
// game server? port 15204
// req:
// 2d 00 len (45)
// d2 00 ee 0c 35 4f ba 72 73 df 00 0c 00 50 6c 61 79 65 72 5f 30 31 34 38 32 10 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
// len P l a y e r _ 0 1 4 8 2 len
// resp:
// 08 00 len
// d3 00 00 00 03 00
// 3 players?
// 26 00 len (38)
// 37 01 81 02 00 07 00 50 49 54 4f 52 55 53 bb 26 ff a7 10 00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00
// P I T O R U S ...IP addr....
// 2b 00 len (43)
// 37 01 81 03 00 0c 00 50 6c 61 79 65 72 5f 30 31 34 38 32 b0 94 41 49 10 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
// P l a y e r _ 0 1 4 8 2 ...IP addr....
// 27 00
// 37 01 81 04 00 08 00 53 63 72 69 76 61 6e 69 c9 00 72 38 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// S c r i v a n i ...IP addr....
// alternative: (first login?)
// 27 00
// d2 00 58 58 58 58 58 58 58 58 00 06 00 6c 6c 6c 6c 6c 6c 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
// X X X X X X X X l l l l l l
// resp: (might be unrelated to the current exchange?) Challenge?
// 0b 00
// 3d 00 02 00 00 00 00 00 00
// req:
// 07 00
// 3e 00 00 00 00
// resp: (normal) 08 00 d3 00 00 00 01 00...
// req:
// 0c 00
// 8a 02 01 00 00 00 00 00 00 00
// resp:
// 27 00
// 8b 02 01 00 00 00 81 01 00 08 00 53 63 72 69 76 61 6e 69 04 00 0c 00 00 00 01 02 05 00 00 00 00 41 00 00 00 00
// S c r i v a n i
// USER JOINS
// 27 00
// 8b 02 01 00 00 00 81 01 00 08 00 53 63 72 69 76 61 6e 69 04 00 0c 00 00 00 01 02 05 00 00 00 00 43 00 00 00 00
// S c r i v a n i
// ping?
// 0a 00
// 90 02 00 00 00 00 01 00
// req: Leave lobby
// 0a 00
// d4 00 02 00 00 00 00 00
static std::string RegionName = "DCNet World";
static std::string LobbyName = "DCNet";
static asio::io_context io_context;
static std::unordered_map<std::string, std::string> Config;
class Connection;
class User : public SharedThis<User>
{
User(const std::string& name, Connection *connection, uint16_t userId, uint16_t recSize, const uint8_t *record)
: name(name), connection(connection), userId(userId)
{
this->record.resize(recSize);
memcpy(this->record.data(), record, recSize);
}
public:
std::string name; // 6-16 chars
Connection *connection;
uint16_t userId = 0;
std::vector<uint8_t> record;
friend super;
};
class Game
{
public:
Game() = default;
Game(User::Ptr host, uint16_t paramSize, const uint8_t *params, uint16_t gameId)
: host(host), gameId(gameId)
{
setParams(paramSize, params);
}
void setParams(uint16_t paramSize, const uint8_t *params) {
this->params.resize(paramSize);
memcpy(this->params.data(), params, this->params.size());
}
User::Ptr host;
std::vector<uint8_t> params;
uint16_t unknown; // sent after game params
uint16_t gameId = 0;
};
class Lobby
{
uint16_t nextUserId()
{
uint16_t id = users.size() + 1;
for (const User::Ptr& user : users)
id = std::max<uint16_t>(id, user->userId + 1);
return id;
}
uint16_t nextGameId()
{
uint16_t id = games.size() + 1;
for (const Game& game : games)
id = std::max<uint16_t>(id, game.gameId + 1);
return id;
}
Game *findGame(Connection *connection, const std::string& hostname)
{
for (Game& game : games) {
if (game.host->connection == connection && game.host->name == hostname)
return &game;
}
return nullptr;
}
public:
Lobby(GameType gameType) : gameType(gameType) {}
User::Ptr addUser(const std::string& name, Connection *connection, uint16_t recSize, const uint8_t *record);
void deleteUser(Connection *connection)
{
uint16_t userId = 0;
for (auto it = users.begin(); it != users.end(); ++it)
{
if ((*it)->connection == connection)
{
// Find if the user is hosting a game
uint16_t gameId = 0;
for (auto gameIt = games.begin(); gameIt != games.end(); ++gameIt) {
if (gameIt->host == *it) {
gameId = gameIt->gameId;
break;
}
}
userId = (*it)->userId;
INFO_LOG(gameType, "Lobby: user %s left (ID=%d)", (*it)->name.c_str(), userId);
(*it)->connection = nullptr;
// Delete the user before deleting the game so he doesn't get the update
users.erase(it);
if (gameId != 0)
deleteGame(nullptr, gameId);
break;
}
}
if (userId != 0)
sendUserLeave(userId);
}
Game *updateGame(Connection *connection, const std::string& hostname, uint16_t paramSize, const uint8_t *params, uint16_t unknown)
{
Game *game = findGame(connection, hostname);
if (game != nullptr) {
game->setParams(paramSize, params);
game->unknown = unknown;
sendGameUpdate(*game, game->host);
return game;
}
for (auto& user : users)
{
if (user->connection == connection) { // IGP uses "CREATE GAME" as host name
games.emplace_back(user, paramSize, params, nextGameId());
games.back().unknown = unknown;
sendGameUpdate(games.back(), user);
std::vector<std::string> userNames;
for (const auto& user : users)
userNames.push_back(user->name);
std::sort(userNames.begin(), userNames.end());
discordGameCreated(gameType, user->name, userNames);
return &games.back();
}
}
ERROR_LOG(gameType, "can't create/update game: host not found");
return nullptr;
}
bool deleteGame(const User::Ptr& user, int gameId)
{
for (auto it = games.begin(); it != games.end(); ++it)
{
if ((user == nullptr || it->host == user) && it->gameId == gameId)
{
INFO_LOG(gameType, "Game %d by host %s deleted", gameId, user ? user->name.c_str() : "?");
games.erase(it);
sendGameDelete(gameId);
return true;
}
}
return false;
}
void getStatus(GameType& gameType, int& playerCount, int& gameCount)
{
gameType = this->gameType;
playerCount = users.size();
gameCount = games.size();
}
void sendChat(const User::Ptr& user, const std::string& chatMsg);
void sendGameUpdate(const Game& game, User::Ptr exceptTo = {});
void sendGameDelete(uint16_t gameId);
void sendUserLeave(uint16_t userId);
GameType gameType;
std::vector<User::Ptr> users;
std::vector<Game> games;
};
class Connection : public SharedThis<Connection>
{
public:
asio::ip::tcp::socket& getSocket() {
return socket;
}
void start()
{
asio::socket_base::keep_alive option(true);
socket.set_option(option);
receive();
}
void sendUser(int idx)
{
if (is2K1()) {
respond(301);
respByte(1);
}
else {
respond(311);
respByte(0x81);
}
const User::Ptr& user = lobby->users[idx];
INFO_LOG(gameType, "Sending user[%d] %s", user->userId, user->name.c_str());
respShort(user->userId);
respStr(user->name);
try {
asio::ip::address_v4 address = user->connection->address();
respData(address.to_bytes());
} catch (const std::system_error& e) {
// connection might have been closed
respLong(0);
}
if (!is2K1()) {
respShort(user->record.size());
respData(user->record);
}
send();
}
void sendChat(int userId, const std::string& chatMsg)
{
respond(501);
respShort(userId);
respStr(chatMsg);
send();
}
void sendGameUpdate(const Game& game)
{
respond(651);
respLong(1);
respByte(0x81);
respShort(game.gameId);
respStr(game.host->name);
respShort(game.host->userId);
respShort(game.params.size());
respData(game.params);
respShort(game.unknown);
send();
}
void sendGameDelete(uint16_t gameId)
{
respond(656);
respSkip(4);
respShort(gameId);
send();
}
void sendUserLeave(uint16_t userId)
{
if (is2K1())
respond(203);
else
respond(213);
respShort(userId);
send();
}
private:
Connection(asio::io_context& io_context, std::shared_ptr<Lobby> lobby)
: io_context(io_context), socket(io_context),
lobby(lobby), gameType(lobby ? lobby->gameType : UNKNOWN) {
}
bool is2K1() const {
return gameType == NFL2K1 || gameType == NBA2K1;
}
void send()
{
*(uint16_t *)&sendBuffer[packetStart] = sendIdx - packetStart;
packetStart = 0;
sendInternal();
}
void sendInternal()
{
if (sending)
return;
sending = true;
dumpSentMsg();
uint16_t packetSize = *(uint16_t *)&sendBuffer[0];
asio::async_write(socket, asio::buffer(sendBuffer, packetSize),
std::bind(&Connection::onSent, shared_from_this(),
asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
void onSent(const std::error_code& ec, size_t len)
{
if (ec)
{
ERROR_LOG(gameType, "onSent: %s", ec.message().c_str());
lobbyLeave();
return;
}
sending = false;
assert(len <= sendIdx);
sendIdx -= len;
if (sendIdx != 0) {
memmove(&sendBuffer[0], &sendBuffer[len], sendIdx);
sendInternal();
}
}
void receive() {
asio::async_read_until(socket, recvBuffer, packetMatcher,
std::bind(&Connection::onReceive, shared_from_this(), asio::placeholders::error, asio::placeholders::bytes_transferred));
}
using iterator = asio::buffers_iterator<asio::const_buffers_1>;
std::pair<iterator, bool>
static packetMatcher(iterator begin, iterator end)
{
if (end - begin < 3)
return std::make_pair(begin, false);
iterator i = begin;
uint16_t len = (uint8_t)*i++;
len |= uint8_t(*i++) << 8;
if (end - begin < len)
return std::make_pair(begin, false);
return std::make_pair(begin + len, true);
}
void onReceive(const std::error_code& ec, size_t len)
{
if (ec || len == 0)
{
if (ec && ec != asio::error::eof)
ERROR_LOG(gameType, "onReceive: %s", ec.message().c_str());
else
DEBUG_LOG(gameType, "Connection closed");
lobbyLeave();
return;
}
#ifdef DUMP_IN
dumpMsg();
#endif
uint16_t msgLen = recvShort(0);
if (len != msgLen)
ERROR_LOG(gameType, "Received %zd bytes but packet len is %d", len, msgLen);
uint16_t msgType = recvShort(2);
switch (msgType)
{
// Auth server
case 5010: // UserLogin
userLogin();
break;
case 5000: // nfl 2k1
case 5004: // nba 2k1
userLogin2K1();
break;
case 5012: // UserCreate
userCreate();
break;
case 5002: // nfl 2k1
userCreate2k1();
break;
// Record server
case 7002: // RecordRetrieve
recordRetrieve();
break;
case 7006: // RecordUpdate
recordUpdate();
break;
case 7014: // get top 50 players
getTopUserList();
break;
// Region server
case 4000: // get regions
getRegions();
break;
case 4002: // get lobby servers
getLobbies();
break;
case 50: // find user
findUser();
break;
case 400: // find user 2k1
findUser();
break;
// Lobby server
case 200: // enter lobby (nfl 2K1)
case 206: // enter lobby (nba 2K1)
lobbyRegister2K1();
break;
case 210: // enter lobby, get user list
lobbyRegister();
break;
case 202: // leave lobby (2k1)
case 212: // leave lobby
lobbyLeave();
break;
case 650: // get game list
getGameList();
break;
case 654: // create game
createUpdateGame();
break;
case 656: // delete/leave game
leaveGame();
break;
case 500: // chat message
chatSent();
break;
case 62:
lobbyUserList();
break;
case 602: // get user list (2k1)
lobbyUserList2K1();
break;
case 10: // ping? refresh request? respond with 311 or 651 if new user or new game
break;
default:
ERROR_LOG(gameType, "TODO: Unknown packet type %04x (len %d)", msgType, msgLen);
break;
}
recvBuffer.consume(len);
receive();
}
int decrypt(BLOWFISH_CTX *ctx, uint8_t *data, int len)
{
int lenw = (len + 7) / 8 * 2;
uint32_t *p = (uint32_t *)data;
for (int i = 0; i < lenw; i += 2, p += 2)
Blowfish_Decrypt(ctx, p, p + 1);
return lenw * 4;
}
bool validLoginString(const std::string& s)
{
for (char c : s) {
if (c < ' ' || c > '~')
return false;
}
return true;
}
int recvLoginPassword(const uint8_t *data, std::string& username, std::string& password)
{
int n = data[0] + 1; // skip key challenge
const uint8_t *puser = &data[n];
uint16_t userlen = *(const uint16_t *)puser;
n += 2;
int cuserlen = (userlen + 7) / 8 * 8;
n += cuserlen;
uint8_t tmpuser[18];
const uint8_t *ppassword = &data[n];
uint16_t passwordlen = *(const uint16_t *)ppassword;
n += 2;
int cpasswordlen = (passwordlen + 7) / 8 * 8;
n += cpasswordlen;
uint8_t tmppassword[18];
if (userlen > 16 || passwordlen > 16)
{
ERROR_LOG(gameType, "username or password too long (%d, %d)", userlen, passwordlen);
username.clear();
password.clear();
return n;
}
// ooga key schedule:
// var4 key
// 29 2f
// 5e 16
// 33 21
// 09 60
// 10 01
// 34 2f
// 07 21
// 49 60
// 22 40
// 4f 16
// 2a 21
// 23 01
// 60 01
// 3e 01
// 27 21
// 12 16
//
BLOWFISH_CTX ctx{};
// 1f 00 92 13 02 00 04 2b 00 00 00 06 00 a9 60 29 07 a9 f5 4c a1 06 00 a7 c9 d2 03 53 f8 eb 2b
// Brute-force crack the encryption key since it's only 8 bits.
// A smarter way would be to derive the key from the included key challenge, which is probably what
// the original server does.
uint32_t key = 0;
for (; key < 0x100; key++)
{
ctx = {};
Blowfish_Init(&ctx, (uint8_t *)&key, sizeof(key));
memcpy(tmpuser, puser, cuserlen + 2);
decrypt(&ctx, tmpuser + 2, userlen);
username = recvStr(tmpuser);
memcpy(tmppassword, ppassword, cpasswordlen + 2);
decrypt(&ctx, tmppassword + 2, passwordlen);
password = recvStr(tmppassword);
if (validLoginString(username) && validLoginString(password))
break;
}
if (key == 0x100) {
ERROR_LOG(gameType, "can't find the encryption key");
username.clear();
password.clear();
}
else {
DEBUG_LOG(gameType, "keychal %x key %x username: %s", data[1], key, username.c_str());
}
return n;
}
void loginFailure(const std::string& msg)
{
respond(5013);
// Invalid username/password:
// 36 00 95 13 01 00 1b 00 49 6e 63 6f 72 72 65 63 6....... Incorrec
// 74 20 75 73 65 72 6e 61 6d 65 2f 70 61 73 73 77 t userna me/passw
// 6f 72 64 08 01 00 00 00 00 00 00 00 08 00 36 10 ord..... ......6.
// fd 38 65 26 f5 7d
respShort(1); // failure
respStr(msg);
respByte(8);
respByte(1);
respSkip(7);
respShort(8);
respByte(0x36);
respByte(0x10);
respByte(0xfd);
respByte(0x38);
respByte(0x65);
respByte(0x26);
respByte(0xf5);
respByte(0x7d);
send();
}
void userLogin()
{
int gameId = recvShort(4);
std::string username, password;
recvLoginPassword(recvBuffer.bytes() + 6, username, password);
if (!username.empty() && !password.empty())
{
INFO_LOG((GameType)gameId, "Login from %s user %s", address().to_string().c_str(), username.c_str());
respond(5013);
respShort(0); // success?
respStr(username);
respShort(8);
respSkip(7);
respByte(8);
send();
}
else {
loginFailure("Authentication error");
}
}
void userLogin2K1()
{
// msg 5004: (nba 2k1)
// 1f 00 8c 13 04 06 00 00 00 06 00 69 8b ac aa 1c 31 6d fa 06 00 b8 84 ba cc d6 a5 3b 9d 01 00
// l key chal len encrypted string....... len encrypted string.......
// msg 5000: (nfl 2k1)
// 2d 00 88 13 04 38 00 00 00 0a 00 16 c7 e6 56 02 f7 1b 70 d1 e3 f8 89 01 e9 d9 95 09 00 b4 0f 5b 01 3a df 05 e1 0e 19 7e 83 70 d1 95 5b
// resp: 5003
// 10 00 8b 13 01 00 08 00 00 00 00 00 00 00 00 08
std::string username, password;
recvLoginPassword(recvBuffer.bytes() + 4, username, password);
if (!username.empty() && !password.empty())
{
INFO_LOG(gameType, "Login(2K1) from %s user %s", address().to_string().c_str(), username.c_str());
respond(5003);
respShort(1);
respShort(8);
respSkip(7);
respByte(8);
}
else
{
// not working either
respond(0xffff);
respLong(0);
// FIXME doesn't work. Need actual net dump.
// nfl 2K1:
// readShort if 0 -> success
// readString user name?
// readString password?
// else
// readString error message?
//respond(5003);
//respShort(1); // error
//respStr("Authentication error POUET");
}
send();
}
void userCreate()
{
// 21 00 94 13 02 00 00 00 04 29 00 00 00 06 00 29 d6 60 bf 28 1e 28 aa 06 00 ed c3 ef 36 3c dc 15 97
// gameId l key chal len user-name.............. len password...............
// response:
// 21 00 95 13 00 00 06 00 6c 6c 6c 6c 6c 6c 08 01 !....... llllll..
// 00 00 00 00 00 00 00 08 00 36 10 fd 38 65 26 f5 ........ .6..8e&.
// 7d
int gameId = recvShort(4);
std::string username, password;
recvLoginPassword(recvBuffer.bytes() + 8, username, password);
INFO_LOG((GameType)gameId, "Create user: %s", username.c_str());
if (!username.empty() && !password.empty())
{
respond(5013);
respShort(0);
respStr(username);
respByte(8);
respByte(1);
respSkip(7);
respShort(8);
respByte(0x36);
respByte(0x10);
respByte(0xfd);
respByte(0x38);
respByte(0x65);
respByte(0x26);
respByte(0xf5);
respByte(0x7d);
send();
}
else {
loginFailure("User creation failed");
}
}
void userCreate2k1()
{
// 28 00 8a 13 05 00 70 61 72 69 73 02 00 66 72 04 (.....paris..fr.
// city state
// 27 00 00 00 06 00 47 a4 fb 5d c2 d7 80 fd 06 00 '.....G..]......
// user name
// c6 89 32 d2 7b 7c f1 09 ..2.{|..
// password
// no city and state:
// 21 00 8a 13 00 00 00 00 04 19 00 00 00 06 00 cb !...............
// city sz key chal username
// state sz
// ee c2 eb b3 34 4c 70 06 00 ea aa 2b 57 ff ba 68 ....4Lp....+W..h
// password
// ac .
size_t n = 4;
std::string city = recvStr(n);
n += 2 + city.length();
std::string state = recvStr(n);
n += 2 + state.length();
std::string username, password;
recvLoginPassword(recvBuffer.bytes() + n, username, password);
INFO_LOG(gameType, "Create user 2k1: login %s city %s state %s", username.c_str(),
city.c_str(), state.c_str());
respond(5003);
if (!username.empty() && !password.empty())
{
respShort(1);
respShort(8);
respSkip(7);
respByte(8);
}
else
{
// FIXME doesn't work. Need actual net dump.
respShort(2); // ???
respStr("Authentication error");
respByte(8);
respByte(1);
respSkip(7);
respShort(8);
}
send();
}
void recordRetrieve()
{
// 25 00
// 5a 1b 04 14 00 00 00 0c 00 b0 23 b8 09 cf 00 af 48 ad 7c 1f 39 5f c8 49 6d 06 00 f8 42 9f d3 8f 65 8e e0
DEBUG_LOG(gameType, "User record:");
std::string username, password;
recvLoginPassword(recvBuffer.bytes() + 4, username, password);
// TODO check username password are valid
std::vector<uint8_t> record = getUserRecord(username, gameType);
respond(7003);
respShort(0);
if (record.empty())
{
uint16_t recordSize = 16;
if (gameType == IGP)
recordSize = 256;
else if (gameType == NBA2K2)
recordSize = 12;
respShort(recordSize);
respSkip(recordSize);
}
else {
respShort(record.size());
respData(record);
}
send();
}
void recordUpdate()
{
DEBUG_LOG(gameType, "Record update:");
std::string username, password;
int n = 4;
const uint8_t *data = recvBuffer.bytes();
n += recvLoginPassword(&data[n], username, password);
// TODO check username password are valid
uint16_t recordSize = recvShort(n);
n += 2;
saveUserRecord(username, gameType, &data[n], recordSize);
// when game starts
// 2f 00 5e 1b 04 3f 00 00 00 07 00 25 a4 49 15 96 3a 3a 7d 06 00 ee 34 49 5c 6b 1a d4 30
// l key chal len encrypted len encrypted ...
// 10 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00
// len params? in-game
// 37 00 5e 1b 04 24 00 00 00 0a 00 d3 67 e8 01 5d 9f 1d e7 b7 5e 19 06 b6 6d a4 06 00 b8 24 63 f4 b6 ee a2 88
// l key chal len encrypted ... len encrypted ...
// 10 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00
// len params in-game
// resp:
// 18 00
// 5b 1b 01 00 10 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00
respond(7003);
if (gameType == OOGABOOGA)
// oogabooga returns 1 here
respShort(1);
else
// other games want 0
respShort(0);
// ooga,wsb2k2 sends 4 longs
// nba2k2 sends 3 longs
// IGP sends 4 or 5 (wins,losses,drops,rating,awards?) * 6 (games) longs? -> 120 bytes
respShort(recordSize);
respBytes(&data[n], recordSize);
send();
}
void getTopUserList()
{
// nba2K2
// 06 00 66 1b 01 00
// resp:
// 7a 04 len (1146!)
// 67 1b 02 00 b2
// 178?
// 07 00 44 43 6d 61 6e 32 31 . .DCman21
// player name
// 33 00 00 00 1f 00 00 00 0a 00 00 00 3....... ....
// 12 bytes data
// 08 00 6e 63 ..nc
// player name
// 6d 61 6e 30 37 31 32 00 00 00 34 00 00 00 0d 00 man0712. ..4.....
// 12 bytes data...
// 00 00
// 0e 00 67 61 6d 69 6e 67 74 72 75 63 6b 65 ....gami ngtrucke
// 72 31 27 00 00 00 0e 00 00 00 04 00 00 00
// 12 bytes data
// end:
// 00 00
std::vector<HighScore> hiScores = getHighScores(gameType);
respond(7015);
respShort(2); // must be 2 (nba2k2)
respByte(0x80 | hiScores.size()); // 0x80: success
// | 0-50: number of records
for (HighScore hs : hiScores)
{
respStr(hs.name);
respLong(hs.wins); // wins
respLong(hs.losses); // losses
respLong(hs.data3); // nfl2k2,nba2k2,ncaa2k2:drops
if (hs.data4 != -1)
respLong(hs.data4); //wsb2k2:drops, not for nba2K2
}
send();
}
void getRegions()
{
// nfl2k1 resp:
// 17 00 a1 0f 01 0a 00 53 68 75 6f 47 61 72 64 65 .......S huoGarde
// 6e 00 00 00 00 00 00
DEBUG_LOG(gameType, "Get regions: len %d", recvShort(0));
respond(4001);
if (is2K1())
respByte(1); // number of regions: { string name, long user count }
else
respByte(0x81);
respStr(RegionName);
respShort(lobby->users.size());
respSkip(4);
send();
}
void getLobbies()
{
// nfl2k1 resp:
// 1b 00 a3 0f 01 08 00 53 68 75 6d 61 6e 69 61 92 .......S humania.
// b9 87 b3 36 b4 00 00 00 00 00 00
std::string region = recvStr(recvBuffer.bytes() + 4);
DEBUG_LOG(gameType, "Get lobby servers: region %s", region.c_str());
std::string lobbyName = LobbyName;
unsigned port = 1;
if (gameType == IGP)
{
lobbyName += " " + region;
if (region == "Poker")
port = 1;
else if (region == "Dominoes")
port = 2;
else if (region == "Othello") {
port = 3;
lobbyName = LobbyName + " Reversi";
}
else if (region == "Spades")
port = 4;
else if (region == "Go")
port = 5;
else if (region == "Chess")
port = 6;
}
respond(4003);
if (is2K1()) {
respByte(1);
}
else {
respShort(0);
respByte(0x81);
}
respStr(lobbyName);
// ip address, port
respData(socket.local_endpoint().address().to_v4().to_bytes());
respShort(htons(socket.local_endpoint().port() + port));
respShort(lobby->users.size());
respSkip(4);
send();
}
void findUser()
{
// 0c 00 32 00 06 00 61 62 63 64 65 66 ..2...abcdef
std::string userName = recvStr(recvBuffer.bytes() + 4);
for (const User::Ptr& user : lobby->users)
{
if (user->name == userName)
{
DEBUG_LOG(gameType, "Found user: %s", userName.c_str());
if (is2K1()) {
respond(401);
respLong(1); // success
}
else
{