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