net: add blacklist field to settings and avoid for duration of program.

This commit is contained in:
draoi
2024-04-02 10:28:42 +02:00
parent b24cde844c
commit 84dcc54433
4 changed files with 20 additions and 9 deletions

View File

@@ -126,7 +126,8 @@ impl Acceptor {
.hosts()
.container
.contains(HostColor::Black as usize, &url)
.await
.await ||
self.session.upgrade().unwrap().p2p().settings().blacklist.contains(&url)
{
warn!(target: "net::acceptor::run_accept_loop()", "Peer {} is blacklisted", url);
continue

View File

@@ -54,7 +54,8 @@ impl Connector {
.hosts()
.container
.contains(HostColor::Black as usize, url)
.await
.await ||
self.session.upgrade().unwrap().p2p().settings().blacklist.contains(url)
{
warn!(target: "net::connector::connect", "Peer {} is blacklisted", url);
return Err(Error::ConnectFailed)

View File

@@ -303,7 +303,9 @@ impl HostContainer {
HostColor::try_from(color).unwrap());
let mut list = self.hostlists[color].write().await;
list.push((addr, last_seen));
list.push((addr.clone(), last_seen));
debug!(target: "net::hosts::store()", "Added [{}] to {:?} list",
addr, HostColor::try_from(color).unwrap());
if color == 0 && list.len() == GREYLIST_MAX_LEN {
let last_entry = list.pop().unwrap();
@@ -928,7 +930,6 @@ impl Hosts {
/// process fails, or when a channel stops. Prevents hosts from getting trapped in the
/// HostState logical machinery.
pub async fn unregister(&self, addr: &Url) {
debug!(target: "net::hosts::unregister()", "Removing {} from HostRegistry", addr);
self.registry.write().await.remove(addr);
debug!(target: "net::hosts::unregister()", "Removed {} from HostRegistry", addr);
}
@@ -1069,7 +1070,9 @@ impl Hosts {
}
// Blacklist peers should never enter the hostlist.
if self.container.contains(HostColor::Black as usize, addr_).await {
if self.container.contains(HostColor::Black as usize, addr_).await ||
settings.blacklist.contains(addr_)
{
warn!(target: "net::hosts::filter_addresses()",
"[{}] is blacklisted", addr_);
continue
@@ -1140,8 +1143,6 @@ impl Hosts {
if !settings.allowed_transports.contains(&addr_.scheme().to_string()) {
self.container.store_or_update(HostColor::Dark, addr_.clone(), *last_seen).await;
debug!(target: "net::hosts::filter_addresses()",
"Added unsupported peer {} to Dark list", addr_);
continue
}
@@ -1152,8 +1153,7 @@ impl Hosts {
self.container.contains(HostColor::White as usize, addr_).await ||
self.container.contains(HostColor::Grey as usize, addr_).await
{
debug!(target: "net::hosts::filter_addresses()",
"We already have {} in the hostlist. Skipping", addr_);
debug!(target: "net::hosts::filter_addresses()", "[{}] exists! Skipping", addr_);
continue
}

View File

@@ -77,6 +77,8 @@ pub struct Settings {
/// Number of seconds with no connections after which refinery
/// process is paused.
pub time_with_no_connections: u64,
/// Nodes to avoid interacting with for the duration of the program.
pub blacklist: Vec<Url>,
}
impl Default for Settings {
@@ -107,6 +109,7 @@ impl Default for Settings {
white_connect_count: 90,
anchor_connect_count: 2,
time_with_no_connections: 30,
blacklist: vec![],
}
}
}
@@ -213,6 +216,11 @@ pub struct SettingsOpt {
/// process is paused.
#[structopt(skip)]
pub time_with_no_connections: Option<u64>,
/// Nodes to avoid interacting with for the duration of the program.
#[serde(default)]
#[structopt(long)]
pub blacklist: Vec<Url>,
}
impl From<SettingsOpt> for Settings {
@@ -256,6 +264,7 @@ impl From<SettingsOpt> for Settings {
time_with_no_connections: opt
.time_with_no_connections
.unwrap_or(def.time_with_no_connections),
blacklist: opt.blacklist,
}
}
}