P2PServer: don't add banned peers back to the peer list

This commit is contained in:
SChernykh 2021-08-27 16:36:06 +02:00
parent 3804bfb0ba
commit 81a12158fc
3 changed files with 28 additions and 21 deletions

View file

@ -364,7 +364,7 @@ void P2PServer::load_saved_peer_list()
} }
} }
if (!already_added) { if (!already_added && !is_banned(p.m_addr)) {
m_peerList.push_back(p); m_peerList.push_back(p);
} }
}); });
@ -383,8 +383,10 @@ void P2PServer::update_peer_in_list(bool is_v6, const raw_ip& ip, int port)
} }
} }
if (!is_banned(ip)) {
m_peerList.emplace_back(Peer{ is_v6, ip, port, 0 }); m_peerList.emplace_back(Peer{ is_v6, ip, port, 0 });
} }
}
void P2PServer::remove_peer_from_list(P2PClient* client) void P2PServer::remove_peer_from_list(P2PClient* client)
{ {
@ -1343,7 +1345,7 @@ bool P2PServer::P2PClient::on_peer_list_response(const uint8_t* buf) const
} }
} }
if (!already_added) { if (!already_added && !server->is_banned(ip)) {
server->m_peerList.emplace_back(Peer{ is_v6, ip, port, 0 }); server->m_peerList.emplace_back(Peer{ is_v6, ip, port, 0 });
} }
} }

View file

@ -193,6 +193,8 @@ protected:
uv_mutex_t m_bansLock; uv_mutex_t m_bansLock;
std::map<raw_ip, time_t> m_bans; std::map<raw_ip, time_t> m_bans;
bool is_banned(const raw_ip& ip);
uv_mutex_t m_pendingConnectionsLock; uv_mutex_t m_pendingConnectionsLock;
std::set<raw_ip> m_pendingConnections; std::set<raw_ip> m_pendingConnections;

View file

@ -313,18 +313,26 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_connect_failed(bool, const raw
} }
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE> template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::connect_to_peer_nolock(Client* client, bool is_v6, const sockaddr* addr) bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::is_banned(const raw_ip& ip)
{
{ {
MutexLock lock(m_bansLock); MutexLock lock(m_bansLock);
auto it = m_bans.find(client->m_addr); auto it = m_bans.find(ip);
if ((it != m_bans.end()) && (time(nullptr) < it->second)) { if ((it != m_bans.end()) && (time(nullptr) < it->second)) {
return true;
}
return false;
}
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::connect_to_peer_nolock(Client* client, bool is_v6, const sockaddr* addr)
{
if (is_banned(client->m_addr)) {
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(client->m_addrString) << log::NoColor() << " is banned, not connecting to it"); LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(client->m_addrString) << log::NoColor() << " is banned, not connecting to it");
m_preallocatedClients.push_back(client); m_preallocatedClients.push_back(client);
return false; return false;
} }
}
client->m_isV6 = is_v6; client->m_isV6 = is_v6;
@ -719,16 +727,11 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_new_client_nolock(uv_stream_t*
client->m_isIncoming = false; client->m_isIncoming = false;
} }
{ if (is_banned(client->m_addr)) {
MutexLock lock(m_bansLock);
auto it = m_bans.find(client->m_addr);
if ((it != m_bans.end()) && (time(nullptr) < it->second)) {
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(client->m_addrString) << log::NoColor() << " is banned, disconnecting"); LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(client->m_addrString) << log::NoColor() << " is banned, disconnecting");
client->close(); client->close();
return; return;
} }
}
if (client->m_owner->m_finished.load() || !client->on_connect()) { if (client->m_owner->m_finished.load() || !client->on_connect()) {
client->close(); client->close();