Skip to content

Commit

Permalink
Calculate connections per ip
Browse files Browse the repository at this point in the history
  • Loading branch information
tvorogme committed Sep 9, 2024
1 parent 7cd1bee commit 451e476
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 12 additions & 0 deletions adnl/adnl-ext-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "adnl-ext-server.hpp"
#include "keys/encryptor.h"
#include "utils.hpp"
#include "td/utils/port/IPAddress.h"

namespace ton {

Expand Down Expand Up @@ -155,7 +156,18 @@ void AdnlExtServerImpl::add_local_id(AdnlNodeIdShort id) {
local_ids_.insert(id);
}


void AdnlExtServerImpl::accepted(td::SocketFd fd) {
td::IPAddress my;
auto s = my.init_peer_address(fd);
if (s.is_ok()){
auto addr = my.get_ip_host();

auto &connection_count = ip_connection_count_[addr];
connection_count++;
LOG(INFO) << "Accept from: " << addr << " connections: " << connection_count;
}

td::actor::create_actor<AdnlInboundConnection>(td::actor::ActorOptions().with_name("inconn").with_poll(),
std::move(fd), peer_table_, actor_id(this))
.release();
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 @@ -45,6 +45,7 @@ class AdnlInboundConnection : public AdnlExtConnection {
td::Status process_init_packet(td::BufferSlice data) override;
td::Status process_custom_packet(td::BufferSlice &data, bool &processed) override;
void inited_crypto(td::Result<td::BufferSlice> R);
void init_stop();

private:
td::actor::ActorId<AdnlPeerTable> peer_table_;
Expand All @@ -60,6 +61,7 @@ class AdnlExtServerImpl : public AdnlExtServer {
void add_tcp_port(td::uint16 port) override;
void add_local_id(AdnlNodeIdShort id) override;
void accepted(td::SocketFd fd);
void stop(std::string ip_addr);
void decrypt_init_packet(AdnlNodeIdShort dst, td::BufferSlice data, td::Promise<td::BufferSlice> promise);

void start_up() override {
Expand Down Expand Up @@ -88,6 +90,8 @@ class AdnlExtServerImpl : public AdnlExtServer {
std::set<AdnlNodeIdShort> local_ids_;
std::set<td::uint16> ports_;
std::map<td::uint16, td::actor::ActorOwn<td::TcpInfiniteListener>> listeners_;
std::unordered_map<std::string, int> ip_connection_count_;
int max_connections_per_ip_ = 3;
};

} // namespace adnl
Expand Down
8 changes: 0 additions & 8 deletions tdnet/td/net/TcpListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Copyright 2017-2020 Telegram Systems LLP
*/
#include "td/net/TcpListener.h"
#include "td/utils/port/IPAddress.h"

namespace td {
TcpListener::TcpListener(int port, std::unique_ptr<Callback> callback) : port_(port), callback_(std::move(callback)) {
Expand Down Expand Up @@ -62,13 +61,6 @@ void TcpListener::loop() {
break;
}
TRY_RESULT(client_socket, std::move(r_socket));

IPAddress my;
auto s = my.init_peer_address(client_socket);
if (s.is_ok()){
LOG(INFO) << "Accept from: " << my.get_ip_host();
}

callback_->accept(std::move(client_socket));
}
if (td::can_close(server_socket_fd_)) {
Expand Down

0 comments on commit 451e476

Please sign in to comment.