hunter: remove evil hosts from redis again

This commit is contained in:
Astro 2022-12-26 02:49:43 +01:00
parent ad8080d9cf
commit 21e670cd2c
3 changed files with 16 additions and 4 deletions

View File

@ -362,6 +362,12 @@ impl Store {
.await .await
} }
pub async fn remove_host(&mut self, host: &str) -> Result<(), RedisError> {
redis::Cmd::del(format!("h:{}", host))
.query_async::<_, ()>(self)
.await
}
pub async fn get_hosts(&mut self) -> Result<impl Stream<Item = String> + '_, RedisError> { pub async fn get_hosts(&mut self) -> Result<impl Stream<Item = String> + '_, RedisError> {
self.scan_prefix("h:") self.scan_prefix("h:")
.await .await

View File

@ -45,11 +45,15 @@ async fn main() {
cave::systemd::status("Loading known hosts from redis"); cave::systemd::status("Loading known hosts from redis");
let mut n = 1; let mut n = 1;
let hosts = store.get_hosts() let mut store_ = store.clone();
let hosts = store_.get_hosts()
.await.expect("get_hosts"); .await.expect("get_hosts");
pin_mut!(hosts); pin_mut!(hosts);
while let Some(host) = hosts.next().await { while let Some(host) = hosts.next().await {
scheduler.introduce(host).await; if scheduler.introduce(host.clone()).await == false {
log::debug!("Remove host {}", host);
store.remove_host(&host).await.expect("remove_host");
}
n += 1; n += 1;
if n > 1000 { if n > 1000 {

View File

@ -43,12 +43,12 @@ impl Scheduler {
self.queue.len() self.queue.len()
} }
pub async fn introduce(&mut self, host: String) { pub async fn introduce(&mut self, host: String) -> bool {
if EVIL_DOMAINS.iter().any(|domain| { if EVIL_DOMAINS.iter().any(|domain| {
let o = host.len().saturating_sub(domain.len()); let o = host.len().saturating_sub(domain.len());
&host[o..] == *domain &host[o..] == *domain
} ) { } ) {
return; return false;
} }
let now = Instant::now(); let now = Instant::now();
@ -61,6 +61,8 @@ impl Scheduler {
}); });
self.queue.insert(now, host); self.queue.insert(now, host);
} }
true
} }
pub fn reenqueue(&mut self, host: Host, new_post_ratio: Option<f64>, mean_interval: Option<Duration>) { pub fn reenqueue(&mut self, host: Host, new_post_ratio: Option<f64>, mean_interval: Option<Duration>) {