Skip to content

Commit

Permalink
Don't allow a lot of TCP connections
Browse files Browse the repository at this point in the history
  • Loading branch information
tvorogme committed Sep 9, 2024
1 parent 451e476 commit 5310017
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion adnl/adnl-ext-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ void AdnlExtServerImpl::add_local_id(AdnlNodeIdShort id) {
local_ids_.insert(id);
}

void AdnlExtServerImpl::alarm() {
alarm_timestamp() = td::Timestamp::in(10);
ip_connection_count_.clear();
}

void AdnlExtServerImpl::accepted(td::SocketFd fd) {
td::IPAddress my;
Expand All @@ -165,7 +169,12 @@ void AdnlExtServerImpl::accepted(td::SocketFd fd) {

auto &connection_count = ip_connection_count_[addr];
connection_count++;
LOG(INFO) << "Accept from: " << addr << " connections: " << connection_count;
if (connection_count > 10){
LOG(INFO) << "Drop connection from: " << addr << " too many connections";
fd.close();
} else {
LOG(INFO) << "Accept from: " << addr << " connections: " << connection_count;
}
}

td::actor::create_actor<AdnlInboundConnection>(td::actor::ActorOptions().with_name("inconn").with_poll(),
Expand Down
4 changes: 4 additions & 0 deletions adnl/adnl-ext-server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ class AdnlExtServerImpl : public AdnlExtServer {
ports_.clear();
}

void alarm() override;

void reopen_port() {
}

AdnlExtServerImpl(td::actor::ActorId<AdnlPeerTable> adnl, std::vector<AdnlNodeIdShort> ids,
std::vector<td::uint16> ports)
: peer_table_(adnl) {
alarm_timestamp() = td::Timestamp::in(10);

for (auto &id : ids) {
add_local_id(id);
}
Expand Down

0 comments on commit 5310017

Please sign in to comment.