use crate::domain::repositories::geo_restriction_repository::GeoRestrictionRepository;
use crate::domain::services::geo_location::{is_ip_in_cidr, GeoLocationService};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::Arc;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TeamGeoRestrictions {
pub enable_geo_restrictions: bool,
pub allowed_countries: Option<Vec<String>>,
pub blocked_countries: Option<Vec<String>>,
pub ip_whitelist: Option<Vec<String>>,
pub domain_blacklist: Option<Vec<String>>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum GeoRestrictionResult {
Allowed,
Denied(String),
}
pub struct TeamService {
geolocation_service: Arc<dyn GeoLocationService>,
geo_restriction_repo: Arc<dyn GeoRestrictionRepository>,
}
impl TeamService {
pub fn new(
geolocation_service: Arc<dyn GeoLocationService>,
geo_restriction_repo: Arc<dyn GeoRestrictionRepository>,
) -> Self {
Self {
geolocation_service,
geo_restriction_repo,
}
}
pub async fn validate_geographic_restriction(
&self,
team_id: crate::domain::id::Id,
ip_address: &str,
restrictions: &TeamGeoRestrictions,
) -> Result<GeoRestrictionResult> {
if !restrictions.enable_geo_restrictions {
return Ok(GeoRestrictionResult::Allowed);
}
let ip = match IpAddr::from_str(ip_address) {
Ok(ip) => ip,
Err(_) => {
return Ok(GeoRestrictionResult::Denied(
"Invalid IP address format".to_string(),
))
}
};
if let Some(outcome) = whitelist_outcome(restrictions, &ip, ip_address, team_id) {
return Ok(outcome);
}
let country_code = self.get_country_code(team_id, ip_address, &ip).await?;
if let Some(outcome) =
country_list_outcome(restrictions, &country_code, ip_address, team_id)
{
return Ok(outcome);
}
log::info!(
"IP {} from country {} allowed for team {}",
ip_address,
country_code,
team_id
);
Ok(GeoRestrictionResult::Allowed)
}
async fn get_country_code(
&self,
team_id: crate::domain::id::Id,
ip_address: &str,
ip: &IpAddr,
) -> Result<String> {
let location = self
.geolocation_service
.get_location(ip)
.await
.map_err(|e| {
log::error!(
"Failed to get geolocation for IP {} for team {}: {}",
ip_address,
team_id,
e
);
anyhow::anyhow!("Unable to determine geographic location")
})?;
Ok(location.country_code.to_uppercase())
}
pub fn validate_domain_blacklist(
&self,
domain: &str,
restrictions: &TeamGeoRestrictions,
) -> Result<GeoRestrictionResult> {
match domain_blacklist_denial(domain, restrictions) {
Some(reason) => Ok(GeoRestrictionResult::Denied(reason)),
None => Ok(GeoRestrictionResult::Allowed),
}
}
pub async fn get_team_geo_restrictions(
&self,
team_id: crate::domain::id::Id,
) -> TeamGeoRestrictions {
match self
.geo_restriction_repo
.get_team_restrictions(team_id)
.await
{
Ok(restrictions) => {
log::debug!(
"Retrieved geo restrictions for team {}: {:?}",
team_id,
restrictions
);
restrictions
}
Err(e) => {
log::error!(
"Failed to get geo restrictions for team {}: {}. \
Falling back to deny-all configuration.",
team_id,
e
);
TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(Vec::new()),
blocked_countries: None,
ip_whitelist: None,
domain_blacklist: None,
}
}
}
}
}
fn whitelist_outcome(
restrictions: &TeamGeoRestrictions,
ip: &IpAddr,
ip_address: &str,
team_id: crate::domain::id::Id,
) -> Option<GeoRestrictionResult> {
if let Some(ref whitelist) = restrictions.ip_whitelist {
for cidr in whitelist {
if is_ip_in_cidr(ip, cidr) {
log::info!(
"IP {} allowed by whitelist (CIDR: {}) for team {}",
ip_address,
cidr,
team_id
);
return Some(GeoRestrictionResult::Allowed);
}
}
}
None
}
fn country_list_outcome(
restrictions: &TeamGeoRestrictions,
country_code: &str,
ip_address: &str,
team_id: crate::domain::id::Id,
) -> Option<GeoRestrictionResult> {
if let Some(ref blocked) = restrictions.blocked_countries {
if blocked
.iter()
.any(|code| code.to_uppercase() == country_code)
{
log::warn!(
"IP {} from country {} blocked for team {} (blocked countries: {:?})",
ip_address,
country_code,
team_id,
blocked
);
return Some(GeoRestrictionResult::Denied(format!(
"Access from country {} is not allowed",
country_code
)));
}
}
if let Some(ref allowed) = restrictions.allowed_countries {
if !allowed
.iter()
.any(|code| code.to_uppercase() == country_code)
{
log::warn!(
"IP {} from country {} not in allowed list for team {} (allowed countries: {:?})",
ip_address,
country_code,
team_id,
allowed
);
return Some(GeoRestrictionResult::Denied(format!(
"Access from country {} is not allowed",
country_code
)));
}
}
None
}
pub const MAX_DOMAIN_BLACKLIST_ENTRIES: usize = 1000;
pub const MAX_DOMAIN_BLACKLIST_ENTRY_LEN: usize = 253;
impl TeamGeoRestrictions {
pub fn effective_domain_blacklist(&self) -> Result<Vec<String>, String> {
let list = self.domain_blacklist.as_deref().unwrap_or(&[]);
if list.len() > MAX_DOMAIN_BLACKLIST_ENTRIES {
return Err(format!(
"domain_blacklist has {} entries; maximum is {}",
list.len(),
MAX_DOMAIN_BLACKLIST_ENTRIES
));
}
for entry in list {
if entry.is_empty() || entry.len() > MAX_DOMAIN_BLACKLIST_ENTRY_LEN {
return Err(format!(
"domain_blacklist entry must be 1-{} bytes: {}",
MAX_DOMAIN_BLACKLIST_ENTRY_LEN, entry
));
}
let normalized = normalize_host(entry);
if normalized.is_empty() {
return Err(format!(
"domain_blacklist entry cannot be normalized to a host: {entry}"
));
}
if normalized.contains('*') {
return Err(format!(
"domain_blacklist wildcard entries are not supported: {entry}"
));
}
}
if !self.enable_geo_restrictions {
return Ok(Vec::new());
}
Ok(normalize_denylist_entries(list))
}
}
pub fn normalize_denylist_entries(list: &[String]) -> Vec<String> {
let mut out = Vec::with_capacity(list.len());
for entry in list {
let normalized = normalize_host(entry);
if normalized.is_empty() || out.contains(&normalized) {
continue;
}
out.push(normalized);
}
out
}
pub fn host_in_denylist(host: &str, normalized_entries: &[String]) -> bool {
normalized_entries.iter().any(|entry| {
host == entry
|| (host.len() > entry.len() + 1
&& host.ends_with(entry.as_str())
&& host.as_bytes()[host.len() - entry.len() - 1] == b'.')
})
}
pub fn domain_in_normalized_denylist(
domain_or_url: &str,
normalized_entries: &[String],
) -> Option<String> {
if normalized_entries.is_empty() {
return None;
}
let host = normalize_host(domain_or_url);
if host_in_denylist(&host, normalized_entries) {
Some(format!("Domain {host} is in the blacklist"))
} else {
None
}
}
pub fn domain_blacklist_denial(domain: &str, restrictions: &TeamGeoRestrictions) -> Option<String> {
if !restrictions.enable_geo_restrictions {
return None;
}
let blacklist = restrictions.domain_blacklist.as_deref().unwrap_or(&[]);
domain_in_denylist(domain, blacklist)
}
pub fn domain_in_denylist(domain_or_url: &str, blacklist: &[String]) -> Option<String> {
let normalized = normalize_denylist_entries(blacklist);
domain_in_normalized_denylist(domain_or_url, &normalized)
}
pub(crate) fn normalize_host(input: &str) -> String {
let candidate = input.trim();
if let Ok(parsed) = url::Url::parse(candidate) {
if let Some(host) = parsed.host_str() {
return host.trim_end_matches('.').to_ascii_lowercase();
}
}
if let Ok(parsed) = url::Url::parse(&format!("http://{candidate}")) {
if let Some(host) = parsed.host_str() {
return host.trim_end_matches('.').to_ascii_lowercase();
}
}
let after_scheme = candidate.split("://").last().unwrap_or(candidate);
let host_with_port = after_scheme
.split(['/', '?', '#'])
.next()
.unwrap_or(after_scheme);
let host_with_port = host_with_port.rsplit('@').next().unwrap_or(host_with_port);
let host = if host_with_port.starts_with('[') {
host_with_port
} else {
host_with_port.split(':').next().unwrap_or(host_with_port)
};
host.trim_end_matches('.').to_ascii_lowercase()
}
#[cfg(test)]
#[path = "team_service_test.rs"]
mod tests;