Skip to content

Commit

Permalink
banning domains
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Dec 19, 2024
1 parent 34f19a7 commit 380ee07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
.env.local
banned_domains.txt
banned_users.txt
35 changes: 31 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,44 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}

fn banned_domains() -> Vec<String> {
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<String> {
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<AppState>) -> Result<Redirect, AppError> {
let redirect_url = format!(
Expand Down Expand Up @@ -265,7 +292,7 @@ async fn onchain_handler(
headers: HeaderMap,
Json(payload): Json<OnchainRequest>,
) -> Result<Json<OnchainResponse>, AppError> {
if get_banned_users().contains(&user.username) {
if is_banned(&user) {
return Err(AppError::new("You are banned"));
}

Expand Down Expand Up @@ -299,7 +326,7 @@ async fn lightning_handler(
headers: HeaderMap,
Json(payload): Json<LightningRequest>,
) -> Result<Json<LightningResponse>, AppError> {
if get_banned_users().contains(&user.username) {
if is_banned(&user) {
return Err(AppError::new("You are banned"));
}

Expand Down Expand Up @@ -385,7 +412,7 @@ async fn channel_handler(
headers: HeaderMap,
Json(payload): Json<ChannelRequest>,
) -> Result<Json<ChannelResponse>, AppError> {
if get_banned_users().contains(&user.username) {
if is_banned(&user) {
return Err(AppError::new("You are banned"));
}

Expand Down

0 comments on commit 380ee07

Please sign in to comment.