1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#include "server.h"
5
6#include "databases/connection.h"
7#include "databases/connection_pool.h"
8#include "register.h"
9
10#include <base/bytes.h>
11#include <base/fs.h>
12#include <base/io.h>
13#include <base/logger.h>
14#include <base/math.h>
15#include <base/secure.h>
16
17#include <engine/config.h>
18#include <engine/console.h>
19#include <engine/engine.h>
20#include <engine/map.h>
21#include <engine/server.h>
22#include <engine/server/authmanager.h>
23#include <engine/shared/compression.h>
24#include <engine/shared/config.h>
25#include <engine/shared/console.h>
26#include <engine/shared/demo.h>
27#include <engine/shared/econ.h>
28#include <engine/shared/fifo.h>
29#include <engine/shared/filecollection.h>
30#include <engine/shared/host_lookup.h>
31#include <engine/shared/http.h>
32#include <engine/shared/json.h>
33#include <engine/shared/jsonwriter.h>
34#include <engine/shared/linereader.h>
35#include <engine/shared/masterserver.h>
36#include <engine/shared/netban.h>
37#include <engine/shared/network.h>
38#include <engine/shared/packer.h>
39#include <engine/shared/protocol.h>
40#include <engine/shared/protocol7.h>
41#include <engine/shared/protocol_ex.h>
42#include <engine/shared/rust_version.h>
43#include <engine/shared/snapshot.h>
44#include <engine/storage.h>
45
46#include <game/version.h>
47
48#include <zlib.h>
49
50#include <chrono>
51#include <vector>
52
53using namespace std::chrono_literals;
54
55#if defined(CONF_PLATFORM_ANDROID)
56extern std::vector<std::string> FetchAndroidServerCommandQueue();
57#endif
58
59void CServerBan::InitServerBan(IConsole *pConsole, IStorage *pStorage, CServer *pServer)
60{
61 CNetBan::Init(pConsole, pStorage);
62
63 m_pServer = pServer;
64
65 Console()->Register(pName: "ban", pParams: "s[ip|id] ?i[minutes] r[reason]", Flags: CFGFLAG_SERVER | CFGFLAG_STORE, pfnFunc: ConBanExt, pUser: this, pHelp: "Ban player with ip/client id for x minutes for any reason");
66 Console()->Register(pName: "ban_region", pParams: "s[region] s[ip|id] ?i[minutes] r[reason]", Flags: CFGFLAG_SERVER | CFGFLAG_STORE, pfnFunc: ConBanRegion, pUser: this, pHelp: "Ban player in a region");
67 Console()->Register(pName: "ban_region_range", pParams: "s[region] s[first ip] s[last ip] ?i[minutes] r[reason]", Flags: CFGFLAG_SERVER | CFGFLAG_STORE, pfnFunc: ConBanRegionRange, pUser: this, pHelp: "Ban range in a region");
68}
69
70template<class T>
71int CServerBan::BanExt(T *pBanPool, const typename T::CDataType *pData, int Seconds, const char *pReason, bool VerbatimReason)
72{
73 // validate address
74 if(Server()->m_RconClientId >= 0 && Server()->m_RconClientId < MAX_CLIENTS &&
75 Server()->m_aClients[Server()->m_RconClientId].m_State != CServer::CClient::STATE_EMPTY)
76 {
77 if(NetMatch(pData, Server()->ClientAddr(ClientId: Server()->m_RconClientId)))
78 {
79 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "net_ban", pStr: "ban error (you can't ban yourself)");
80 return -1;
81 }
82
83 for(int i = 0; i < MAX_CLIENTS; ++i)
84 {
85 if(i == Server()->m_RconClientId || Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY)
86 continue;
87
88 if(Server()->GetAuthedState(ClientId: i) >= Server()->m_RconAuthLevel && NetMatch(pData, Server()->ClientAddr(ClientId: i)))
89 {
90 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "net_ban", pStr: "ban error (command denied)");
91 return -1;
92 }
93 }
94 }
95 else if(Server()->m_RconClientId == IServer::RCON_CID_VOTE)
96 {
97 for(int i = 0; i < MAX_CLIENTS; ++i)
98 {
99 if(Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY)
100 continue;
101
102 if(Server()->IsRconAuthed(ClientId: i) && NetMatch(pData, Server()->ClientAddr(ClientId: i)))
103 {
104 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "net_ban", pStr: "ban error (command denied)");
105 return -1;
106 }
107 }
108 }
109
110 int Result = Ban(pBanPool, pData, Seconds, pReason, VerbatimReason);
111 if(Result != 0)
112 return Result;
113
114 // drop banned clients
115 typename T::CDataType Data = *pData;
116 for(int i = 0; i < MAX_CLIENTS; ++i)
117 {
118 if(Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY)
119 continue;
120
121 if(NetMatch(&Data, Server()->ClientAddr(ClientId: i)))
122 {
123 CNetHash NetHash(&Data);
124 char aBuf[256];
125 MakeBanInfo(pBanPool->Find(&Data, &NetHash), aBuf, sizeof(aBuf), MSGTYPE_PLAYER);
126 Server()->m_NetServer.Drop(ClientId: i, pReason: aBuf);
127 }
128 }
129
130 return Result;
131}
132
133int CServerBan::BanAddr(const NETADDR *pAddr, int Seconds, const char *pReason, bool VerbatimReason)
134{
135 return BanExt(pBanPool: &m_BanAddrPool, pData: pAddr, Seconds, pReason, VerbatimReason);
136}
137
138int CServerBan::BanRange(const CNetRange *pRange, int Seconds, const char *pReason)
139{
140 if(pRange->IsValid())
141 return BanExt(pBanPool: &m_BanRangePool, pData: pRange, Seconds, pReason, VerbatimReason: true);
142
143 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "net_ban", pStr: "ban failed (invalid range)");
144 return -1;
145}
146
147void CServerBan::ConBanExt(IConsole::IResult *pResult, void *pUser)
148{
149 CServerBan *pThis = static_cast<CServerBan *>(pUser);
150
151 const char *pStr = pResult->GetString(Index: 0);
152 int Minutes = pResult->NumArguments() > 1 ? std::clamp(val: pResult->GetInteger(Index: 1), lo: 0, hi: 525600) : 10;
153 const char *pReason = pResult->NumArguments() > 2 ? pResult->GetString(Index: 2) : "Follow the server rules. Type /rules into the chat.";
154
155 if(str_isallnum(str: pStr))
156 {
157 int ClientId = str_toint(str: pStr);
158 if(ClientId < 0 || ClientId >= MAX_CLIENTS || pThis->Server()->m_aClients[ClientId].m_State == CServer::CClient::STATE_EMPTY)
159 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "net_ban", pStr: "ban error (invalid client id)");
160 else
161 pThis->BanAddr(pAddr: pThis->Server()->ClientAddr(ClientId), Seconds: Minutes * 60, pReason, VerbatimReason: false);
162 }
163 else
164 {
165 NETADDR Addr;
166 if(net_addr_from_str(addr: &Addr, string: pStr) == 0)
167 pThis->BanAddr(pAddr: &Addr, Seconds: Minutes * 60, pReason, VerbatimReason: false);
168 else
169 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "net_ban", pStr: "ban error (invalid network address)");
170 }
171}
172
173void CServerBan::ConBanRegion(IConsole::IResult *pResult, void *pUser)
174{
175 const char *pRegion = pResult->GetString(Index: 0);
176 if(str_comp_nocase(a: pRegion, b: g_Config.m_SvRegionName))
177 return;
178
179 pResult->RemoveArgument(Index: 0);
180 ConBanExt(pResult, pUser);
181}
182
183void CServerBan::ConBanRegionRange(IConsole::IResult *pResult, void *pUser)
184{
185 CServerBan *pServerBan = static_cast<CServerBan *>(pUser);
186
187 const char *pRegion = pResult->GetString(Index: 0);
188 if(str_comp_nocase(a: pRegion, b: g_Config.m_SvRegionName))
189 return;
190
191 pResult->RemoveArgument(Index: 0);
192 ConBanRange(pResult, pUser: static_cast<CNetBan *>(pServerBan));
193}
194
195// Not thread-safe!
196class CRconClientLogger : public ILogger
197{
198 CServer *m_pServer;
199 int m_ClientId;
200
201public:
202 CRconClientLogger(CServer *pServer, int ClientId) :
203 m_pServer(pServer),
204 m_ClientId(ClientId)
205 {
206 }
207 void Log(const CLogMessage *pMessage) override;
208};
209
210void CRconClientLogger::Log(const CLogMessage *pMessage)
211{
212 if(m_Filter.Filters(pMessage))
213 {
214 return;
215 }
216 m_pServer->SendRconLogLine(ClientId: m_ClientId, pMessage);
217}
218
219void CServer::CClient::Reset()
220{
221 // reset input
222 for(auto &Input : m_aInputs)
223 Input.m_GameTick = -1;
224 m_CurrentInput = 0;
225 mem_zero(block: &m_LastPreInput, size: sizeof(m_LastPreInput));
226 mem_zero(block: &m_LatestInput, size: sizeof(m_LatestInput));
227
228 m_Snapshots.PurgeAll();
229 m_LastAckedSnapshot = -1;
230 m_LastInputTick = -1;
231 m_SnapRate = CClient::SNAPRATE_INIT;
232 m_Score = -1;
233 m_NextMapChunk = 0;
234 m_Flags = 0;
235 m_RedirectDropTime = 0;
236}
237
238CServer::CServer() :
239 m_pSnapshotDelta(CSnapshotDelta_New()),
240 m_pSnapshotDeltaSixup(CSnapshotDelta_New()),
241 m_pSnapshotBuilder(CSnapshotBuilder_New())
242{
243 m_pConfig = &g_Config;
244 for(int i = 0; i < MAX_CLIENTS; i++)
245 m_aDemoRecorder[i] = CDemoRecorder(&*m_pSnapshotDelta, true);
246 m_aDemoRecorder[RECORDER_MANUAL] = CDemoRecorder(&*m_pSnapshotDelta, false);
247 m_aDemoRecorder[RECORDER_AUTO] = CDemoRecorder(&*m_pSnapshotDelta, false);
248
249 m_pGameServer = nullptr;
250
251 m_CurrentGameTick = MIN_TICK;
252 m_RunServer = UNINITIALIZED;
253
254 m_aShutdownReason[0] = 0;
255
256 for(int i = 0; i < NUM_MAP_TYPES; i++)
257 {
258 m_apCurrentMapData[i] = nullptr;
259 m_aCurrentMapSize[i] = 0;
260 }
261
262 m_MapReload = false;
263 m_SameMapReload = false;
264 m_ReloadedWhenEmpty = false;
265 m_aMapDownloadUrl[0] = '\0';
266
267 m_RconClientId = IServer::RCON_CID_SERV;
268 m_RconAuthLevel = AUTHED_ADMIN;
269
270 m_ServerInfoFirstRequest = 0;
271 m_ServerInfoNumRequests = 0;
272
273#ifdef CONF_FAMILY_UNIX
274 m_ConnLoggingSocketCreated = false;
275#endif
276
277 m_pConnectionPool = new CDbConnectionPool();
278 m_pRegister = nullptr;
279
280 m_aErrorShutdownReason[0] = 0;
281
282 Init();
283}
284
285CServer::~CServer()
286{
287 for(auto &pCurrentMapData : m_apCurrentMapData)
288 {
289 free(ptr: pCurrentMapData);
290 }
291
292 if(m_RunServer != UNINITIALIZED)
293 {
294 for(auto &Client : m_aClients)
295 {
296 free(ptr: Client.m_pPersistentData);
297 }
298 }
299 free(ptr: m_pPersistentData);
300
301 delete m_pRegister;
302 delete m_pConnectionPool;
303}
304
305const char *CServer::DnsblStateStr(EDnsblState State)
306{
307 switch(State)
308 {
309 case EDnsblState::NONE:
310 return "n/a";
311 case EDnsblState::PENDING:
312 return "pending";
313 case EDnsblState::BLACKLISTED:
314 return "black";
315 case EDnsblState::WHITELISTED:
316 return "white";
317 }
318
319 dbg_assert_failed("Invalid dnsbl State: %d", static_cast<int>(State));
320}
321
322IConsole::EAccessLevel CServer::ConsoleAccessLevel(int ClientId) const
323{
324 int AuthLevel = GetAuthedState(ClientId);
325 switch(AuthLevel)
326 {
327 case AUTHED_ADMIN:
328 return IConsole::EAccessLevel::ADMIN;
329 case AUTHED_MOD:
330 return IConsole::EAccessLevel::MODERATOR;
331 case AUTHED_HELPER:
332 return IConsole::EAccessLevel::HELPER;
333 };
334
335 dbg_assert_failed("Invalid AuthLevel: %d", AuthLevel);
336}
337
338bool CServer::IsClientNameAvailable(int ClientId, const char *pNameRequest)
339{
340 // check for empty names
341 if(!pNameRequest[0])
342 return false;
343
344 // check for names starting with /, as they can be abused to make people
345 // write chat commands
346 if(pNameRequest[0] == '/')
347 return false;
348
349 // make sure that two clients don't have the same name
350 for(int i = 0; i < MAX_CLIENTS; i++)
351 {
352 if(i != ClientId && m_aClients[i].m_State >= CClient::STATE_READY)
353 {
354 if(str_utf8_comp_confusable(str1: pNameRequest, str2: m_aClients[i].m_aName) == 0)
355 return false;
356 }
357 }
358
359 return true;
360}
361
362bool CServer::SetClientNameImpl(int ClientId, const char *pNameRequest, bool Set)
363{
364 dbg_assert(0 <= ClientId && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
365 if(m_aClients[ClientId].m_State < CClient::STATE_READY)
366 return false;
367
368 const CNameBan *pBanned = m_NameBans.IsBanned(pName: pNameRequest);
369 if(pBanned)
370 {
371 if(m_aClients[ClientId].m_State == CClient::STATE_READY && Set)
372 {
373 char aBuf[256];
374 if(pBanned->m_aReason[0])
375 {
376 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Kicked (your name is banned: %s)", pBanned->m_aReason);
377 }
378 else
379 {
380 str_copy(dst&: aBuf, src: "Kicked (your name is banned)");
381 }
382 Kick(ClientId, pReason: aBuf);
383 }
384 return false;
385 }
386
387 // trim the name
388 char aTrimmedName[MAX_NAME_LENGTH];
389 str_copy(dst&: aTrimmedName, src: str_utf8_skip_whitespaces(str: pNameRequest));
390 str_utf8_trim_right(param: aTrimmedName);
391
392 char aNameTry[MAX_NAME_LENGTH];
393 str_copy(dst&: aNameTry, src: aTrimmedName);
394
395 if(!IsClientNameAvailable(ClientId, pNameRequest: aNameTry))
396 {
397 // auto rename
398 for(int i = 1;; i++)
399 {
400 str_format(buffer: aNameTry, buffer_size: sizeof(aNameTry), format: "(%d)%s", i, aTrimmedName);
401 if(IsClientNameAvailable(ClientId, pNameRequest: aNameTry))
402 break;
403 }
404 }
405
406 bool Changed = str_comp(a: m_aClients[ClientId].m_aName, b: aNameTry) != 0;
407
408 if(Set && Changed)
409 {
410 // set the client name
411 str_copy(dst&: m_aClients[ClientId].m_aName, src: aNameTry);
412 GameServer()->TeehistorianRecordPlayerName(ClientId, pName: m_aClients[ClientId].m_aName);
413 }
414
415 return Changed;
416}
417
418bool CServer::SetClientClanImpl(int ClientId, const char *pClanRequest, bool Set)
419{
420 dbg_assert(0 <= ClientId && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
421 if(m_aClients[ClientId].m_State < CClient::STATE_READY)
422 return false;
423
424 const CNameBan *pBanned = m_NameBans.IsBanned(pName: pClanRequest);
425 if(pBanned)
426 {
427 if(m_aClients[ClientId].m_State == CClient::STATE_READY && Set)
428 {
429 char aBuf[256];
430 if(pBanned->m_aReason[0])
431 {
432 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Kicked (your clan is banned: %s)", pBanned->m_aReason);
433 }
434 else
435 {
436 str_copy(dst&: aBuf, src: "Kicked (your clan is banned)");
437 }
438 Kick(ClientId, pReason: aBuf);
439 }
440 return false;
441 }
442
443 // trim the clan
444 char aTrimmedClan[MAX_CLAN_LENGTH];
445 str_copy(dst&: aTrimmedClan, src: str_utf8_skip_whitespaces(str: pClanRequest));
446 str_utf8_trim_right(param: aTrimmedClan);
447
448 bool Changed = str_comp(a: m_aClients[ClientId].m_aClan, b: aTrimmedClan) != 0;
449
450 if(Set)
451 {
452 // set the client clan
453 str_copy(dst&: m_aClients[ClientId].m_aClan, src: aTrimmedClan);
454 }
455
456 return Changed;
457}
458
459bool CServer::WouldClientNameChange(int ClientId, const char *pNameRequest)
460{
461 return SetClientNameImpl(ClientId, pNameRequest, Set: false);
462}
463
464bool CServer::WouldClientClanChange(int ClientId, const char *pClanRequest)
465{
466 return SetClientClanImpl(ClientId, pClanRequest, Set: false);
467}
468
469void CServer::SetClientName(int ClientId, const char *pName)
470{
471 SetClientNameImpl(ClientId, pNameRequest: pName, Set: true);
472}
473
474void CServer::SetClientClan(int ClientId, const char *pClan)
475{
476 SetClientClanImpl(ClientId, pClanRequest: pClan, Set: true);
477}
478
479void CServer::SetClientCountry(int ClientId, int Country)
480{
481 if(ClientId < 0 || ClientId >= MAX_CLIENTS || m_aClients[ClientId].m_State < CClient::STATE_READY)
482 return;
483
484 m_aClients[ClientId].m_Country = Country;
485}
486
487void CServer::SetClientScore(int ClientId, std::optional<int> Score)
488{
489 if(ClientId < 0 || ClientId >= MAX_CLIENTS || m_aClients[ClientId].m_State < CClient::STATE_READY)
490 return;
491
492 if(m_aClients[ClientId].m_Score != Score)
493 ExpireServerInfo();
494
495 m_aClients[ClientId].m_Score = Score;
496}
497
498void CServer::SetClientFlags(int ClientId, int Flags)
499{
500 if(ClientId < 0 || ClientId >= MAX_CLIENTS || m_aClients[ClientId].m_State < CClient::STATE_READY)
501 return;
502
503 m_aClients[ClientId].m_Flags = Flags;
504}
505
506void CServer::Kick(int ClientId, const char *pReason)
507{
508 if(ClientId < 0 || ClientId >= MAX_CLIENTS || m_aClients[ClientId].m_State == CClient::STATE_EMPTY)
509 {
510 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "invalid client id to kick");
511 return;
512 }
513 else if(m_RconClientId == ClientId)
514 {
515 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "you can't kick yourself");
516 return;
517 }
518 else if(GetAuthedState(ClientId) > m_RconAuthLevel)
519 {
520 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "kick command denied");
521 return;
522 }
523
524 m_NetServer.Drop(ClientId, pReason);
525}
526
527void CServer::Ban(int ClientId, int Seconds, const char *pReason, bool VerbatimReason)
528{
529 m_NetServer.NetBan()->BanAddr(pAddr: ClientAddr(ClientId), Seconds, pReason, VerbatimReason);
530}
531
532void CServer::ReconnectClient(int ClientId)
533{
534 dbg_assert(0 <= ClientId && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
535 dbg_assert(m_aClients[ClientId].m_State != CClient::STATE_EMPTY, "Client slot empty: %d", ClientId);
536
537 if(GetClientVersion(ClientId) < VERSION_DDNET_RECONNECT)
538 {
539 RedirectClient(ClientId, Port: m_NetServer.Address().port);
540 return;
541 }
542 log_info("server", "telling client to reconnect, cid=%d", ClientId);
543
544 CMsgPacker Msg(NETMSG_RECONNECT, true);
545 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientId);
546
547 if(m_aClients[ClientId].m_State >= CClient::STATE_READY)
548 {
549 GameServer()->OnClientDrop(ClientId, pReason: "reconnect");
550 }
551
552 m_aClients[ClientId].m_RedirectDropTime = time_get() + time_freq() * 10;
553 m_aClients[ClientId].m_State = CClient::STATE_REDIRECTED;
554}
555
556void CServer::RedirectClient(int ClientId, int Port)
557{
558 dbg_assert(0 <= ClientId && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
559 dbg_assert(m_aClients[ClientId].m_State != CClient::STATE_EMPTY, "Client slot empty: %d", ClientId);
560
561 bool SupportsRedirect = GetClientVersion(ClientId) >= VERSION_DDNET_REDIRECT;
562
563 log_info("server", "redirecting client, cid=%d port=%d supported=%d", ClientId, Port, SupportsRedirect);
564
565 if(!SupportsRedirect)
566 {
567 char aBuf[128];
568 bool SamePort = Port == this->Port();
569 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Redirect unsupported: please connect to port %d", Port);
570 Kick(ClientId, pReason: SamePort ? "Redirect unsupported: please reconnect" : aBuf);
571 return;
572 }
573
574 CMsgPacker Msg(NETMSG_REDIRECT, true);
575 Msg.AddInt(i: Port);
576 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientId);
577
578 if(m_aClients[ClientId].m_State >= CClient::STATE_READY)
579 {
580 GameServer()->OnClientDrop(ClientId, pReason: "redirect");
581 }
582
583 m_aClients[ClientId].m_RedirectDropTime = time_get() + time_freq() * 10;
584 m_aClients[ClientId].m_State = CClient::STATE_REDIRECTED;
585}
586
587int64_t CServer::TickStartTime(int Tick)
588{
589 return m_GameStartTime + (time_freq() * Tick) / TickSpeed();
590}
591
592int CServer::Init()
593{
594 for(auto &Client : m_aClients)
595 {
596 Client.m_State = CClient::STATE_EMPTY;
597 Client.m_aName[0] = 0;
598 Client.m_aClan[0] = 0;
599 Client.m_Country = -1;
600 Client.m_Snapshots.Init();
601 Client.m_Traffic = 0;
602 Client.m_TrafficSince = 0;
603 Client.m_ShowIps = false;
604 Client.m_DebugDummy = false;
605 Client.m_AuthKey = -1;
606 Client.m_Latency = 0;
607 Client.m_Sixup = false;
608 Client.m_RedirectDropTime = 0;
609 }
610
611 m_CurrentGameTick = MIN_TICK;
612
613 m_AnnouncementLastLine = -1;
614 std::fill(first: std::begin(arr&: m_aPrevStates), last: std::end(arr&: m_aPrevStates), value: 0);
615
616 return 0;
617}
618
619bool CServer::StrHideIps(const char *pInput, char *pOutputWithIps, int OutputWithIpsSize, char *pOutputWithoutIps, int OutputWithoutIpsSize)
620{
621 const char *pStart = str_find(haystack: pInput, needle: "<{");
622 const char *pEnd = pStart == nullptr ? nullptr : str_find(haystack: pStart + 2, needle: "}>");
623 pOutputWithIps[0] = '\0';
624 pOutputWithoutIps[0] = '\0';
625
626 if(pStart == nullptr || pEnd == nullptr)
627 {
628 str_copy(dst: pOutputWithIps, src: pInput, dst_size: OutputWithIpsSize);
629 str_copy(dst: pOutputWithoutIps, src: pInput, dst_size: OutputWithoutIpsSize);
630 return false;
631 }
632
633 str_append(dst: pOutputWithIps, src: pInput, dst_size: minimum<size_t>(a: pStart - pInput + 1, b: OutputWithIpsSize));
634 str_append(dst: pOutputWithIps, src: pStart + 2, dst_size: minimum<size_t>(a: pEnd - pInput - 1, b: OutputWithIpsSize));
635 str_append(dst: pOutputWithIps, src: pEnd + 2, dst_size: OutputWithIpsSize);
636
637 str_append(dst: pOutputWithoutIps, src: pInput, dst_size: minimum<size_t>(a: pStart - pInput + 1, b: OutputWithoutIpsSize));
638 str_append(dst: pOutputWithoutIps, src: "XXX", dst_size: OutputWithoutIpsSize);
639 str_append(dst: pOutputWithoutIps, src: pEnd + 2, dst_size: OutputWithoutIpsSize);
640 return true;
641}
642
643void CServer::SendLogLine(const CLogMessage *pMessage)
644{
645 if(pMessage->m_Level <= IConsole::ToLogLevelFilter(ConsoleLevel: g_Config.m_ConsoleOutputLevel))
646 {
647 SendRconLogLine(ClientId: -1, pMessage);
648 }
649 if(pMessage->m_Level <= IConsole::ToLogLevelFilter(ConsoleLevel: g_Config.m_EcOutputLevel))
650 {
651 m_Econ.Send(ClientId: -1, pLine: pMessage->m_aLine);
652 }
653}
654
655void CServer::SetRconCid(int ClientId)
656{
657 m_RconClientId = ClientId;
658}
659
660int CServer::GetAuthedState(int ClientId) const
661{
662 if(ClientId == IConsole::CLIENT_ID_UNSPECIFIED)
663 return AUTHED_ADMIN;
664 if(ClientId == IConsole::CLIENT_ID_GAME)
665 return AUTHED_ADMIN;
666 if(ClientId == IConsole::CLIENT_ID_NO_GAME)
667 return AUTHED_ADMIN;
668 dbg_assert(ClientId >= 0 && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
669 dbg_assert(m_aClients[ClientId].m_State != CServer::CClient::STATE_EMPTY, "Client slot %d is empty", ClientId);
670 return m_AuthManager.KeyLevel(Slot: m_aClients[ClientId].m_AuthKey);
671}
672
673bool CServer::IsRconAuthed(int ClientId) const
674{
675 return GetAuthedState(ClientId) != AUTHED_NO;
676}
677
678bool CServer::IsRconAuthedAdmin(int ClientId) const
679{
680 return GetAuthedState(ClientId) == AUTHED_ADMIN;
681}
682
683const char *CServer::GetAuthName(int ClientId) const
684{
685 dbg_assert(ClientId >= 0 && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
686 dbg_assert(m_aClients[ClientId].m_State != CServer::CClient::STATE_EMPTY, "Client slot %d is empty", ClientId);
687 int Key = m_aClients[ClientId].m_AuthKey;
688 dbg_assert(Key != -1, "Client not authed");
689 return m_AuthManager.KeyIdent(Slot: Key);
690}
691
692bool CServer::HasAuthHidden(int ClientId) const
693{
694 dbg_assert(ClientId >= 0 && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
695 return m_aClients[ClientId].m_AuthHidden;
696}
697
698bool CServer::GetClientInfo(int ClientId, CClientInfo *pInfo) const
699{
700 dbg_assert(ClientId >= 0 && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
701 dbg_assert(pInfo != nullptr, "pInfo cannot be null");
702
703 if(m_aClients[ClientId].m_State == CClient::STATE_INGAME)
704 {
705 pInfo->m_pName = m_aClients[ClientId].m_aName;
706 pInfo->m_Latency = m_aClients[ClientId].m_Latency;
707 pInfo->m_GotDDNetVersion = m_aClients[ClientId].m_DDNetVersionSettled;
708 pInfo->m_DDNetVersion = m_aClients[ClientId].m_DDNetVersion >= 0 ? m_aClients[ClientId].m_DDNetVersion : VERSION_VANILLA;
709 if(m_aClients[ClientId].m_GotDDNetVersionPacket)
710 {
711 pInfo->m_pConnectionId = &m_aClients[ClientId].m_ConnectionId;
712 pInfo->m_pDDNetVersionStr = m_aClients[ClientId].m_aDDNetVersionStr;
713 }
714 else
715 {
716 pInfo->m_pConnectionId = nullptr;
717 pInfo->m_pDDNetVersionStr = nullptr;
718 }
719 return true;
720 }
721 return false;
722}
723
724void CServer::SetClientDDNetVersion(int ClientId, int DDNetVersion)
725{
726 dbg_assert(ClientId >= 0 && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
727
728 if(m_aClients[ClientId].m_State == CClient::STATE_INGAME)
729 {
730 m_aClients[ClientId].m_DDNetVersion = DDNetVersion;
731 m_aClients[ClientId].m_DDNetVersionSettled = true;
732 }
733}
734
735const NETADDR *CServer::ClientAddr(int ClientId) const
736{
737 dbg_assert(ClientId >= 0 && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
738 dbg_assert(m_aClients[ClientId].m_State != CServer::CClient::STATE_EMPTY, "Client slot %d is empty", ClientId);
739 if(m_aClients[ClientId].m_DebugDummy)
740 {
741 return &m_aClients[ClientId].m_DebugDummyAddr;
742 }
743 return m_NetServer.ClientAddr(ClientId);
744}
745
746const std::array<char, NETADDR_MAXSTRSIZE> &CServer::ClientAddrStringImpl(int ClientId, bool IncludePort) const
747{
748 dbg_assert(ClientId >= 0 && ClientId < MAX_CLIENTS, "Invalid ClientId: %d", ClientId);
749 dbg_assert(m_aClients[ClientId].m_State != CServer::CClient::STATE_EMPTY, "Client slot %d is empty", ClientId);
750 if(m_aClients[ClientId].m_DebugDummy)
751 {
752 return IncludePort ? m_aClients[ClientId].m_aDebugDummyAddrString : m_aClients[ClientId].m_aDebugDummyAddrStringNoPort;
753 }
754 return m_NetServer.ClientAddrString(ClientId, IncludePort);
755}
756
757const char *CServer::ClientName(int ClientId) const
758{
759 if(ClientId < 0 || ClientId >= MAX_CLIENTS || m_aClients[ClientId].m_State == CServer::CClient::STATE_EMPTY)
760 return "(invalid)";
761 if(m_aClients[ClientId].m_State == CServer::CClient::STATE_INGAME || m_aClients[ClientId].m_State == CServer::CClient::STATE_REDIRECTED)
762 return m_aClients[ClientId].m_aName;
763 else
764 return "(connecting)";
765}
766
767const char *CServer::ClientClan(int ClientId) const
768{
769 if(ClientId < 0 || ClientId >= MAX_CLIENTS || m_aClients[ClientId].m_State == CServer::CClient::STATE_EMPTY)
770 return "";
771 if(m_aClients[ClientId].m_State == CServer::CClient::STATE_INGAME)
772 return m_aClients[ClientId].m_aClan;
773 else
774 return "";
775}
776
777int CServer::ClientCountry(int ClientId) const
778{
779 if(ClientId < 0 || ClientId >= MAX_CLIENTS || m_aClients[ClientId].m_State == CServer::CClient::STATE_EMPTY)
780 return -1;
781 if(m_aClients[ClientId].m_State == CServer::CClient::STATE_INGAME)
782 return m_aClients[ClientId].m_Country;
783 else
784 return -1;
785}
786
787bool CServer::ClientSlotEmpty(int ClientId) const
788{
789 return ClientId >= 0 && ClientId < MAX_CLIENTS && m_aClients[ClientId].m_State == CServer::CClient::STATE_EMPTY;
790}
791
792bool CServer::ClientIngame(int ClientId) const
793{
794 return ClientId >= 0 && ClientId < MAX_CLIENTS && m_aClients[ClientId].m_State == CServer::CClient::STATE_INGAME;
795}
796
797int CServer::Port() const
798{
799 return m_NetServer.Address().port;
800}
801
802int CServer::MaxClients() const
803{
804 return m_RunServer == UNINITIALIZED ? 0 : m_NetServer.MaxClients();
805}
806
807int CServer::ClientCount() const
808{
809 int ClientCount = 0;
810 for(const auto &Client : m_aClients)
811 {
812 if(Client.m_State != CClient::STATE_EMPTY)
813 {
814 ClientCount++;
815 }
816 }
817
818 return ClientCount;
819}
820
821int CServer::DistinctClientCount() const
822{
823 const NETADDR *apAddresses[MAX_CLIENTS];
824 for(int i = 0; i < MAX_CLIENTS; i++)
825 {
826 // connecting clients with spoofed ips can clog slots without being ingame
827 apAddresses[i] = ClientIngame(ClientId: i) ? ClientAddr(ClientId: i) : nullptr;
828 }
829
830 int ClientCount = 0;
831 for(int i = 0; i < MAX_CLIENTS; i++)
832 {
833 if(apAddresses[i] == nullptr)
834 {
835 continue;
836 }
837 ClientCount++;
838 for(int j = 0; j < i; j++)
839 {
840 if(apAddresses[j] != nullptr && !net_addr_comp_noport(a: apAddresses[i], b: apAddresses[j]))
841 {
842 ClientCount--;
843 break;
844 }
845 }
846 }
847 return ClientCount;
848}
849
850int CServer::GetClientVersion(int ClientId) const
851{
852 // Assume latest client version for server demos
853 if(ClientId == SERVER_DEMO_CLIENT)
854 return DDNET_VERSION_NUMBER;
855
856 CClientInfo Info;
857 if(GetClientInfo(ClientId, pInfo: &Info))
858 return Info.m_DDNetVersion;
859 return VERSION_NONE;
860}
861
862static inline bool RepackMsg(const CMsgPacker *pMsg, CPacker &Packer, bool Sixup)
863{
864 int MsgId = pMsg->m_MsgId;
865 Packer.Reset();
866
867 if(Sixup && !pMsg->m_NoTranslate)
868 {
869 if(pMsg->m_System)
870 {
871 if(MsgId >= OFFSET_UUID)
872 ;
873 else if(MsgId >= NETMSG_MAP_CHANGE && MsgId <= NETMSG_MAP_DATA)
874 ;
875 else if(MsgId >= NETMSG_CON_READY && MsgId <= NETMSG_INPUTTIMING)
876 MsgId += 1;
877 else if(MsgId == NETMSG_RCON_LINE)
878 MsgId = protocol7::NETMSG_RCON_LINE;
879 else if(MsgId >= NETMSG_PING && MsgId <= NETMSG_PING_REPLY)
880 MsgId += 4;
881 else if(MsgId >= NETMSG_RCON_CMD_ADD && MsgId <= NETMSG_RCON_CMD_REM)
882 MsgId -= 11;
883 else
884 {
885 log_error("net", "DROP send sys %d", MsgId);
886 return false;
887 }
888 }
889 else
890 {
891 if(MsgId >= 0 && MsgId < OFFSET_UUID)
892 MsgId = Msg_SixToSeven(a: MsgId);
893
894 if(MsgId < 0)
895 return false;
896 }
897 }
898
899 if(MsgId < OFFSET_UUID)
900 {
901 Packer.AddInt(i: (MsgId << 1) | (pMsg->m_System ? 1 : 0));
902 }
903 else
904 {
905 Packer.AddInt(i: pMsg->m_System ? 1 : 0); // NETMSG_EX, NETMSGTYPE_EX
906 g_UuidManager.PackUuid(Id: MsgId, pPacker: &Packer);
907 }
908 Packer.AddRaw(pData: pMsg->Data(), Size: pMsg->Size());
909
910 return true;
911}
912
913int CServer::SendMsg(CMsgPacker *pMsg, int Flags, int ClientId)
914{
915 CNetChunk Packet;
916 mem_zero(block: &Packet, size: sizeof(CNetChunk));
917 if(Flags & MSGFLAG_VITAL)
918 Packet.m_Flags |= NETSENDFLAG_VITAL;
919 if(Flags & MSGFLAG_FLUSH)
920 Packet.m_Flags |= NETSENDFLAG_FLUSH;
921
922 if(ClientId < 0)
923 {
924 CPacker Pack6, Pack7;
925 if(!RepackMsg(pMsg, Packer&: Pack6, Sixup: false))
926 return -1;
927 if(!RepackMsg(pMsg, Packer&: Pack7, Sixup: true))
928 return -1;
929
930 // write message to demo recorders
931 if(!(Flags & MSGFLAG_NORECORD))
932 {
933 for(auto &Recorder : m_aDemoRecorder)
934 if(Recorder.IsRecording())
935 Recorder.RecordMessage(pData: Pack6.Data(), Size: Pack6.Size());
936 }
937
938 if(!(Flags & MSGFLAG_NOSEND))
939 {
940 for(int i = 0; i < MAX_CLIENTS; i++)
941 {
942 if(m_aClients[i].m_State == CClient::STATE_INGAME)
943 {
944 CPacker *pPack = m_aClients[i].m_Sixup ? &Pack7 : &Pack6;
945 Packet.m_pData = pPack->Data();
946 Packet.m_DataSize = pPack->Size();
947 Packet.m_ClientId = i;
948 if(Antibot()->OnEngineServerMessage(ClientId: i, pData: Packet.m_pData, Size: Packet.m_DataSize, Flags))
949 {
950 continue;
951 }
952 m_NetServer.Send(pChunk: &Packet);
953 }
954 }
955 }
956 }
957 else
958 {
959 CPacker Pack;
960 if(!RepackMsg(pMsg, Packer&: Pack, Sixup: m_aClients[ClientId].m_Sixup))
961 return -1;
962
963 Packet.m_ClientId = ClientId;
964 Packet.m_pData = Pack.Data();
965 Packet.m_DataSize = Pack.Size();
966
967 if(Antibot()->OnEngineServerMessage(ClientId, pData: Packet.m_pData, Size: Packet.m_DataSize, Flags))
968 {
969 return 0;
970 }
971
972 // write message to demo recorders
973 if(!(Flags & MSGFLAG_NORECORD))
974 {
975 if(m_aDemoRecorder[ClientId].IsRecording())
976 m_aDemoRecorder[ClientId].RecordMessage(pData: Pack.Data(), Size: Pack.Size());
977 if(m_aDemoRecorder[RECORDER_MANUAL].IsRecording())
978 m_aDemoRecorder[RECORDER_MANUAL].RecordMessage(pData: Pack.Data(), Size: Pack.Size());
979 if(m_aDemoRecorder[RECORDER_AUTO].IsRecording())
980 m_aDemoRecorder[RECORDER_AUTO].RecordMessage(pData: Pack.Data(), Size: Pack.Size());
981 }
982
983 if(!(Flags & MSGFLAG_NOSEND))
984 m_NetServer.Send(pChunk: &Packet);
985 }
986
987 return 0;
988}
989
990void CServer::SendMsgRaw(int ClientId, const void *pData, int Size, int Flags)
991{
992 CNetChunk Packet;
993 mem_zero(block: &Packet, size: sizeof(CNetChunk));
994 Packet.m_ClientId = ClientId;
995 Packet.m_pData = pData;
996 Packet.m_DataSize = Size;
997 Packet.m_Flags = 0;
998 if(Flags & MSGFLAG_VITAL)
999 {
1000 Packet.m_Flags |= NETSENDFLAG_VITAL;
1001 }
1002 if(Flags & MSGFLAG_FLUSH)
1003 {
1004 Packet.m_Flags |= NETSENDFLAG_FLUSH;
1005 }
1006 m_NetServer.Send(pChunk: &Packet);
1007}
1008
1009void CServer::DoSnapshot()
1010{
1011 bool IsGlobalSnap = Config()->m_SvHighBandwidth || (m_CurrentGameTick % 2) == 0;
1012
1013 if(m_aDemoRecorder[RECORDER_MANUAL].IsRecording() || m_aDemoRecorder[RECORDER_AUTO].IsRecording())
1014 {
1015 // create snapshot for demo recording
1016 CSnapshotBuffer Data;
1017
1018 // build snap and possibly add some messages
1019 m_pSnapshotBuilder->Init(sixup: false);
1020 GameServer()->OnSnap(ClientId: -1, GlobalSnap: IsGlobalSnap, RecordingDemo: true);
1021 int SnapshotSize = m_pSnapshotBuilder->Finish(buffer&: Data);
1022
1023 // write snapshot
1024 if(m_aDemoRecorder[RECORDER_MANUAL].IsRecording())
1025 m_aDemoRecorder[RECORDER_MANUAL].RecordSnapshot(Tick: Tick(), pData: Data.AsSnapshot(), Size: SnapshotSize);
1026 if(m_aDemoRecorder[RECORDER_AUTO].IsRecording())
1027 m_aDemoRecorder[RECORDER_AUTO].RecordSnapshot(Tick: Tick(), pData: Data.AsSnapshot(), Size: SnapshotSize);
1028 }
1029
1030 // create snapshots for all clients
1031 for(int i = 0; i < MaxClients(); i++)
1032 {
1033 // client must be ingame to receive snapshots
1034 if(m_aClients[i].m_State != CClient::STATE_INGAME)
1035 continue;
1036
1037 // this client is trying to recover, don't spam snapshots
1038 if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_RECOVER && (Tick() % TickSpeed()) != 0)
1039 continue;
1040
1041 // this client is trying to recover, don't spam snapshots
1042 if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_INIT && (Tick() % 10) != 0)
1043 continue;
1044
1045 // only allow clients with forced high bandwidth on spectate to receive snapshots on non-global ticks
1046 if(!IsGlobalSnap && !(m_aClients[i].m_ForceHighBandwidthOnSpectate && GameServer()->IsClientHighBandwidth(ClientId: i)))
1047 continue;
1048
1049 {
1050 m_pSnapshotBuilder->Init(sixup: m_aClients[i].m_Sixup);
1051
1052 // only snap events on global ticks
1053 GameServer()->OnSnap(ClientId: i, GlobalSnap: IsGlobalSnap, RecordingDemo: m_aDemoRecorder[i].IsRecording());
1054
1055 // finish snapshot
1056 CSnapshotBuffer Data;
1057 int SnapshotSize = m_pSnapshotBuilder->Finish(buffer&: Data);
1058
1059 if(m_aDemoRecorder[i].IsRecording())
1060 {
1061 // write snapshot
1062 m_aDemoRecorder[i].RecordSnapshot(Tick: Tick(), pData: Data.AsSnapshot(), Size: SnapshotSize);
1063 }
1064
1065 int Crc = Data.AsSnapshot()->Crc();
1066
1067 // remove old snapshots
1068 // keep 3 seconds worth of snapshots
1069 m_aClients[i].m_Snapshots.PurgeUntil(Tick: m_CurrentGameTick - TickSpeed() * 3);
1070
1071 // save the snapshot
1072 m_aClients[i].m_Snapshots.Add(Tick: m_CurrentGameTick, Tagtime: time_get(), DataSize: SnapshotSize, pData: Data.AsSnapshot(), AltDataSize: 0, pAltData: nullptr);
1073
1074 // find snapshot that we can perform delta against
1075 int DeltaTick = -1;
1076 const CSnapshot *pDeltashot = CSnapshot::EmptySnapshot();
1077 {
1078 int DeltashotSize = m_aClients[i].m_Snapshots.Get(Tick: m_aClients[i].m_LastAckedSnapshot, pTagtime: nullptr, ppData: &pDeltashot, ppAltData: nullptr);
1079 if(DeltashotSize >= 0)
1080 DeltaTick = m_aClients[i].m_LastAckedSnapshot;
1081 else
1082 {
1083 // no acked package found, force client to recover rate
1084 if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_FULL)
1085 m_aClients[i].m_SnapRate = CClient::SNAPRATE_RECOVER;
1086 }
1087 }
1088
1089 // create delta
1090 CSnapshotDelta *const pSnapshotDelta = IsSixup(ClientId: i) ? &*m_pSnapshotDeltaSixup : &*m_pSnapshotDelta;
1091 int32_t aDeltaData[CSnapshot::MAX_SIZE / sizeof(int32_t)];
1092 int DeltaSize = pSnapshotDelta->CreateDelta(from: *pDeltashot, to: *Data.AsSnapshot(), delta: rust::Slice(aDeltaData, std::size(aDeltaData)));
1093
1094 if(DeltaSize)
1095 {
1096 // compress it
1097 const int MaxSize = MAX_SNAPSHOT_PACKSIZE;
1098
1099 char aCompData[CSnapshot::MAX_SIZE];
1100 SnapshotSize = CVariableInt::Compress(pSrc: aDeltaData, SrcSize: DeltaSize, pDst: aCompData, DstSize: sizeof(aCompData));
1101 int NumPackets = (SnapshotSize + MaxSize - 1) / MaxSize;
1102
1103 for(int n = 0, Left = SnapshotSize; Left > 0; n++)
1104 {
1105 int Chunk = Left < MaxSize ? Left : MaxSize;
1106 Left -= Chunk;
1107
1108 if(NumPackets == 1)
1109 {
1110 CMsgPacker Msg(NETMSG_SNAPSINGLE, true);
1111 Msg.AddInt(i: m_CurrentGameTick);
1112 Msg.AddInt(i: m_CurrentGameTick - DeltaTick);
1113 Msg.AddInt(i: Crc);
1114 Msg.AddInt(i: Chunk);
1115 Msg.AddRaw(pData: &aCompData[n * MaxSize], Size: Chunk);
1116 SendMsg(pMsg: &Msg, Flags: MSGFLAG_FLUSH, ClientId: i);
1117 }
1118 else
1119 {
1120 CMsgPacker Msg(NETMSG_SNAP, true);
1121 Msg.AddInt(i: m_CurrentGameTick);
1122 Msg.AddInt(i: m_CurrentGameTick - DeltaTick);
1123 Msg.AddInt(i: NumPackets);
1124 Msg.AddInt(i: n);
1125 Msg.AddInt(i: Crc);
1126 Msg.AddInt(i: Chunk);
1127 Msg.AddRaw(pData: &aCompData[n * MaxSize], Size: Chunk);
1128 SendMsg(pMsg: &Msg, Flags: MSGFLAG_FLUSH, ClientId: i);
1129 }
1130 }
1131 }
1132 else
1133 {
1134 CMsgPacker Msg(NETMSG_SNAPEMPTY, true);
1135 Msg.AddInt(i: m_CurrentGameTick);
1136 Msg.AddInt(i: m_CurrentGameTick - DeltaTick);
1137 SendMsg(pMsg: &Msg, Flags: MSGFLAG_FLUSH, ClientId: i);
1138 }
1139 }
1140 }
1141
1142 if(IsGlobalSnap)
1143 {
1144 GameServer()->OnPostGlobalSnap();
1145 }
1146}
1147
1148int CServer::ClientRejoinCallback(int ClientId, void *pUser)
1149{
1150 CServer *pThis = (CServer *)pUser;
1151
1152 pThis->m_aClients[ClientId].m_AuthKey = -1;
1153 pThis->m_aClients[ClientId].m_pRconCmdToSend = nullptr;
1154 pThis->m_aClients[ClientId].m_MaplistEntryToSend = CClient::MAPLIST_UNINITIALIZED;
1155 pThis->m_aClients[ClientId].m_DDNetVersion = VERSION_NONE;
1156 pThis->m_aClients[ClientId].m_GotDDNetVersionPacket = false;
1157 pThis->m_aClients[ClientId].m_DDNetVersionSettled = false;
1158
1159 pThis->m_aClients[ClientId].Reset();
1160
1161 pThis->GameServer()->TeehistorianRecordPlayerRejoin(ClientId);
1162 pThis->Antibot()->OnEngineClientDrop(ClientId, pReason: "rejoin");
1163 pThis->Antibot()->OnEngineClientJoin(ClientId);
1164
1165 pThis->SendMap(ClientId);
1166
1167 return 0;
1168}
1169
1170int CServer::NewClientNoAuthCallback(int ClientId, void *pUser)
1171{
1172 CServer *pThis = (CServer *)pUser;
1173
1174 pThis->m_aClients[ClientId].m_DnsblState = EDnsblState::NONE;
1175
1176 pThis->m_aClients[ClientId].m_State = CClient::STATE_CONNECTING;
1177 pThis->m_aClients[ClientId].m_aName[0] = 0;
1178 pThis->m_aClients[ClientId].m_aClan[0] = 0;
1179 pThis->m_aClients[ClientId].m_Country = -1;
1180 pThis->m_aClients[ClientId].m_AuthKey = -1;
1181 pThis->m_aClients[ClientId].m_AuthTries = 0;
1182 pThis->m_aClients[ClientId].m_AuthHidden = false;
1183 pThis->m_aClients[ClientId].m_pRconCmdToSend = nullptr;
1184 pThis->m_aClients[ClientId].m_MaplistEntryToSend = CClient::MAPLIST_UNINITIALIZED;
1185 pThis->m_aClients[ClientId].m_ShowIps = false;
1186 pThis->m_aClients[ClientId].m_DebugDummy = false;
1187 pThis->m_aClients[ClientId].m_ForceHighBandwidthOnSpectate = false;
1188 pThis->m_aClients[ClientId].m_DDNetVersion = VERSION_NONE;
1189 pThis->m_aClients[ClientId].m_GotDDNetVersionPacket = false;
1190 pThis->m_aClients[ClientId].m_DDNetVersionSettled = false;
1191 pThis->m_aClients[ClientId].Reset();
1192
1193 pThis->GameServer()->TeehistorianRecordPlayerJoin(ClientId, Sixup: false);
1194 pThis->Antibot()->OnEngineClientJoin(ClientId);
1195
1196 pThis->SendCapabilities(ClientId);
1197 pThis->SendMap(ClientId);
1198#if defined(CONF_FAMILY_UNIX)
1199 pThis->SendConnLoggingCommand(Cmd: OPEN_SESSION, pAddr: pThis->ClientAddr(ClientId));
1200#endif
1201 return 0;
1202}
1203
1204int CServer::NewClientCallback(int ClientId, void *pUser, bool Sixup)
1205{
1206 CServer *pThis = (CServer *)pUser;
1207 pThis->m_aClients[ClientId].m_State = CClient::STATE_PREAUTH;
1208 pThis->m_aClients[ClientId].m_DnsblState = EDnsblState::NONE;
1209 pThis->m_aClients[ClientId].m_aName[0] = 0;
1210 pThis->m_aClients[ClientId].m_aClan[0] = 0;
1211 pThis->m_aClients[ClientId].m_Country = -1;
1212 pThis->m_aClients[ClientId].m_AuthKey = -1;
1213 pThis->m_aClients[ClientId].m_AuthTries = 0;
1214 pThis->m_aClients[ClientId].m_AuthHidden = false;
1215 pThis->m_aClients[ClientId].m_pRconCmdToSend = nullptr;
1216 pThis->m_aClients[ClientId].m_MaplistEntryToSend = CClient::MAPLIST_UNINITIALIZED;
1217 pThis->m_aClients[ClientId].m_Traffic = 0;
1218 pThis->m_aClients[ClientId].m_TrafficSince = 0;
1219 pThis->m_aClients[ClientId].m_ShowIps = false;
1220 pThis->m_aClients[ClientId].m_DebugDummy = false;
1221 pThis->m_aClients[ClientId].m_ForceHighBandwidthOnSpectate = false;
1222 pThis->m_aClients[ClientId].m_DDNetVersion = VERSION_NONE;
1223 pThis->m_aClients[ClientId].m_GotDDNetVersionPacket = false;
1224 pThis->m_aClients[ClientId].m_DDNetVersionSettled = false;
1225 pThis->m_aClients[ClientId].Reset();
1226 pThis->m_aClients[ClientId].m_Sixup = Sixup;
1227
1228 pThis->GameServer()->TeehistorianRecordPlayerJoin(ClientId, Sixup);
1229 pThis->Antibot()->OnEngineClientJoin(ClientId);
1230
1231#if defined(CONF_FAMILY_UNIX)
1232 pThis->SendConnLoggingCommand(Cmd: OPEN_SESSION, pAddr: pThis->ClientAddr(ClientId));
1233#endif
1234 return 0;
1235}
1236
1237void CServer::InitDnsbl(int ClientId)
1238{
1239 NETADDR Addr = *ClientAddr(ClientId);
1240
1241 //TODO: support ipv6
1242 if(Addr.type != NETTYPE_IPV4)
1243 return;
1244
1245 // build dnsbl host lookup
1246 char aBuf[256];
1247 if(Config()->m_SvDnsblKey[0] == '\0')
1248 {
1249 // without key
1250 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d.%d.%d.%d.%s", Addr.ip[3], Addr.ip[2], Addr.ip[1], Addr.ip[0], Config()->m_SvDnsblHost);
1251 }
1252 else
1253 {
1254 // with key
1255 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s.%d.%d.%d.%d.%s", Config()->m_SvDnsblKey, Addr.ip[3], Addr.ip[2], Addr.ip[1], Addr.ip[0], Config()->m_SvDnsblHost);
1256 }
1257
1258 m_aClients[ClientId].m_pDnsblLookup = std::make_shared<CHostLookup>(args&: aBuf, args: NETTYPE_IPV4);
1259 Engine()->AddJob(pJob: m_aClients[ClientId].m_pDnsblLookup);
1260 m_aClients[ClientId].m_DnsblState = EDnsblState::PENDING;
1261}
1262
1263#ifdef CONF_FAMILY_UNIX
1264void CServer::SendConnLoggingCommand(CONN_LOGGING_CMD Cmd, const NETADDR *pAddr)
1265{
1266 if(!Config()->m_SvConnLoggingServer[0] || !m_ConnLoggingSocketCreated)
1267 return;
1268
1269 // pack the data and send it
1270 unsigned char aData[23] = {0};
1271 aData[0] = Cmd;
1272 mem_copy(dest: &aData[1], source: &pAddr->type, size: 4);
1273 mem_copy(dest: &aData[5], source: pAddr->ip, size: 16);
1274 mem_copy(dest: &aData[21], source: &pAddr->port, size: 2);
1275
1276 net_unix_send(sock: m_ConnLoggingSocket, addr: &m_ConnLoggingDestAddr, data: aData, size: sizeof(aData));
1277}
1278#endif
1279
1280int CServer::DelClientCallback(int ClientId, const char *pReason, void *pUser)
1281{
1282 CServer *pThis = (CServer *)pUser;
1283
1284 char aBuf[256];
1285 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "client dropped. cid=%d addr=<{%s}> reason='%s'", ClientId, pThis->ClientAddrString(ClientId, IncludePort: true), pReason);
1286 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_ADDINFO, pFrom: "server", pStr: aBuf);
1287
1288#if defined(CONF_FAMILY_UNIX)
1289 // Make copy of address because the client slot will be empty at the end of the function
1290 const NETADDR Addr = *pThis->ClientAddr(ClientId);
1291#endif
1292
1293 // notify the mod about the drop
1294 if(pThis->m_aClients[ClientId].m_State >= CClient::STATE_READY)
1295 pThis->GameServer()->OnClientDrop(ClientId, pReason);
1296
1297 pThis->m_aClients[ClientId].m_State = CClient::STATE_EMPTY;
1298 pThis->m_aClients[ClientId].m_aName[0] = 0;
1299 pThis->m_aClients[ClientId].m_aClan[0] = 0;
1300 pThis->m_aClients[ClientId].m_Country = -1;
1301 pThis->m_aClients[ClientId].m_AuthKey = -1;
1302 pThis->m_aClients[ClientId].m_AuthTries = 0;
1303 pThis->m_aClients[ClientId].m_AuthHidden = false;
1304 pThis->m_aClients[ClientId].m_pRconCmdToSend = nullptr;
1305 pThis->m_aClients[ClientId].m_MaplistEntryToSend = CClient::MAPLIST_UNINITIALIZED;
1306 pThis->m_aClients[ClientId].m_Traffic = 0;
1307 pThis->m_aClients[ClientId].m_TrafficSince = 0;
1308 pThis->m_aClients[ClientId].m_ShowIps = false;
1309 pThis->m_aClients[ClientId].m_DebugDummy = false;
1310 pThis->m_aClients[ClientId].m_ForceHighBandwidthOnSpectate = false;
1311 pThis->m_aPrevStates[ClientId] = CClient::STATE_EMPTY;
1312 pThis->m_aClients[ClientId].m_Snapshots.PurgeAll();
1313 pThis->m_aClients[ClientId].m_Sixup = false;
1314 pThis->m_aClients[ClientId].m_RedirectDropTime = 0;
1315 pThis->m_aClients[ClientId].m_HasPersistentData = false;
1316
1317 pThis->GameServer()->TeehistorianRecordPlayerDrop(ClientId, pReason);
1318 pThis->Antibot()->OnEngineClientDrop(ClientId, pReason);
1319#if defined(CONF_FAMILY_UNIX)
1320 pThis->SendConnLoggingCommand(Cmd: CLOSE_SESSION, pAddr: &Addr);
1321#endif
1322 return 0;
1323}
1324
1325void CServer::SendRconType(int ClientId, bool UsernameReq)
1326{
1327 CMsgPacker Msg(NETMSG_RCONTYPE, true);
1328 Msg.AddInt(i: UsernameReq);
1329 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1330}
1331
1332void CServer::SendCapabilities(int ClientId)
1333{
1334 CMsgPacker Msg(NETMSG_CAPABILITIES, true);
1335 Msg.AddInt(i: SERVERCAP_CURVERSION); // version
1336 Msg.AddInt(i: SERVERCAPFLAG_DDNET | SERVERCAPFLAG_CHATTIMEOUTCODE | SERVERCAPFLAG_ANYPLAYERFLAG | SERVERCAPFLAG_PINGEX | SERVERCAPFLAG_ALLOWDUMMY | SERVERCAPFLAG_SYNCWEAPONINPUT); // flags
1337 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1338}
1339
1340void CServer::SendMap(int ClientId)
1341{
1342 int MapType = IsSixup(ClientId) ? MAP_TYPE_SIXUP : MAP_TYPE_SIX;
1343 {
1344 CMsgPacker Msg(NETMSG_MAP_DETAILS, true);
1345 Msg.AddString(pStr: GameServer()->Map()->BaseName(), Limit: 0);
1346 Msg.AddRaw(pData: &m_aCurrentMapSha256[MapType].data, Size: sizeof(m_aCurrentMapSha256[MapType].data));
1347 Msg.AddInt(i: m_aCurrentMapCrc[MapType]);
1348 Msg.AddInt(i: m_aCurrentMapSize[MapType]);
1349 if(m_aMapDownloadUrl[0])
1350 {
1351 Msg.AddString(pStr: m_aMapDownloadUrl, Limit: 0);
1352 }
1353 else
1354 {
1355 Msg.AddString(pStr: "", Limit: 0);
1356 }
1357 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1358 }
1359 {
1360 CMsgPacker Msg(NETMSG_MAP_CHANGE, true);
1361 Msg.AddString(pStr: GameServer()->Map()->BaseName(), Limit: 0);
1362 Msg.AddInt(i: m_aCurrentMapCrc[MapType]);
1363 Msg.AddInt(i: m_aCurrentMapSize[MapType]);
1364 if(MapType == MAP_TYPE_SIXUP)
1365 {
1366 Msg.AddInt(i: Config()->m_SvMapWindow);
1367 Msg.AddInt(i: NET_MAX_CHUNK_SIZE - 128);
1368 Msg.AddRaw(pData: m_aCurrentMapSha256[MapType].data, Size: sizeof(m_aCurrentMapSha256[MapType].data));
1369 }
1370 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientId);
1371 }
1372
1373 m_aClients[ClientId].m_NextMapChunk = 0;
1374}
1375
1376void CServer::SendMapData(int ClientId, int Chunk)
1377{
1378 int MapType = IsSixup(ClientId) ? MAP_TYPE_SIXUP : MAP_TYPE_SIX;
1379 unsigned int ChunkSize = NET_MAX_CHUNK_SIZE - 128;
1380 unsigned int Offset = Chunk * ChunkSize;
1381 int Last = 0;
1382
1383 // drop faulty map data requests
1384 if(Chunk < 0 || Offset > m_aCurrentMapSize[MapType])
1385 return;
1386
1387 if(Offset + ChunkSize >= m_aCurrentMapSize[MapType])
1388 {
1389 ChunkSize = m_aCurrentMapSize[MapType] - Offset;
1390 Last = 1;
1391 }
1392
1393 CMsgPacker Msg(NETMSG_MAP_DATA, true);
1394 if(MapType == MAP_TYPE_SIX)
1395 {
1396 Msg.AddInt(i: Last);
1397 Msg.AddInt(i: m_aCurrentMapCrc[MAP_TYPE_SIX]);
1398 Msg.AddInt(i: Chunk);
1399 Msg.AddInt(i: ChunkSize);
1400 }
1401 Msg.AddRaw(pData: &m_apCurrentMapData[MapType][Offset], Size: ChunkSize);
1402 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientId);
1403
1404 if(Config()->m_Debug)
1405 {
1406 char aBuf[256];
1407 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "sending chunk %d with size %d", Chunk, ChunkSize);
1408 Console()->Print(Level: IConsole::OUTPUT_LEVEL_DEBUG, pFrom: "server", pStr: aBuf);
1409 }
1410}
1411
1412void CServer::SendMapReload(int ClientId)
1413{
1414 CMsgPacker Msg(NETMSG_MAP_RELOAD, true);
1415 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientId);
1416}
1417
1418void CServer::SendConnectionReady(int ClientId)
1419{
1420 CMsgPacker Msg(NETMSG_CON_READY, true);
1421 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientId);
1422}
1423
1424void CServer::SendRconLine(int ClientId, const char *pLine)
1425{
1426 CMsgPacker Msg(NETMSG_RCON_LINE, true);
1427 Msg.AddString(pStr: pLine, Limit: 512);
1428 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1429}
1430
1431void CServer::SendRconLogLine(int ClientId, const CLogMessage *pMessage)
1432{
1433 char aLine[sizeof(CLogMessage().m_aLine)];
1434 char aLineWithoutIps[sizeof(CLogMessage().m_aLine)];
1435 StrHideIps(pInput: pMessage->m_aLine, pOutputWithIps: aLine, OutputWithIpsSize: sizeof(aLine), pOutputWithoutIps: aLineWithoutIps, OutputWithoutIpsSize: sizeof(aLineWithoutIps));
1436
1437 if(ClientId == -1)
1438 {
1439 for(int i = 0; i < MAX_CLIENTS; i++)
1440 {
1441 if(m_aClients[i].m_State != CClient::STATE_EMPTY && IsRconAuthedAdmin(ClientId: i))
1442 SendRconLine(ClientId: i, pLine: m_aClients[i].m_ShowIps ? aLine : aLineWithoutIps);
1443 }
1444 }
1445 else
1446 {
1447 if(m_aClients[ClientId].m_State != CClient::STATE_EMPTY)
1448 SendRconLine(ClientId, pLine: m_aClients[ClientId].m_ShowIps ? aLine : aLineWithoutIps);
1449 }
1450}
1451
1452void CServer::SendRconCmdAdd(const IConsole::ICommandInfo *pCommandInfo, int ClientId)
1453{
1454 CMsgPacker Msg(NETMSG_RCON_CMD_ADD, true);
1455 Msg.AddString(pStr: pCommandInfo->Name(), Limit: IConsole::TEMPCMD_NAME_LENGTH);
1456 Msg.AddString(pStr: pCommandInfo->Help(), Limit: IConsole::TEMPCMD_HELP_LENGTH);
1457 Msg.AddString(pStr: pCommandInfo->Params(), Limit: IConsole::TEMPCMD_PARAMS_LENGTH);
1458 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1459}
1460
1461void CServer::SendRconCmdRem(const IConsole::ICommandInfo *pCommandInfo, int ClientId)
1462{
1463 CMsgPacker Msg(NETMSG_RCON_CMD_REM, true);
1464 Msg.AddString(pStr: pCommandInfo->Name(), Limit: IConsole::TEMPCMD_NAME_LENGTH);
1465 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1466}
1467
1468void CServer::SendRconCmdGroupStart(int ClientId)
1469{
1470 CMsgPacker Msg(NETMSG_RCON_CMD_GROUP_START, true);
1471 Msg.AddInt(i: NumRconCommands(ClientId));
1472 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1473}
1474
1475void CServer::SendRconCmdGroupEnd(int ClientId)
1476{
1477 CMsgPacker Msg(NETMSG_RCON_CMD_GROUP_END, true);
1478 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1479}
1480
1481int CServer::NumRconCommands(int ClientId)
1482{
1483 int Num = 0;
1484 for(const IConsole::ICommandInfo *pCmd = Console()->FirstCommandInfo(ClientId, FlagMask: CFGFLAG_SERVER);
1485 pCmd; pCmd = Console()->NextCommandInfo(pInfo: pCmd, ClientId, FlagMask: CFGFLAG_SERVER))
1486 {
1487 Num++;
1488 }
1489 return Num;
1490}
1491
1492void CServer::UpdateClientRconCommands(int ClientId)
1493{
1494 CClient &Client = m_aClients[ClientId];
1495 if(Client.m_State != CClient::STATE_INGAME ||
1496 !IsRconAuthed(ClientId) ||
1497 Client.m_pRconCmdToSend == nullptr)
1498 {
1499 return;
1500 }
1501
1502 for(int i = 0; i < MAX_RCONCMD_SEND && Client.m_pRconCmdToSend; ++i)
1503 {
1504 SendRconCmdAdd(pCommandInfo: Client.m_pRconCmdToSend, ClientId);
1505 Client.m_pRconCmdToSend = Console()->NextCommandInfo(pInfo: Client.m_pRconCmdToSend, ClientId, FlagMask: CFGFLAG_SERVER);
1506 if(Client.m_pRconCmdToSend == nullptr)
1507 {
1508 SendRconCmdGroupEnd(ClientId);
1509 }
1510 }
1511}
1512
1513CServer::CMaplistEntry::CMaplistEntry(const char *pName)
1514{
1515 str_copy(dst&: m_aName, src: pName);
1516}
1517
1518bool CServer::CMaplistEntry::operator<(const CMaplistEntry &Other) const
1519{
1520 return str_comp_filenames(a: m_aName, b: Other.m_aName) < 0;
1521}
1522
1523void CServer::SendMaplistGroupStart(int ClientId)
1524{
1525 CMsgPacker Msg(NETMSG_MAPLIST_GROUP_START, true);
1526 Msg.AddInt(i: m_vMaplistEntries.size());
1527 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1528}
1529
1530void CServer::SendMaplistGroupEnd(int ClientId)
1531{
1532 CMsgPacker Msg(NETMSG_MAPLIST_GROUP_END, true);
1533 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1534}
1535
1536void CServer::UpdateClientMaplistEntries(int ClientId)
1537{
1538 CClient &Client = m_aClients[ClientId];
1539 if(Client.m_State != CClient::STATE_INGAME ||
1540 !IsRconAuthed(ClientId) ||
1541 Client.m_Sixup ||
1542 Client.m_pRconCmdToSend != nullptr || // wait for command sending
1543 Client.m_MaplistEntryToSend == CClient::MAPLIST_DISABLED ||
1544 Client.m_MaplistEntryToSend == CClient::MAPLIST_DONE)
1545 {
1546 return;
1547 }
1548
1549 if(Client.m_MaplistEntryToSend == CClient::MAPLIST_UNINITIALIZED)
1550 {
1551 static const char *const MAP_COMMANDS[] = {"sv_map", "change_map"};
1552 const IConsole::EAccessLevel AccessLevel = ConsoleAccessLevel(ClientId);
1553 const bool MapCommandAllowed = std::any_of(first: std::begin(arr: MAP_COMMANDS), last: std::end(arr: MAP_COMMANDS), pred: [&](const char *pMapCommand) {
1554 const IConsole::ICommandInfo *pInfo = Console()->GetCommandInfo(pName: pMapCommand, FlagMask: CFGFLAG_SERVER, Temp: false);
1555 dbg_assert(pInfo != nullptr, "Map command not found");
1556 return AccessLevel <= pInfo->GetAccessLevel();
1557 });
1558 if(MapCommandAllowed)
1559 {
1560 Client.m_MaplistEntryToSend = 0;
1561 SendMaplistGroupStart(ClientId);
1562 }
1563 else
1564 {
1565 Client.m_MaplistEntryToSend = CClient::MAPLIST_DISABLED;
1566 return;
1567 }
1568 }
1569
1570 if((size_t)Client.m_MaplistEntryToSend < m_vMaplistEntries.size())
1571 {
1572 CMsgPacker Msg(NETMSG_MAPLIST_ADD, true);
1573 int Limit = NET_MAX_CHUNK_SIZE - 128;
1574 while((size_t)Client.m_MaplistEntryToSend < m_vMaplistEntries.size())
1575 {
1576 // Space for null termination not included in Limit
1577 const int SizeBefore = Msg.Size();
1578 Msg.AddString(pStr: m_vMaplistEntries[Client.m_MaplistEntryToSend].m_aName, Limit: Limit - 1, AllowTruncation: false);
1579 if(Msg.Error())
1580 {
1581 break;
1582 }
1583 Limit -= Msg.Size() - SizeBefore;
1584 if(Limit <= 1)
1585 {
1586 break;
1587 }
1588 ++Client.m_MaplistEntryToSend;
1589 }
1590 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
1591 }
1592
1593 if((size_t)Client.m_MaplistEntryToSend >= m_vMaplistEntries.size())
1594 {
1595 SendMaplistGroupEnd(ClientId);
1596 Client.m_MaplistEntryToSend = CClient::MAPLIST_DONE;
1597 }
1598}
1599
1600static inline int MsgFromSixup(int Msg, bool System)
1601{
1602 if(System)
1603 {
1604 if(Msg == NETMSG_INFO)
1605 ;
1606 else if(Msg >= 14 && Msg <= 15)
1607 Msg += 11;
1608 else if(Msg >= 18 && Msg <= 28)
1609 Msg = NETMSG_READY + Msg - 18;
1610 else if(Msg < OFFSET_UUID)
1611 return -1;
1612 }
1613
1614 return Msg;
1615}
1616
1617bool CServer::CheckReservedSlotAuth(int ClientId, const char *pPassword)
1618{
1619 if(Config()->m_SvReservedSlotsPass[0] && !str_comp(a: Config()->m_SvReservedSlotsPass, b: pPassword))
1620 {
1621 log_info("server", "ClientId=%d joining reserved slot with reserved slots password", ClientId);
1622 return true;
1623 }
1624
1625 // "^([^:]*):(.*)$"
1626 if(Config()->m_SvReservedSlotsAuthLevel != 4)
1627 {
1628 char aName[sizeof(Config()->m_Password)];
1629 const char *pInnerPassword = str_next_token(str: pPassword, delim: ":", buffer: aName, buffer_size: sizeof(aName));
1630 if(!pInnerPassword)
1631 {
1632 return false;
1633 }
1634 int Slot = m_AuthManager.FindKey(pIdent: aName);
1635 if(m_AuthManager.CheckKey(Slot, pPw: pInnerPassword + 1) && m_AuthManager.KeyLevel(Slot) >= Config()->m_SvReservedSlotsAuthLevel)
1636 {
1637 log_info("server", "ClientId=%d joining reserved slot with key='%s'", ClientId, m_AuthManager.KeyIdent(Slot));
1638 return true;
1639 }
1640 }
1641
1642 return false;
1643}
1644
1645void CServer::ProcessClientPacket(CNetChunk *pPacket)
1646{
1647 int ClientId = pPacket->m_ClientId;
1648 CUnpacker Unpacker;
1649 Unpacker.Reset(pData: pPacket->m_pData, Size: pPacket->m_DataSize);
1650 CMsgPacker Packer(NETMSG_EX, true);
1651
1652 // unpack msgid and system flag
1653 int Msg;
1654 bool Sys;
1655 CUuid Uuid;
1656
1657 int Result = UnpackMessageId(pId: &Msg, pSys: &Sys, pUuid: &Uuid, pUnpacker: &Unpacker, pPacker: &Packer);
1658 if(Result == UNPACKMESSAGE_ERROR)
1659 {
1660 return;
1661 }
1662
1663 if(m_aClients[ClientId].m_Sixup && (Msg = MsgFromSixup(Msg, System: Sys)) < 0)
1664 {
1665 return;
1666 }
1667
1668 if(Config()->m_SvNetlimit && Msg != NETMSG_REQUEST_MAP_DATA)
1669 {
1670 int64_t Now = time_get();
1671 int64_t Diff = Now - m_aClients[ClientId].m_TrafficSince;
1672 double Alpha = Config()->m_SvNetlimitAlpha / 100.0;
1673 double Limit = (double)(Config()->m_SvNetlimit * 1024) / time_freq();
1674
1675 if(m_aClients[ClientId].m_Traffic > Limit)
1676 {
1677 m_NetServer.NetBan()->BanAddr(pAddr: &pPacket->m_Address, Seconds: 600, pReason: "Stressing network", VerbatimReason: false);
1678 return;
1679 }
1680 if(Diff > 100)
1681 {
1682 m_aClients[ClientId].m_Traffic = (Alpha * ((double)pPacket->m_DataSize / Diff)) + (1.0 - Alpha) * m_aClients[ClientId].m_Traffic;
1683 m_aClients[ClientId].m_TrafficSince = Now;
1684 }
1685 }
1686
1687 if(Result == UNPACKMESSAGE_ANSWER)
1688 {
1689 SendMsg(pMsg: &Packer, Flags: MSGFLAG_VITAL, ClientId);
1690 }
1691
1692 {
1693 bool VitalFlag = (pPacket->m_Flags & NET_CHUNKFLAG_VITAL) != 0;
1694 bool NonVitalMsg = Sys && (Msg == NETMSG_INPUT || Msg == NETMSG_PING || Msg == NETMSG_PINGEX);
1695 if(!VitalFlag && !NonVitalMsg)
1696 {
1697 if(g_Config.m_Debug)
1698 {
1699 log_debug(
1700 "server",
1701 "strange message ClientId=%d msg=%d data_size=%d (missing vital flag)",
1702 ClientId,
1703 Msg,
1704 pPacket->m_DataSize);
1705 }
1706 return;
1707 }
1708 }
1709
1710 if(Sys)
1711 {
1712 // system message
1713 if(Msg == NETMSG_CLIENTVER)
1714 {
1715 CUuid *pConnectionId = (CUuid *)Unpacker.GetRaw(Size: sizeof(*pConnectionId));
1716 int DDNetVersion = Unpacker.GetInt();
1717 const char *pDDNetVersionStr = Unpacker.GetString(SanitizeType: CUnpacker::SANITIZE_CC);
1718 if(Unpacker.Error())
1719 return;
1720
1721 OnNetMsgClientVer(ClientId, pConnectionId, DDNetVersion, pDDNetVersionStr);
1722 }
1723 else if(Msg == NETMSG_INFO)
1724 {
1725 const char *pVersion = Unpacker.GetString(SanitizeType: CUnpacker::SANITIZE_CC);
1726 if(Unpacker.Error())
1727 return;
1728 const char *pPassword = Unpacker.GetString(SanitizeType: CUnpacker::SANITIZE_CC);
1729 if(Unpacker.Error())
1730 pPassword = nullptr;
1731
1732 OnNetMsgInfo(ClientId, pVersion, pPasswordOrNullptr: pPassword);
1733 }
1734 else if(Msg == NETMSG_REQUEST_MAP_DATA)
1735 {
1736 if(m_aClients[ClientId].m_State < CClient::STATE_CONNECTING)
1737 return;
1738
1739 if(m_aClients[ClientId].m_Sixup)
1740 {
1741 for(int i = 0; i < Config()->m_SvMapWindow; i++)
1742 {
1743 SendMapData(ClientId, Chunk: m_aClients[ClientId].m_NextMapChunk++);
1744 }
1745 return;
1746 }
1747
1748 int Chunk = Unpacker.GetInt();
1749 if(Unpacker.Error())
1750 {
1751 return;
1752 }
1753 if(Chunk != m_aClients[ClientId].m_NextMapChunk || !Config()->m_SvFastDownload)
1754 {
1755 SendMapData(ClientId, Chunk);
1756 return;
1757 }
1758
1759 if(Chunk == 0)
1760 {
1761 for(int i = 0; i < Config()->m_SvMapWindow; i++)
1762 {
1763 SendMapData(ClientId, Chunk: i);
1764 }
1765 }
1766 SendMapData(ClientId, Chunk: Config()->m_SvMapWindow + m_aClients[ClientId].m_NextMapChunk);
1767 m_aClients[ClientId].m_NextMapChunk++;
1768 }
1769 else if(Msg == NETMSG_READY)
1770 {
1771 OnNetMsgReady(ClientId);
1772 }
1773 else if(Msg == NETMSG_ENTERGAME)
1774 {
1775 OnNetMsgEnterGame(ClientId);
1776 }
1777 else if(Msg == NETMSG_INPUT)
1778 {
1779 const int LastAckedSnapshot = Unpacker.GetInt();
1780 int IntendedTick = Unpacker.GetInt();
1781 int Size = Unpacker.GetInt();
1782 if(Unpacker.Error() || Size / 4 > MAX_INPUT_SIZE || IntendedTick < MIN_TICK || IntendedTick >= MAX_TICK)
1783 {
1784 return;
1785 }
1786
1787 m_aClients[ClientId].m_LastAckedSnapshot = LastAckedSnapshot;
1788 if(m_aClients[ClientId].m_LastAckedSnapshot > 0)
1789 m_aClients[ClientId].m_SnapRate = CClient::SNAPRATE_FULL;
1790
1791 int64_t TagTime;
1792 if(m_aClients[ClientId].m_Snapshots.Get(Tick: m_aClients[ClientId].m_LastAckedSnapshot, pTagtime: &TagTime, ppData: nullptr, ppAltData: nullptr) >= 0)
1793 m_aClients[ClientId].m_Latency = (int)(((time_get() - TagTime) * 1000) / time_freq());
1794
1795 // add message to report the input timing
1796 // skip packets that are old
1797 if(IntendedTick > m_aClients[ClientId].m_LastInputTick)
1798 {
1799 const int TimeLeft = (TickStartTime(Tick: IntendedTick) - time_get()) / (time_freq() / 1000);
1800
1801 CMsgPacker Msgp(NETMSG_INPUTTIMING, true);
1802 Msgp.AddInt(i: IntendedTick);
1803 Msgp.AddInt(i: TimeLeft);
1804 SendMsg(pMsg: &Msgp, Flags: 0, ClientId);
1805 }
1806
1807 m_aClients[ClientId].m_LastInputTick = IntendedTick;
1808
1809 CClient::CInput *pInput = &m_aClients[ClientId].m_aInputs[m_aClients[ClientId].m_CurrentInput];
1810
1811 if(IntendedTick <= Tick())
1812 IntendedTick = Tick() + 1;
1813
1814 pInput->m_GameTick = IntendedTick;
1815
1816 for(int i = 0; i < Size / 4; i++)
1817 {
1818 pInput->m_aData[i] = Unpacker.GetInt();
1819 }
1820 if(Unpacker.Error())
1821 {
1822 return;
1823 }
1824
1825 if(g_Config.m_SvPreInput)
1826 {
1827 // send preinputs of ClientId to valid clients
1828 bool aPreInputClients[MAX_CLIENTS] = {};
1829 GameServer()->PreInputClients(ClientId, pClients: aPreInputClients);
1830
1831 CNetMsg_Sv_PreInput PreInput = {};
1832 mem_zero(block: &PreInput, size: sizeof(PreInput));
1833 CNetObj_PlayerInput *pInputData = (CNetObj_PlayerInput *)&pInput->m_aData;
1834
1835 PreInput.m_Direction = pInputData->m_Direction;
1836 PreInput.m_Jump = pInputData->m_Jump;
1837 PreInput.m_Fire = pInputData->m_Fire;
1838 PreInput.m_Hook = pInputData->m_Hook;
1839 PreInput.m_WantedWeapon = pInputData->m_WantedWeapon;
1840 PreInput.m_NextWeapon = pInputData->m_NextWeapon;
1841 PreInput.m_PrevWeapon = pInputData->m_PrevWeapon;
1842
1843 if(mem_comp(a: &m_aClients[ClientId].m_LastPreInput, b: &PreInput, size: sizeof(CNetMsg_Sv_PreInput)) != 0)
1844 {
1845 m_aClients[ClientId].m_LastPreInput = PreInput;
1846
1847 PreInput.m_Owner = ClientId;
1848 PreInput.m_IntendedTick = IntendedTick;
1849
1850 // target angle isn't updated all the time to save bandwidth
1851 PreInput.m_TargetX = pInputData->m_TargetX;
1852 PreInput.m_TargetY = pInputData->m_TargetY;
1853
1854 for(int Id = 0; Id < MAX_CLIENTS; Id++)
1855 {
1856 if(!aPreInputClients[Id])
1857 continue;
1858
1859 SendPackMsg(pMsg: &PreInput, Flags: MSGFLAG_FLUSH | MSGFLAG_NORECORD, ClientId: Id);
1860 }
1861 }
1862 }
1863
1864 GameServer()->OnClientPrepareInput(ClientId, pInput: pInput->m_aData);
1865 mem_copy(dest: m_aClients[ClientId].m_LatestInput.m_aData, source: pInput->m_aData, size: MAX_INPUT_SIZE * sizeof(int));
1866
1867 m_aClients[ClientId].m_CurrentInput++;
1868 m_aClients[ClientId].m_CurrentInput %= 200;
1869
1870 // call the mod with the fresh input data
1871 if(m_aClients[ClientId].m_State == CClient::STATE_INGAME)
1872 GameServer()->OnClientDirectInput(ClientId, pInput: m_aClients[ClientId].m_LatestInput.m_aData);
1873 }
1874 else if(Msg == NETMSG_RCON_CMD)
1875 {
1876 const char *pCmd = Unpacker.GetString();
1877 if(Unpacker.Error())
1878 return;
1879
1880 OnNetMsgRconCmd(ClientId, pCmd);
1881 }
1882 else if(Msg == NETMSG_RCON_AUTH)
1883 {
1884 const char *pName = "";
1885 if(!IsSixup(ClientId))
1886 pName = Unpacker.GetString(SanitizeType: CUnpacker::SANITIZE_CC); // login name, now used
1887 const char *pPw = Unpacker.GetString(SanitizeType: CUnpacker::SANITIZE_CC);
1888 bool SendRconCmds = true;
1889 if(!IsSixup(ClientId))
1890 SendRconCmds = Unpacker.GetInt() != 0;
1891 if(Unpacker.Error())
1892 return;
1893
1894 OnNetMsgRconAuth(ClientId, pName, pPw, SendRconCmds);
1895 }
1896 else if(Msg == NETMSG_PING)
1897 {
1898 CMsgPacker Msgp(NETMSG_PING_REPLY, true);
1899 int Vital = (pPacket->m_Flags & NET_CHUNKFLAG_VITAL) != 0 ? MSGFLAG_VITAL : 0;
1900 SendMsg(pMsg: &Msgp, Flags: MSGFLAG_FLUSH | Vital, ClientId);
1901 }
1902 else if(Msg == NETMSG_PINGEX)
1903 {
1904 CUuid *pId = (CUuid *)Unpacker.GetRaw(Size: sizeof(*pId));
1905 if(Unpacker.Error())
1906 {
1907 return;
1908 }
1909 CMsgPacker Msgp(NETMSG_PONGEX, true);
1910 Msgp.AddRaw(pData: pId, Size: sizeof(*pId));
1911 int Vital = (pPacket->m_Flags & NET_CHUNKFLAG_VITAL) != 0 ? MSGFLAG_VITAL : 0;
1912 SendMsg(pMsg: &Msgp, Flags: MSGFLAG_FLUSH | Vital, ClientId);
1913 }
1914 else
1915 {
1916 if(Config()->m_Debug)
1917 {
1918 constexpr int MaxDumpedDataSize = 32;
1919 char aBuf[MaxDumpedDataSize * 3 + 1];
1920 str_hex(dst: aBuf, dst_size: sizeof(aBuf), data: pPacket->m_pData, data_size: minimum(a: pPacket->m_DataSize, b: MaxDumpedDataSize));
1921
1922 char aBufMsg[256];
1923 str_format(buffer: aBufMsg, buffer_size: sizeof(aBufMsg), format: "strange message ClientId=%d msg=%d data_size=%d", ClientId, Msg, pPacket->m_DataSize);
1924 Console()->Print(Level: IConsole::OUTPUT_LEVEL_DEBUG, pFrom: "server", pStr: aBufMsg);
1925 Console()->Print(Level: IConsole::OUTPUT_LEVEL_DEBUG, pFrom: "server", pStr: aBuf);
1926 }
1927 }
1928 }
1929 else if(m_aClients[ClientId].m_State >= CClient::STATE_READY)
1930 {
1931 // game message
1932 GameServer()->OnMessage(MsgId: Msg, pUnpacker: &Unpacker, ClientId);
1933 }
1934}
1935
1936void CServer::OnNetMsgClientVer(int ClientId, CUuid *pConnectionId, int DDNetVersion, const char *pDDNetVersionStr)
1937{
1938 if(m_aClients[ClientId].m_State != CClient::STATE_PREAUTH)
1939 return;
1940 if(DDNetVersion < 0)
1941 return;
1942
1943 m_aClients[ClientId].m_ConnectionId = *pConnectionId;
1944 m_aClients[ClientId].m_DDNetVersion = DDNetVersion;
1945 str_copy(dst&: m_aClients[ClientId].m_aDDNetVersionStr, src: pDDNetVersionStr);
1946 m_aClients[ClientId].m_DDNetVersionSettled = true;
1947 m_aClients[ClientId].m_GotDDNetVersionPacket = true;
1948 m_aClients[ClientId].m_State = CClient::STATE_AUTH;
1949}
1950
1951void CServer::OnNetMsgInfo(int ClientId, const char *pVersion, const char *pPasswordOrNullptr)
1952{
1953 if((m_aClients[ClientId].m_State != CClient::STATE_PREAUTH && m_aClients[ClientId].m_State != CClient::STATE_AUTH))
1954 return;
1955
1956 if(str_comp(a: pVersion, b: GameServer()->NetVersion()) != 0 && str_comp(a: pVersion, b: "0.7 802f1be60a05665f") != 0)
1957 {
1958 // wrong version
1959 char aReason[256];
1960 str_format(buffer: aReason, buffer_size: sizeof(aReason), format: "Wrong version. Server is running '%s' and client '%s'", GameServer()->NetVersion(), pVersion);
1961 m_NetServer.Drop(ClientId, pReason: aReason);
1962 return;
1963 }
1964
1965 const char *pPassword = pPasswordOrNullptr;
1966 if(!pPassword)
1967 return;
1968
1969 if(Config()->m_Password[0] != 0 && str_comp(a: Config()->m_Password, b: pPassword) != 0)
1970 {
1971 // wrong password
1972 m_NetServer.Drop(ClientId, pReason: "Wrong password");
1973 return;
1974 }
1975
1976 int NumConnectedClients = 0;
1977 for(int i = 0; i < MaxClients(); ++i)
1978 {
1979 if(m_aClients[i].m_State != CClient::STATE_EMPTY)
1980 {
1981 NumConnectedClients++;
1982 }
1983 }
1984
1985 // reserved slot
1986 if(NumConnectedClients > MaxClients() - Config()->m_SvReservedSlots && !CheckReservedSlotAuth(ClientId, pPassword))
1987 {
1988 m_NetServer.Drop(ClientId, pReason: "This server is full");
1989 return;
1990 }
1991
1992 m_aClients[ClientId].m_State = CClient::STATE_CONNECTING;
1993 SendRconType(ClientId, UsernameReq: m_AuthManager.NumNonDefaultKeys() > 0);
1994 SendCapabilities(ClientId);
1995 SendMap(ClientId);
1996}
1997
1998void CServer::OnNetMsgReady(int ClientId)
1999{
2000 if(m_aClients[ClientId].m_State == CClient::STATE_CONNECTING)
2001 {
2002 log_debug(
2003 "server",
2004 "player is ready. ClientId=%d addr=<{%s}> secure=%s",
2005 ClientId,
2006 ClientAddrString(ClientId, true),
2007 m_NetServer.HasSecurityToken(ClientId) ? "yes" : "no");
2008
2009 void *pPersistentData = nullptr;
2010 if(m_aClients[ClientId].m_HasPersistentData)
2011 {
2012 pPersistentData = m_aClients[ClientId].m_pPersistentData;
2013 m_aClients[ClientId].m_HasPersistentData = false;
2014 }
2015 m_aClients[ClientId].m_State = CClient::STATE_READY;
2016 GameServer()->OnClientConnected(ClientId, pPersistentData);
2017 }
2018
2019 // Make rejoining session possible before timeout protection triggers
2020 // https://github.com/ddnet/ddnet/pull/301
2021 SendConnectionReady(ClientId);
2022}
2023
2024void CServer::OnNetMsgEnterGame(int ClientId)
2025{
2026 if(m_aClients[ClientId].m_State != CClient::STATE_READY)
2027 return;
2028 if(!GameServer()->IsClientReady(ClientId))
2029 return;
2030
2031 log_info(
2032 "server",
2033 "player has entered the game. ClientId=%d addr=<{%s}> sixup=%d",
2034 ClientId,
2035 ClientAddrString(ClientId, true),
2036 IsSixup(ClientId));
2037 m_aClients[ClientId].m_State = CClient::STATE_INGAME;
2038 if(!IsSixup(ClientId))
2039 {
2040 SendServerInfo(pAddr: ClientAddr(ClientId), Token: -1, Type: SERVERINFO_EXTENDED, SendClients: false);
2041 }
2042 else
2043 {
2044 CMsgPacker ServerInfoMessage(protocol7::NETMSG_SERVERINFO, true, true);
2045 GetServerInfoSixup(pPacker: &ServerInfoMessage, SendClients: false);
2046 SendMsg(pMsg: &ServerInfoMessage, Flags: MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientId);
2047 }
2048 GameServer()->OnClientEnter(ClientId);
2049}
2050
2051void CServer::OnNetMsgRconCmd(int ClientId, const char *pCmd)
2052{
2053 if(!str_comp(a: pCmd, b: "crashmeplx"))
2054 {
2055 int Version = m_aClients[ClientId].m_DDNetVersion;
2056 if(GameServer()->PlayerExists(ClientId) && Version < VERSION_DDNET_OLD)
2057 {
2058 m_aClients[ClientId].m_DDNetVersion = VERSION_DDNET_OLD;
2059 }
2060 }
2061 else if(IsRconAuthed(ClientId))
2062 {
2063 if(GameServer()->PlayerExists(ClientId))
2064 {
2065 log_info("server", "ClientId=%d key='%s' rcon='%s'", ClientId, GetAuthName(ClientId), pCmd);
2066 m_RconClientId = ClientId;
2067 m_RconAuthLevel = GetAuthedState(ClientId);
2068 {
2069 CRconClientLogger Logger(this, ClientId);
2070 CLogScope Scope(&Logger);
2071 Console()->ExecuteLineFlag(pStr: pCmd, FlasgMask: CFGFLAG_SERVER, ClientId);
2072 }
2073 m_RconClientId = IServer::RCON_CID_SERV;
2074 m_RconAuthLevel = AUTHED_ADMIN;
2075 }
2076 }
2077}
2078
2079void CServer::OnNetMsgRconAuth(int ClientId, const char *pName, const char *pPw, bool SendRconCmds)
2080{
2081 int AuthLevel = -1;
2082 int KeySlot = -1;
2083
2084 if(!pName[0])
2085 {
2086 if(m_AuthManager.CheckKey(Slot: (KeySlot = m_AuthManager.DefaultKey(pRoleName: RoleName::ADMIN)), pPw))
2087 AuthLevel = AUTHED_ADMIN;
2088 else if(m_AuthManager.CheckKey(Slot: (KeySlot = m_AuthManager.DefaultKey(pRoleName: RoleName::MODERATOR)), pPw))
2089 AuthLevel = AUTHED_MOD;
2090 else if(m_AuthManager.CheckKey(Slot: (KeySlot = m_AuthManager.DefaultKey(pRoleName: RoleName::HELPER)), pPw))
2091 AuthLevel = AUTHED_HELPER;
2092 }
2093 else
2094 {
2095 KeySlot = m_AuthManager.FindKey(pIdent: pName);
2096 if(m_AuthManager.CheckKey(Slot: KeySlot, pPw))
2097 AuthLevel = m_AuthManager.KeyLevel(Slot: KeySlot);
2098 }
2099
2100 if(AuthLevel != -1)
2101 {
2102 if(GetAuthedState(ClientId) != AuthLevel)
2103 {
2104 if(!IsSixup(ClientId))
2105 {
2106 CMsgPacker Msgp(NETMSG_RCON_AUTH_STATUS, true);
2107 Msgp.AddInt(i: 1); //authed
2108 Msgp.AddInt(i: 1); //cmdlist
2109 SendMsg(pMsg: &Msgp, Flags: MSGFLAG_VITAL, ClientId);
2110 }
2111 else
2112 {
2113 CMsgPacker Msgp(protocol7::NETMSG_RCON_AUTH_ON, true, true);
2114 SendMsg(pMsg: &Msgp, Flags: MSGFLAG_VITAL, ClientId);
2115 }
2116
2117 m_aClients[ClientId].m_AuthKey = KeySlot;
2118 if(SendRconCmds)
2119 {
2120 m_aClients[ClientId].m_pRconCmdToSend = Console()->FirstCommandInfo(ClientId, FlagMask: CFGFLAG_SERVER);
2121 SendRconCmdGroupStart(ClientId);
2122 if(m_aClients[ClientId].m_pRconCmdToSend == nullptr)
2123 {
2124 SendRconCmdGroupEnd(ClientId);
2125 }
2126 }
2127
2128 const char *pIdent = m_AuthManager.KeyIdent(Slot: KeySlot);
2129 switch(AuthLevel)
2130 {
2131 case AUTHED_ADMIN:
2132 {
2133 SendRconLine(ClientId, pLine: "Admin authentication successful. Full remote console access granted.");
2134 log_info("server", "ClientId=%d authed with key='%s' (admin)", ClientId, pIdent);
2135 break;
2136 }
2137 case AUTHED_MOD:
2138 {
2139 SendRconLine(ClientId, pLine: "Moderator authentication successful. Limited remote console access granted.");
2140 log_info("server", "ClientId=%d authed with key='%s' (moderator)", ClientId, pIdent);
2141 break;
2142 }
2143 case AUTHED_HELPER:
2144 {
2145 SendRconLine(ClientId, pLine: "Helper authentication successful. Limited remote console access granted.");
2146 log_info("server", "ClientId=%d authed with key='%s' (helper)", ClientId, pIdent);
2147 break;
2148 }
2149 }
2150
2151 // DDRace
2152 GameServer()->OnSetAuthed(ClientId, Level: AuthLevel);
2153 }
2154 }
2155 else if(Config()->m_SvRconMaxTries)
2156 {
2157 m_aClients[ClientId].m_AuthTries++;
2158 char aBuf[128];
2159 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Wrong password %d/%d.", m_aClients[ClientId].m_AuthTries, Config()->m_SvRconMaxTries);
2160 SendRconLine(ClientId, pLine: aBuf);
2161 if(m_aClients[ClientId].m_AuthTries >= Config()->m_SvRconMaxTries)
2162 {
2163 if(!Config()->m_SvRconBantime)
2164 m_NetServer.Drop(ClientId, pReason: "Too many remote console authentication tries");
2165 else
2166 m_ServerBan.BanAddr(pAddr: ClientAddr(ClientId), Seconds: Config()->m_SvRconBantime * 60, pReason: "Too many remote console authentication tries", VerbatimReason: false);
2167 }
2168 }
2169 else
2170 {
2171 SendRconLine(ClientId, pLine: "Wrong password.");
2172 }
2173}
2174
2175bool CServer::RateLimitServerInfoConnless()
2176{
2177 bool SendClients = true;
2178 if(Config()->m_SvServerInfoPerSecond)
2179 {
2180 SendClients = m_ServerInfoNumRequests <= Config()->m_SvServerInfoPerSecond;
2181 const int64_t Now = Tick();
2182
2183 if(Now <= m_ServerInfoFirstRequest + TickSpeed())
2184 {
2185 m_ServerInfoNumRequests++;
2186 }
2187 else
2188 {
2189 m_ServerInfoNumRequests = 1;
2190 m_ServerInfoFirstRequest = Now;
2191 }
2192 }
2193
2194 return SendClients;
2195}
2196
2197void CServer::SendServerInfoConnless(const NETADDR *pAddr, int Token, int Type)
2198{
2199 SendServerInfo(pAddr, Token, Type, SendClients: RateLimitServerInfoConnless());
2200}
2201
2202static inline int GetCacheIndex(int Type, bool SendClient)
2203{
2204 if(Type == SERVERINFO_INGAME)
2205 Type = SERVERINFO_VANILLA;
2206 else if(Type == SERVERINFO_EXTENDED_MORE)
2207 Type = SERVERINFO_EXTENDED;
2208
2209 return Type * 2 + SendClient;
2210}
2211
2212CServer::CCache::CCache()
2213{
2214 m_vCache.clear();
2215}
2216
2217CServer::CCache::~CCache()
2218{
2219 Clear();
2220}
2221
2222CServer::CCache::CCacheChunk::CCacheChunk(const void *pData, int Size)
2223{
2224 m_vData.assign(first: (const uint8_t *)pData, last: (const uint8_t *)pData + Size);
2225}
2226
2227void CServer::CCache::AddChunk(const void *pData, int Size)
2228{
2229 m_vCache.emplace_back(args&: pData, args&: Size);
2230}
2231
2232void CServer::CCache::Clear()
2233{
2234 m_vCache.clear();
2235}
2236
2237void CServer::CacheServerInfo(CCache *pCache, int Type, bool SendClients)
2238{
2239 pCache->Clear();
2240
2241 // One chance to improve the protocol!
2242 CPacker p;
2243 char aBuf[128];
2244
2245 // count the players
2246 int PlayerCount = 0, ClientCount = 0;
2247 for(int i = 0; i < MAX_CLIENTS; i++)
2248 {
2249 if(m_aClients[i].IncludedInServerInfo())
2250 {
2251 if(GameServer()->IsClientPlayer(ClientId: i))
2252 PlayerCount++;
2253
2254 ClientCount++;
2255 }
2256 }
2257
2258 p.Reset();
2259
2260#define ADD_RAW(p, x) (p).AddRaw(x, sizeof(x))
2261#define ADD_INT(p, x) \
2262 do \
2263 { \
2264 str_format(aBuf, sizeof(aBuf), "%d", x); \
2265 (p).AddString(aBuf, 0); \
2266 } while(0)
2267
2268 p.AddString(pStr: GameServer()->Version(), Limit: 32);
2269 if(Type != SERVERINFO_VANILLA)
2270 {
2271 p.AddString(pStr: Config()->m_SvName, Limit: 256);
2272 }
2273 else
2274 {
2275 if(m_NetServer.MaxClients() <= VANILLA_MAX_CLIENTS)
2276 {
2277 p.AddString(pStr: Config()->m_SvName, Limit: 64);
2278 }
2279 else
2280 {
2281 const int MaxClients = maximum(a: ClientCount, b: m_NetServer.MaxClients() - Config()->m_SvReservedSlots);
2282 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s [%d/%d]", Config()->m_SvName, ClientCount, MaxClients);
2283 p.AddString(pStr: aBuf, Limit: 64);
2284 }
2285 }
2286 p.AddString(pStr: GameServer()->Map()->BaseName(), Limit: 32);
2287
2288 if(Type == SERVERINFO_EXTENDED)
2289 {
2290 ADD_INT(p, m_aCurrentMapCrc[MAP_TYPE_SIX]);
2291 ADD_INT(p, m_aCurrentMapSize[MAP_TYPE_SIX]);
2292 }
2293
2294 // gametype
2295 p.AddString(pStr: GameServer()->GameType(), Limit: 16);
2296
2297 // flags
2298 ADD_INT(p, Config()->m_Password[0] ? SERVER_FLAG_PASSWORD : 0);
2299
2300 int MaxClients = m_NetServer.MaxClients();
2301 // How many clients the used serverinfo protocol supports, has to be tracked
2302 // separately to make sure we don't subtract the reserved slots from it
2303 int MaxClientsProtocol = MAX_CLIENTS;
2304 if(Type == SERVERINFO_VANILLA || Type == SERVERINFO_INGAME)
2305 {
2306 if(ClientCount >= VANILLA_MAX_CLIENTS)
2307 {
2308 if(ClientCount < MaxClients)
2309 ClientCount = VANILLA_MAX_CLIENTS - 1;
2310 else
2311 ClientCount = VANILLA_MAX_CLIENTS;
2312 }
2313 MaxClientsProtocol = VANILLA_MAX_CLIENTS;
2314 if(PlayerCount > ClientCount)
2315 PlayerCount = ClientCount;
2316 }
2317
2318 ADD_INT(p, PlayerCount); // num players
2319 ADD_INT(p, minimum(MaxClientsProtocol, maximum(MaxClients - maximum(Config()->m_SvSpectatorSlots, Config()->m_SvReservedSlots), PlayerCount))); // max players
2320 ADD_INT(p, ClientCount); // num clients
2321 ADD_INT(p, minimum(MaxClientsProtocol, maximum(MaxClients - Config()->m_SvReservedSlots, ClientCount))); // max clients
2322
2323 if(Type == SERVERINFO_EXTENDED)
2324 p.AddString(pStr: "", Limit: 0); // extra info, reserved
2325
2326 const void *pPrefix = p.Data();
2327 int PrefixSize = p.Size();
2328
2329 CPacker q;
2330 int ChunksStored = 0;
2331 int PlayersStored = 0;
2332
2333#define SAVE(size) \
2334 do \
2335 { \
2336 pCache->AddChunk(q.Data(), size); \
2337 ChunksStored++; \
2338 } while(0)
2339
2340#define RESET() \
2341 do \
2342 { \
2343 q.Reset(); \
2344 q.AddRaw(pPrefix, PrefixSize); \
2345 } while(0)
2346
2347 RESET();
2348
2349 if(Type == SERVERINFO_64_LEGACY)
2350 q.AddInt(i: PlayersStored); // offset
2351
2352 if(!SendClients)
2353 {
2354 SAVE(q.Size());
2355 return;
2356 }
2357
2358 if(Type == SERVERINFO_EXTENDED)
2359 {
2360 pPrefix = "";
2361 PrefixSize = 0;
2362 }
2363
2364 int Remaining;
2365 switch(Type)
2366 {
2367 case SERVERINFO_EXTENDED: Remaining = -1; break;
2368 case SERVERINFO_64_LEGACY: Remaining = 24; break;
2369 case SERVERINFO_VANILLA: Remaining = VANILLA_MAX_CLIENTS; break;
2370 case SERVERINFO_INGAME: Remaining = VANILLA_MAX_CLIENTS; break;
2371 default: dbg_assert_failed("Invalid Type: %d", Type);
2372 }
2373
2374 // Use the following strategy for sending:
2375 // For vanilla, send the first 16 players.
2376 // For legacy 64p, send 24 players per packet.
2377 // For extended, send as much players as possible.
2378
2379 for(int i = 0; i < MAX_CLIENTS; i++)
2380 {
2381 if(m_aClients[i].IncludedInServerInfo())
2382 {
2383 if(Remaining == 0)
2384 {
2385 if(Type == SERVERINFO_VANILLA || Type == SERVERINFO_INGAME)
2386 break;
2387
2388 // Otherwise we're SERVERINFO_64_LEGACY.
2389 SAVE(q.Size());
2390 RESET();
2391 q.AddInt(i: PlayersStored); // offset
2392 Remaining = 24;
2393 }
2394 if(Remaining > 0)
2395 {
2396 Remaining--;
2397 }
2398
2399 int PreviousSize = q.Size();
2400
2401 q.AddString(pStr: ClientName(ClientId: i), Limit: MAX_NAME_LENGTH); // client name
2402 q.AddString(pStr: ClientClan(ClientId: i), Limit: MAX_CLAN_LENGTH); // client clan
2403
2404 ADD_INT(q, m_aClients[i].m_Country); // client country (ISO 3166-1 numeric)
2405
2406 int Score;
2407 if(m_aClients[i].m_Score.has_value())
2408 {
2409 Score = m_aClients[i].m_Score.value();
2410 if(Score == -FinishTime::NOT_FINISHED_TIMESCORE)
2411 Score = FinishTime::NOT_FINISHED_TIMESCORE - 1;
2412 else if(Score == 0) // 0 time isn't displayed otherwise.
2413 Score = -1;
2414 else
2415 Score = -Score;
2416 }
2417 else
2418 {
2419 Score = FinishTime::NOT_FINISHED_TIMESCORE;
2420 }
2421
2422 ADD_INT(q, Score); // client score
2423 ADD_INT(q, GameServer()->IsClientPlayer(i) ? 1 : 0); // is player?
2424 if(Type == SERVERINFO_EXTENDED)
2425 q.AddString(pStr: "", Limit: 0); // extra info, reserved
2426
2427 if(Type == SERVERINFO_EXTENDED)
2428 {
2429 if(q.Size() >= NET_MAX_PAYLOAD - 18) // 8 bytes for type, 10 bytes for the largest token
2430 {
2431 // Retry current player.
2432 i--;
2433 SAVE(PreviousSize);
2434 RESET();
2435 ADD_INT(q, ChunksStored);
2436 q.AddString(pStr: "", Limit: 0); // extra info, reserved
2437 continue;
2438 }
2439 }
2440 PlayersStored++;
2441 }
2442 }
2443
2444 SAVE(q.Size());
2445#undef SAVE
2446#undef RESET
2447#undef ADD_RAW
2448#undef ADD_INT
2449}
2450
2451void CServer::CacheServerInfoSixup(CCache *pCache, bool SendClients, int MaxConsideredClients)
2452{
2453 pCache->Clear();
2454
2455 CPacker Packer;
2456 Packer.Reset();
2457
2458 // Could be moved to a separate function and cached
2459 // count the players
2460 int PlayerCount = 0, ClientCount = 0, ClientCountAll = 0;
2461 for(int i = 0; i < MAX_CLIENTS; i++)
2462 {
2463 if(m_aClients[i].IncludedInServerInfo())
2464 {
2465 ClientCountAll++;
2466 if(i < MaxConsideredClients)
2467 {
2468 if(GameServer()->IsClientPlayer(ClientId: i))
2469 PlayerCount++;
2470
2471 ClientCount++;
2472 }
2473 }
2474 }
2475
2476 char aVersion[32];
2477 str_format(buffer: aVersion, buffer_size: sizeof(aVersion), format: "0.7↔%s", GameServer()->Version());
2478 Packer.AddString(pStr: aVersion, Limit: 32);
2479 if(!SendClients || ClientCountAll == ClientCount)
2480 {
2481 Packer.AddString(pStr: Config()->m_SvName, Limit: 64);
2482 }
2483 else
2484 {
2485 char aName[64];
2486 str_format(buffer: aName, buffer_size: sizeof(aName), format: "%s [%d/%d]", Config()->m_SvName, ClientCountAll, m_NetServer.MaxClients() - Config()->m_SvReservedSlots);
2487 Packer.AddString(pStr: aName, Limit: 64);
2488 }
2489 Packer.AddString(pStr: Config()->m_SvHostname, Limit: 128);
2490 Packer.AddString(pStr: GameServer()->Map()->BaseName(), Limit: 32);
2491
2492 // gametype
2493 Packer.AddString(pStr: GameServer()->GameType(), Limit: 16);
2494
2495 // flags
2496 int Flags = SERVER_FLAG_TIMESCORE;
2497 if(Config()->m_Password[0]) // password set
2498 Flags |= SERVER_FLAG_PASSWORD;
2499 Packer.AddInt(i: Flags);
2500
2501 int MaxClients = m_NetServer.MaxClients();
2502 Packer.AddInt(i: Config()->m_SvSkillLevel); // server skill level
2503 Packer.AddInt(i: PlayerCount); // num players
2504 Packer.AddInt(i: maximum(a: MaxClients - maximum(a: Config()->m_SvSpectatorSlots, b: Config()->m_SvReservedSlots), b: PlayerCount)); // max players
2505 Packer.AddInt(i: ClientCount); // num clients
2506 Packer.AddInt(i: maximum(a: MaxClients - Config()->m_SvReservedSlots, b: ClientCount)); // max clients
2507
2508 if(SendClients)
2509 {
2510 for(int i = 0; i < MaxConsideredClients; i++)
2511 {
2512 if(m_aClients[i].IncludedInServerInfo())
2513 {
2514 Packer.AddString(pStr: ClientName(ClientId: i), Limit: MAX_NAME_LENGTH); // client name
2515 Packer.AddString(pStr: ClientClan(ClientId: i), Limit: MAX_CLAN_LENGTH); // client clan
2516 Packer.AddInt(i: m_aClients[i].m_Country); // client country (ISO 3166-1 numeric)
2517 Packer.AddInt(i: m_aClients[i].m_Score.value_or(u: -1)); // client score
2518 Packer.AddInt(i: GameServer()->IsClientPlayer(ClientId: i) ? 0 : 1); // flag spectator=1, bot=2 (player=0)
2519
2520 const int MaxPacketSize = NET_MAX_PAYLOAD - 128;
2521 if(MaxConsideredClients == MAX_CLIENTS)
2522 {
2523 if(Packer.Size() > MaxPacketSize - 32) // -32 because repacking will increase the length of the name
2524 {
2525 // Server info is too large for a packet. Only include as many clients as fit.
2526 // We need to ensure that the client counts match, otherwise the 0.7 client
2527 // will ignore the info, so we repack but only consider the first i clients.
2528 CacheServerInfoSixup(pCache, SendClients: true, MaxConsideredClients: i);
2529 return;
2530 }
2531 }
2532 else
2533 {
2534 dbg_assert(Packer.Size() <= MaxPacketSize, "Max packet size exceeded while repacking. Packer.Size()=%d MaxPacketSize=%d", Packer.Size(), MaxPacketSize);
2535 }
2536 }
2537 }
2538 }
2539
2540 pCache->AddChunk(pData: Packer.Data(), Size: Packer.Size());
2541}
2542
2543void CServer::SendServerInfo(const NETADDR *pAddr, int Token, int Type, bool SendClients)
2544{
2545 CPacker p;
2546 char aBuf[128];
2547 p.Reset();
2548
2549 CCache *pCache = &m_aServerInfoCache[GetCacheIndex(Type, SendClient: SendClients)];
2550
2551#define ADD_RAW(p, x) (p).AddRaw(x, sizeof(x))
2552#define ADD_INT(p, x) \
2553 do \
2554 { \
2555 str_format(aBuf, sizeof(aBuf), "%d", x); \
2556 (p).AddString(aBuf, 0); \
2557 } while(0)
2558
2559 CNetChunk Packet;
2560 Packet.m_ClientId = -1;
2561 Packet.m_Address = *pAddr;
2562 Packet.m_Flags = NETSENDFLAG_CONNLESS;
2563
2564 for(const auto &Chunk : pCache->m_vCache)
2565 {
2566 p.Reset();
2567 if(Type == SERVERINFO_EXTENDED)
2568 {
2569 if(&Chunk == &pCache->m_vCache.front())
2570 p.AddRaw(pData: SERVERBROWSE_INFO_EXTENDED, Size: sizeof(SERVERBROWSE_INFO_EXTENDED));
2571 else
2572 p.AddRaw(pData: SERVERBROWSE_INFO_EXTENDED_MORE, Size: sizeof(SERVERBROWSE_INFO_EXTENDED_MORE));
2573 ADD_INT(p, Token);
2574 }
2575 else if(Type == SERVERINFO_64_LEGACY)
2576 {
2577 ADD_RAW(p, SERVERBROWSE_INFO_64_LEGACY);
2578 ADD_INT(p, Token);
2579 }
2580 else if(Type == SERVERINFO_VANILLA || Type == SERVERINFO_INGAME)
2581 {
2582 ADD_RAW(p, SERVERBROWSE_INFO);
2583 ADD_INT(p, Token);
2584 }
2585 else
2586 {
2587 dbg_assert_failed("Invalid serverinfo Type: %d", Type);
2588 }
2589
2590 p.AddRaw(pData: Chunk.m_vData.data(), Size: Chunk.m_vData.size());
2591 Packet.m_pData = p.Data();
2592 Packet.m_DataSize = p.Size();
2593 m_NetServer.Send(pChunk: &Packet);
2594 }
2595}
2596
2597void CServer::GetServerInfoSixup(CPacker *pPacker, bool SendClients)
2598{
2599 CCache::CCacheChunk &FirstChunk = m_aSixupServerInfoCache[SendClients].m_vCache.front();
2600 pPacker->AddRaw(pData: FirstChunk.m_vData.data(), Size: FirstChunk.m_vData.size());
2601}
2602
2603void CServer::FillAntibot(CAntibotRoundData *pData)
2604{
2605 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
2606 {
2607 CAntibotPlayerData *pPlayer = &pData->m_aPlayers[ClientId];
2608 if(m_aClients[ClientId].m_State == CServer::CClient::STATE_EMPTY)
2609 {
2610 pPlayer->m_aAddress[0] = '\0';
2611 }
2612 else
2613 {
2614 // No need for expensive str_copy since we don't truncate and the string is
2615 // ASCII anyway
2616 static_assert(std::size((CAntibotPlayerData{}).m_aAddress) >= NETADDR_MAXSTRSIZE);
2617 static_assert(std::is_same_v<decltype(CServer{}.ClientAddrStringImpl(ClientId, IncludePort: true)), const std::array<char, NETADDR_MAXSTRSIZE> &>);
2618 mem_copy(dest: pPlayer->m_aAddress, source: ClientAddrStringImpl(ClientId, IncludePort: true).data(), size: NETADDR_MAXSTRSIZE);
2619 pPlayer->m_Sixup = m_aClients[ClientId].m_Sixup;
2620 pPlayer->m_DnsblNone = m_aClients[ClientId].m_DnsblState == EDnsblState::NONE;
2621 pPlayer->m_DnsblPending = m_aClients[ClientId].m_DnsblState == EDnsblState::PENDING;
2622 pPlayer->m_DnsblBlacklisted = m_aClients[ClientId].m_DnsblState == EDnsblState::BLACKLISTED;
2623 pPlayer->m_Authed = IsRconAuthed(ClientId);
2624 }
2625 }
2626}
2627
2628void CServer::ExpireServerInfo()
2629{
2630 m_ServerInfoNeedsUpdate = true;
2631}
2632
2633void CServer::ExpireServerInfoAndQueueResend()
2634{
2635 m_ServerInfoNeedsUpdate = true;
2636 m_ServerInfoNeedsResend = true;
2637}
2638
2639void CServer::UpdateRegisterServerInfo()
2640{
2641 // count the players
2642 int PlayerCount = 0, ClientCount = 0;
2643 for(int i = 0; i < MAX_CLIENTS; i++)
2644 {
2645 if(m_aClients[i].IncludedInServerInfo())
2646 {
2647 if(GameServer()->IsClientPlayer(ClientId: i))
2648 PlayerCount++;
2649
2650 ClientCount++;
2651 }
2652 }
2653
2654 int MaxPlayers = maximum(a: m_NetServer.MaxClients() - maximum(a: g_Config.m_SvSpectatorSlots, b: g_Config.m_SvReservedSlots), b: PlayerCount);
2655 int MaxClients = maximum(a: m_NetServer.MaxClients() - g_Config.m_SvReservedSlots, b: ClientCount);
2656 char aMapSha256[SHA256_MAXSTRSIZE];
2657
2658 sha256_str(digest: m_aCurrentMapSha256[MAP_TYPE_SIX], str: aMapSha256, max_len: sizeof(aMapSha256));
2659
2660 CJsonStringWriter JsonWriter;
2661
2662 JsonWriter.BeginObject();
2663 JsonWriter.WriteAttribute(pName: "max_clients");
2664 JsonWriter.WriteIntValue(Value: MaxClients);
2665
2666 JsonWriter.WriteAttribute(pName: "max_players");
2667 JsonWriter.WriteIntValue(Value: MaxPlayers);
2668
2669 JsonWriter.WriteAttribute(pName: "passworded");
2670 JsonWriter.WriteBoolValue(Value: g_Config.m_Password[0]);
2671
2672 JsonWriter.WriteAttribute(pName: "game_type");
2673 JsonWriter.WriteStrValue(pValue: GameServer()->GameType());
2674
2675 if(g_Config.m_SvRegisterCommunityToken[0])
2676 {
2677 if(g_Config.m_SvFlag != -1)
2678 {
2679 JsonWriter.WriteAttribute(pName: "country");
2680 JsonWriter.WriteIntValue(Value: g_Config.m_SvFlag); // ISO 3166-1 numeric
2681 }
2682 }
2683
2684 JsonWriter.WriteAttribute(pName: "name");
2685 JsonWriter.WriteStrValue(pValue: g_Config.m_SvName);
2686
2687 JsonWriter.WriteAttribute(pName: "map");
2688 JsonWriter.BeginObject();
2689 JsonWriter.WriteAttribute(pName: "name");
2690 JsonWriter.WriteStrValue(pValue: GameServer()->Map()->BaseName());
2691 JsonWriter.WriteAttribute(pName: "sha256");
2692 JsonWriter.WriteStrValue(pValue: aMapSha256);
2693 JsonWriter.WriteAttribute(pName: "size");
2694 JsonWriter.WriteIntValue(Value: m_aCurrentMapSize[MAP_TYPE_SIX]);
2695 if(m_aMapDownloadUrl[0])
2696 {
2697 JsonWriter.WriteAttribute(pName: "url");
2698 JsonWriter.WriteStrValue(pValue: m_aMapDownloadUrl);
2699 }
2700 JsonWriter.EndObject();
2701
2702 JsonWriter.WriteAttribute(pName: "version");
2703 JsonWriter.WriteStrValue(pValue: GameServer()->Version());
2704
2705 JsonWriter.WriteAttribute(pName: "client_score_kind");
2706 JsonWriter.WriteStrValue(pValue: "time"); // "points" or "time"
2707
2708 JsonWriter.WriteAttribute(pName: "requires_login");
2709 JsonWriter.WriteBoolValue(Value: false);
2710
2711 {
2712 bool FoundFlags = false;
2713 auto Flag = [&](const char *pFlag) {
2714 if(!FoundFlags)
2715 {
2716 JsonWriter.WriteAttribute(pName: "flags");
2717 JsonWriter.BeginArray();
2718 FoundFlags = true;
2719 }
2720 JsonWriter.WriteStrValue(pValue: pFlag);
2721 };
2722
2723 if(g_Config.m_SvRegisterCommunityToken[0] && g_Config.m_SvOfficialTutorial[0])
2724 {
2725 SHA256_DIGEST Sha256 = sha256(message: g_Config.m_SvOfficialTutorial, message_len: str_length(str: g_Config.m_SvOfficialTutorial));
2726 char aSha256[SHA256_MAXSTRSIZE];
2727 sha256_str(digest: Sha256, str: aSha256, max_len: sizeof(aSha256));
2728 if(str_comp(a: aSha256, b: "8a11dc71274313e78a09ff58b8e696fb5009ce8606d12077ceb78ebf99a57464") == 0)
2729 {
2730 Flag("tutorial");
2731 }
2732 }
2733
2734 if(FoundFlags)
2735 {
2736 JsonWriter.EndArray();
2737 }
2738 }
2739
2740 JsonWriter.WriteAttribute(pName: "clients");
2741 JsonWriter.BeginArray();
2742
2743 for(int i = 0; i < MAX_CLIENTS; i++)
2744 {
2745 if(m_aClients[i].IncludedInServerInfo())
2746 {
2747 JsonWriter.BeginObject();
2748
2749 JsonWriter.WriteAttribute(pName: "name");
2750 JsonWriter.WriteStrValue(pValue: ClientName(ClientId: i));
2751
2752 JsonWriter.WriteAttribute(pName: "clan");
2753 JsonWriter.WriteStrValue(pValue: ClientClan(ClientId: i));
2754
2755 JsonWriter.WriteAttribute(pName: "country");
2756 JsonWriter.WriteIntValue(Value: m_aClients[i].m_Country); // ISO 3166-1 numeric
2757
2758 JsonWriter.WriteAttribute(pName: "score");
2759 JsonWriter.WriteIntValue(Value: m_aClients[i].m_Score.value_or(u: FinishTime::NOT_FINISHED_TIMESCORE));
2760
2761 JsonWriter.WriteAttribute(pName: "is_player");
2762 JsonWriter.WriteBoolValue(Value: GameServer()->IsClientPlayer(ClientId: i));
2763
2764 GameServer()->OnUpdatePlayerServerInfo(pJsonWriter: &JsonWriter, ClientId: i);
2765
2766 JsonWriter.EndObject();
2767 }
2768 }
2769
2770 JsonWriter.EndArray();
2771 JsonWriter.EndObject();
2772
2773 m_pRegister->OnNewInfo(pInfo: JsonWriter.GetOutputString().c_str());
2774}
2775
2776void CServer::UpdateServerInfo(bool Resend)
2777{
2778 if(m_RunServer == UNINITIALIZED)
2779 return;
2780
2781 UpdateRegisterServerInfo();
2782
2783 for(int i = 0; i < 3; i++)
2784 for(int j = 0; j < 2; j++)
2785 CacheServerInfo(pCache: &m_aServerInfoCache[i * 2 + j], Type: i, SendClients: j);
2786
2787 for(int i = 0; i < 2; i++)
2788 CacheServerInfoSixup(pCache: &m_aSixupServerInfoCache[i], SendClients: i, MaxConsideredClients: MAX_CLIENTS);
2789
2790 if(Resend)
2791 {
2792 for(int i = 0; i < MaxClients(); ++i)
2793 {
2794 if(m_aClients[i].m_State != CClient::STATE_EMPTY)
2795 {
2796 if(!IsSixup(ClientId: i))
2797 SendServerInfo(pAddr: ClientAddr(ClientId: i), Token: -1, Type: SERVERINFO_INGAME, SendClients: false);
2798 else
2799 {
2800 CMsgPacker ServerInfoMessage(protocol7::NETMSG_SERVERINFO, true, true);
2801 GetServerInfoSixup(pPacker: &ServerInfoMessage, SendClients: false);
2802 SendMsg(pMsg: &ServerInfoMessage, Flags: MSGFLAG_VITAL | MSGFLAG_FLUSH, ClientId: i);
2803 }
2804 }
2805 }
2806 m_ServerInfoNeedsResend = false;
2807 }
2808
2809 m_ServerInfoNeedsUpdate = false;
2810}
2811
2812void CServer::PumpNetwork(bool PacketWaiting)
2813{
2814 CNetChunk Packet;
2815 SECURITY_TOKEN ResponseToken;
2816
2817 m_NetServer.Update();
2818
2819 if(PacketWaiting)
2820 {
2821 // process packets
2822 ResponseToken = NET_SECURITY_TOKEN_UNKNOWN;
2823 while(m_NetServer.Recv(pChunk: &Packet, pResponseToken: &ResponseToken))
2824 {
2825 if(Packet.m_ClientId == -1)
2826 {
2827 if(ResponseToken == NET_SECURITY_TOKEN_UNKNOWN && m_pRegister->OnPacket(pPacket: &Packet))
2828 continue;
2829
2830 {
2831 int ExtraToken = 0;
2832 int Type = -1;
2833 if(Packet.m_DataSize >= (int)sizeof(SERVERBROWSE_GETINFO) + 1 &&
2834 mem_comp(a: Packet.m_pData, b: SERVERBROWSE_GETINFO, size: sizeof(SERVERBROWSE_GETINFO)) == 0)
2835 {
2836 if(Packet.m_Flags & NETSENDFLAG_EXTENDED)
2837 {
2838 Type = SERVERINFO_EXTENDED;
2839 ExtraToken = (Packet.m_aExtraData[0] << 8) | Packet.m_aExtraData[1];
2840 }
2841 else
2842 Type = SERVERINFO_VANILLA;
2843 }
2844 else if(Packet.m_DataSize >= (int)sizeof(SERVERBROWSE_GETINFO_64_LEGACY) + 1 &&
2845 mem_comp(a: Packet.m_pData, b: SERVERBROWSE_GETINFO_64_LEGACY, size: sizeof(SERVERBROWSE_GETINFO_64_LEGACY)) == 0)
2846 {
2847 Type = SERVERINFO_64_LEGACY;
2848 }
2849 if(Type == SERVERINFO_VANILLA && ResponseToken != NET_SECURITY_TOKEN_UNKNOWN && Config()->m_SvSixup)
2850 {
2851 CUnpacker Unpacker;
2852 Unpacker.Reset(pData: (unsigned char *)Packet.m_pData + sizeof(SERVERBROWSE_GETINFO), Size: Packet.m_DataSize - sizeof(SERVERBROWSE_GETINFO));
2853 int SrvBrwsToken = Unpacker.GetInt();
2854 if(Unpacker.Error())
2855 {
2856 continue;
2857 }
2858
2859 CPacker Packer;
2860 Packer.Reset();
2861 Packer.AddRaw(pData: SERVERBROWSE_INFO, Size: sizeof(SERVERBROWSE_INFO));
2862 Packer.AddInt(i: SrvBrwsToken);
2863 GetServerInfoSixup(pPacker: &Packer, SendClients: RateLimitServerInfoConnless());
2864 CNetBase::SendPacketConnlessWithToken7(Socket: m_NetServer.Socket(), pAddr: &Packet.m_Address, pData: Packer.Data(), DataSize: Packer.Size(), Token: ResponseToken, ResponseToken: m_NetServer.GetToken(Addr: Packet.m_Address));
2865 }
2866 else if(Type != -1)
2867 {
2868 int Token = ((unsigned char *)Packet.m_pData)[sizeof(SERVERBROWSE_GETINFO)];
2869 Token |= ExtraToken << 8;
2870 SendServerInfoConnless(pAddr: &Packet.m_Address, Token, Type);
2871 }
2872 }
2873 }
2874 else
2875 {
2876 if(m_aClients[Packet.m_ClientId].m_State == CClient::STATE_REDIRECTED)
2877 continue;
2878
2879 int GameFlags = 0;
2880 if(Packet.m_Flags & NET_CHUNKFLAG_VITAL)
2881 {
2882 GameFlags |= MSGFLAG_VITAL;
2883 }
2884 if(Antibot()->OnEngineClientMessage(ClientId: Packet.m_ClientId, pData: Packet.m_pData, Size: Packet.m_DataSize, Flags: GameFlags))
2885 {
2886 continue;
2887 }
2888
2889 ProcessClientPacket(pPacket: &Packet);
2890 }
2891 }
2892 }
2893 {
2894 unsigned char aBuffer[NET_MAX_PAYLOAD];
2895 int Flags;
2896 mem_zero(block: &Packet, size: sizeof(Packet));
2897 Packet.m_pData = aBuffer;
2898 while(Antibot()->OnEngineSimulateClientMessage(pClientId: &Packet.m_ClientId, pBuffer: aBuffer, BufferSize: sizeof(aBuffer), pOutSize: &Packet.m_DataSize, pFlags: &Flags))
2899 {
2900 Packet.m_Flags = 0;
2901 if(Flags & MSGFLAG_VITAL)
2902 {
2903 Packet.m_Flags |= NET_CHUNKFLAG_VITAL;
2904 }
2905 ProcessClientPacket(pPacket: &Packet);
2906 }
2907 }
2908
2909 m_ServerBan.Update();
2910 m_Econ.Update();
2911}
2912
2913void CServer::ChangeMap(const char *pMap)
2914{
2915 str_copy(dst&: Config()->m_SvMap, src: pMap);
2916 m_MapReload = str_comp(a: Config()->m_SvMap, b: GameServer()->Map()->FullName()) != 0;
2917}
2918
2919void CServer::ReloadMap()
2920{
2921 m_SameMapReload = true;
2922}
2923
2924int CServer::LoadMap(const char *pMapName)
2925{
2926 m_MapReload = false;
2927 m_SameMapReload = false;
2928
2929 char aBuf[IO_MAX_PATH_LENGTH];
2930 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "maps/%s.map", pMapName);
2931 if(!str_valid_filename(str: fs_filename(path: aBuf)))
2932 {
2933 log_error("server", "The name '%s' cannot be used for maps because not all platforms support it", aBuf);
2934 return 0;
2935 }
2936 if(!GameServer()->OnMapChange(pNewMapName: aBuf, MapNameSize: sizeof(aBuf)))
2937 {
2938 return 0;
2939 }
2940 if(!GameServer()->Map()->Load(pFullName: pMapName, pStorage: Storage(), pPath: aBuf, StorageType: IStorage::TYPE_ALL))
2941 {
2942 return 0;
2943 }
2944
2945 // reinit snapshot ids
2946 m_IdPool.TimeoutIds();
2947
2948 // get the crc of the map
2949 m_aCurrentMapSha256[MAP_TYPE_SIX] = GameServer()->Map()->Sha256();
2950 m_aCurrentMapCrc[MAP_TYPE_SIX] = GameServer()->Map()->Crc();
2951 char aBufMsg[256];
2952 char aSha256[SHA256_MAXSTRSIZE];
2953 sha256_str(digest: m_aCurrentMapSha256[MAP_TYPE_SIX], str: aSha256, max_len: sizeof(aSha256));
2954 str_format(buffer: aBufMsg, buffer_size: sizeof(aBufMsg), format: "%s sha256 is %s", aBuf, aSha256);
2955 Console()->Print(Level: IConsole::OUTPUT_LEVEL_ADDINFO, pFrom: "server", pStr: aBufMsg);
2956
2957 // load complete map into memory for download
2958 {
2959 free(ptr: m_apCurrentMapData[MAP_TYPE_SIX]);
2960 void *pData;
2961 Storage()->ReadFile(pFilename: aBuf, Type: IStorage::TYPE_ALL, ppResult: &pData, pResultLen: &m_aCurrentMapSize[MAP_TYPE_SIX]);
2962 m_apCurrentMapData[MAP_TYPE_SIX] = (unsigned char *)pData;
2963 }
2964
2965 if(Config()->m_SvMapsBaseUrl[0])
2966 {
2967 char aEscaped[256];
2968 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s_%s.map", pMapName, aSha256);
2969 EscapeUrl(aBuf&: aEscaped, pStr: aBuf);
2970 str_format(buffer: m_aMapDownloadUrl, buffer_size: sizeof(m_aMapDownloadUrl), format: "%s%s", Config()->m_SvMapsBaseUrl, aEscaped);
2971 }
2972 else
2973 {
2974 m_aMapDownloadUrl[0] = '\0';
2975 }
2976
2977 // load sixup version of the map
2978 if(Config()->m_SvSixup)
2979 {
2980 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "maps7/%s.map", pMapName);
2981 void *pData;
2982 if(!Storage()->ReadFile(pFilename: aBuf, Type: IStorage::TYPE_ALL, ppResult: &pData, pResultLen: &m_aCurrentMapSize[MAP_TYPE_SIXUP]))
2983 {
2984 Config()->m_SvSixup = 0;
2985 if(m_pRegister)
2986 {
2987 m_pRegister->OnConfigChange();
2988 }
2989 log_error("sixup", "couldn't load map %s", aBuf);
2990 log_info("sixup", "disabling 0.7 compatibility");
2991 }
2992 else
2993 {
2994 free(ptr: m_apCurrentMapData[MAP_TYPE_SIXUP]);
2995 m_apCurrentMapData[MAP_TYPE_SIXUP] = (unsigned char *)pData;
2996
2997 m_aCurrentMapSha256[MAP_TYPE_SIXUP] = sha256(message: m_apCurrentMapData[MAP_TYPE_SIXUP], message_len: m_aCurrentMapSize[MAP_TYPE_SIXUP]);
2998 m_aCurrentMapCrc[MAP_TYPE_SIXUP] = crc32(crc: 0, buf: m_apCurrentMapData[MAP_TYPE_SIXUP], len: m_aCurrentMapSize[MAP_TYPE_SIXUP]);
2999 sha256_str(digest: m_aCurrentMapSha256[MAP_TYPE_SIXUP], str: aSha256, max_len: sizeof(aSha256));
3000 str_format(buffer: aBufMsg, buffer_size: sizeof(aBufMsg), format: "%s sha256 is %s", aBuf, aSha256);
3001 Console()->Print(Level: IConsole::OUTPUT_LEVEL_ADDINFO, pFrom: "sixup", pStr: aBufMsg);
3002 }
3003 }
3004 if(!Config()->m_SvSixup)
3005 {
3006 free(ptr: m_apCurrentMapData[MAP_TYPE_SIXUP]);
3007 m_apCurrentMapData[MAP_TYPE_SIXUP] = nullptr;
3008 }
3009
3010 for(int i = 0; i < MAX_CLIENTS; i++)
3011 m_aPrevStates[i] = m_aClients[i].m_State;
3012
3013 return 1;
3014}
3015
3016void CServer::UpdateDebugDummies(bool ForceDisconnect)
3017{
3018 if(m_PreviousDebugDummies == g_Config.m_DbgDummies && !ForceDisconnect)
3019 return;
3020
3021 g_Config.m_DbgDummies = std::clamp(val: g_Config.m_DbgDummies, lo: 0, hi: MaxClients());
3022 for(int DummyIndex = 0; DummyIndex < maximum(a: m_PreviousDebugDummies, b: g_Config.m_DbgDummies); ++DummyIndex)
3023 {
3024 const bool AddDummy = !ForceDisconnect && DummyIndex < g_Config.m_DbgDummies;
3025 const int ClientId = MaxClients() - DummyIndex - 1;
3026 CClient &Client = m_aClients[ClientId];
3027 if(AddDummy && m_aClients[ClientId].m_State == CClient::STATE_EMPTY)
3028 {
3029 NewClientCallback(ClientId, pUser: this, Sixup: false);
3030 Client.m_DebugDummy = true;
3031
3032 // See https://en.wikipedia.org/wiki/Unique_local_address
3033 Client.m_DebugDummyAddr.type = NETTYPE_IPV6;
3034 Client.m_DebugDummyAddr.ip[0] = 0xfd;
3035 // Global ID (40 bits): random
3036 secure_random_fill(bytes: &Client.m_DebugDummyAddr.ip[1], length: 5);
3037 // Subnet ID (16 bits): constant
3038 Client.m_DebugDummyAddr.ip[6] = 0xc0;
3039 Client.m_DebugDummyAddr.ip[7] = 0xde;
3040 // Interface ID (64 bits): set to client ID
3041 Client.m_DebugDummyAddr.ip[8] = 0x00;
3042 Client.m_DebugDummyAddr.ip[9] = 0x00;
3043 Client.m_DebugDummyAddr.ip[10] = 0x00;
3044 Client.m_DebugDummyAddr.ip[11] = 0x00;
3045 uint_to_bytes_be(bytes: &Client.m_DebugDummyAddr.ip[12], value: ClientId);
3046 // Port: random like normal clients
3047 Client.m_DebugDummyAddr.port = secure_rand_below(below: 65535 - 1024) + 1024;
3048 net_addr_str(addr: &Client.m_DebugDummyAddr, string: Client.m_aDebugDummyAddrString.data(), max_length: Client.m_aDebugDummyAddrString.size(), add_port: true);
3049 net_addr_str(addr: &Client.m_DebugDummyAddr, string: Client.m_aDebugDummyAddrStringNoPort.data(), max_length: Client.m_aDebugDummyAddrStringNoPort.size(), add_port: false);
3050
3051 GameServer()->OnClientConnected(ClientId, pPersistentData: nullptr);
3052 Client.m_State = CClient::STATE_INGAME;
3053 str_format(buffer: Client.m_aName, buffer_size: sizeof(Client.m_aName), format: "Debug dummy %d", DummyIndex + 1);
3054 GameServer()->OnClientEnter(ClientId);
3055 }
3056 else if(!AddDummy && Client.m_DebugDummy)
3057 {
3058 DelClientCallback(ClientId, pReason: "Dropping debug dummy", pUser: this);
3059 }
3060
3061 if(AddDummy && Client.m_DebugDummy)
3062 {
3063 CNetObj_PlayerInput Input = {.m_Direction: 0};
3064 Input.m_Direction = (ClientId & 1) ? -1 : 1;
3065 Client.m_aInputs[0].m_GameTick = Tick() + 1;
3066 mem_copy(dest: Client.m_aInputs[0].m_aData, source: &Input, size: minimum(a: sizeof(Input), b: sizeof(Client.m_aInputs[0].m_aData)));
3067 Client.m_LatestInput = Client.m_aInputs[0];
3068 Client.m_CurrentInput = 0;
3069 }
3070 }
3071
3072 m_PreviousDebugDummies = ForceDisconnect ? 0 : g_Config.m_DbgDummies;
3073}
3074
3075int CServer::Run()
3076{
3077 if(m_RunServer == UNINITIALIZED)
3078 m_RunServer = RUNNING;
3079
3080 m_AuthManager.Init();
3081
3082 if(Config()->m_Debug)
3083 {
3084 g_UuidManager.DebugDump();
3085 }
3086
3087 {
3088 int Size = GameServer()->PersistentClientDataSize();
3089 for(auto &Client : m_aClients)
3090 {
3091 Client.m_HasPersistentData = false;
3092 Client.m_pPersistentData = malloc(size: Size);
3093 }
3094 }
3095 m_pPersistentData = malloc(size: GameServer()->PersistentDataSize());
3096
3097 // load map
3098 if(!LoadMap(pMapName: Config()->m_SvMap))
3099 {
3100 log_error("server", "failed to load map. mapname='%s'", Config()->m_SvMap);
3101 return -1;
3102 }
3103
3104 if(Config()->m_SvSqliteFile[0] != '\0')
3105 {
3106 char aFullPath[IO_MAX_PATH_LENGTH];
3107 Storage()->GetCompletePath(Type: IStorage::TYPE_SAVE_OR_ABSOLUTE, pDir: Config()->m_SvSqliteFile, pBuffer: aFullPath, BufferSize: sizeof(aFullPath));
3108
3109 if(Config()->m_SvUseSql)
3110 {
3111 DbPool()->RegisterSqliteDatabase(DatabaseMode: CDbConnectionPool::WRITE_BACKUP, aFilename: aFullPath);
3112 }
3113 else
3114 {
3115 DbPool()->RegisterSqliteDatabase(DatabaseMode: CDbConnectionPool::READ, aFilename: aFullPath);
3116 DbPool()->RegisterSqliteDatabase(DatabaseMode: CDbConnectionPool::WRITE, aFilename: aFullPath);
3117 }
3118 }
3119
3120 // start server
3121 NETADDR BindAddr;
3122 if(g_Config.m_Bindaddr[0] == '\0')
3123 {
3124 mem_zero(block: &BindAddr, size: sizeof(BindAddr));
3125 }
3126 else if(net_host_lookup(hostname: g_Config.m_Bindaddr, addr: &BindAddr, types: NETTYPE_ALL) != 0)
3127 {
3128 log_error("server", "The configured bindaddr '%s' cannot be resolved", g_Config.m_Bindaddr);
3129 return -1;
3130 }
3131 BindAddr.type = Config()->m_SvIpv4Only ? NETTYPE_IPV4 : NETTYPE_ALL;
3132
3133 int Port = Config()->m_SvPort;
3134 for(BindAddr.port = Port != 0 ? Port : 8303; !m_NetServer.Open(BindAddr, pNetBan: &m_ServerBan, MaxClients: Config()->m_SvMaxClients, MaxClientsPerIp: Config()->m_SvMaxClientsPerIp); BindAddr.port++)
3135 {
3136 if(Port != 0 || BindAddr.port >= 8310)
3137 {
3138 log_error("server", "couldn't open socket. port %d might already be in use", BindAddr.port);
3139 return -1;
3140 }
3141 }
3142
3143 if(Port == 0)
3144 log_info("server", "using port %d", BindAddr.port);
3145
3146#if defined(CONF_UPNP)
3147 m_UPnP.Open(Address: BindAddr);
3148#endif
3149
3150 if(!m_pHttp->Init(ShutdownDelay: std::chrono::seconds{2}))
3151 {
3152 log_error("server", "Failed to initialize the HTTP client.");
3153 return -1;
3154 }
3155
3156 m_pEngine = Kernel()->RequestInterface<IEngine>();
3157 m_pRegister = CreateRegister(pConfig: &g_Config, pConsole: m_pConsole, pEngine: m_pEngine, pHttp: m_pHttp, ServerPort: g_Config.m_SvRegisterPort > 0 ? g_Config.m_SvRegisterPort : this->Port(), SixupSecurityToken: m_NetServer.GetGlobalToken());
3158
3159 m_NetServer.SetCallbacks(pfnNewClient: NewClientCallback, pfnNewClientNoAuth: NewClientNoAuthCallback, pfnClientRejoin: ClientRejoinCallback, pfnDelClient: DelClientCallback, pUser: this);
3160
3161 m_Econ.Init(pConfig: Config(), pConsole: Console(), pNetBan: &m_ServerBan);
3162
3163 m_Fifo.Init(pConsole: Console(), pFifoFile: Config()->m_SvInputFifo, Flag: CFGFLAG_SERVER);
3164
3165 char aBuf[256];
3166 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "server name is '%s'", Config()->m_SvName);
3167 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: aBuf);
3168
3169 Antibot()->Init();
3170 GameServer()->OnInit(pPersistentData: nullptr);
3171 if(ErrorShutdown())
3172 {
3173 m_RunServer = STOPPING;
3174 }
3175 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "version " GAME_RELEASE_VERSION " on " CONF_PLATFORM_STRING " " CONF_ARCH_STRING);
3176 if(GIT_SHORTREV_HASH)
3177 {
3178 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "git revision hash: %s", GIT_SHORTREV_HASH);
3179 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: aBuf);
3180 }
3181
3182 ReadAnnouncementsFile();
3183 InitMaplist();
3184
3185 // process pending commands
3186 m_pConsole->StoreCommands(Store: false);
3187 m_pRegister->OnConfigChange();
3188
3189 if(m_AuthManager.IsGenerated())
3190 {
3191 log_info("server", "+-------------------------+");
3192 log_info("server", "| rcon password: '%s' |", Config()->m_SvRconPassword);
3193 log_info("server", "+-------------------------+");
3194 }
3195
3196 // start game
3197 {
3198 bool NonActive = false;
3199 bool PacketWaiting = false;
3200
3201 m_GameStartTime = time_get();
3202
3203 UpdateServerInfo(Resend: false);
3204 while(m_RunServer < STOPPING)
3205 {
3206 if(NonActive)
3207 PumpNetwork(PacketWaiting);
3208
3209 set_new_tick();
3210
3211 int64_t LastTime = time_get();
3212 int NewTicks = 0;
3213
3214 // load new map
3215 if(m_MapReload || m_SameMapReload || m_CurrentGameTick >= MAX_TICK) // force reload to make sure the ticks stay within a valid range
3216 {
3217 const bool SameMapReload = m_SameMapReload;
3218 // load map
3219 if(LoadMap(pMapName: Config()->m_SvMap))
3220 {
3221 // new map loaded
3222
3223 // ask the game for the data it wants to persist past a map change
3224 for(int i = 0; i < MAX_CLIENTS; i++)
3225 {
3226 if(m_aClients[i].m_State == CClient::STATE_INGAME)
3227 {
3228 m_aClients[i].m_HasPersistentData = GameServer()->OnClientDataPersist(ClientId: i, pData: m_aClients[i].m_pPersistentData);
3229 }
3230 }
3231
3232 UpdateDebugDummies(ForceDisconnect: true);
3233 GameServer()->OnShutdown(pPersistentData: m_pPersistentData);
3234
3235 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
3236 {
3237 if(m_aClients[ClientId].m_State <= CClient::STATE_AUTH)
3238 continue;
3239
3240 if(SameMapReload)
3241 SendMapReload(ClientId);
3242
3243 SendMap(ClientId);
3244 bool HasPersistentData = m_aClients[ClientId].m_HasPersistentData;
3245 m_aClients[ClientId].Reset();
3246 m_aClients[ClientId].m_HasPersistentData = HasPersistentData;
3247 m_aClients[ClientId].m_State = CClient::STATE_CONNECTING;
3248 }
3249
3250 m_GameStartTime = time_get();
3251 m_CurrentGameTick = MIN_TICK;
3252 m_ServerInfoFirstRequest = 0;
3253 Kernel()->ReregisterInterface(pInterface: GameServer());
3254 Console()->StoreCommands(Store: true);
3255 GameServer()->OnInit(pPersistentData: m_pPersistentData);
3256 Console()->StoreCommands(Store: false);
3257
3258 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
3259 {
3260 CClient &Client = m_aClients[ClientId];
3261 if(Client.m_State < CClient::STATE_PREAUTH)
3262 continue;
3263
3264 // When doing a map change, a new Teehistorian file is created. For players that are already
3265 // on the server, no PlayerJoin event is produced in Teehistorian from the network engine.
3266 // Record PlayerJoin events here to record the Sixup version and player join event.
3267 GameServer()->TeehistorianRecordPlayerJoin(ClientId, Sixup: Client.m_Sixup);
3268
3269 // Record the players auth state aswell if needed.
3270 // This was recorded in AuthInit in the past.
3271 if(IsRconAuthed(ClientId))
3272 {
3273 GameServer()->TeehistorianRecordAuthLogin(ClientId, Level: GetAuthedState(ClientId), pAuthName: GetAuthName(ClientId));
3274 }
3275 }
3276
3277 if(ErrorShutdown())
3278 {
3279 break;
3280 }
3281 ExpireServerInfo();
3282 }
3283 else
3284 {
3285 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "failed to load map. mapname='%s'", Config()->m_SvMap);
3286 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: aBuf);
3287 str_copy(dst&: Config()->m_SvMap, src: GameServer()->Map()->FullName());
3288 }
3289 }
3290
3291 while(LastTime > TickStartTime(Tick: m_CurrentGameTick + 1))
3292 {
3293 GameServer()->OnPreTickTeehistorian();
3294 UpdateDebugDummies(ForceDisconnect: false);
3295
3296 for(int c = 0; c < MAX_CLIENTS; c++)
3297 {
3298 if(m_aClients[c].m_State != CClient::STATE_INGAME)
3299 continue;
3300 bool ClientHadInput = false;
3301 for(auto &Input : m_aClients[c].m_aInputs)
3302 {
3303 if(Input.m_GameTick == Tick() + 1)
3304 {
3305 GameServer()->OnClientPredictedEarlyInput(ClientId: c, pInput: Input.m_aData);
3306 ClientHadInput = true;
3307 break;
3308 }
3309 }
3310 if(!ClientHadInput)
3311 GameServer()->OnClientPredictedEarlyInput(ClientId: c, pInput: nullptr);
3312 }
3313
3314 m_CurrentGameTick++;
3315 NewTicks++;
3316
3317 // apply new input
3318 for(int c = 0; c < MAX_CLIENTS; c++)
3319 {
3320 if(m_aClients[c].m_State != CClient::STATE_INGAME)
3321 continue;
3322 bool ClientHadInput = false;
3323 for(auto &Input : m_aClients[c].m_aInputs)
3324 {
3325 if(Input.m_GameTick == Tick())
3326 {
3327 GameServer()->OnClientPredictedInput(ClientId: c, pInput: Input.m_aData);
3328 ClientHadInput = true;
3329 break;
3330 }
3331 }
3332 if(!ClientHadInput)
3333 GameServer()->OnClientPredictedInput(ClientId: c, pInput: nullptr);
3334 }
3335
3336 GameServer()->OnTick();
3337 if(ErrorShutdown())
3338 {
3339 break;
3340 }
3341 }
3342
3343 // snap game
3344 if(NewTicks)
3345 {
3346 DoSnapshot();
3347
3348 const int CommandSendingClientId = Tick() % MAX_CLIENTS;
3349 UpdateClientRconCommands(ClientId: CommandSendingClientId);
3350 UpdateClientMaplistEntries(ClientId: CommandSendingClientId);
3351
3352 m_Fifo.Update();
3353
3354#if defined(CONF_PLATFORM_ANDROID)
3355 std::vector<std::string> vAndroidCommandQueue = FetchAndroidServerCommandQueue();
3356 for(const std::string &Command : vAndroidCommandQueue)
3357 {
3358 Console()->ExecuteLineFlag(Command.c_str(), CFGFLAG_SERVER, IConsole::CLIENT_ID_UNSPECIFIED);
3359 }
3360#endif
3361
3362 // master server stuff
3363 m_pRegister->Update();
3364
3365 if(m_ServerInfoNeedsUpdate)
3366 {
3367 UpdateServerInfo(Resend: m_ServerInfoNeedsResend);
3368 }
3369
3370 Antibot()->OnEngineTick();
3371
3372 // handle dnsbl
3373 if(Config()->m_SvDnsbl)
3374 {
3375 for(int ClientId = 0; ClientId < MAX_CLIENTS; ClientId++)
3376 {
3377 if(m_aClients[ClientId].m_State == CClient::STATE_EMPTY)
3378 continue;
3379
3380 if(m_aClients[ClientId].m_DnsblState == EDnsblState::NONE)
3381 {
3382 // initiate dnsbl lookup
3383 InitDnsbl(ClientId);
3384 }
3385 else if(m_aClients[ClientId].m_DnsblState == EDnsblState::PENDING &&
3386 m_aClients[ClientId].m_pDnsblLookup->State() == IJob::STATE_DONE)
3387 {
3388 if(m_aClients[ClientId].m_pDnsblLookup->Result() != 0)
3389 {
3390 // entry not found -> whitelisted
3391 m_aClients[ClientId].m_DnsblState = EDnsblState::WHITELISTED;
3392
3393 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "ClientId=%d addr=<{%s}> secure=%s whitelisted", ClientId, ClientAddrString(ClientId, IncludePort: true), m_NetServer.HasSecurityToken(ClientId) ? "yes" : "no");
3394 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "dnsbl", pStr: aBuf);
3395 }
3396 else
3397 {
3398 // entry found -> blacklisted
3399 m_aClients[ClientId].m_DnsblState = EDnsblState::BLACKLISTED;
3400
3401 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "ClientId=%d addr=<{%s}> secure=%s blacklisted", ClientId, ClientAddrString(ClientId, IncludePort: true), m_NetServer.HasSecurityToken(ClientId) ? "yes" : "no");
3402 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "dnsbl", pStr: aBuf);
3403
3404 if(Config()->m_SvDnsblBan)
3405 {
3406 m_NetServer.NetBan()->BanAddr(pAddr: ClientAddr(ClientId), Seconds: 60, pReason: Config()->m_SvDnsblBanReason, VerbatimReason: true);
3407 }
3408 }
3409 }
3410 }
3411 }
3412 for(int i = 0; i < MAX_CLIENTS; ++i)
3413 {
3414 if(m_aClients[i].m_State == CClient::STATE_REDIRECTED)
3415 {
3416 if(time_get() > m_aClients[i].m_RedirectDropTime)
3417 {
3418 m_NetServer.Drop(ClientId: i, pReason: "redirected");
3419 }
3420 }
3421 }
3422 }
3423
3424 if(!NonActive)
3425 PumpNetwork(PacketWaiting);
3426
3427 NonActive = true;
3428 for(const auto &Client : m_aClients)
3429 {
3430 if(Client.m_State != CClient::STATE_EMPTY)
3431 {
3432 NonActive = false;
3433 break;
3434 }
3435 }
3436
3437 if(NonActive)
3438 {
3439 if(Config()->m_SvReloadWhenEmpty == 1)
3440 {
3441 m_MapReload = true;
3442 Config()->m_SvReloadWhenEmpty = 0;
3443 }
3444 else if(Config()->m_SvReloadWhenEmpty == 2 && !m_ReloadedWhenEmpty)
3445 {
3446 m_MapReload = true;
3447 m_ReloadedWhenEmpty = true;
3448 }
3449 }
3450 else
3451 {
3452 m_ReloadedWhenEmpty = false;
3453 }
3454
3455 // wait for incoming data
3456 if(NonActive && Config()->m_SvShutdownWhenEmpty)
3457 {
3458 m_RunServer = STOPPING;
3459 }
3460 else if(NonActive &&
3461 !m_aDemoRecorder[RECORDER_MANUAL].IsRecording() &&
3462 !m_aDemoRecorder[RECORDER_AUTO].IsRecording())
3463 {
3464 PacketWaiting = net_socket_read_wait(sock: m_NetServer.Socket(), nanoseconds: 1s);
3465 }
3466 else
3467 {
3468 set_new_tick();
3469 LastTime = time_get();
3470 const auto MicrosecondsToWait = std::chrono::duration_cast<std::chrono::microseconds>(d: std::chrono::nanoseconds(TickStartTime(Tick: m_CurrentGameTick + 1) - LastTime)) + 1us;
3471 PacketWaiting = MicrosecondsToWait > 0us ? net_socket_read_wait(sock: m_NetServer.Socket(), nanoseconds: MicrosecondsToWait) : true;
3472 }
3473 if(IsInterrupted())
3474 {
3475 Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "interrupted");
3476 break;
3477 }
3478 }
3479 }
3480 const char *pDisconnectReason = "Server shutdown";
3481 if(m_aShutdownReason[0])
3482 pDisconnectReason = m_aShutdownReason;
3483
3484 if(ErrorShutdown())
3485 {
3486 log_info("server", "shutdown from game server (%s)", m_aErrorShutdownReason);
3487 pDisconnectReason = m_aErrorShutdownReason;
3488 }
3489 // disconnect all clients on shutdown
3490 for(int i = 0; i < MAX_CLIENTS; ++i)
3491 {
3492 if(m_aClients[i].m_State != CClient::STATE_EMPTY)
3493 m_NetServer.Drop(ClientId: i, pReason: pDisconnectReason);
3494 }
3495
3496 m_pRegister->OnShutdown();
3497 m_Econ.Shutdown();
3498 m_Fifo.Shutdown();
3499 m_pHttp->Shutdown();
3500 Engine()->ShutdownJobs();
3501
3502 GameServer()->OnShutdown(pPersistentData: nullptr);
3503 GameServer()->Map()->Unload();
3504 DbPool()->OnShutdown();
3505
3506#if defined(CONF_UPNP)
3507 m_UPnP.Shutdown();
3508#endif
3509 m_NetServer.Close();
3510
3511 return ErrorShutdown();
3512}
3513
3514void CServer::ConKick(IConsole::IResult *pResult, void *pUser)
3515{
3516 if(pResult->NumArguments() > 1)
3517 {
3518 char aBuf[128];
3519 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Kicked (%s)", pResult->GetString(Index: 1));
3520 ((CServer *)pUser)->Kick(ClientId: pResult->GetInteger(Index: 0), pReason: aBuf);
3521 }
3522 else
3523 ((CServer *)pUser)->Kick(ClientId: pResult->GetInteger(Index: 0), pReason: "Kicked by console");
3524}
3525
3526void CServer::ConStatus(IConsole::IResult *pResult, void *pUser)
3527{
3528 char aBuf[1024];
3529 CServer *pThis = static_cast<CServer *>(pUser);
3530 const char *pName = pResult->NumArguments() == 1 ? pResult->GetString(Index: 0) : "";
3531
3532 for(int i = 0; i < MAX_CLIENTS; i++)
3533 {
3534 if(pThis->m_aClients[i].m_State == CClient::STATE_EMPTY)
3535 continue;
3536
3537 if(!str_utf8_find_nocase(haystack: pThis->m_aClients[i].m_aName, needle: pName))
3538 continue;
3539
3540 if(pThis->m_aClients[i].m_State == CClient::STATE_INGAME)
3541 {
3542 char aDnsblStr[64];
3543 aDnsblStr[0] = '\0';
3544 if(pThis->Config()->m_SvDnsbl)
3545 {
3546 str_format(buffer: aDnsblStr, buffer_size: sizeof(aDnsblStr), format: " dnsbl=%s", DnsblStateStr(State: pThis->m_aClients[i].m_DnsblState));
3547 }
3548
3549 char aAuthStr[128];
3550 aAuthStr[0] = '\0';
3551 if(pThis->m_aClients[i].m_AuthKey >= 0)
3552 {
3553 const char *pAuthStr = "";
3554 const int AuthState = pThis->GetAuthedState(ClientId: i);
3555
3556 if(AuthState == AUTHED_ADMIN)
3557 {
3558 pAuthStr = "(Admin)";
3559 }
3560 else if(AuthState == AUTHED_MOD)
3561 {
3562 pAuthStr = "(Mod)";
3563 }
3564 else if(AuthState == AUTHED_HELPER)
3565 {
3566 pAuthStr = "(Helper)";
3567 }
3568
3569 str_format(buffer: aAuthStr, buffer_size: sizeof(aAuthStr), format: " key='%s' %s", pThis->m_AuthManager.KeyIdent(Slot: pThis->m_aClients[i].m_AuthKey), pAuthStr);
3570 }
3571
3572 const char *pClientPrefix = "";
3573 if(pThis->m_aClients[i].m_Sixup)
3574 {
3575 pClientPrefix = "0.7:";
3576 }
3577 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "id=%d addr=<{%s}> name='%s' client=%s%d secure=%s flags=%d%s%s",
3578 i, pThis->ClientAddrString(ClientId: i, IncludePort: true), pThis->m_aClients[i].m_aName, pClientPrefix, pThis->m_aClients[i].m_DDNetVersion,
3579 pThis->m_NetServer.HasSecurityToken(ClientId: i) ? "yes" : "no", pThis->m_aClients[i].m_Flags, aDnsblStr, aAuthStr);
3580 }
3581 else
3582 {
3583 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "id=%d addr=<{%s}> connecting", i, pThis->ClientAddrString(ClientId: i, IncludePort: true));
3584 }
3585 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: aBuf);
3586 }
3587}
3588
3589static int GetAuthLevel(const char *pLevel)
3590{
3591 int Level = -1;
3592 if(!str_comp_nocase(a: pLevel, b: "admin"))
3593 Level = AUTHED_ADMIN;
3594 else if(str_startswith(str: pLevel, prefix: "mod"))
3595 Level = AUTHED_MOD;
3596 else if(!str_comp_nocase(a: pLevel, b: "helper"))
3597 Level = AUTHED_HELPER;
3598
3599 return Level;
3600}
3601
3602bool CServer::CanClientUseCommandCallback(int ClientId, const IConsole::ICommandInfo *pCommand, void *pUser)
3603{
3604 return ((CServer *)pUser)->CanClientUseCommand(ClientId, pCommand);
3605}
3606
3607bool CServer::CanClientUseCommand(int ClientId, const IConsole::ICommandInfo *pCommand) const
3608{
3609 if(pCommand->Flags() & CFGFLAG_CHAT)
3610 return true;
3611 if(pCommand->Flags() & CMDFLAG_PRACTICE)
3612 return true;
3613 if(!IsRconAuthed(ClientId))
3614 return false;
3615 return pCommand->GetAccessLevel() >= ConsoleAccessLevel(ClientId);
3616}
3617
3618void CServer::AuthRemoveKey(int KeySlot)
3619{
3620 m_AuthManager.RemoveKey(Slot: KeySlot);
3621 LogoutKey(Key: KeySlot, pReason: "key removal");
3622
3623 // Update indices.
3624 for(auto &Client : m_aClients)
3625 {
3626 if(Client.m_AuthKey == KeySlot)
3627 {
3628 Client.m_AuthKey = -1;
3629 }
3630 else if(Client.m_AuthKey > KeySlot)
3631 {
3632 --Client.m_AuthKey;
3633 }
3634 }
3635}
3636
3637void CServer::ConAuthAdd(IConsole::IResult *pResult, void *pUser)
3638{
3639 CServer *pThis = (CServer *)pUser;
3640 CAuthManager *pManager = &pThis->m_AuthManager;
3641
3642 const char *pIdent = pResult->GetString(Index: 0);
3643 const char *pLevel = pResult->GetString(Index: 1);
3644 const char *pPw = pResult->GetString(Index: 2);
3645
3646 if(!pManager->IsValidIdent(pIdent))
3647 {
3648 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "ident is invalid");
3649 return;
3650 }
3651
3652 int Level = GetAuthLevel(pLevel);
3653 if(Level == -1)
3654 {
3655 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "level can be one of {\"admin\", \"mod(erator)\", \"helper\"}");
3656 return;
3657 }
3658 // back compat to change "mod", "modder" and so on as parameters to "moderator"
3659 pLevel = CAuthManager::AuthLevelToRoleName(AuthLevel: Level);
3660
3661 bool NeedUpdate = !pManager->NumNonDefaultKeys();
3662 if(pManager->AddKey(pIdent, pPw, pRoleName: pLevel) < 0)
3663 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "ident already exists");
3664 else
3665 {
3666 if(NeedUpdate)
3667 pThis->SendRconType(ClientId: -1, UsernameReq: true);
3668 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "key added");
3669 }
3670}
3671
3672void CServer::ConAuthAddHashed(IConsole::IResult *pResult, void *pUser)
3673{
3674 CServer *pThis = (CServer *)pUser;
3675 CAuthManager *pManager = &pThis->m_AuthManager;
3676
3677 const char *pIdent = pResult->GetString(Index: 0);
3678 const char *pLevel = pResult->GetString(Index: 1);
3679 const char *pPw = pResult->GetString(Index: 2);
3680 const char *pSalt = pResult->GetString(Index: 3);
3681
3682 if(!pManager->IsValidIdent(pIdent))
3683 {
3684 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "ident is invalid");
3685 return;
3686 }
3687
3688 int Level = GetAuthLevel(pLevel);
3689 if(Level == -1)
3690 {
3691 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "level can be one of {\"admin\", \"mod(erator)\", \"helper\"}");
3692 return;
3693 }
3694 // back compat to change "mod", "modder" and so on as parameters to "moderator"
3695 pLevel = CAuthManager::AuthLevelToRoleName(AuthLevel: Level);
3696
3697 MD5_DIGEST Hash;
3698 unsigned char aSalt[SALT_BYTES];
3699
3700 if(md5_from_str(out: &Hash, str: pPw))
3701 {
3702 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "Malformed password hash");
3703 return;
3704 }
3705 if(str_hex_decode(dst: aSalt, dst_size: sizeof(aSalt), src: pSalt))
3706 {
3707 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "Malformed salt hash");
3708 return;
3709 }
3710
3711 bool NeedUpdate = !pManager->NumNonDefaultKeys();
3712
3713 if(pManager->AddKeyHash(pIdent, Hash, pSalt: aSalt, pRoleName: pLevel) < 0)
3714 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "ident already exists");
3715 else
3716 {
3717 if(NeedUpdate)
3718 pThis->SendRconType(ClientId: -1, UsernameReq: true);
3719 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "key added");
3720 }
3721}
3722
3723void CServer::ConAuthUpdate(IConsole::IResult *pResult, void *pUser)
3724{
3725 CServer *pThis = (CServer *)pUser;
3726 CAuthManager *pManager = &pThis->m_AuthManager;
3727
3728 const char *pIdent = pResult->GetString(Index: 0);
3729 const char *pLevel = pResult->GetString(Index: 1);
3730 const char *pPw = pResult->GetString(Index: 2);
3731
3732 int KeySlot = pManager->FindKey(pIdent);
3733 if(KeySlot == -1)
3734 {
3735 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "ident couldn't be found");
3736 return;
3737 }
3738
3739 int Level = GetAuthLevel(pLevel);
3740 if(Level == -1)
3741 {
3742 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "level can be one of {\"admin\", \"mod(erator)\", \"helper\"}");
3743 return;
3744 }
3745 // back compat to change "mod", "modder" and so on as parameters to "moderator"
3746 pLevel = CAuthManager::AuthLevelToRoleName(AuthLevel: Level);
3747
3748 pManager->UpdateKey(Slot: KeySlot, pPw, pRoleName: pLevel);
3749 pThis->LogoutKey(Key: KeySlot, pReason: "key update");
3750
3751 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "key updated");
3752}
3753
3754void CServer::ConAuthUpdateHashed(IConsole::IResult *pResult, void *pUser)
3755{
3756 CServer *pThis = (CServer *)pUser;
3757 CAuthManager *pManager = &pThis->m_AuthManager;
3758
3759 const char *pIdent = pResult->GetString(Index: 0);
3760 const char *pLevel = pResult->GetString(Index: 1);
3761 const char *pPw = pResult->GetString(Index: 2);
3762 const char *pSalt = pResult->GetString(Index: 3);
3763
3764 int KeySlot = pManager->FindKey(pIdent);
3765 if(KeySlot == -1)
3766 {
3767 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "ident couldn't be found");
3768 return;
3769 }
3770
3771 int Level = GetAuthLevel(pLevel);
3772 if(Level == -1)
3773 {
3774 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "level can be one of {\"admin\", \"mod(erator)\", \"helper\"}");
3775 return;
3776 }
3777 // back compat to change "mod", "modder" and so on as parameters to "moderator"
3778 pLevel = CAuthManager::AuthLevelToRoleName(AuthLevel: Level);
3779
3780 MD5_DIGEST Hash;
3781 unsigned char aSalt[SALT_BYTES];
3782
3783 if(md5_from_str(out: &Hash, str: pPw))
3784 {
3785 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "Malformed password hash");
3786 return;
3787 }
3788 if(str_hex_decode(dst: aSalt, dst_size: sizeof(aSalt), src: pSalt))
3789 {
3790 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "Malformed salt hash");
3791 return;
3792 }
3793
3794 pManager->UpdateKeyHash(Slot: KeySlot, Hash, pSalt: aSalt, pRoleName: pLevel);
3795 pThis->LogoutKey(Key: KeySlot, pReason: "key update");
3796
3797 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "key updated");
3798}
3799
3800void CServer::ConAuthRemove(IConsole::IResult *pResult, void *pUser)
3801{
3802 CServer *pThis = (CServer *)pUser;
3803 CAuthManager *pManager = &pThis->m_AuthManager;
3804
3805 const char *pIdent = pResult->GetString(Index: 0);
3806
3807 int KeySlot = pManager->FindKey(pIdent);
3808 if(KeySlot == -1)
3809 {
3810 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "ident couldn't be found");
3811 return;
3812 }
3813
3814 pThis->AuthRemoveKey(KeySlot);
3815
3816 if(!pManager->NumNonDefaultKeys())
3817 pThis->SendRconType(ClientId: -1, UsernameReq: false);
3818
3819 pThis->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "auth", pStr: "key removed, all users logged out");
3820}
3821
3822static void ListKeysCallback(const char *pIdent, const char *pRoleName, void *pUser)
3823{
3824 log_info("auth", "%s %s", pIdent, pRoleName);
3825}
3826
3827void CServer::ConAuthList(IConsole::IResult *pResult, void *pUser)
3828{
3829 CServer *pThis = (CServer *)pUser;
3830 CAuthManager *pManager = &pThis->m_AuthManager;
3831
3832 pManager->ListKeys(pfnListCallback: ListKeysCallback, pUser: pThis);
3833}
3834
3835void CServer::ConShutdown(IConsole::IResult *pResult, void *pUser)
3836{
3837 CServer *pThis = static_cast<CServer *>(pUser);
3838 pThis->m_RunServer = STOPPING;
3839 const char *pReason = pResult->GetString(Index: 0);
3840 if(pReason[0])
3841 {
3842 str_copy(dst&: pThis->m_aShutdownReason, src: pReason);
3843 }
3844}
3845
3846void CServer::DemoRecorder_HandleAutoStart()
3847{
3848 if(Config()->m_SvAutoDemoRecord)
3849 {
3850 m_aDemoRecorder[RECORDER_AUTO].Stop(Mode: IDemoRecorder::EStopMode::KEEP_FILE);
3851
3852 char aTimestamp[20];
3853 str_timestamp(buffer: aTimestamp, buffer_size: sizeof(aTimestamp));
3854 char aFilename[IO_MAX_PATH_LENGTH];
3855 str_format(buffer: aFilename, buffer_size: sizeof(aFilename), format: "demos/auto/server/%s_%s.demo", GameServer()->Map()->BaseName(), aTimestamp);
3856 m_aDemoRecorder[RECORDER_AUTO].Start(
3857 pStorage: Storage(),
3858 pConsole: m_pConsole,
3859 pFilename: aFilename,
3860 pNetversion: GameServer()->NetVersion(),
3861 pMap: GameServer()->Map()->BaseName(),
3862 Sha256: m_aCurrentMapSha256[MAP_TYPE_SIX],
3863 MapCrc: m_aCurrentMapCrc[MAP_TYPE_SIX],
3864 pType: "server",
3865 MapSize: m_aCurrentMapSize[MAP_TYPE_SIX],
3866 pMapData: m_apCurrentMapData[MAP_TYPE_SIX],
3867 MapFile: nullptr,
3868 pfnFilter: nullptr,
3869 pUser: nullptr);
3870
3871 if(Config()->m_SvAutoDemoMax)
3872 {
3873 // clean up auto recorded demos
3874 CFileCollection AutoDemos;
3875 AutoDemos.Init(pStorage: Storage(), pPath: "demos/auto/server", pFileDesc: "", pFileExt: ".demo", MaxEntries: Config()->m_SvAutoDemoMax);
3876 }
3877 }
3878}
3879
3880void CServer::SaveDemo(int ClientId, float Time)
3881{
3882 if(IsRecording(ClientId))
3883 {
3884 char aNewFilename[IO_MAX_PATH_LENGTH];
3885 str_format(buffer: aNewFilename, buffer_size: sizeof(aNewFilename), format: "demos/%s_%s_%05.2f.demo", GameServer()->Map()->BaseName(), m_aClients[ClientId].m_aName, Time);
3886 m_aDemoRecorder[ClientId].Stop(Mode: IDemoRecorder::EStopMode::KEEP_FILE, pTargetFilename: aNewFilename);
3887 }
3888}
3889
3890void CServer::StartRecord(int ClientId)
3891{
3892 if(Config()->m_SvPlayerDemoRecord)
3893 {
3894 char aFilename[IO_MAX_PATH_LENGTH];
3895 str_format(buffer: aFilename, buffer_size: sizeof(aFilename), format: "demos/%s_%d_%d_tmp.demo", GameServer()->Map()->BaseName(), m_NetServer.Address().port, ClientId);
3896 m_aDemoRecorder[ClientId].Start(
3897 pStorage: Storage(),
3898 pConsole: Console(),
3899 pFilename: aFilename,
3900 pNetversion: GameServer()->NetVersion(),
3901 pMap: GameServer()->Map()->BaseName(),
3902 Sha256: m_aCurrentMapSha256[MAP_TYPE_SIX],
3903 MapCrc: m_aCurrentMapCrc[MAP_TYPE_SIX],
3904 pType: "server",
3905 MapSize: m_aCurrentMapSize[MAP_TYPE_SIX],
3906 pMapData: m_apCurrentMapData[MAP_TYPE_SIX],
3907 MapFile: nullptr,
3908 pfnFilter: nullptr,
3909 pUser: nullptr);
3910 }
3911}
3912
3913void CServer::StopRecord(int ClientId)
3914{
3915 if(IsRecording(ClientId))
3916 {
3917 m_aDemoRecorder[ClientId].Stop(Mode: IDemoRecorder::EStopMode::REMOVE_FILE);
3918 }
3919}
3920
3921bool CServer::IsRecording(int ClientId)
3922{
3923 return m_aDemoRecorder[ClientId].IsRecording();
3924}
3925
3926void CServer::StopDemos()
3927{
3928 for(int i = 0; i < NUM_RECORDERS; i++)
3929 {
3930 if(!m_aDemoRecorder[i].IsRecording())
3931 continue;
3932
3933 m_aDemoRecorder[i].Stop(Mode: i < MAX_CLIENTS ? IDemoRecorder::EStopMode::REMOVE_FILE : IDemoRecorder::EStopMode::KEEP_FILE);
3934 }
3935}
3936
3937void CServer::ConRecord(IConsole::IResult *pResult, void *pUser)
3938{
3939 CServer *pServer = (CServer *)pUser;
3940
3941 if(pServer->IsRecording(ClientId: RECORDER_MANUAL))
3942 {
3943 pServer->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "demo_recorder", pStr: "Demo recorder already recording");
3944 return;
3945 }
3946
3947 char aFilename[IO_MAX_PATH_LENGTH];
3948 if(pResult->NumArguments())
3949 {
3950 str_format(buffer: aFilename, buffer_size: sizeof(aFilename), format: "demos/%s.demo", pResult->GetString(Index: 0));
3951 }
3952 else
3953 {
3954 char aTimestamp[20];
3955 str_timestamp(buffer: aTimestamp, buffer_size: sizeof(aTimestamp));
3956 str_format(buffer: aFilename, buffer_size: sizeof(aFilename), format: "demos/demo_%s.demo", aTimestamp);
3957 }
3958 pServer->m_aDemoRecorder[RECORDER_MANUAL].Start(
3959 pStorage: pServer->Storage(),
3960 pConsole: pServer->Console(),
3961 pFilename: aFilename,
3962 pNetversion: pServer->GameServer()->NetVersion(),
3963 pMap: pServer->GameServer()->Map()->BaseName(),
3964 Sha256: pServer->m_aCurrentMapSha256[MAP_TYPE_SIX],
3965 MapCrc: pServer->m_aCurrentMapCrc[MAP_TYPE_SIX],
3966 pType: "server",
3967 MapSize: pServer->m_aCurrentMapSize[MAP_TYPE_SIX],
3968 pMapData: pServer->m_apCurrentMapData[MAP_TYPE_SIX],
3969 MapFile: nullptr,
3970 pfnFilter: nullptr,
3971 pUser: nullptr);
3972}
3973
3974void CServer::ConStopRecord(IConsole::IResult *pResult, void *pUser)
3975{
3976 ((CServer *)pUser)->m_aDemoRecorder[RECORDER_MANUAL].Stop(Mode: IDemoRecorder::EStopMode::KEEP_FILE);
3977}
3978
3979void CServer::ConMapReload(IConsole::IResult *pResult, void *pUser)
3980{
3981 ((CServer *)pUser)->ReloadMap();
3982}
3983
3984void CServer::ConLogout(IConsole::IResult *pResult, void *pUser)
3985{
3986 CServer *pServer = (CServer *)pUser;
3987
3988 if(pServer->m_RconClientId >= 0 && pServer->m_RconClientId < MAX_CLIENTS &&
3989 pServer->m_aClients[pServer->m_RconClientId].m_State != CServer::CClient::STATE_EMPTY)
3990 {
3991 pServer->LogoutClient(ClientId: pServer->m_RconClientId, pReason: "");
3992 }
3993}
3994
3995void CServer::ConShowIps(IConsole::IResult *pResult, void *pUser)
3996{
3997 CServer *pServer = (CServer *)pUser;
3998
3999 if(pServer->m_RconClientId >= 0 && pServer->m_RconClientId < MAX_CLIENTS &&
4000 pServer->m_aClients[pServer->m_RconClientId].m_State != CServer::CClient::STATE_EMPTY)
4001 {
4002 if(pResult->NumArguments())
4003 {
4004 pServer->m_aClients[pServer->m_RconClientId].m_ShowIps = pResult->GetInteger(Index: 0);
4005 }
4006 else
4007 {
4008 char aStr[9];
4009 str_format(buffer: aStr, buffer_size: sizeof(aStr), format: "Value: %d", pServer->m_aClients[pServer->m_RconClientId].m_ShowIps);
4010 pServer->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: aStr);
4011 }
4012 }
4013}
4014
4015void CServer::ConHideAuthStatus(IConsole::IResult *pResult, void *pUser)
4016{
4017 CServer *pServer = (CServer *)pUser;
4018
4019 if(pServer->m_RconClientId >= 0 && pServer->m_RconClientId < MAX_CLIENTS &&
4020 pServer->m_aClients[pServer->m_RconClientId].m_State != CServer::CClient::STATE_EMPTY)
4021 {
4022 if(pResult->NumArguments())
4023 {
4024 pServer->m_aClients[pServer->m_RconClientId].m_AuthHidden = pResult->GetInteger(Index: 0);
4025 }
4026 else
4027 {
4028 char aStr[9];
4029 str_format(buffer: aStr, buffer_size: sizeof(aStr), format: "Value: %d", pServer->m_aClients[pServer->m_RconClientId].m_AuthHidden);
4030 pServer->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: aStr);
4031 }
4032 }
4033}
4034
4035void CServer::ConForceHighBandwidthOnSpectate(IConsole::IResult *pResult, void *pUser)
4036{
4037 CServer *pServer = (CServer *)pUser;
4038
4039 if(pServer->m_RconClientId >= 0 && pServer->m_RconClientId < MAX_CLIENTS &&
4040 pServer->m_aClients[pServer->m_RconClientId].m_State != CServer::CClient::STATE_EMPTY)
4041 {
4042 if(pResult->NumArguments())
4043 {
4044 pServer->m_aClients[pServer->m_RconClientId].m_ForceHighBandwidthOnSpectate = pResult->GetInteger(Index: 0);
4045 }
4046 else
4047 {
4048 char aStr[9];
4049 str_format(buffer: aStr, buffer_size: sizeof(aStr), format: "Value: %d", pServer->m_aClients[pServer->m_RconClientId].m_ForceHighBandwidthOnSpectate);
4050 pServer->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: aStr);
4051 }
4052 }
4053}
4054
4055void CServer::ConAddSqlServer(IConsole::IResult *pResult, void *pUserData)
4056{
4057 CServer *pSelf = (CServer *)pUserData;
4058
4059 if(!MysqlAvailable())
4060 {
4061 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "can't add MySQL server: compiled without MySQL support");
4062 return;
4063 }
4064
4065 if(!pSelf->Config()->m_SvUseSql)
4066 return;
4067
4068 if(pResult->NumArguments() != 7 && pResult->NumArguments() != 8)
4069 {
4070 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "7 or 8 arguments are required");
4071 return;
4072 }
4073
4074 CMysqlConfig Config;
4075 bool Write;
4076 if(str_comp_nocase(a: pResult->GetString(Index: 0), b: "r") == 0)
4077 Write = false;
4078 else if(str_comp_nocase(a: pResult->GetString(Index: 0), b: "w") == 0)
4079 Write = true;
4080 else
4081 {
4082 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "choose either 'r' for SqlReadServer or 'w' for SqlWriteServer");
4083 return;
4084 }
4085
4086 str_copy(dst: Config.m_aDatabase, src: pResult->GetString(Index: 1), dst_size: sizeof(Config.m_aDatabase));
4087 str_copy(dst: Config.m_aPrefix, src: pResult->GetString(Index: 2), dst_size: sizeof(Config.m_aPrefix));
4088 str_copy(dst: Config.m_aUser, src: pResult->GetString(Index: 3), dst_size: sizeof(Config.m_aUser));
4089 str_copy(dst: Config.m_aPass, src: pResult->GetString(Index: 4), dst_size: sizeof(Config.m_aPass));
4090 str_copy(dst: Config.m_aIp, src: pResult->GetString(Index: 5), dst_size: sizeof(Config.m_aIp));
4091 Config.m_aBindaddr[0] = '\0';
4092 Config.m_Port = pResult->GetInteger(Index: 6);
4093 Config.m_Setup = pResult->NumArguments() == 8 ? pResult->GetInteger(Index: 7) : true;
4094
4095 char aBuf[512];
4096 str_format(buffer: aBuf, buffer_size: sizeof(aBuf),
4097 format: "Adding new Sql%sServer: DB: '%s' Prefix: '%s' User: '%s' IP: <{%s}> Port: %d",
4098 Write ? "Write" : "Read",
4099 Config.m_aDatabase, Config.m_aPrefix, Config.m_aUser, Config.m_aIp, Config.m_Port);
4100 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: aBuf);
4101 pSelf->DbPool()->RegisterMysqlDatabase(DatabaseMode: Write ? CDbConnectionPool::WRITE : CDbConnectionPool::READ, pMysqlConfig: &Config);
4102}
4103
4104void CServer::ConDumpSqlServers(IConsole::IResult *pResult, void *pUserData)
4105{
4106 CServer *pSelf = (CServer *)pUserData;
4107
4108 if(str_comp_nocase(a: pResult->GetString(Index: 0), b: "w") == 0)
4109 {
4110 pSelf->DbPool()->Print(pConsole: pSelf->Console(), DatabaseMode: CDbConnectionPool::WRITE);
4111 pSelf->DbPool()->Print(pConsole: pSelf->Console(), DatabaseMode: CDbConnectionPool::WRITE_BACKUP);
4112 }
4113 else if(str_comp_nocase(a: pResult->GetString(Index: 0), b: "r") == 0)
4114 {
4115 pSelf->DbPool()->Print(pConsole: pSelf->Console(), DatabaseMode: CDbConnectionPool::READ);
4116 }
4117 else
4118 {
4119 pSelf->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "choose either 'r' for SqlReadServer or 'w' for SqlWriteServer");
4120 return;
4121 }
4122}
4123
4124void CServer::ConReloadAnnouncement(IConsole::IResult *pResult, void *pUserData)
4125{
4126 CServer *pThis = static_cast<CServer *>(pUserData);
4127 pThis->ReadAnnouncementsFile();
4128}
4129
4130void CServer::ConReloadMaplist(IConsole::IResult *pResult, void *pUserData)
4131{
4132 CServer *pThis = static_cast<CServer *>(pUserData);
4133 pThis->InitMaplist();
4134}
4135
4136void CServer::ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4137{
4138 pfnCallback(pResult, pCallbackUserData);
4139 if(pResult->NumArguments())
4140 {
4141 CServer *pThis = static_cast<CServer *>(pUserData);
4142 str_clean_whitespaces(str: pThis->Config()->m_SvName);
4143 pThis->ExpireServerInfoAndQueueResend();
4144 }
4145}
4146
4147void CServer::ConchainMaxclientsperipUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4148{
4149 pfnCallback(pResult, pCallbackUserData);
4150 if(pResult->NumArguments())
4151 ((CServer *)pUserData)->m_NetServer.SetMaxClientsPerIp(pResult->GetInteger(Index: 0));
4152}
4153
4154void CServer::ConchainCommandAccessUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4155{
4156 if(pResult->NumArguments() == 2)
4157 {
4158 CServer *pThis = static_cast<CServer *>(pUserData);
4159 const IConsole::ICommandInfo *pInfo = pThis->Console()->GetCommandInfo(pName: pResult->GetString(Index: 0), FlagMask: CFGFLAG_SERVER, Temp: false);
4160 IConsole::EAccessLevel OldAccessLevel = IConsole::EAccessLevel::ADMIN;
4161 if(pInfo)
4162 OldAccessLevel = pInfo->GetAccessLevel();
4163 pfnCallback(pResult, pCallbackUserData);
4164 if(pInfo && OldAccessLevel != pInfo->GetAccessLevel())
4165 {
4166 for(int i = 0; i < MAX_CLIENTS; ++i)
4167 {
4168 if(pThis->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY)
4169 continue;
4170 if(!pThis->IsRconAuthed(ClientId: i))
4171 continue;
4172
4173 const IConsole::EAccessLevel ClientAccessLevel = pThis->ConsoleAccessLevel(ClientId: i);
4174 bool HadAccess = OldAccessLevel >= ClientAccessLevel;
4175 bool HasAccess = pInfo->GetAccessLevel() >= ClientAccessLevel;
4176
4177 // Nothing changed
4178 if(HadAccess == HasAccess)
4179 continue;
4180 // Command not sent yet. The sending will happen in alphabetical order with correctly updated permissions.
4181 if(pThis->m_aClients[i].m_pRconCmdToSend && str_comp(a: pResult->GetString(Index: 0), b: pThis->m_aClients[i].m_pRconCmdToSend->Name()) >= 0)
4182 continue;
4183
4184 if(HasAccess)
4185 pThis->SendRconCmdAdd(pCommandInfo: pInfo, ClientId: i);
4186 else
4187 pThis->SendRconCmdRem(pCommandInfo: pInfo, ClientId: i);
4188 }
4189 }
4190 }
4191 else
4192 pfnCallback(pResult, pCallbackUserData);
4193}
4194
4195void CServer::LogoutClient(int ClientId, const char *pReason)
4196{
4197 if(!IsSixup(ClientId))
4198 {
4199 CMsgPacker Msg(NETMSG_RCON_AUTH_STATUS, true);
4200 Msg.AddInt(i: 0); //authed
4201 Msg.AddInt(i: 0); //cmdlist
4202 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
4203 }
4204 else
4205 {
4206 CMsgPacker Msg(protocol7::NETMSG_RCON_AUTH_OFF, true, true);
4207 SendMsg(pMsg: &Msg, Flags: MSGFLAG_VITAL, ClientId);
4208 }
4209
4210 m_aClients[ClientId].m_AuthTries = 0;
4211 m_aClients[ClientId].m_pRconCmdToSend = nullptr;
4212 m_aClients[ClientId].m_MaplistEntryToSend = CClient::MAPLIST_UNINITIALIZED;
4213
4214 if(*pReason)
4215 {
4216 char aBuf[64];
4217 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Logged out by %s.", pReason);
4218 SendRconLine(ClientId, pLine: aBuf);
4219 log_info("server", "ClientId=%d with key='%s' logged out by %s", ClientId, m_AuthManager.KeyIdent(m_aClients[ClientId].m_AuthKey), pReason);
4220 }
4221 else
4222 {
4223 SendRconLine(ClientId, pLine: "Logout successful.");
4224 log_info("server", "ClientId=%d with key='%s' logged out", ClientId, m_AuthManager.KeyIdent(m_aClients[ClientId].m_AuthKey));
4225 }
4226
4227 m_aClients[ClientId].m_AuthKey = -1;
4228
4229 GameServer()->OnSetAuthed(ClientId, Level: AUTHED_NO);
4230}
4231
4232void CServer::LogoutKey(int Key, const char *pReason)
4233{
4234 for(int i = 0; i < MAX_CLIENTS; i++)
4235 if(m_aClients[i].m_AuthKey == Key)
4236 LogoutClient(ClientId: i, pReason);
4237}
4238
4239void CServer::ConchainRconPasswordChangeGeneric(const char *pRoleName, const char *pCurrent, IConsole::IResult *pResult)
4240{
4241 if(pResult->NumArguments() == 1)
4242 {
4243 int KeySlot = m_AuthManager.DefaultKey(pRoleName);
4244 const char *pNew = pResult->GetString(Index: 0);
4245 if(str_comp(a: pCurrent, b: pNew) == 0)
4246 {
4247 return;
4248 }
4249 if(KeySlot == -1 && pNew[0])
4250 {
4251 m_AuthManager.AddDefaultKey(pRoleName, pPw: pNew);
4252 }
4253 else if(KeySlot >= 0)
4254 {
4255 if(!pNew[0])
4256 {
4257 AuthRemoveKey(KeySlot);
4258 // Already logs users out.
4259 }
4260 else
4261 {
4262 m_AuthManager.UpdateKey(Slot: KeySlot, pPw: pNew, pRoleName);
4263 LogoutKey(Key: KeySlot, pReason: "key update");
4264 }
4265 }
4266 }
4267}
4268
4269void CServer::ConchainRconPasswordChange(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4270{
4271 CServer *pThis = static_cast<CServer *>(pUserData);
4272 pThis->ConchainRconPasswordChangeGeneric(pRoleName: RoleName::ADMIN, pCurrent: pThis->Config()->m_SvRconPassword, pResult);
4273 pfnCallback(pResult, pCallbackUserData);
4274}
4275
4276void CServer::ConchainRconModPasswordChange(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4277{
4278 CServer *pThis = static_cast<CServer *>(pUserData);
4279 pThis->ConchainRconPasswordChangeGeneric(pRoleName: RoleName::MODERATOR, pCurrent: pThis->Config()->m_SvRconModPassword, pResult);
4280 pfnCallback(pResult, pCallbackUserData);
4281}
4282
4283void CServer::ConchainRconHelperPasswordChange(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4284{
4285 CServer *pThis = static_cast<CServer *>(pUserData);
4286 pThis->ConchainRconPasswordChangeGeneric(pRoleName: RoleName::HELPER, pCurrent: pThis->Config()->m_SvRconHelperPassword, pResult);
4287 pfnCallback(pResult, pCallbackUserData);
4288}
4289
4290void CServer::ConchainMapUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4291{
4292 CServer *pThis = static_cast<CServer *>(pUserData);
4293 pfnCallback(pResult, pCallbackUserData);
4294 if(pResult->NumArguments() >= 1 && pThis->GameServer()->Map()->IsLoaded())
4295 {
4296 pThis->m_MapReload = str_comp(a: pThis->Config()->m_SvMap, b: pThis->GameServer()->Map()->FullName()) != 0;
4297 }
4298}
4299
4300void CServer::ConchainSixupUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4301{
4302 pfnCallback(pResult, pCallbackUserData);
4303 CServer *pThis = static_cast<CServer *>(pUserData);
4304 if(pResult->NumArguments() >= 1 && pThis->GameServer()->Map()->IsLoaded())
4305 {
4306 pThis->m_MapReload |= (pThis->m_apCurrentMapData[MAP_TYPE_SIXUP] != nullptr) != (pResult->GetInteger(Index: 0) != 0);
4307 }
4308}
4309
4310void CServer::ConchainRegisterCommunityTokenRedact(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4311{
4312 // community tokens look like this:
4313 // ddtc_6DnZq5Ix0J2kvDHbkPNtb6bsZxOVQg4ly2jw. The first 11 bytes are
4314 // shared between the token and the verification token, so they're
4315 // semi-public. Redact everything beyond that point.
4316 static constexpr int REDACT_FROM = 11;
4317 if(pResult->NumArguments() == 0 && str_length(str: g_Config.m_SvRegisterCommunityToken) > REDACT_FROM)
4318 {
4319 char aTruncated[16];
4320 str_truncate(dst: aTruncated, dst_size: sizeof(aTruncated), src: g_Config.m_SvRegisterCommunityToken, truncation_len: REDACT_FROM);
4321 log_info("config", "Value: %s[REDACTED] (total length %d)", aTruncated, str_length(g_Config.m_SvRegisterCommunityToken));
4322 return;
4323 }
4324 pfnCallback(pResult, pCallbackUserData);
4325}
4326
4327void CServer::ConchainLoglevel(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4328{
4329 CServer *pSelf = (CServer *)pUserData;
4330 pfnCallback(pResult, pCallbackUserData);
4331 if(pResult->NumArguments())
4332 {
4333 pSelf->m_pFileLogger->SetFilter(CLogFilter{.m_MaxLevel: IConsole::ToLogLevelFilter(ConsoleLevel: g_Config.m_Loglevel)});
4334 }
4335}
4336
4337void CServer::ConchainStdoutOutputLevel(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4338{
4339 CServer *pSelf = (CServer *)pUserData;
4340 pfnCallback(pResult, pCallbackUserData);
4341 if(pResult->NumArguments() && pSelf->m_pStdoutLogger)
4342 {
4343 pSelf->m_pStdoutLogger->SetFilter(CLogFilter{.m_MaxLevel: IConsole::ToLogLevelFilter(ConsoleLevel: g_Config.m_StdoutOutputLevel)});
4344 }
4345}
4346
4347void CServer::ConchainAnnouncementFilename(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4348{
4349 CServer *pSelf = (CServer *)pUserData;
4350 bool Changed = pResult->NumArguments() && str_comp(a: pResult->GetString(Index: 0), b: g_Config.m_SvAnnouncementFilename);
4351 pfnCallback(pResult, pCallbackUserData);
4352 if(Changed)
4353 {
4354 pSelf->ReadAnnouncementsFile();
4355 }
4356}
4357
4358void CServer::ConchainInputFifo(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4359{
4360 CServer *pSelf = (CServer *)pUserData;
4361 pfnCallback(pResult, pCallbackUserData);
4362 if(pSelf->m_Fifo.IsInit())
4363 {
4364 pSelf->m_Fifo.Shutdown();
4365 pSelf->m_Fifo.Init(pConsole: pSelf->Console(), pFifoFile: pSelf->Config()->m_SvInputFifo, Flag: CFGFLAG_SERVER);
4366 }
4367}
4368
4369#if defined(CONF_FAMILY_UNIX)
4370void CServer::ConchainConnLoggingServerChange(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
4371{
4372 pfnCallback(pResult, pCallbackUserData);
4373 if(pResult->NumArguments() == 1)
4374 {
4375 CServer *pServer = (CServer *)pUserData;
4376
4377 // open socket to send new connections
4378 if(!pServer->m_ConnLoggingSocketCreated)
4379 {
4380 pServer->m_ConnLoggingSocket = net_unix_create_unnamed();
4381 if(pServer->m_ConnLoggingSocket == -1)
4382 {
4383 pServer->Console()->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "server", pStr: "Failed to created socket for communication with the connection logging server.");
4384 }
4385 else
4386 {
4387 pServer->m_ConnLoggingSocketCreated = true;
4388 }
4389 }
4390
4391 // set the destination address for the connection logging
4392 net_unix_set_addr(addr: &pServer->m_ConnLoggingDestAddr, path: pResult->GetString(Index: 0));
4393 }
4394}
4395#endif
4396
4397void CServer::RegisterCommands()
4398{
4399 m_pConsole = Kernel()->RequestInterface<IConsole>();
4400 m_pGameServer = Kernel()->RequestInterface<IGameServer>();
4401 m_pHttp = Kernel()->RequestInterface<IEngineHttp>();
4402 m_pStorage = Kernel()->RequestInterface<IStorage>();
4403 m_pAntibot = Kernel()->RequestInterface<IEngineAntibot>();
4404
4405 // register console commands
4406 Console()->Register(pName: "kick", pParams: "i[id] ?r[reason]", Flags: CFGFLAG_SERVER, pfnFunc: ConKick, pUser: this, pHelp: "Kick player with specified id for any reason");
4407 Console()->Register(pName: "status", pParams: "?r[name]", Flags: CFGFLAG_SERVER, pfnFunc: ConStatus, pUser: this, pHelp: "List players containing name or all players");
4408 Console()->Register(pName: "shutdown", pParams: "?r[reason]", Flags: CFGFLAG_SERVER, pfnFunc: ConShutdown, pUser: this, pHelp: "Shut down");
4409 Console()->Register(pName: "logout", pParams: "", Flags: CFGFLAG_SERVER, pfnFunc: ConLogout, pUser: this, pHelp: "Logout of rcon");
4410 Console()->Register(pName: "show_ips", pParams: "?i[show]", Flags: CFGFLAG_SERVER, pfnFunc: ConShowIps, pUser: this, pHelp: "Show IP addresses in rcon commands (1 = on, 0 = off)");
4411 Console()->Register(pName: "hide_auth_status", pParams: "?i[hide]", Flags: CFGFLAG_SERVER, pfnFunc: ConHideAuthStatus, pUser: this, pHelp: "Opt out of spectator count and hide auth status to non-authed players (1 = hidden, 0 = shown)");
4412 Console()->Register(pName: "force_high_bandwidth_on_spectate", pParams: "?i[enable]", Flags: CFGFLAG_SERVER, pfnFunc: ConForceHighBandwidthOnSpectate, pUser: this, pHelp: "Force high bandwidth mode when spectating (1 = on, 0 = off)");
4413
4414 Console()->Register(pName: "record", pParams: "?s[file]", Flags: CFGFLAG_SERVER | CFGFLAG_STORE, pfnFunc: ConRecord, pUser: this, pHelp: "Record to a file");
4415 Console()->Register(pName: "stoprecord", pParams: "", Flags: CFGFLAG_SERVER, pfnFunc: ConStopRecord, pUser: this, pHelp: "Stop recording");
4416
4417 Console()->Register(pName: "reload", pParams: "", Flags: CFGFLAG_SERVER, pfnFunc: ConMapReload, pUser: this, pHelp: "Reload the map");
4418
4419 Console()->Register(pName: "add_sqlserver", pParams: "s['r'|'w'] s[Database] s[Prefix] s[User] s[Password] s[IP] i[Port] ?i[SetUpDatabase ?]", Flags: CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, pfnFunc: ConAddSqlServer, pUser: this, pHelp: "add a sqlserver");
4420 Console()->Register(pName: "dump_sqlservers", pParams: "s['r'|'w']", Flags: CFGFLAG_SERVER, pfnFunc: ConDumpSqlServers, pUser: this, pHelp: "dumps all sqlservers readservers = r, writeservers = w");
4421
4422 Console()->Register(pName: "auth_add", pParams: "s[ident] s[level] r[pw]", Flags: CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, pfnFunc: ConAuthAdd, pUser: this, pHelp: "Add a rcon key");
4423 Console()->Register(pName: "auth_add_p", pParams: "s[ident] s[level] s[hash] s[salt]", Flags: CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, pfnFunc: ConAuthAddHashed, pUser: this, pHelp: "Add a prehashed rcon key");
4424 Console()->Register(pName: "auth_change", pParams: "s[ident] s[level] r[pw]", Flags: CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, pfnFunc: ConAuthUpdate, pUser: this, pHelp: "Update a rcon key");
4425 Console()->Register(pName: "auth_change_p", pParams: "s[ident] s[level] s[hash] s[salt]", Flags: CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, pfnFunc: ConAuthUpdateHashed, pUser: this, pHelp: "Update a rcon key with prehashed data");
4426 Console()->Register(pName: "auth_remove", pParams: "s[ident]", Flags: CFGFLAG_SERVER | CFGFLAG_NONTEEHISTORIC, pfnFunc: ConAuthRemove, pUser: this, pHelp: "Remove a rcon key");
4427 Console()->Register(pName: "auth_list", pParams: "", Flags: CFGFLAG_SERVER, pfnFunc: ConAuthList, pUser: this, pHelp: "List all rcon keys");
4428
4429 Console()->Register(pName: "reload_announcement", pParams: "", Flags: CFGFLAG_SERVER, pfnFunc: ConReloadAnnouncement, pUser: this, pHelp: "Reload the announcements");
4430 Console()->Register(pName: "reload_maplist", pParams: "", Flags: CFGFLAG_SERVER, pfnFunc: ConReloadMaplist, pUser: this, pHelp: "Reload the maplist");
4431
4432 RustVersionRegister(console&: *Console());
4433
4434 Console()->Chain(pName: "sv_name", pfnChainFunc: ConchainSpecialInfoupdate, pUser: this);
4435 Console()->Chain(pName: "password", pfnChainFunc: ConchainSpecialInfoupdate, pUser: this);
4436 Console()->Chain(pName: "sv_reserved_slots", pfnChainFunc: ConchainSpecialInfoupdate, pUser: this);
4437 Console()->Chain(pName: "sv_spectator_slots", pfnChainFunc: ConchainSpecialInfoupdate, pUser: this);
4438
4439 Console()->Chain(pName: "sv_max_clients_per_ip", pfnChainFunc: ConchainMaxclientsperipUpdate, pUser: this);
4440 Console()->Chain(pName: "access_level", pfnChainFunc: ConchainCommandAccessUpdate, pUser: this);
4441
4442 Console()->Chain(pName: "sv_rcon_password", pfnChainFunc: ConchainRconPasswordChange, pUser: this);
4443 Console()->Chain(pName: "sv_rcon_mod_password", pfnChainFunc: ConchainRconModPasswordChange, pUser: this);
4444 Console()->Chain(pName: "sv_rcon_helper_password", pfnChainFunc: ConchainRconHelperPasswordChange, pUser: this);
4445 Console()->Chain(pName: "sv_map", pfnChainFunc: ConchainMapUpdate, pUser: this);
4446 Console()->Chain(pName: "sv_sixup", pfnChainFunc: ConchainSixupUpdate, pUser: this);
4447 Console()->Chain(pName: "sv_register_community_token", pfnChainFunc: ConchainRegisterCommunityTokenRedact, pUser: nullptr);
4448
4449 Console()->Chain(pName: "loglevel", pfnChainFunc: ConchainLoglevel, pUser: this);
4450 Console()->Chain(pName: "stdout_output_level", pfnChainFunc: ConchainStdoutOutputLevel, pUser: this);
4451
4452 Console()->Chain(pName: "sv_announcement_filename", pfnChainFunc: ConchainAnnouncementFilename, pUser: this);
4453
4454 Console()->Chain(pName: "sv_input_fifo", pfnChainFunc: ConchainInputFifo, pUser: this);
4455
4456#if defined(CONF_FAMILY_UNIX)
4457 Console()->Chain(pName: "sv_conn_logging_server", pfnChainFunc: ConchainConnLoggingServerChange, pUser: this);
4458#endif
4459
4460 // register console commands in sub parts
4461 m_ServerBan.InitServerBan(pConsole: Console(), pStorage: Storage(), pServer: this);
4462 m_NameBans.InitConsole(pConsole: Console());
4463 m_pGameServer->OnConsoleInit();
4464 Console()->SetCanUseCommandCallback(pfnCallback: CanClientUseCommandCallback, pUser: this);
4465}
4466
4467std::optional<int> CServer::SnapNewId()
4468{
4469 return m_IdPool.NewId();
4470}
4471
4472void CServer::SnapFreeId(int Id)
4473{
4474 m_IdPool.FreeId(Id);
4475}
4476
4477bool CServer::SnapNewItem(int Type, int Id, rust::Slice<const int32_t> Data)
4478{
4479 return m_pSnapshotBuilder->NewItem(type_: Type, id: Id, data: Data);
4480}
4481
4482void CServer::SnapSetStaticsize(int ItemType, int Size)
4483{
4484 m_pSnapshotDelta->SetStaticsize(type_: ItemType, size: Size);
4485}
4486
4487void CServer::SnapSetStaticsize7(int ItemType, int Size)
4488{
4489 m_pSnapshotDeltaSixup->SetStaticsize(type_: ItemType, size: Size);
4490}
4491
4492CServer *CreateServer() { return new CServer(); }
4493
4494// DDRace
4495
4496void CServer::ReadAnnouncementsFile()
4497{
4498 m_vAnnouncements.clear();
4499
4500 if(g_Config.m_SvAnnouncementFilename[0] == '\0')
4501 return;
4502
4503 CLineReader LineReader;
4504 if(!LineReader.OpenFile(File: m_pStorage->OpenFile(pFilename: g_Config.m_SvAnnouncementFilename, Flags: IOFLAG_READ, Type: IStorage::TYPE_ALL)))
4505 {
4506 log_error("server", "Failed load announcements from '%s'", g_Config.m_SvAnnouncementFilename);
4507 return;
4508 }
4509 while(const char *pLine = LineReader.Get())
4510 {
4511 if(str_length(str: pLine) && pLine[0] != '#')
4512 {
4513 m_vAnnouncements.emplace_back(args&: pLine);
4514 }
4515 }
4516 log_info("server", "Loaded %" PRIzu " announcements", m_vAnnouncements.size());
4517}
4518
4519const char *CServer::GetAnnouncementLine()
4520{
4521 if(m_vAnnouncements.empty())
4522 {
4523 return nullptr;
4524 }
4525 else if(m_vAnnouncements.size() == 1)
4526 {
4527 m_AnnouncementLastLine = 0;
4528 }
4529 else if(!g_Config.m_SvAnnouncementRandom)
4530 {
4531 if(++m_AnnouncementLastLine >= m_vAnnouncements.size())
4532 m_AnnouncementLastLine %= m_vAnnouncements.size();
4533 }
4534 else
4535 {
4536 unsigned Rand;
4537 do
4538 {
4539 Rand = rand() % m_vAnnouncements.size();
4540 } while(Rand == m_AnnouncementLastLine);
4541
4542 m_AnnouncementLastLine = Rand;
4543 }
4544
4545 return m_vAnnouncements[m_AnnouncementLastLine].c_str();
4546}
4547
4548struct CSubdirCallbackUserdata
4549{
4550 CServer *m_pServer;
4551 char m_aCurrentFolder[IO_MAX_PATH_LENGTH];
4552};
4553
4554int CServer::MaplistEntryCallback(const char *pFilename, int IsDir, int DirType, void *pUser)
4555{
4556 CSubdirCallbackUserdata *pUserdata = static_cast<CSubdirCallbackUserdata *>(pUser);
4557 CServer *pThis = pUserdata->m_pServer;
4558
4559 if(str_comp(a: pFilename, b: ".") == 0 || str_comp(a: pFilename, b: "..") == 0)
4560 return 0;
4561
4562 char aFilename[IO_MAX_PATH_LENGTH];
4563 if(pUserdata->m_aCurrentFolder[0] != '\0')
4564 str_format(buffer: aFilename, buffer_size: sizeof(aFilename), format: "%s/%s", pUserdata->m_aCurrentFolder, pFilename);
4565 else
4566 str_copy(dst&: aFilename, src: pFilename);
4567
4568 if(IsDir)
4569 {
4570 CSubdirCallbackUserdata Userdata;
4571 Userdata.m_pServer = pThis;
4572 str_copy(dst&: Userdata.m_aCurrentFolder, src: aFilename);
4573 char aFindPath[IO_MAX_PATH_LENGTH];
4574 str_format(buffer: aFindPath, buffer_size: sizeof(aFindPath), format: "maps/%s/", aFilename);
4575 pThis->Storage()->ListDirectory(Type: IStorage::TYPE_ALL, pPath: aFindPath, pfnCallback: MaplistEntryCallback, pUser: &Userdata);
4576 return 0;
4577 }
4578
4579 const char *pSuffix = str_endswith(str: aFilename, suffix: ".map");
4580 if(!pSuffix) // not ending with .map
4581 return 0;
4582 const size_t FilenameLength = pSuffix - aFilename;
4583 aFilename[FilenameLength] = '\0'; // remove suffix
4584 if(FilenameLength >= sizeof(CMaplistEntry().m_aName)) // name too long
4585 return 0;
4586
4587 pThis->m_vMaplistEntries.emplace_back(args&: aFilename);
4588 return 0;
4589}
4590
4591void CServer::InitMaplist()
4592{
4593 m_vMaplistEntries.clear();
4594
4595 CSubdirCallbackUserdata Userdata;
4596 Userdata.m_pServer = this;
4597 Userdata.m_aCurrentFolder[0] = '\0';
4598 Storage()->ListDirectory(Type: IStorage::TYPE_ALL, pPath: "maps/", pfnCallback: MaplistEntryCallback, pUser: &Userdata);
4599
4600 std::sort(first: m_vMaplistEntries.begin(), last: m_vMaplistEntries.end());
4601 log_info("server", "Found %d maps for maplist", (int)m_vMaplistEntries.size());
4602
4603 for(CClient &Client : m_aClients)
4604 {
4605 if(Client.m_State != CClient::STATE_INGAME)
4606 continue;
4607
4608 // Resend maplist to clients that already got it or are currently getting it
4609 if(Client.m_MaplistEntryToSend == CClient::MAPLIST_DONE || Client.m_MaplistEntryToSend >= 0)
4610 {
4611 Client.m_MaplistEntryToSend = CClient::MAPLIST_UNINITIALIZED;
4612 }
4613 }
4614}
4615
4616int *CServer::GetIdMap(int ClientId)
4617{
4618 return m_aIdMap + VANILLA_MAX_CLIENTS * ClientId;
4619}
4620
4621bool CServer::SetTimedOut(int ClientId, int OrigId)
4622{
4623 if(!m_NetServer.HasErrored(ClientId))
4624 {
4625 return false;
4626 }
4627
4628 // The login was on the current conn, logout should also be on the current conn
4629 if(IsRconAuthed(ClientId: OrigId))
4630 {
4631 LogoutClient(ClientId: OrigId, pReason: "Timeout Protection");
4632 }
4633
4634 m_NetServer.ResumeOldConnection(ClientId, OrigId);
4635
4636 m_aClients[ClientId].m_Sixup = m_aClients[OrigId].m_Sixup;
4637
4638 DelClientCallback(ClientId: OrigId, pReason: "Timeout Protection used", pUser: this);
4639 m_aClients[ClientId].m_AuthKey = -1;
4640 m_aClients[ClientId].m_Flags = m_aClients[OrigId].m_Flags;
4641 m_aClients[ClientId].m_DDNetVersion = m_aClients[OrigId].m_DDNetVersion;
4642 m_aClients[ClientId].m_GotDDNetVersionPacket = m_aClients[OrigId].m_GotDDNetVersionPacket;
4643 m_aClients[ClientId].m_DDNetVersionSettled = m_aClients[OrigId].m_DDNetVersionSettled;
4644 return true;
4645}
4646
4647void CServer::SetErrorShutdown(const char *pReason)
4648{
4649 str_copy(dst&: m_aErrorShutdownReason, src: pReason);
4650}
4651
4652void CServer::SetLoggers(std::shared_ptr<ILogger> &&pFileLogger, std::shared_ptr<ILogger> &&pStdoutLogger)
4653{
4654 m_pFileLogger = pFileLogger;
4655 m_pStdoutLogger = pStdoutLogger;
4656}
4657