Clippy + small tweaks
This commit is contained in:
parent
03c63986d1
commit
8ea5dfae87
2 changed files with 9 additions and 15 deletions
|
|
@ -3,6 +3,7 @@ use async_compression::tokio::bufread::GzipDecoder;
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use intervaltree::IntervalTree;
|
use intervaltree::IntervalTree;
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||||
|
use std::path::Path;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio::io::{AsyncBufRead, AsyncRead};
|
use tokio::io::{AsyncBufRead, AsyncRead};
|
||||||
|
|
@ -19,7 +20,7 @@ pub struct Blocklist {
|
||||||
|
|
||||||
impl Blocklist {
|
impl Blocklist {
|
||||||
pub fn empty() -> Self {
|
pub fn empty() -> Self {
|
||||||
return Self::new(std::iter::empty());
|
Self::new(std::iter::empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(ip_ranges: impl IntoIterator<Item = std::ops::Range<IpAddr>>) -> Self {
|
pub fn new(ip_ranges: impl IntoIterator<Item = std::ops::Range<IpAddr>>) -> Self {
|
||||||
|
|
@ -34,25 +35,18 @@ impl Blocklist {
|
||||||
if parsed_url.scheme() == "file" {
|
if parsed_url.scheme() == "file" {
|
||||||
let path = parsed_url
|
let path = parsed_url
|
||||||
.to_file_path()
|
.to_file_path()
|
||||||
.map_err(|_| anyhow::anyhow!("Failed to convert file URL to path"))?;
|
.ok()
|
||||||
return Self::load_from_file(path.to_str().unwrap()).await;
|
.context("failed to convert file URL to path")?;
|
||||||
|
return Self::load_from_file(path).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = reqwest::get(parsed_url)
|
let response = reqwest::get(parsed_url)
|
||||||
.await
|
.await
|
||||||
.context("Failed to send request for blocklist")?;
|
.context("Failed to send request for blocklist")?;
|
||||||
if response.status() != 200 {
|
if !response.status().is_success() {
|
||||||
anyhow::bail!("Failed to fetch blocklist: HTTP {}", response.status());
|
anyhow::bail!("Failed to fetch blocklist: HTTP {}", response.status());
|
||||||
}
|
}
|
||||||
|
|
||||||
let content_length = response
|
|
||||||
.content_length()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Failed to get content length"))?;
|
|
||||||
|
|
||||||
if content_length < 2 {
|
|
||||||
anyhow::bail!("Content too short: not enough data to determine compression");
|
|
||||||
}
|
|
||||||
|
|
||||||
let reader = StreamReader::new(
|
let reader = StreamReader::new(
|
||||||
response
|
response
|
||||||
.bytes_stream()
|
.bytes_stream()
|
||||||
|
|
@ -61,7 +55,7 @@ impl Blocklist {
|
||||||
Self::create_from_stream(reader).await
|
Self::create_from_stream(reader).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn load_from_file(path: &str) -> Result<Self> {
|
pub async fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||||
let file = tokio::fs::File::open(path).await?;
|
let file = tokio::fs::File::open(path).await?;
|
||||||
let reader = tokio::io::BufReader::new(file);
|
let reader = tokio::io::BufReader::new(file);
|
||||||
Self::create_from_stream(reader).await
|
Self::create_from_stream(reader).await
|
||||||
|
|
@ -97,7 +91,7 @@ impl Blocklist {
|
||||||
let mut ip_ranges: Vec<std::ops::Range<IpAddr>> = Vec::new();
|
let mut ip_ranges: Vec<std::ops::Range<IpAddr>> = Vec::new();
|
||||||
while reader.read_line(&mut line).await? > 0 {
|
while reader.read_line(&mut line).await? > 0 {
|
||||||
if let Some((start_ip, end_ip)) = parse_ip_range(&line) {
|
if let Some((start_ip, end_ip)) = parse_ip_range(&line) {
|
||||||
let range = start_ip..(increment_ip(end_ip));
|
let range = start_ip..increment_ip(end_ip);
|
||||||
ip_ranges.push(range);
|
ip_ranges.push(range);
|
||||||
}
|
}
|
||||||
line.clear();
|
line.clear();
|
||||||
|
|
|
||||||
|
|
@ -942,7 +942,7 @@ struct PeerHandler {
|
||||||
first_message_received: AtomicBool,
|
first_message_received: AtomicBool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PeerConnectionHandler for &'a PeerHandler {
|
impl PeerConnectionHandler for &PeerHandler {
|
||||||
fn on_connected(&self, connection_time: Duration) {
|
fn on_connected(&self, connection_time: Duration) {
|
||||||
self.counters
|
self.counters
|
||||||
.outgoing_connections
|
.outgoing_connections
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue