2019-01-21 18:45:59 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2019-12-31 18:35:41 +01:00
|
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
2019-01-21 18:45:59 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <banman.h>
|
|
|
|
|
|
|
|
#include <netaddress.h>
|
2020-07-01 15:18:55 +02:00
|
|
|
#include <node/ui_interface.h>
|
2022-01-31 08:55:33 +01:00
|
|
|
#include <sync.h>
|
2019-01-21 18:45:59 +01:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/time.h>
|
2022-03-24 05:13:51 +01:00
|
|
|
#include <util/translation.h>
|
2019-01-21 18:45:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time)
|
|
|
|
: m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time)
|
|
|
|
{
|
2022-03-24 05:13:51 +01:00
|
|
|
if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist...").translated);
|
2019-01-21 18:45:59 +01:00
|
|
|
|
|
|
|
int64_t n_start = GetTimeMillis();
|
2023-09-20 13:05:01 +02:00
|
|
|
if (m_ban_db.Read(m_banned)) {
|
2023-09-10 15:49:43 +02:00
|
|
|
SweepBanned(); // sweep out unused entries
|
2019-01-21 18:45:59 +01:00
|
|
|
|
2023-09-10 15:49:43 +02:00
|
|
|
LogPrint(BCLog::NET, "Loaded %d banned node addresses/subnets %dms\n", m_banned.size(),
|
|
|
|
GetTimeMillis() - n_start);
|
2019-01-21 18:45:59 +01:00
|
|
|
} else {
|
2023-09-10 15:49:43 +02:00
|
|
|
LogPrintf("Recreating the banlist database\n");
|
|
|
|
m_banned = {};
|
|
|
|
m_is_dirty = true;
|
2019-01-21 18:45:59 +01:00
|
|
|
}
|
2023-09-10 15:49:43 +02:00
|
|
|
|
|
|
|
DumpBanlist();
|
2019-01-21 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BanMan::~BanMan()
|
|
|
|
{
|
|
|
|
DumpBanlist();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BanMan::DumpBanlist()
|
|
|
|
{
|
2022-01-31 08:55:33 +01:00
|
|
|
static Mutex dump_mutex;
|
|
|
|
LOCK(dump_mutex);
|
2019-01-21 18:45:59 +01:00
|
|
|
|
|
|
|
banmap_t banmap;
|
2022-01-31 08:55:33 +01:00
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
SweepBanned();
|
|
|
|
if (!BannedSetIsDirty()) return;
|
|
|
|
banmap = m_banned;
|
2019-01-21 18:45:59 +01:00
|
|
|
SetBannedSetDirty(false);
|
|
|
|
}
|
|
|
|
|
2022-01-31 08:55:33 +01:00
|
|
|
int64_t n_start = GetTimeMillis();
|
|
|
|
if (!m_ban_db.Write(banmap)) {
|
|
|
|
SetBannedSetDirty(true);
|
|
|
|
}
|
|
|
|
|
2023-09-10 15:49:43 +02:00
|
|
|
LogPrint(BCLog::NET, "Flushed %d banned node addresses/subnets to disk %dms\n", banmap.size(),
|
|
|
|
GetTimeMillis() - n_start);
|
2019-01-21 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BanMan::ClearBanned()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
m_banned.clear();
|
|
|
|
m_is_dirty = true;
|
|
|
|
}
|
|
|
|
DumpBanlist(); //store banlist to disk
|
|
|
|
if (m_client_interface) m_client_interface->BannedListChanged();
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:23:45 +02:00
|
|
|
void BanMan::ClearDiscouraged()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
m_discouraged.reset();
|
|
|
|
m_is_dirty = true;
|
|
|
|
}
|
|
|
|
if (m_client_interface) m_client_interface->BannedListChanged();
|
|
|
|
}
|
|
|
|
|
2022-06-10 11:16:05 +02:00
|
|
|
bool BanMan::IsDiscouraged(const CNetAddr& net_addr)
|
2019-01-30 00:40:45 +01:00
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
2022-06-10 11:16:05 +02:00
|
|
|
return m_discouraged.contains(net_addr.GetAddrBytes());
|
2019-01-30 00:40:45 +01:00
|
|
|
}
|
|
|
|
|
2022-06-10 11:16:05 +02:00
|
|
|
bool BanMan::IsBanned(const CNetAddr& net_addr)
|
2019-01-21 18:45:59 +01:00
|
|
|
{
|
2019-01-30 00:40:45 +01:00
|
|
|
auto current_time = GetTime();
|
2019-01-21 18:45:59 +01:00
|
|
|
LOCK(m_cs_banned);
|
|
|
|
for (const auto& it : m_banned) {
|
|
|
|
CSubNet sub_net = it.first;
|
|
|
|
CBanEntry ban_entry = it.second;
|
|
|
|
|
2019-01-30 00:40:45 +01:00
|
|
|
if (current_time < ban_entry.nBanUntil && sub_net.Match(net_addr)) {
|
2019-01-21 18:45:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-10 11:16:05 +02:00
|
|
|
bool BanMan::IsBanned(const CSubNet& sub_net)
|
2019-01-21 18:45:59 +01:00
|
|
|
{
|
2019-01-30 00:40:45 +01:00
|
|
|
auto current_time = GetTime();
|
2019-01-21 18:45:59 +01:00
|
|
|
LOCK(m_cs_banned);
|
|
|
|
banmap_t::iterator i = m_banned.find(sub_net);
|
|
|
|
if (i != m_banned.end()) {
|
|
|
|
CBanEntry ban_entry = (*i).second;
|
2019-01-30 00:40:45 +01:00
|
|
|
if (current_time < ban_entry.nBanUntil) {
|
2019-01-21 18:45:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-10 11:16:05 +02:00
|
|
|
void BanMan::Ban(const CNetAddr& net_addr, int64_t ban_time_offset, bool since_unix_epoch)
|
2019-01-21 18:45:59 +01:00
|
|
|
{
|
|
|
|
CSubNet sub_net(net_addr);
|
2022-06-10 11:16:05 +02:00
|
|
|
Ban(sub_net, ban_time_offset, since_unix_epoch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BanMan::Discourage(const CNetAddr& net_addr)
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
m_discouraged.insert(net_addr.GetAddrBytes());
|
2019-01-21 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2022-06-10 11:16:05 +02:00
|
|
|
void BanMan::Ban(const CSubNet& sub_net, int64_t ban_time_offset, bool since_unix_epoch)
|
2019-01-21 18:45:59 +01:00
|
|
|
{
|
2022-06-10 11:16:05 +02:00
|
|
|
CBanEntry ban_entry(GetTime());
|
2019-01-21 18:45:59 +01:00
|
|
|
|
|
|
|
int64_t normalized_ban_time_offset = ban_time_offset;
|
|
|
|
bool normalized_since_unix_epoch = since_unix_epoch;
|
|
|
|
if (ban_time_offset <= 0) {
|
|
|
|
normalized_ban_time_offset = m_default_ban_time;
|
|
|
|
normalized_since_unix_epoch = false;
|
|
|
|
}
|
|
|
|
ban_entry.nBanUntil = (normalized_since_unix_epoch ? 0 : GetTime()) + normalized_ban_time_offset;
|
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
if (m_banned[sub_net].nBanUntil < ban_entry.nBanUntil) {
|
|
|
|
m_banned[sub_net] = ban_entry;
|
|
|
|
m_is_dirty = true;
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_client_interface) m_client_interface->BannedListChanged();
|
|
|
|
|
2022-06-10 11:16:05 +02:00
|
|
|
//store banlist to disk immediately
|
|
|
|
DumpBanlist();
|
2019-01-21 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BanMan::Unban(const CNetAddr& net_addr)
|
|
|
|
{
|
|
|
|
CSubNet sub_net(net_addr);
|
|
|
|
return Unban(sub_net);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BanMan::Unban(const CSubNet& sub_net)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
if (m_banned.erase(sub_net) == 0) return false;
|
|
|
|
m_is_dirty = true;
|
|
|
|
}
|
|
|
|
if (m_client_interface) m_client_interface->BannedListChanged();
|
|
|
|
DumpBanlist(); //store banlist to disk immediately
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BanMan::GetBanned(banmap_t& banmap)
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
// Sweep the banlist so expired bans are not returned
|
|
|
|
SweepBanned();
|
|
|
|
banmap = m_banned; //create a thread safe copy
|
|
|
|
}
|
|
|
|
|
|
|
|
void BanMan::SweepBanned()
|
|
|
|
{
|
|
|
|
int64_t now = GetTime();
|
|
|
|
bool notify_ui = false;
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
banmap_t::iterator it = m_banned.begin();
|
|
|
|
while (it != m_banned.end()) {
|
|
|
|
CSubNet sub_net = (*it).first;
|
|
|
|
CBanEntry ban_entry = (*it).second;
|
2020-10-15 11:22:52 +02:00
|
|
|
if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
|
2019-01-21 18:45:59 +01:00
|
|
|
m_banned.erase(it++);
|
|
|
|
m_is_dirty = true;
|
|
|
|
notify_ui = true;
|
2023-09-10 15:49:43 +02:00
|
|
|
LogPrint(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString());
|
2019-01-21 18:45:59 +01:00
|
|
|
} else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// update UI
|
|
|
|
if (notify_ui && m_client_interface) {
|
|
|
|
m_client_interface->BannedListChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BanMan::BannedSetIsDirty()
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned);
|
|
|
|
return m_is_dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BanMan::SetBannedSetDirty(bool dirty)
|
|
|
|
{
|
|
|
|
LOCK(m_cs_banned); //reuse m_banned lock for the m_is_dirty flag
|
|
|
|
m_is_dirty = dirty;
|
|
|
|
}
|