-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore!: refactor ip filters and also fix some docs
- Loading branch information
Showing
5 changed files
with
69 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,55 @@ | ||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; | ||
use std::net::IpAddr; | ||
|
||
pub type Ipv4Filter<'a> = Box<dyn Fn(&Ipv4Addr) -> bool + Send + Sync + 'a>; | ||
pub type Ipv6Filter<'a> = Box<dyn Fn(&Ipv6Addr) -> bool + Send + Sync + 'a>; | ||
pub type IpFilter<'a> = Box<dyn Fn(&IpAddr, &IpAddr) -> bool + Send + Sync + 'a>; | ||
|
||
pub struct Filters<'a> { | ||
ipv4_filters: Vec<Ipv4Filter<'a>>, | ||
ipv6_filters: Vec<Ipv6Filter<'a>>, | ||
pub struct IpFilters<'a> { | ||
filters: Vec<IpFilter<'a>>, | ||
} | ||
|
||
impl<'a> Default for Filters<'a> { | ||
impl<'a> Default for IpFilters<'a> { | ||
fn default() -> Self { | ||
Self::new( | ||
vec![ | ||
Box::new(|v4| !v4.is_broadcast()), | ||
Box::new(|v4| !v4.is_multicast()), | ||
Box::new(|v4| !v4.is_unspecified()), | ||
], | ||
vec![ | ||
Box::new(|v6| !v6.is_multicast()), | ||
Box::new(|v6| !v6.is_unspecified()), | ||
], | ||
) | ||
Self::new(vec![]) | ||
} | ||
} | ||
|
||
impl<'a> Filters<'a> { | ||
pub fn new(ipv4_filters: Vec<Ipv4Filter<'a>>, ipv6_filters: Vec<Ipv6Filter<'a>>) -> Self { | ||
Self { | ||
ipv4_filters, | ||
ipv6_filters, | ||
} | ||
impl<'a> IpFilters<'a> { | ||
pub fn new(filters: Vec<IpFilter<'a>>) -> Self { | ||
Self { filters } | ||
} | ||
|
||
pub fn is_allowed(&self, addr: &IpAddr) -> bool { | ||
match addr { | ||
IpAddr::V4(v4) => { | ||
for filter in &self.ipv4_filters { | ||
if !filter(v4) { | ||
return false; | ||
} | ||
} | ||
} | ||
IpAddr::V6(v6) => { | ||
for filter in &self.ipv6_filters { | ||
if !filter(v6) { | ||
return false; | ||
pub fn with_non_broadcast() -> Self { | ||
Self::new(vec![Box::new(|src, dst| { | ||
macro_rules! non_broadcast { | ||
($addr:expr) => { | ||
match $addr { | ||
IpAddr::V4(v4) => { | ||
!(v4.is_broadcast() || v4.is_multicast() || v4.is_multicast()) | ||
} | ||
IpAddr::V6(v6) => !(v6.is_multicast() || v6.is_unspecified()), | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
true | ||
non_broadcast!(src) && non_broadcast!(dst) | ||
})]) | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn add_v4(&mut self, filter: Ipv4Filter<'a>) { | ||
self.ipv4_filters.push(filter); | ||
pub fn add(&mut self, filter: IpFilter<'a>) { | ||
self.filters.push(filter); | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn add_v6(&mut self, filter: Ipv6Filter<'a>) { | ||
self.ipv6_filters.push(filter); | ||
pub fn add_fn<F>(&mut self, filter: F) | ||
where | ||
F: Fn(&IpAddr, &IpAddr) -> bool + Send + Sync + 'a, | ||
{ | ||
self.filters.push(Box::new(filter)); | ||
} | ||
|
||
pub fn is_allowed(&self, src: &IpAddr, dst: &IpAddr) -> bool { | ||
for filter in &self.filters { | ||
if !filter(src, dst) { | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters