daemon: add unban all command

This commit is contained in:
Rene Guenon
2026-08-11 16:54:47 +04:00
parent 13b63d281c
commit 4b3eabf858
6 changed files with 47 additions and 2 deletions
+2 -2
View File
@@ -240,8 +240,8 @@ t_command_server::t_command_server(
m_command_lookup.set_handler(
"unban"
, std::bind(&t_command_parser_executor::unban, &m_parser, p::_1)
, "unban <address>"
, "Unban a given <IP>."
, "unban <address>|all"
, "Unban a given <address>, or all banned addresses."
);
m_command_lookup.set_handler(
"banned"
+1
View File
@@ -281,6 +281,7 @@ namespace nodetool
virtual bool unblock_host(const epee::net_utils::network_address &address);
virtual bool block_subnet(const epee::net_utils::ipv4_network_subnet &subnet, time_t seconds = P2P_IP_BLOCKTIME);
virtual bool unblock_subnet(const epee::net_utils::ipv4_network_subnet &subnet);
virtual bool clear_bans();
virtual bool is_host_blocked(const epee::net_utils::network_address &address, time_t *seconds) { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock); return !is_remote_host_allowed(address, seconds); }
virtual std::map<std::string, time_t> get_blocked_hosts() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock); return m_blocked_hosts; }
virtual std::map<epee::net_utils::ipv4_network_subnet, time_t> get_blocked_subnets() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock); return m_blocked_subnets; }
+12
View File
@@ -520,6 +520,18 @@ namespace nodetool
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::clear_bans()
{
CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
const bool had_bans = !m_blocked_hosts.empty() || !m_blocked_subnets.empty();
m_blocked_hosts.clear();
m_blocked_subnets.clear();
if (had_bans)
MCLOG_CYAN(el::Level::Info, "global", "All bans cleared.");
return had_bans;
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::add_host_fail(const epee::net_utils::network_address &address, unsigned int score)
{
if(!address.is_blockable())
+6
View File
@@ -2419,6 +2419,12 @@ namespace cryptonote
{
epee::net_utils::network_address na;
if (!i->ban && i->host == "all")
{
m_p2p.clear_bans();
continue;
}
// try subnet first
if (!i->host.empty())
{
+10
View File
@@ -112,6 +112,16 @@ class BanTest():
res = daemon.get_bans()
assert 'bans' not in res or len(res.bans) == 0
daemon.set_bans([
{'host': '1.2.3.4', 'ban': True, 'seconds': 100},
{'host': '5.6.7.0/24', 'ban': True, 'seconds': 100},
])
res = daemon.get_bans()
assert len(res.bans) == 2
daemon.set_bans([{'host': 'all', 'ban': False}])
res = daemon.get_bans()
assert 'bans' not in res or len(res.bans) == 0
if __name__ == '__main__':
BanTest().run_test()
+16
View File
@@ -491,6 +491,22 @@ TEST(ban, limit)
ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
}
TEST(ban, clear)
{
test_core pr_core;
cryptonote::t_cryptonote_protocol_handler<test_core> cprotocol(pr_core, NULL);
Server server(cprotocol);
cprotocol.set_p2p_endpoint(&server);
ASSERT_FALSE(server.clear_bans());
ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
ASSERT_TRUE(server.block_subnet(MAKE_IPV4_SUBNET(5,6,7,0,24)));
ASSERT_TRUE(server.clear_bans());
ASSERT_TRUE(server.get_blocked_hosts().empty());
ASSERT_TRUE(server.get_blocked_subnets().empty());
ASSERT_FALSE(server.clear_bans());
}
TEST(ban, subnet)
{
time_t seconds;