Avoid spamming with connect to peer message with localhost

This commit is contained in:
Benau 2018-09-11 15:23:45 +08:00
parent ade3551c16
commit 1c94cd6880
3 changed files with 23 additions and 16 deletions

View File

@ -69,17 +69,7 @@ void ConnectToPeer::asynchronousUpdate()
BareNetworkString aloha("aloha-stk"); BareNetworkString aloha("aloha-stk");
aloha.getBuffer().insert(aloha.getBuffer().begin(), 2, 0xFF); aloha.getBuffer().insert(aloha.getBuffer().begin(), 2, 0xFF);
STKHost::get()->sendRawPacket(aloha, broadcast_address); STKHost::get()->sendRawPacket(aloha, broadcast_address);
Log::verbose("ConnectToPeer", "Broadcast aloha sent."); Log::debug("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.");
}
// 20 seconds timeout // 20 seconds timeout
if (m_tried_connection++ > 10) if (m_tried_connection++ > 10)
{ {

View File

@ -719,14 +719,24 @@ void STKHost::mainLoop()
uint64_t last_ping_time = StkTime::getRealTimeMs(); uint64_t last_ping_time = StkTime::getRealTimeMs();
uint64_t last_ping_time_update_for_client = StkTime::getRealTimeMs(); uint64_t last_ping_time_update_for_client = StkTime::getRealTimeMs();
std::map<std::string, uint64_t> ctp;
while (m_exit_timeout.load() > StkTime::getRealTimeMs()) 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<ServerLobby>(); auto sl = LobbyProtocol::get<ServerLobby>();
if (direct_socket && sl && sl->waitingForPlayers()) if (direct_socket && sl && sl->waitingForPlayers())
{ {
try try
{ {
handleDirectSocketRequest(direct_socket, sl); handleDirectSocketRequest(direct_socket, sl, ctp);
} }
catch (std::exception& e) catch (std::exception& e)
{ {
@ -1006,7 +1016,8 @@ void STKHost::mainLoop()
* (and sender IP address and port). * (and sender IP address and port).
*/ */
void STKHost::handleDirectSocketRequest(Network* direct_socket, void STKHost::handleDirectSocketRequest(Network* direct_socket,
std::shared_ptr<ServerLobby> sl) std::shared_ptr<ServerLobby> sl,
std::map<std::string, uint64_t>& ctp)
{ {
const int LEN=2048; const int LEN=2048;
char buffer[LEN]; char buffer[LEN];
@ -1044,18 +1055,23 @@ void STKHost::handleDirectSocketRequest(Network* direct_socket,
} // if message is server-requested } // if message is server-requested
else if (command == connection_cmd) else if (command == connection_cmd)
{ {
const std::string& peer_addr = sender.toString();
// In case of a LAN connection, we only allow connections from // In case of a LAN connection, we only allow connections from
// a LAN address (192.168*, ..., and 127.*). // a LAN address (192.168*, ..., and 127.*).
if (!sender.isLAN() && !sender.isPublicAddressLocalhost() && if (!sender.isLAN() && !sender.isPublicAddressLocalhost() &&
!NetworkConfig::get()->isPublicServer()) !NetworkConfig::get()->isPublicServer())
{ {
Log::error("STKHost", "Client trying to connect from '%s'", 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."); Log::error("STKHost", "which is outside of LAN - rejected.");
return; return;
} }
if (ctp.find(peer_addr) == ctp.end())
{
ctp[peer_addr] = StkTime::getRealTimeMs();
std::make_shared<ConnectToPeer>(sender)->requestStart(); std::make_shared<ConnectToPeer>(sender)->requestStart();
} }
}
else if (command == "stk-server-port") else if (command == "stk-server-port")
{ {
BareNetworkString s; BareNetworkString s;

View File

@ -152,7 +152,8 @@ private:
void init(); void init();
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void handleDirectSocketRequest(Network* direct_socket, void handleDirectSocketRequest(Network* direct_socket,
std::shared_ptr<ServerLobby> sl); std::shared_ptr<ServerLobby> sl,
std::map<std::string, uint64_t>& ctp);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void mainLoop(); void mainLoop();