Skip to content

Commit d04e7b2

Browse files
authored
Merge pull request #80 from rtw1x1/main
code cleanup
2 parents 4f9b84d + c8da3dc commit d04e7b2

File tree

14 files changed

+6
-502
lines changed

14 files changed

+6
-502
lines changed

src/game/SecureCipher.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ bool SecureCipher::ComputeServerKeys(const uint8_t* client_pk)
119119
sodium_memzero(m_rx_stream_nonce, NONCE_SIZE);
120120
m_rx_stream_nonce[0] = 0x02;
121121

122-
sys_log(0, "[CIPHER] Server keys computed (tx_key: %02x%02x%02x%02x, rx_key: %02x%02x%02x%02x)",
123-
m_tx_key[0], m_tx_key[1], m_tx_key[2], m_tx_key[3],
124-
m_rx_key[0], m_rx_key[1], m_rx_key[2], m_rx_key[3]);
125-
126122
return true;
127123
}
128124

src/game/config.cpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,6 @@ bool g_bCheckMultiHack = true;
101101

102102
int g_iSpamBlockMaxLevel = 10;
103103

104-
int g_iFloodMaxPacketsPerSec = 300;
105-
int g_iFloodMaxConnectionsPerIP = 10;
106-
int g_iFloodMaxGlobalConnections = 8192;
107-
108-
bool g_bFirewallEnable = false;
109-
int g_iFirewallTcpSynLimit = 500;
110-
int g_iFirewallTcpSynBurst = 1000;
111-
112104
void LoadStateUserCount();
113105
void LoadValidCRCList();
114106
bool LoadClientVersion();
@@ -703,41 +695,6 @@ void config_init(const string& st_localeServiceName)
703695
str_to_number(g_iSpamBlockMaxLevel, value_string);
704696
}
705697

706-
TOKEN("flood_max_packets_per_sec")
707-
{
708-
str_to_number(g_iFloodMaxPacketsPerSec, value_string);
709-
g_iFloodMaxPacketsPerSec = MAX(50, g_iFloodMaxPacketsPerSec);
710-
}
711-
712-
TOKEN("flood_max_connections_per_ip")
713-
{
714-
str_to_number(g_iFloodMaxConnectionsPerIP, value_string);
715-
g_iFloodMaxConnectionsPerIP = MAX(1, g_iFloodMaxConnectionsPerIP);
716-
}
717-
718-
TOKEN("flood_max_global_connections")
719-
{
720-
str_to_number(g_iFloodMaxGlobalConnections, value_string);
721-
g_iFloodMaxGlobalConnections = MAX(64, g_iFloodMaxGlobalConnections);
722-
}
723-
724-
TOKEN("firewall_enable")
725-
{
726-
str_to_number(g_bFirewallEnable, value_string);
727-
}
728-
729-
TOKEN("firewall_tcp_syn_limit")
730-
{
731-
str_to_number(g_iFirewallTcpSynLimit, value_string);
732-
g_iFirewallTcpSynLimit = MAX(10, g_iFirewallTcpSynLimit);
733-
}
734-
735-
TOKEN("firewall_tcp_syn_burst")
736-
{
737-
str_to_number(g_iFirewallTcpSynBurst, value_string);
738-
g_iFirewallTcpSynBurst = MAX(10, g_iFirewallTcpSynBurst);
739-
}
740-
741698
TOKEN("protect_normal_player")
742699
{
743700
str_to_number(g_protectNormalPlayer, value_string);

src/game/config.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,5 @@ extern int gPlayerMaxLevel;
105105

106106
extern bool g_BlockCharCreation;
107107

108-
extern bool g_bFirewallEnable;
109-
extern int g_iFirewallTcpSynLimit;
110-
extern int g_iFirewallTcpSynBurst;
111-
112108
#endif /* __INC_METIN_II_GAME_CONFIG_H__ */
113109

src/game/desc.cpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ void DESC::Initialize()
8181
m_offtime = 0;
8282

8383
m_pkDisconnectEvent = NULL;
84-
85-
m_iFloodCheckPulse = 0;
86-
m_dwFloodPacketCount = 0;
87-
m_bIPCountTracked = false;
8884
}
8985

9086
void DESC::Destroy()
@@ -216,7 +212,6 @@ bool DESC::Setup(LPFDWATCH _fdw, socket_t _fd, const struct sockaddr_in & c_rSoc
216212

217213
m_pkPingEvent = event_create(ping_event, info, ping_event_second_cycle);
218214

219-
// Set Phase to handshake and begin secure key exchange
220215
SetPhase(PHASE_HANDSHAKE);
221216
m_handshake_time = get_dword_time();
222217
SendKeyChallenge();
@@ -242,7 +237,6 @@ int DESC::ProcessInput()
242237
else if (bytes_read == 0)
243238
return 0;
244239

245-
// Decrypt only the newly received bytes before committing to the buffer
246240
if (m_secureCipher.IsActivated()) {
247241
m_secureCipher.DecryptInPlace(m_inputBuffer.WritePtr(), bytes_read);
248242
}
@@ -328,7 +322,6 @@ void DESC::Packet(const void * c_pvData, int iSize)
328322
if (m_iPhase == PHASE_CLOSE)
329323
return;
330324

331-
// Log the packet for sequence tracking (only for real packet sends, not buffered flushes)
332325
if (!m_hasBufferedOutput && iSize >= (int)sizeof(uint16_t) * 2)
333326
{
334327
const uint16_t wHeader = *static_cast<const uint16_t*>(c_pvData);
@@ -465,54 +458,22 @@ bool DESC::IsExpiredHandshake() const
465458
return (m_handshake_time + (5 * 1000)) < get_dword_time();
466459
}
467460

468-
bool DESC::CheckPacketFlood()
469-
{
470-
extern int g_iFloodMaxPacketsPerSec;
471-
472-
// Use thecore_pulse() (cached per game-loop iteration) instead of
473-
// get_dword_time() (gettimeofday syscall) to avoid per-packet syscall overhead.
474-
int pulse = thecore_pulse();
475-
int pps = static_cast<int>(thecore_pulse_per_second());
476-
477-
if (pulse - m_iFloodCheckPulse >= pps)
478-
{
479-
m_iFloodCheckPulse = pulse;
480-
m_dwFloodPacketCount = 1;
481-
return false;
482-
}
483-
484-
++m_dwFloodPacketCount;
485-
486-
if (m_dwFloodPacketCount > (uint32_t)g_iFloodMaxPacketsPerSec)
487-
{
488-
sys_log(0, "FLOOD: %s exceeded %d packets/sec (count: %u), disconnecting",
489-
GetHostName(), g_iFloodMaxPacketsPerSec, m_dwFloodPacketCount);
490-
return true;
491-
}
492-
493-
return false;
494-
}
495-
496461
DWORD DESC::GetClientTime()
497462
{
498463
return m_dwClientTime;
499464
}
500465

501-
// Secure key exchange methods (libsodium/XChaCha20-Poly1305)
502466
void DESC::SendKeyChallenge()
503467
{
504-
// Initialize cipher and generate keypair
505468
if (!m_secureCipher.Initialize())
506469
{
507470
sys_err("Failed to initialize SecureCipher");
508471
SetPhase(PHASE_CLOSE);
509472
return;
510473
}
511474

512-
// Generate challenge
513475
m_secureCipher.GenerateChallenge(m_challenge);
514476

515-
// Build and send challenge packet
516477
TPacketGCKeyChallenge packet;
517478
packet.header = GC::KEY_CHALLENGE;
518479
packet.length = sizeof(packet);
@@ -529,14 +490,12 @@ void DESC::SendKeyChallenge()
529490

530491
bool DESC::HandleKeyResponse(const uint8_t* client_pk, const uint8_t* challenge_response)
531492
{
532-
// Compute session keys from client's public key
533493
if (!m_secureCipher.ComputeServerKeys(client_pk))
534494
{
535495
sys_err("Failed to compute server session keys for %s", GetHostName());
536496
return false;
537497
}
538498

539-
// Verify challenge response
540499
if (!m_secureCipher.VerifyChallengeResponse(m_challenge, challenge_response))
541500
{
542501
sys_err("Challenge response verification failed for %s", GetHostName());
@@ -550,17 +509,14 @@ bool DESC::HandleKeyResponse(const uint8_t* client_pk, const uint8_t* challenge_
550509

551510
void DESC::SendKeyComplete()
552511
{
553-
// Generate session token
554512
uint8_t session_token[SecureCipher::SESSION_TOKEN_SIZE];
555513
randombytes_buf(session_token, sizeof(session_token));
556514
m_secureCipher.SetSessionToken(session_token);
557515

558-
// Build and send complete packet
559516
TPacketGCKeyComplete packet;
560517
packet.header = GC::KEY_COMPLETE;
561518
packet.length = sizeof(packet);
562519

563-
// Encrypt the session token
564520
if (!m_secureCipher.EncryptToken(session_token, sizeof(session_token),
565521
packet.encrypted_token, packet.nonce))
566522
{
@@ -571,10 +527,7 @@ void DESC::SendKeyComplete()
571527

572528
Packet(&packet, sizeof(packet));
573529

574-
// Flush before activating encryption
575530
ProcessOutput();
576-
577-
// Activate encryption
578531
m_secureCipher.SetActivated(true);
579532

580533
sys_log(0, "[HANDSHAKE] Cipher ACTIVATED for %s (tx_nonce: %llu, rx_nonce: %llu)",
@@ -852,8 +805,6 @@ void DESC::ChatPacket(BYTE type, const char * format, ...)
852805
Packet(buf.read_peek(), buf.size());
853806
}
854807

855-
// --- Packet sequence tracking ---
856-
857808
void DESC::LogRecvPacket(uint16_t header, uint16_t length)
858809
{
859810
auto& e = m_aRecentRecvPackets[m_dwRecvPacketSeq % PACKET_LOG_SIZE];

src/game/desc.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ class DESC
104104

105105
DWORD GetClientTime();
106106

107-
// Secure key exchange (libsodium/XChaCha20-Poly1305)
108107
void SendKeyChallenge();
109108
bool HandleKeyResponse(const uint8_t* client_pk, const uint8_t* challenge_response);
110109
void SendKeyComplete();
@@ -142,15 +141,9 @@ class DESC
142141
bool isChannelStatusRequested() const { return m_bChannelStatusRequested; }
143142
void SetChannelStatusRequested(bool bChannelStatusRequested) { m_bChannelStatusRequested = bChannelStatusRequested; }
144143

145-
// Handshake timeout check
146144
bool IsExpiredHandshake() const;
147145
void SetHandshakeTime(uint32_t handshake_time) { m_handshake_time = handshake_time; }
148146

149-
// Flood protection
150-
bool CheckPacketFlood();
151-
void SetIPCountTracked(bool b) { m_bIPCountTracked = b; }
152-
bool IsIPCountTracked() const { return m_bIPCountTracked; }
153-
154147
protected:
155148
void Initialize();
156149

@@ -215,15 +208,7 @@ class DESC
215208
bool m_bDestroyed;
216209
bool m_bChannelStatusRequested;
217210

218-
// Handshake timeout protection
219211
uint32_t m_handshake_time;
220-
221-
// Flood protection
222-
int m_iFloodCheckPulse;
223-
uint32_t m_dwFloodPacketCount;
224-
bool m_bIPCountTracked;
225-
226-
// Secure cipher (libsodium/XChaCha20-Poly1305)
227212
SecureCipher m_secureCipher;
228213
uint8_t m_challenge[SecureCipher::CHALLENGE_SIZE];
229214

@@ -242,7 +227,6 @@ class DESC
242227
void RawPacket(const void * c_pvData, int iSize);
243228
void ChatPacket(BYTE type, const char * format, ...);
244229

245-
// --- Packet sequence tracking (debug aid) ---
246230
public:
247231
struct PacketLogEntry
248232
{

src/game/desc_manager.cpp

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,6 @@ LPDESC DESC_MANAGER::AcceptDesc(LPFDWATCH fdw, socket_t s)
8888

8989
strlcpy(host, inet_ntoa(peer.sin_addr), sizeof(host));
9090

91-
// Global connection limit
92-
extern int g_iFloodMaxGlobalConnections;
93-
if (m_iSocketsConnected >= g_iFloodMaxGlobalConnections)
94-
{
95-
sys_log(0, "FLOOD: rejecting connection from %s (global limit %d/%d reached)",
96-
host, m_iSocketsConnected, g_iFloodMaxGlobalConnections);
97-
socket_close(desc);
98-
return NULL;
99-
}
100-
101-
// Per-IP connection limit
102-
extern int g_iFloodMaxConnectionsPerIP;
103-
auto itIP = m_map_ipConnCount.find(host);
104-
if (itIP != m_map_ipConnCount.end() && itIP->second >= g_iFloodMaxConnectionsPerIP)
105-
{
106-
sys_log(0, "FLOOD: rejecting connection from %s (%d/%d connections)",
107-
host, itIP->second, g_iFloodMaxConnectionsPerIP);
108-
socket_close(desc);
109-
return NULL;
110-
}
111-
11291
newd = M2_NEW DESC;
11392

11493
if (!newd->Setup(fdw, desc, peer, ++m_iHandleCount))
@@ -123,10 +102,6 @@ LPDESC DESC_MANAGER::AcceptDesc(LPFDWATCH fdw, socket_t s)
123102
m_set_pkDesc.insert(newd);
124103
++m_iSocketsConnected;
125104

126-
// Track per-IP count
127-
++m_map_ipConnCount[host];
128-
newd->SetIPCountTracked(true);
129-
130105
return (newd);
131106
}
132107

@@ -181,17 +156,6 @@ void DESC_MANAGER::DestroyDesc(LPDESC d, bool bEraseFromSet)
181156
else
182157
m_set_pkClientDesc.erase((LPCLIENT_DESC) d);
183158

184-
// Decrement per-IP connection count (before Destroy invalidates state)
185-
if (d->IsIPCountTracked())
186-
{
187-
auto it = m_map_ipConnCount.find(d->GetHostName());
188-
if (it != m_map_ipConnCount.end())
189-
{
190-
if (--it->second <= 0)
191-
m_map_ipConnCount.erase(it);
192-
}
193-
}
194-
195159
// Explicit call to the virtual function Destroy()
196160
d->Destroy();
197161

@@ -419,7 +383,7 @@ DWORD DESC_MANAGER::CreateLoginKey(LPDESC d)
419383

420384
do
421385
{
422-
dwKey = randombytes_uniform(INT_MAX) + 1; // CSPRNG: [1, INT_MAX]
386+
dwKey = randombytes_uniform(INT_MAX) + 1;
423387

424388
if (m_map_pkLoginKey.find(dwKey) != m_map_pkLoginKey.end())
425389
continue;
@@ -445,7 +409,6 @@ void DESC_MANAGER::ProcessExpiredLoginKey()
445409
{
446410
it2 = it++;
447411

448-
// Clean up orphaned keys (descriptor gone but never expired)
449412
if (it2->second->m_dwExpireTime == 0 && it2->second->m_pkDesc == NULL)
450413
{
451414
M2_DELETE(it2->second);

src/game/desc_manager.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ class DESC_MANAGER : public singleton<DESC_MANAGER>
6767
CLIENT_DESC_SET m_set_pkClientDesc;
6868
DESC_SET m_set_pkDesc;
6969

70-
std::unordered_map<std::string, int> m_map_ipConnCount;
71-
7270
DESC_HANDLE_MAP m_map_handle;
7371
//DESC_ACCOUNTID_MAP m_AccountIDMap;
7472
DESC_LOGINNAME_MAP m_map_loginName;

0 commit comments

Comments
 (0)