Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
RickiNano committed Jan 30, 2024
1 parent ced8cf0 commit 14ec64a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
40 changes: 24 additions & 16 deletions nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,7 @@ void nano::network::start ()
}
ongoing_keepalive ();

auto blocked_peers = node.config.blocked_peers;

for (const std::string & ip_string : blocked_peers)
{
auto ip = boost::asio::ip::make_address (ip_string);
block_ip (ip);
node.logger.info (nano::log::type::network, "Added blocking rule for ip {}", ip.to_string ());
}
Configure_blocked_peers ();
}

void nano::network::stop ()
Expand Down Expand Up @@ -356,16 +349,31 @@ void nano::network::broadcast_confirm_req_many (std::deque<std::pair<std::shared
}
}

void nano::network::block_ip (const boost::asio::ip::address & ip_address)
void nano::network::Configure_blocked_peers ()
{
if (ip_address.is_v4 ())
for (const std::string & ip_string : node.config.blocked_peers)
{
// Convert IPv4 address to IPv4-mapped IPv6 address
blocked_ips.insert (boost::asio::ip::address_v6::v4_mapped (ip_address.to_v4 ()));
}
else
{
blocked_ips.insert (ip_address);
boost::system::error_code ec;
auto ip_address = boost::asio::ip::address::from_string (ip_string, ec);

if (!ec)
{
if (ip_address.is_v4 ())
{
// Convert IPv4 address to IPv4-mapped IPv6 address
blocked_ips.insert (boost::asio::ip::address_v6::v4_mapped (ip_address.to_v4 ()));
}
else
{
blocked_ips.insert (ip_address);
}

node.logger.info (nano::log::type::network, "Added blocking rule for ip {}", ip_address.to_string ());
}
else
{
node.logger.error (nano::log::type::network, "Invalid IP address: {}", ip_string);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion nano/node/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class network final
void broadcast_confirm_req_base (std::shared_ptr<nano::block> const &, std::shared_ptr<std::vector<std::shared_ptr<nano::transport::channel>>> const &, unsigned, bool = false);
void broadcast_confirm_req_batched_many (std::unordered_map<std::shared_ptr<nano::transport::channel>, std::deque<std::pair<nano::block_hash, nano::root>>>, std::function<void ()> = nullptr, unsigned = broadcast_interval_ms, bool = false);
void broadcast_confirm_req_many (std::deque<std::pair<std::shared_ptr<nano::block>, std::shared_ptr<std::vector<std::shared_ptr<nano::transport::channel>>>>>, std::function<void ()> = nullptr, unsigned = broadcast_interval_ms);
void block_ip (const boost::asio::ip::address & ip_address);
void Configure_blocked_peers ();
bool is_ip_blocked (const boost::asio::ip::address & ip_address) const;
std::shared_ptr<nano::transport::channel> find_node_id (nano::account const &);
std::shared_ptr<nano::transport::channel> find_channel (nano::endpoint const &);
Expand Down
2 changes: 2 additions & 0 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ nano::error nano::node_config::serialize_toml (nano::tomlconfig & toml) const
preconfigured_peers_l->push_back (*i);
}

auto blocked_peers_l (toml.create_array ("blocked_peers", "A list of \"address\" (hostname or ipv4 or ipv6 notation ip address) that you want to ignore all requests from. \nExample: [\"192.168.0.1\",\"::ffff:10.0.0.1]\""));

auto preconfigured_representatives_l (toml.create_array ("preconfigured_representatives", "A list of representative account addresses used when creating new accounts in internal wallets."));
for (auto i (preconfigured_representatives.begin ()), n (preconfigured_representatives.end ()); i != n; ++i)
{
Expand Down

0 comments on commit 14ec64a

Please sign in to comment.