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

pull/5/head
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);
}
});
@ -383,7 +383,9 @@ void P2PServer::update_peer_in_list(bool is_v6, const raw_ip& ip, int port)
}
}
m_peerList.emplace_back(Peer{ is_v6, ip, port, 0 });
if (!is_banned(ip)) {
m_peerList.emplace_back(Peer{ is_v6, ip, port, 0 });
}
}
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 });
}
}

View File

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

View File

@ -312,18 +312,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>
bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::is_banned(const raw_ip& ip)
{
MutexLock lock(m_bansLock);
auto it = m_bans.find(ip);
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)
{
{
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, not connecting to it");
m_preallocatedClients.push_back(client);
return false;
}
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");
m_preallocatedClients.push_back(client);
return false;
}
client->m_isV6 = is_v6;
@ -719,15 +727,10 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::on_new_client_nolock(uv_stream_t*
client->m_isIncoming = false;
}
{
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");
client->close();
return;
}
if (is_banned(client->m_addr)) {
LOGINFO(5, "peer " << log::Gray() << static_cast<char*>(client->m_addrString) << log::NoColor() << " is banned, disconnecting");
client->close();
return;
}
if (client->m_owner->m_finished.load() || !client->on_connect()) {