-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.cpp
1650 lines (1432 loc) · 57.8 KB
/
client.cpp
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
#include "client.hpp"
#include "utils.hpp"
#include <winsock2.h>
#include <ctime>
#include "config.hpp"
#include "login.hpp"
#include "session.hpp"
#include "version.hpp"
#include <algorithm>
#include "server.hpp"
#include "character.hpp"
#include "session.hpp"
#include "CRC_32.h"
std::vector<Client*> Clients;
bool CL_AddConnection(SOCKET socket, sockaddr_in addr)
{
Client* cl = new Client();
if(!cl) return false;
cl->HisIP = inet_ntoa(addr.sin_addr);
cl->HisPort = ntohs(addr.sin_port);
cl->HisAddr = Format("%s:%u", cl->HisIP.c_str(), cl->HisPort);
cl->Version = 0;
cl->Flags = CLIENT_CONNECTED;
cl->GameMode = 0;
cl->Socket = socket;
cl->Receiver.Connect(cl->Socket);
cl->JoinTime = GetTickCount();
cl->IsBot = false;
cl->DoNotUnlock = false;
Printf(LOG_Trivial, "[CL] %s - Connected.\n", cl->HisAddr.c_str());
Clients.push_back(cl);
return true;
}
bool CL_Screenshot(Client* conn, Packet& pack)
{
std::string login;
uint32_t uid;
uint8_t status;
std::string url;
pack >> login;
pack >> uid;
pack >> status;
if (status)
pack >> url;
uint32_t server_id = (uid & 0xFFFF0000) >> 16;
Server* server = NULL;
for(std::vector<Server*>::iterator it = Servers.begin(); it != Servers.end(); ++it)
{
Server* srv = (*it);
if(!srv) continue;
if (srv->Port == server_id)
{
server = srv;
break;
}
}
if (!server)
{
Printf(LOG_Error, "[CL] %s (%s) - Client tried to send a screenshot for unknown server, ignoring.\n", conn->HisAddr.c_str(), login.c_str());
return false;
}
conn->SessionServer = server;
conn->SessionID1 = uid;
if (status == 0)
{
Printf(LOG_Info, "[CL] %s (%s) - Client is sending a screenshot from server ID %u.\n", conn->HisAddr.c_str(), login.c_str(), server->Number);
SLCMD_Screenshot(server, login, uid, false, "");
conn->Login = login;
conn->Flags |= CLIENT_SCREENSHOT;
return true;
}
else
{
Printf(LOG_Info, "[CL] %s (%s) - Client sent a screenshot (located in \"%s\") to server ID %u.\n", conn->HisAddr.c_str(), login.c_str(), url.c_str(), server->Number);
SLCMD_Screenshot(server, login, uid, true, url);
conn->SessionServer = NULL;
return false;
}
}
bool CL_Process(Client* conn)
{
if(!(conn->Flags & CLIENT_CONNECTED)) return false;
if(!conn->Socket) return false;
if(!conn->Receiver.Receive(conn->Version)) return false;
// inactive kick
if(GetTickCount()-conn->JoinTime > (Config::ClientTimeout*1000) && !(conn->Flags & (CLIENT_LOGGED_IN|CLIENT_PATCHFILE)))
{
Printf(LOG_Warning, "[CL] %s - Client has timed out.\n", conn->HisAddr.c_str());
CLCMD_Kick(conn, P_FHTAGN);
return false;
}
// active kick
if(GetTickCount()-conn->JoinTime > (Config::ClientActiveTimeout*1000) && !(conn->Flags & (CLIENT_PATCHFILE)))
{
Printf(LOG_Warning, "[CL] %s%s - Client (active) has timed out.\n", conn->HisAddr.c_str(), (conn->Login.length() ? Format(" (%s)", conn->Login.c_str()).c_str() : ""));
CLCMD_Kick(conn, P_FHTAGN);
return false;
}
if((conn->Flags & CLIENT_LOGGED_IN) &&
(conn->Flags & CLIENT_COMPLETE)) return CL_ServerProcess(conn);
Packet pack;
while(conn->Receiver.GetPacket(pack))
{
// check 0x5C0EE250
uint32_t packet_uid;
pack >> packet_uid;
if ((packet_uid == SCREENSHOT_PID) && (conn->Flags == CLIENT_CONNECTED || (conn->Flags & CLIENT_SCREENSHOT)))
{
if (!CL_Screenshot(conn, pack))
return false;
continue;
}
else if ((packet_uid != SCREENSHOT_PID) && (conn->Flags & CLIENT_SCREENSHOT))
{
Printf(LOG_Error, "[CL] %s (%s) - Client sent unexpected packet while in screenshot state.\n", conn->HisAddr.c_str(), conn->Login.c_str());
return false;
}
pack.ResetPosition();
if(!(conn->Flags & CLIENT_LOGGED_IN))
{
if(!CL_Login(conn, pack))
return false;
continue;
}
uint8_t packet_id;
pack >> packet_id;
pack.ResetPosition();
switch(packet_id)
{
default:
Printf(LOG_Error, "[CL] %s%s - Received unknown packet %02X.\n", conn->HisAddr.c_str(), (conn->Login.length() ? Format(" (%s)", conn->Login.c_str()).c_str() : ""), packet_id);
return false;
case 0xCA: // character request
if(!CL_Character(conn, pack)) return false;
break;
case 0xC8: // server list request
if(!CL_ServerList(conn, pack)) return false;
break;
case 0xCB: // enter server
if(!CL_EnterServer(conn, pack)) return false;
break;
case 0x4E: // nickname check
if(!CL_CheckNickname(conn, pack)) return false;
break;
case 0xCC: // delete character
if(!CL_DeleteCharacter(conn, pack)) return false;
break;
}
}
return true;
}
bool CL_TransferProcess(Client* conn)
{
return true;
}
bool CL_PatchDownload(Client* conn, Packet& pack)
{
return true;
}
bool CL_ServerProcess(Client* conn)
{
if(conn->IsBot)
{
CLCMD_Kick(conn, P_WRONG_VERSION);
return false;
}
uint32_t dtime = time(NULL);
if((dtime - conn->SessionTime) > 15)
{
SESSION_DelLogin(conn->SessionID1, conn->SessionID2);
return false;
}
unsigned long result = SESSION_GetLogin(conn->SessionID1, conn->SessionID2);
if(result == 0xFFFFFFFF) return true;
SESSION_DelLogin(conn->SessionID1, conn->SessionID2);
if(result == 0xBADFACE0) // db error
{
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
else if(result == 0xBADFACE1) // server lost
{
CLCMD_Kick(conn, P_SERVER_LOST);
return false;
}
else if(result == 0xBADFACE2) // hacking
{
CLCMD_Kick(conn, P_FUCK_OFF);
return false;
}
std::string cas = "unknown";
unsigned long retval = 0;
switch(result)
{
case 1:
cas = "server full";
retval = P_SERVER_FULL;
break;
case 2:
cas = "duplicated nickname";
retval = P_CHARACTER_EXISTS;
break;
case 3:
cas = "invalid nickname";
retval = P_WRONG_NAME;
break;
case 4:
cas = "too short nickname";
retval = P_SHORT_NAME;
break;
case 5:
cas = "invalid character data";
retval = P_BAD_CHARACTER;
break;
case 6:
cas = "too strong for this map";
retval = P_S_TOO_STRONG;
break;
case 7:
cas = "too weak for this map";
retval = P_S_TOO_WEAK;
break;
case 8:
cas = "teamplay already started";
retval = P_TEAMPLAY_STARTED;
break;
case 9:
cas = "shutdown initiated";
retval = P_SERVER_SHUTDOWN;
break;
default: break;
}
if(retval != 0)
{
Printf(LOG_Error, "[CL] %s (%s) - Character \"%s\" rejected by server ID %u (reason: %s).\n", conn->HisAddr.c_str(), conn->Login.c_str(), conn->SessionNickname.c_str(), conn->SessionServer->Number, cas.c_str());
CLCMD_Kick(conn, retval);
return false;
}
Login_SetLocked(conn->Login, false, true, conn->SessionID1, conn->SessionID2, conn->SessionServer->Number);
if(!CLCMD_EnterSuccess(conn, conn->SessionID1, conn->SessionID2))
return false;
Printf(LOG_Info, "[CL] %s (%s) - Character \"%s\" entered server ID %u.\n", conn->HisAddr.c_str(), conn->Login.c_str(), conn->SessionNickname.c_str(), conn->SessionServer->Number);
bool l_muted = false;
unsigned long l_muted_date;
unsigned long l_muted_unmutedate;
std::string l_reason;
if (!Login_GetMuted(conn->Login, l_muted, l_muted_date, l_muted_unmutedate, l_reason))
{
Printf(LOG_Error, "[DB] Error: Login_GetMuted(\"%s\", ...).\n", conn->Login.c_str());
l_muted = false;
}
if (l_muted && l_muted_unmutedate > time(NULL))
{
Printf(LOG_Info, "[CL] %s (%s) - Character should be muted (reason: %s).\n", conn->HisAddr.c_str(), conn->Login.c_str(), l_reason.c_str());
SLCMD_MutePlayer(conn->SessionServer, conn->Login, l_muted_unmutedate);
}
return false;
}
void CL_Disconnect(Client* conn)
{
if ((conn->Flags & CLIENT_SCREENSHOT) && conn->SessionServer)
SLCMD_Screenshot(conn->SessionServer, conn->Login, conn->SessionID1, true, "");
Printf(LOG_Trivial, "[CL] %s%s - Disconnected.\n", conn->HisAddr.c_str(), (conn->Login.length() ? Format(" (%s)", conn->Login.c_str()).c_str() : ""));
if(!conn->DoNotUnlock && conn->Login.length() && !Login_UnlockOne(conn->Login))
Printf(LOG_Error, "[DB] Error: Login_UnlockOne(\"%s\").\n", conn->Login.c_str());
SOCK_Destroy(conn->Socket);
conn->Socket = 0;
conn->Flags &= ~CLIENT_CONNECTED;
}
void Net_ProcessClients()
{
for(std::vector<Client*>::iterator it = Clients.begin(); it != Clients.end(); ++it)
{
Client* conn = (*it);
if(!CL_Process(conn))
{
std::vector<Client*>::iterator do_it = it;
it--;
Clients.erase(do_it);
CL_Disconnect(conn);
delete conn;
}
}
}
void CL_VersionInfo(Client* conn, Packet& pack)
{
uint16_t key1, key2;
srand(time(NULL));
key1 = rand();
srand(key1);
key2 = rand();
uint32_t key = key1;
key <<= 16;
key |= key2;
//conn->ClientKey = key;
uint32_t sesskey = key ^ 0xDEADFACE;
uint32_t sessid = V_AddSession(key) ^ sesskey;
//conn->ClientID = sessid;
Packet pck;
pck << (uint8_t)0xFF;
pck << sessid;
pck << key;
SOCK_SendPacket(conn->Socket, pck, conn->Version);
}
bool CL_Login(Client* conn, Packet& pack)
{
std::ifstream ifs;
ifs.open("redhat.ipf", std::ios::in);
int32_t admin_level = 0;
int32_t access_level = 0;
std::string admin_name = "";
IPFilter::IPFFile ipf;
if(ifs.is_open())
{
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
ipf.ReadIPF(str);
int32_t result = ipf.CheckAddress(conn->HisIP, admin_name);
if(result == 2 || result == 3)
{
admin_level = result - 1;
}
else if(result == -10)
{
admin_level = -10;
}
access_level = result;
ifs.close();
}
admin_name = Trim(admin_name);
if(admin_level >= 1 && !admin_name.length()) admin_name = "unknown";
// detect version
uint8_t cr;
pack >> cr;
uint32_t uoth;
pack >> uoth;
pack.ResetPosition();
uint8_t datatmp[0x41];
pack.GetData(datatmp, 0x41);
uint32_t utth;
pack >> utth;
pack.ResetPosition();
if(cr == (0xFF ^ key_20[0])) // 2.0 version info, not auth at all
{
conn->Version = 20;
PACKET_Crypt(pack, conn->Version);
CL_VersionInfo(conn, pack);
return false;
}
/*else if(cr == (0xFC ^ key_20[0])) // 2.0 patch download request
{
conn->Version = 20;
conn->Flags |= CLIENT_PATCHFILE;
PACKET_Crypt(pack, conn->Version);
return CL_PatchDownload(conn, pack);
}*/
else if(cr == key_20[0]) // 2.0 auth
{
utth = utth ^ *(uint32_t*)(key_20+0x41);
unsigned char ver = (utth & 0xFF000000) >> 24;
if(ver >= 20) conn->Version = ver;
}
/*else if(cr == key_15[0])
{
utth = utth ^ *(uint32_t*)(key_15+0x41);
unsigned char ver = (utth & 0xFF000000) >> 24;
if(ver >= 15 && ver <= 19) conn->Version = ver;
}*/ // absent protocol version
else if(cr == key_11[0]) // 1.11 auth
{
utth = utth ^ *(uint32_t*)(key_11+0x41);
unsigned char ver = (utth & 0xFF000000) >> 24;
if(ver >= 11 && ver <= 15) conn->Version = ver;
}
else if(cr == (0x29 ^ key_10[0]))
{
uoth = uoth ^ *(uint32_t*)(key_10+1);
if((uoth & 0x8C000000) == 0x8C000000)
conn->Version = 10;
}
else if(cr == (0xC9 ^ key_08[0]))
{
uoth = uoth ^ *(uint32_t*)(key_08+1);
unsigned char ver = (uoth & 0xFF000000) >> 24;
if(ver > 0 && ver <= 7) conn->Version = ver;
else if(ver == 0x0A5) conn->Version = 8;
conn->Version = 8;
}
if(conn->Version == 0)
{
Printf(LOG_Error, "[CL] %s - Unknown client version (signature %02X).\n", conn->HisAddr.c_str(), cr);
CLCMD_Kick(conn, P_WRONG_VERSION);
return false;
}
PACKET_Crypt(pack, conn->Version);
uint8_t packet_id;
pack >> packet_id;
if(conn->Version >= 11) pack.GetData(datatmp, 0x40);
pack >> uoth;
uint8_t prc_uuid[20];
memset(prc_uuid, 0, 20);
// check version
if(conn->Version >= 20)
{
uint32_t prc_allods2_exe = *(uint32_t*)(datatmp),
prc_a2mgr_dll = *(uint32_t*)(datatmp + 0x04),
prc_patch_res = *(uint32_t*)(datatmp + 0x08),
prc_world_res = *(uint32_t*)(datatmp + 0x0C),
prc_graphics_res = *(uint32_t*)(datatmp + 0x10),
prc_sessid = *(uint32_t*)(datatmp + 0x14);
uint32_t lrc_sessid = prc_sessid ^ 0xDEADBEEF;
uint32_t net_key = 0x6CDB248D;//V_GetSession(lrc_sessid);
memcpy(prc_uuid, datatmp + 0x18, 20);
for(int i = 0; i < 5; i++)
*(uint32_t*)(prc_uuid + i * 4) ^= net_key;
CRC_32 crc;
uint32_t prc_uuid_crc = crc.CalcCRC(prc_uuid, 20);
uint32_t prc_uuid_crc_original = *(uint32_t*)(datatmp + 0x2C) ^ net_key;
if(prc_uuid_crc_original != prc_uuid_crc)
{
Printf(LOG_Error, "[CL] %s - Hacking: UUID has been tampered with.\n", conn->HisAddr.c_str());
CLCMD_Kick(conn, P_FHTAGN);
return false;
}
uint32_t lrc_allods2_exe = prc_allods2_exe ^ net_key,
lrc_a2mgr_dll = (prc_a2mgr_dll - lrc_allods2_exe) ^ net_key,
lrc_patch_res = prc_patch_res ^ lrc_a2mgr_dll,
lrc_world_res = (prc_world_res + lrc_patch_res) ^ net_key,
lrc_graphics_res = (prc_graphics_res ^ net_key) - lrc_allods2_exe;
std::string crcstr = "";
bool badcrc = false;
if(std::find(Config::ExecutableCRC.begin(), Config::ExecutableCRC.end(), lrc_allods2_exe) == Config::ExecutableCRC.end())
{
badcrc = true;
crcstr += Format("allods2.exe: %08X", lrc_allods2_exe);
}
if(std::find(Config::LibraryCRC.begin(), Config::LibraryCRC.end(), lrc_a2mgr_dll) == Config::LibraryCRC.end())
{
if(badcrc) crcstr += "; ";
crcstr += Format("a2mgr.dll: %08X", lrc_a2mgr_dll);
badcrc = true;
}
/*
if(std::find(Config::PatchCRC.begin(), Config::PatchCRC.end(), lrc_patch_res) == Config::PatchCRC.end())
{
if(badcrc) crcstr += "; ";
crcstr += Format("patch.res: %08X", lrc_patch_res);
badcrc = true;
}
if(std::find(Config::WorldCRC.begin(), Config::WorldCRC.end(), lrc_world_res) == Config::WorldCRC.end())
{
if(badcrc) crcstr += "; ";
crcstr += Format("world.res: %08X", lrc_world_res);
badcrc = true;
}*/
/*if(std::find(Config::GraphicsCRC.begin(), Config::GraphicsCRC.end(), lrc_graphics_res) == Config::GraphicsCRC.end())
{
if(badcrc) crcstr += "; ";
crcstr += Format("graphics.res: %08X", lrc_graphics_res);
badcrc = true;
}*/
V_DelSession(lrc_sessid);
if(badcrc)
{
Printf(LOG_Error, "[CL] %s - Client CRC mismatch (%s).\n", conn->HisAddr.c_str(), crcstr.c_str());
if(admin_level < 1)
{
CLCMD_Kick(conn, P_WRONG_VERSION);
return false;
}
else Printf(LOG_Info, "[CL] %s - Admin access used (auth: %s)\n", conn->HisAddr.c_str(), admin_name.c_str());
}
}
std::string uuid = "";
for(int i = 0; i < 20; i++)
uuid += Format("%02x", prc_uuid[i]);
if(conn->Version != Config::ProtocolVersion)
{
if(admin_level == -10) // ex-lend
{
Printf(LOG_Warning, "[CL] %s - Switching to bot mode...\n", conn->HisAddr.c_str());
conn->IsBot = true;
}
else
{
Printf(LOG_Error, "[CL] %s - Client connected with wrong protocol version (%u).\n", conn->HisAddr.c_str(), conn->Version);
CLCMD_Kick(conn, P_WRONG_VERSION);
return false;
}
}
uint8_t p_version = (uoth & 0xFF000000) >> 24;
uint8_t p_gamemode = (uoth & 0x00FF0000) >> 16;
uint16_t p_loginlen = (uoth & 0x0000FFFF);
if(p_gamemode != GAMEMODE_Arena &&
p_gamemode != GAMEMODE_Cooperative &&
p_gamemode != GAMEMODE_Softcore &&
p_gamemode != GAMEMODE_Sandbox)
{
Printf(LOG_Error, "[CL] %s - Bad game mode %u.\n", conn->HisAddr.c_str(), p_gamemode);
CLCMD_Kick(conn, P_BAD_GAMEMODE);
return false;
}
if (p_gamemode == GAMEMODE_Softcore)
conn->HatID = Config::HatIDSoftcore;
else if (p_gamemode == GAMEMODE_Sandbox)
conn->HatID = Config::HatIDSandbox;
else if (p_gamemode == GAMEMODE_Arena)
conn->HatID = 0xFFFFFFFF;
else conn->HatID = Config::HatID;
if(conn->IsBot) // ex-lend
{
conn->GameMode = p_gamemode;
conn->Flags |= CLIENT_LOGGED_IN;
return CLCMD_SendCharacterList(conn);
}
std::string logstring;
pack >> logstring;
std::string s_login = logstring;
s_login.erase(p_loginlen);
std::string s_password = logstring;
s_password.erase(0, p_loginlen);
s_login = Trim(s_login);
if(access_level == -1)
{
Printf(LOG_Error, "[CL] %s (%s) - IP blocked by global rules.\n", conn->HisAddr.c_str(), s_login.c_str());
CLCMD_Kick(conn, P_FHTAGN); // "Êòóëõó ôõòàãí!"
return false;
}
// 09.09.2013 - added check for UUID
int32_t result = ipf.CheckUUID(uuid);
access_level = result;
if(access_level == -100)
{
Printf(LOG_Error, "[CL] %s (%s) - UUID blocked by global rules.\n", conn->HisAddr.c_str(), s_login.c_str());
Printf(LOG_Info, "[CL] %s (%s) - UUID: %s.\n", conn->HisAddr.c_str(), s_login.c_str(), uuid.c_str());
CLCMD_Kick(conn, P_FHTAGN);
return false;
}
if(!Login_Exists(s_login))
{
if(Config::AutoRegister && Login_Create(s_login, s_password))
Printf(LOG_Info, "[CL] %s - Auto-registered login %s.\n", conn->HisAddr.c_str(), s_login.c_str(), s_login.c_str());
else
{
Printf(LOG_Error, "[CL] %s - Tried to open non-existent login %s.\n", conn->HisAddr.c_str(), s_login.c_str());
CLCMD_Kick(conn, P_WRONG_CREDENTIALS);
return false;
}
}
std::string l_ipf;
if(!Login_GetIPF(s_login, l_ipf))
{
Printf(LOG_Error, "[DB] Error: Login_GetIPF(\"%s\", <ipf>).\n", s_login.c_str());
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
if(l_ipf.length())
{
IPFilter::IPFFile ipf;
ipf.ReadIPF(l_ipf);
if(ipf.CheckAddress(conn->HisIP, admin_name) != 1)
{
if(CheckInt(s_login) && (admin_level >= 1))
{
Printf(LOG_Warning, "[CL] %s (%s) - IP blocked by local rules, GM access used (auth: %s)\n", conn->HisAddr.c_str(), s_login.c_str(), admin_name.c_str());
}
else if(!CheckInt(s_login) && (admin_level >= 2))
{
Printf(LOG_Warning, "[CL] %s (%s) - IP blocked by local rules, admin access used (auth: %s)\n", conn->HisAddr.c_str(), s_login.c_str(), admin_name.c_str());
}
else if(!admin_level)
{
Printf(LOG_Error, "[CL] %s (%s) - IP blocked by local rules.\n", conn->HisAddr.c_str(), s_login.c_str());
CLCMD_Kick(conn, P_IP_BLOCKED);
return false;
}
}
}
std::string passwd_1;
if(!Login_GetPassword(s_login, passwd_1))
{
Printf(LOG_Error, "[DB] Error: Login_GetPassword(\"%s\", <password>).\n", s_login.c_str());
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
std::string passwd_2 = Login_MakePassword(s_password);
if(passwd_1 != passwd_2)
{
if(CheckInt(s_login) && (admin_level >= 1))
{
Printf(LOG_Warning, "[CL] %s (%s) - Password mismatch, GM access used (auth: %s)\n", conn->HisAddr.c_str(), s_login.c_str(), admin_name.c_str());
}
else if(!CheckInt(s_login) && (admin_level >= 2))
{
Printf(LOG_Warning, "[CL] %s (%s) - Password mismatch, admin access used (auth: %s)\n", conn->HisAddr.c_str(), s_login.c_str(), admin_name.c_str());
}
else
{
Printf(LOG_Error, "[CL] %s (%s) - Password mismatch.\n", conn->HisAddr.c_str(), s_login.c_str());
CLCMD_Kick(conn, P_WRONG_CREDENTIALS);
return false;
}
}
bool l_locked_hat, l_locked;
unsigned long l_id1, l_id2, l_srvid;
if(!Login_GetLocked(s_login, l_locked_hat, l_locked, l_id1, l_id2, l_srvid))
{
Printf(LOG_Error, "[DB] Error: Login_GetLocked(\"%s\", <locked_hat>, <locked>, <id1>, <id2>, <srvid>).\n", s_login.c_str());
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
unsigned long ban_time, unban_time;
std::string ban_reason;
bool ban_active;
unsigned long ctime = time(NULL);
if(!Login_GetBanned(s_login, ban_active, ban_time, unban_time, ban_reason))
{
Printf(LOG_Error, "[DB] Error: Login_GetBanned(\"%s\", <banned>, <date_ban>, <date_unban>, <reason>).\n", s_login.c_str());
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
if(ban_active)
{
bool ban_intime = false;
if((ban_time > unban_time) || (unban_time > 0x7FFFFFFF))
{
Printf(LOG_Error, "[CL] %s (%s) - Login banned forever (reason: %s).\n", conn->HisAddr.c_str(), s_login.c_str(), ban_reason.c_str());
if(conn->Version >= 20 && conn->Version <= 10) CLCMD_Kick(conn, P_LOGIN_BLOCKED_FVR);
else CLCMD_Kick(conn, P_LOGIN_BLOCKED);
ban_intime = true;
}
else if(ctime < unban_time)
{
if(ban_time > ctime) Printf(LOG_Warning, "[CL] %s (%s) - Ban date is bigger than current date (by %us)!\n", conn->HisAddr.c_str(), s_login.c_str(), ban_time - ctime);
Printf(LOG_Error, "[CL] %s (%s) - Login banned (reason: %s).\n", conn->HisAddr.c_str(), s_login.c_str(), ban_reason.c_str());
CLCMD_Kick(conn, P_LOGIN_BLOCKED);
ban_intime = true;
}
if(!ban_intime)
{
if(!Login_SetBanned(s_login, false, 0, 0, ""))
{
Printf(LOG_Error, "[DB] Error: Login_SetBanned(\"%s\", false, 0, 0, \"\").\n", s_login.c_str());
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
}
else return false;
}
if(l_locked)
{
bool r_cancel_lock = false;
for(std::vector<Server*>::iterator it = Servers.begin(); it != Servers.end(); ++it)
{
Server* srv = (*it);
if(!srv) continue;
if(srv->Number == l_srvid)
{
if(!srv->Connection || !srv->Connection->Active)
{
Printf(LOG_Error, "[CL] %s (%s) - Locked on offline server ID %u!\n", conn->HisAddr.c_str(), s_login.c_str(), srv->Number);
CLCMD_Kick(conn, P_SERVER_OFFLINE);
return false;
}
if(srv->Info.ServerCaps & SERVER_CAP_DETAILED_INFO)
{
bool char_on_server = false;
for(std::vector<ServerPlayer>::iterator jt = srv->Info.Players.begin(); jt != srv->Info.Players.end(); ++jt)
{
ServerPlayer& player = (*jt);
if(player.Login == s_login && player.Id1 == l_id1 && player.Id2 == l_id2)
char_on_server = true;
}
for(std::vector<std::string>::iterator jt = srv->Info.Locked.begin(); jt != srv->Info.Locked.end(); ++jt)
{
std::string& login = (*jt);
if(ToLower(login) == ToLower(s_login))
char_on_server = true;
}
if(!char_on_server && srv->Info.Time <= 15) char_on_server = true;
if(!char_on_server)
{
Printf(LOG_Info, "[CL] %s (%s) - Login lock dropped (not on server ID %u).\n", conn->HisAddr.c_str(), s_login.c_str(), srv->Number);
r_cancel_lock = true;
}
/// ÄÞÏ!!!!!
// todo: ïîôèêñèòü ïðîâåðêó ëîãèíîâ íà ñåðâåðå!
/// ñåðâåðà ñîõðàíÿþò ïåðñîíàæåé íàïðÿìóþ â áàçó, âåðíóëè íà ìåñòî
/// 19.06.2014 - ýòî ÷òî ÿ èìåë â âèäó? (è ãëàâíîå, êîãäà?)
/// 2022 - ïðèâåò èç íîâîãî äåñÿòèëåòèÿ, äþï âèäèìî îñòàíåòñÿ
}
if(r_cancel_lock)
{
/*l_locked = false;
if(!Login_SetLocked(s_login, true, false, 0, 0, 0))
{
Printf(LOG_Error, "[DB] Error: Login_SetLocked(\"%s\", <locked>, <id1>, <id2>, <srvid>).\n", s_login.c_str());
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}*/
break;
}
//if((((srv->Info.ServerMode & SVF_SOFTCORE) == SVF_SOFTCORE) != (p_gamemode == GAMEMODE_Softcore)) || (((srv->Info.ServerMode & SVF_SOFTCORE) != SVF_SOFTCORE) && (srv->Info.GameMode != p_gamemode)))
if (srv->Info.GameMode != p_gamemode)
{
Printf(LOG_Error, "[CL] %s (%s) - Locked on server ID %u with different game mode (%u != %u)!\n", conn->HisAddr.c_str(), s_login.c_str(), srv->Number, p_gamemode, srv->Info.GameMode);
CLCMD_Kick(conn, P_WRONG_GAMEMODE);
return false;
}
char* c_data = NULL;
unsigned long c_size = 0;
std::string c_nickname;
if(!Login_GetCharacter(s_login, l_id1, l_id2, c_size, c_data, c_nickname) || !c_data)
{
Printf(LOG_Error, "[DB] Error: Login_GetCharacter(\"%s\", %u, %u, <size>, <data>, <nickname>).\n", s_login.c_str(), l_id1, l_id2);
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
if(c_size != 0x30)
{
Character chr;
chr.LoadFromBuffer(c_data, c_size);
delete[] c_data;
c_nickname = chr.Nick;
if(chr.Clan.length()) c_nickname += "|" + chr.Clan;
}
else
{
char* nik = new char[(unsigned char)c_data[4] + 1];
nik[(unsigned char)c_data[4]] = 0;
memcpy(nik, c_data + 20, (unsigned char)c_data[4]);
c_nickname = std::string(nik);
delete[] nik;
}
if(!CLCMD_SendReconnect(conn, l_id1, l_id2, Format("%s:%u", srv->Address.c_str(), srv->Port)))return false;
Printf(LOG_Info, "[CL] %s (%s) - Character \"%s\" entered server ID %u (reconnected).\n", conn->HisAddr.c_str(), s_login.c_str(), c_nickname.c_str(), srv->Number);
return false;
}
}
if(!r_cancel_lock)
{
Printf(LOG_Error, "[CL] %s (%s) - Login locked on invalid server ID %u!\n", conn->HisAddr.c_str(), s_login.c_str(), l_srvid);
CLCMD_Kick(conn, P_SERVER_INVALID);
return false;
}
}
else
{
// login is not locked, but hat-locked (is online)
if(l_locked_hat)
{
// check if the client is actually online (most likely) and destroy the instance
for(size_t i = 0; i < Clients.size(); i++)
{
if(Clients[i] == conn) continue;
if(Clients[i]->Login == s_login)
{
Printf(LOG_Error, "[CL] %s (%s) - Discarding connection (logged in again).\n", Clients[i]->HisAddr.c_str(), s_login.c_str());
CLCMD_Kick(Clients[i], P_LOGIN_EXISTS);
SOCK_Destroy(Clients[i]->Socket);
Clients[i]->DoNotUnlock = true;
}
}
//Printf(LOG_Error, "[CL] %s (%s) - Login is hat-locked, rejecting.\n", conn->HisAddr.c_str(), s_login.c_str());
//CLCMD_Kick(conn, P_LOGIN_EXISTS);
//return false;
}
l_locked_hat = false;
// 19.06.2014 dupe fix
for(std::vector<Server*>::iterator it = Servers.begin(); it != Servers.end(); ++it)
{
Server* srv = (*it);
if(!srv) continue;
if(!srv->Connection || !srv->Connection->Active)
continue; // âîò òóò âñ¸ ðàâíî åñòü øàíñ ïðîëåçòü ñêâîçü ïðîâåðêó... õîòÿ ïî èäåå òîãäà íå ïóñòèò
if(srv->Info.ServerCaps & SERVER_CAP_DETAILED_INFO)
{
bool char_on_server = false;
for(std::vector<ServerPlayer>::iterator jt = srv->Info.Players.begin(); jt != srv->Info.Players.end(); ++jt)
{
ServerPlayer& player = (*jt);
if(player.Login == s_login && player.Id1 == l_id1 && player.Id2 == l_id2)
char_on_server = true;
}
for(std::vector<std::string>::iterator jt = srv->Info.Locked.begin(); jt != srv->Info.Locked.end(); ++jt)
{
std::string& login = (*jt);
if(ToLower(login) == ToLower(s_login))
char_on_server = true;
}
//if(!char_on_server && srv->Info.Time <= 15) char_on_server = true; /// óáðàíî: ëþäè íå ñìîãóò âõîäèòü íà õýò âî âðåìÿ ñìåíû ëþáîé êàðòû
if(char_on_server)
{
Printf(LOG_Error, "[CL] %s (%s) - Bug: login unlocked but still ingame (playing on server ID %u)!\n", conn->HisAddr.c_str(), s_login.c_str(), srv->Number);
//CLCMD_Kick(conn, P_FHTAGN);
CLCMD_Kick(conn, P_LOGIN_EXISTS); // ÿ íå ïîìíþ, ÷òî ýòî... ñêîðåå âñåãî "âàø ëîãèí óæå â èãðå"
return false;
}
}
}
}
if(!Login_SetLocked(s_login, true, false, 0, 0, 0))
{
Printf(LOG_Error, "[DB] Error: Login_SetLocked(\"%s\", <locked_hat>, <locked>, <id1>, <id2>, <srvid>).\n", s_login.c_str());
Printf(LOG_Error, "[DB] %s\n", SQL_Error().c_str());
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
Printf(LOG_Info, "[CL] %s (%s) - Logged in successfully.\n", conn->HisAddr.c_str(), s_login.c_str());
Printf(LOG_Info, "[CL] %s (%s) - UUID: %s.\n", conn->HisAddr.c_str(), s_login.c_str(), uuid.c_str());
if (!Login_LogAuthentication(s_login, conn->HisIP, uuid))
{
Printf(LOG_Error, "[DB] Error: Login_LogAuthentication(\"%s\", \"%s\", \"%s\").\n", s_login.c_str(), conn->HisIP.c_str(), uuid.c_str());
//CLCMD_Kick(conn, P_UPDATE_ERROR);
//return false;
}
conn->Login = s_login;
conn->GameMode = p_gamemode;
conn->Flags |= CLIENT_LOGGED_IN;
return CLCMD_SendCharacterList(conn);
}
bool CL_Character(Client* conn, Packet& pack)
{
uint8_t packet_id;
pack >> packet_id;
if(packet_id != 0xCA) return false;
uint32_t id1 = 0, id2 = 0;
pack >> id1 >> id2;
return CLCMD_SendCharacter(conn, id1, id2);
}
bool CL_Authorize(Client* conn, Packet& pack)
{
return false;
}
void CLCMD_Kick(Client* conn, uint8_t reason)
{
if(reason == P_FUCK_OFF && conn->Version >= 20)
reason = P_FHTAGN;
Packet pack;
pack << (uint8_t)0x0B;
pack << reason;
pack << (uint32_t)0;
SOCK_SendPacket(conn->Socket, pack, conn->Version);
}
bool CLCMD_SendCharacterList(Client* conn)
{
std::vector<CharacterInfo> chars;
if(!conn->IsBot)
{
if(!Login_GetCharacterList(conn->Login, chars, conn->HatID))
{
Printf(LOG_Error, "[DB] Error: Login_GetCharacterList(\"%s\", <info>).\n", conn->Login.c_str());
CLCMD_Kick(conn, P_UPDATE_ERROR);
return false;
}
}
Packet pack;
pack << (uint8_t)0xCE;
pack << (uint32_t)chars.size() * 8 + 4;
pack << (uint32_t)Config::HatID;
for(size_t i = 0; i < chars.size(); i++)
{
pack << (uint32_t)chars[i].ID1;
pack << (uint32_t)chars[i].ID2;
}
return (SOCK_SendPacket(conn->Socket, pack, conn->Version) == 0);
}
bool CLCMD_SendCharacter(Client* conn, unsigned long id1, unsigned long id2)
{