P2PServer: ban peers that disconnect without finishing handshake

pull/22/head
SChernykh 2021-09-06 16:17:20 +02:00
parent d3dc4c731f
commit 7b0cc256c5
4 changed files with 18 additions and 1 deletions

View File

@ -912,6 +912,19 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
return true; return true;
} }
void P2PServer::P2PClient::on_read_failed(int /*err*/)
{
if (!m_handshakeComplete) {
LOGWARN(5, "peer " << static_cast<char*>(m_addrString) << " disconnected before finishing handshake");
ban(DEFAULT_BAN_TIME);
P2PServer* server = static_cast<P2PServer*>(m_owner);
if (server) {
server->remove_peer_from_list(this);
}
}
}
bool P2PServer::P2PClient::send_handshake_challenge() bool P2PServer::P2PClient::send_handshake_challenge()
{ {
P2PServer* owner = static_cast<P2PServer*>(m_owner); P2PServer* owner = static_cast<P2PServer*>(m_owner);

View File

@ -63,6 +63,7 @@ public:
void reset() override; void reset() override;
bool on_connect() override; bool on_connect() override;
bool on_read(char* data, uint32_t size) override; bool on_read(char* data, uint32_t size) override;
void on_read_failed(int err) override;
// Both peers send handshake challenge immediately after a connection is established // Both peers send handshake challenge immediately after a connection is established
// Both peers must have the same consensus ID for handshake to succeed // Both peers must have the same consensus ID for handshake to succeed

View File

@ -89,6 +89,7 @@ public:
virtual void reset(); virtual void reset();
virtual bool on_connect() = 0; virtual bool on_connect() = 0;
virtual bool on_read(char* data, uint32_t size) = 0; virtual bool on_read(char* data, uint32_t size) = 0;
virtual void on_read_failed(int /*err*/) {}
static void on_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf); static void on_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf); static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);

View File

@ -835,7 +835,9 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::Client::on_read(uv_stream_t* stre
} }
else if (nread < 0) { else if (nread < 0) {
if (nread != UV_EOF) { if (nread != UV_EOF) {
LOGWARN(5, "client " << static_cast<const char*>(pThis->m_addrString) << " failed to read response, err = " << uv_err_name(static_cast<int>(nread))); const int err = static_cast<int>(nread);
LOGWARN(5, "client " << static_cast<const char*>(pThis->m_addrString) << " failed to read response, err = " << uv_err_name(err));
pThis->on_read_failed(err);
} }
pThis->close(); pThis->close();
} }