From 380ee07059656a63731d536e0bebe8e99cbd7e2f Mon Sep 17 00:00:00 2001 From: benthecarman Date: Thu, 19 Dec 2024 11:39:33 -0600 Subject: [PATCH] banning domains --- .gitignore | 2 ++ src/main.rs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 5f6d1fa..507e6c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target .env.local +banned_domains.txt +banned_users.txt diff --git a/src/main.rs b/src/main.rs index 84d76bf..30c9086 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,17 +161,44 @@ async fn main() -> anyhow::Result<()> { Ok(()) } +fn banned_domains() -> Vec { + let mut domains = vec![]; + let file = std::fs::read_to_string("banned_domains.txt"); + if let Ok(file) = file { + for line in file.lines() { + let line = line.trim(); + if !line.is_empty() { + domains.push(line.to_string()); + } + } + } + domains +} + fn get_banned_users() -> Vec { let mut banned_users = vec![]; let file = std::fs::read_to_string("banned_users.txt"); if let Ok(file) = file { for line in file.lines() { - banned_users.push(line.to_string()); + let line = line.trim(); + if !line.is_empty() { + banned_users.push(line.to_string()); + } } } banned_users } +fn is_banned(user: &AuthUser) -> bool { + let domains = banned_domains(); + let user_host = user.username.split('@').next().unwrap_or(""); + if domains.contains(&user_host.to_lowercase()) { + return true; + } + let banned_users = get_banned_users(); + banned_users.contains(&user.username) +} + #[axum::debug_handler] async fn github_auth(Extension(state): Extension) -> Result { let redirect_url = format!( @@ -265,7 +292,7 @@ async fn onchain_handler( headers: HeaderMap, Json(payload): Json, ) -> Result, AppError> { - if get_banned_users().contains(&user.username) { + if is_banned(&user) { return Err(AppError::new("You are banned")); } @@ -299,7 +326,7 @@ async fn lightning_handler( headers: HeaderMap, Json(payload): Json, ) -> Result, AppError> { - if get_banned_users().contains(&user.username) { + if is_banned(&user) { return Err(AppError::new("You are banned")); } @@ -385,7 +412,7 @@ async fn channel_handler( headers: HeaderMap, Json(payload): Json, ) -> Result, AppError> { - if get_banned_users().contains(&user.username) { + if is_banned(&user) { return Err(AppError::new("You are banned")); }