Merge branch 'fix-geolocation-after-refactor' into 'main'

Fix geolocation feature after recent refactor

See merge request veilid/veilid!355
This commit is contained in:
Christien Rioux 2025-02-26 14:24:39 +00:00
commit 8de19e0d26
3 changed files with 84 additions and 119 deletions

View File

@ -235,6 +235,11 @@ impl RouteSpecStore {
.relay_node(RoutingDomain::PublicInternet)
.map(|nr| nr.locked(rti));
#[cfg(feature = "geolocation")]
let country_code_denylist = self
.config()
.with(|config| config.network.privacy.country_code_denylist.clone());
// Get list of all nodes, and sort them for selection
let cur_ts = Timestamp::now();
let filter = Box::new(
@ -277,17 +282,6 @@ impl RouteSpecStore {
// Exclude nodes from blacklisted countries
#[cfg(feature = "geolocation")]
{
let country_code_denylist = self
.unlocked_inner
.routing_table
.config
.get()
.network
.privacy
.country_code_denylist
.clone();
if !country_code_denylist.is_empty() {
let geolocation_info =
sni.get_geolocation_info(RoutingDomain::PublicInternet);
@ -350,7 +344,6 @@ impl RouteSpecStore {
return false;
}
}
}
// Exclude nodes on our same ipblock, or their relay is on our same ipblock
// or our relay is on their ipblock, or their relay is on our relays same ipblock

View File

@ -239,7 +239,7 @@ pub fn fix_veilidconfiginner() -> VeilidConfigInner {
},
#[cfg(feature = "geolocation")]
privacy: VeilidConfigPrivacy {
country_code_denylist: vec![CountryCode([b'N', b'Z'])],
country_code_denylist: vec![CountryCode::from_str("NZ").unwrap()],
},
#[cfg(feature = "virtual-network")]
virtual_network: VeilidConfigVirtualNetwork {

View File

@ -1,17 +1,12 @@
use super::*;
use std::hash::{Hash, Hasher};
/// Two-letter country code. Case-insensitive when comparing.
#[derive(Copy, Default, Clone, Ord, Eq, Serialize, Deserialize, JsonSchema)]
/// Two-letter country code. Stored in upper case internally.
#[derive(
Copy, Default, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize, JsonSchema,
)]
#[serde(try_from = "String")]
#[serde(into = "String")]
pub struct CountryCode(pub [u8; 2]);
impl From<[u8; 2]> for CountryCode {
fn from(b: [u8; 2]) -> Self {
Self(b)
}
}
pub struct CountryCode([u8; 2]);
impl From<CountryCode> for String {
fn from(u: CountryCode) -> Self {
@ -22,7 +17,18 @@ impl From<CountryCode> for String {
impl TryFrom<&[u8]> for CountryCode {
type Error = VeilidAPIError;
fn try_from(b: &[u8]) -> Result<Self, Self::Error> {
Ok(Self(b.try_into().map_err(VeilidAPIError::generic)?))
let cc: [u8; 2] = b.try_into().map_err(VeilidAPIError::generic)?;
if !cc[0].is_ascii_alphabetic() || !cc[1].is_ascii_alphabetic() {
return Err(VeilidAPIError::generic(
"country code must only contain alphabetic chars",
));
}
Ok(Self([
cc[0].to_ascii_uppercase(),
cc[1].to_ascii_uppercase(),
]))
}
}
@ -35,55 +41,21 @@ impl TryFrom<String> for CountryCode {
impl fmt::Display for CountryCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{}", String::from_utf8_lossy(&self.0))
// self.0 is guaranteed to be a valid ASCII string, checked in Self::try_from(&[u8])
write!(f, "{}{}", self.0[0] as char, self.0[1] as char)
}
}
impl fmt::Debug for CountryCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{}", String::from_utf8_lossy(&self.0))
<Self as fmt::Display>::fmt(self, f)
}
}
impl FromStr for CountryCode {
type Err = VeilidAPIError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(
s.as_bytes().try_into().map_err(VeilidAPIError::generic)?,
))
}
}
impl Hash for CountryCode {
fn hash<H: Hasher>(&self, state: &mut H) {
let this = [
self.0[0].to_ascii_uppercase(),
self.0[1].to_ascii_uppercase(),
];
state.write(&this[..]);
}
}
impl PartialEq for CountryCode {
fn eq(&self, other: &Self) -> bool {
self.0[0].to_ascii_uppercase() == other.0[0].to_ascii_uppercase()
&& self.0[1].to_ascii_uppercase() == other.0[1].to_ascii_uppercase()
}
}
impl PartialOrd for CountryCode {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
let this = [
self.0[0].to_ascii_uppercase(),
self.0[1].to_ascii_uppercase(),
];
let other = [
other.0[0].to_ascii_uppercase(),
other.0[1].to_ascii_uppercase(),
];
this.partial_cmp(&other)
Ok(Self::try_from(s.as_bytes())?)
}
}