From 1c94cd68802c8df40cf7e94d99f21240294706fb Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 11 Sep 2018 15:23:45 +0800 Subject: [PATCH] Avoid spamming with connect to peer message with localhost --- src/network/protocols/connect_to_peer.cpp | 12 +----------- src/network/stk_host.cpp | 24 +++++++++++++++++++---- src/network/stk_host.hpp | 3 ++- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/network/protocols/connect_to_peer.cpp b/src/network/protocols/connect_to_peer.cpp index 4ccb1a682..6bf27207b 100644 --- a/src/network/protocols/connect_to_peer.cpp +++ b/src/network/protocols/connect_to_peer.cpp @@ -69,17 +69,7 @@ void ConnectToPeer::asynchronousUpdate() BareNetworkString aloha("aloha-stk"); aloha.getBuffer().insert(aloha.getBuffer().begin(), 2, 0xFF); STKHost::get()->sendRawPacket(aloha, broadcast_address); - Log::verbose("ConnectToPeer", "Broadcast aloha sent."); - StkTime::sleep(1); - - if (m_peer_address.isPublicAddressLocalhost()) - { - broadcast_address.setIP(0x7f000001); // 127.0.0.1 (localhost) - broadcast_address.setPort(m_peer_address.getPort()); - STKHost::get()->sendRawPacket(aloha, broadcast_address); - Log::verbose("ConnectToPeer", "Broadcast aloha to self."); - } - + Log::debug("ConnectToPeer", "Broadcast aloha sent."); // 20 seconds timeout if (m_tried_connection++ > 10) { diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 2cc8c2a52..af6d5c122 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -719,14 +719,24 @@ void STKHost::mainLoop() uint64_t last_ping_time = StkTime::getRealTimeMs(); uint64_t last_ping_time_update_for_client = StkTime::getRealTimeMs(); + std::map ctp; while (m_exit_timeout.load() > StkTime::getRealTimeMs()) { + // Clear outdated connect to peer list every 15 seconds + for (auto it = ctp.begin(); it != ctp.end();) + { + if (it->second + 15000 < StkTime::getRealTimeMs()) + it = ctp.erase(it); + else + it++; + } + auto sl = LobbyProtocol::get(); if (direct_socket && sl && sl->waitingForPlayers()) { try { - handleDirectSocketRequest(direct_socket, sl); + handleDirectSocketRequest(direct_socket, sl, ctp); } catch (std::exception& e) { @@ -1006,7 +1016,8 @@ void STKHost::mainLoop() * (and sender IP address and port). */ void STKHost::handleDirectSocketRequest(Network* direct_socket, - std::shared_ptr sl) + std::shared_ptr sl, + std::map& ctp) { const int LEN=2048; char buffer[LEN]; @@ -1044,17 +1055,22 @@ void STKHost::handleDirectSocketRequest(Network* direct_socket, } // if message is server-requested else if (command == connection_cmd) { + const std::string& peer_addr = sender.toString(); // In case of a LAN connection, we only allow connections from // a LAN address (192.168*, ..., and 127.*). if (!sender.isLAN() && !sender.isPublicAddressLocalhost() && !NetworkConfig::get()->isPublicServer()) { Log::error("STKHost", "Client trying to connect from '%s'", - sender.toString().c_str()); + peer_addr.c_str()); Log::error("STKHost", "which is outside of LAN - rejected."); return; } - std::make_shared(sender)->requestStart(); + if (ctp.find(peer_addr) == ctp.end()) + { + ctp[peer_addr] = StkTime::getRealTimeMs(); + std::make_shared(sender)->requestStart(); + } } else if (command == "stk-server-port") { diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index e2724331a..18e6fa6d6 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -152,7 +152,8 @@ private: void init(); // ------------------------------------------------------------------------ void handleDirectSocketRequest(Network* direct_socket, - std::shared_ptr sl); + std::shared_ptr sl, + std::map& ctp); // ------------------------------------------------------------------------ void mainLoop();