Skip to content

Commit

Permalink
fix: IP bans
Browse files Browse the repository at this point in the history
Have to check if there's no reversion, but also we should only be
checking for an IP ban in the event of no player ban
  • Loading branch information
chatasma committed Jun 27, 2024
1 parent 33ac800 commit 1887882
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/http/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub async fn prelogin(
let ip = hash_ip(&state, &data.ip);
let player_optional = Database::find_by_id(&state.database.players, &data.player.id).await;
if let Some(mut returning_player) = player_optional {
println!("the player was found!");
returning_player.name = data.player.name.clone();
returning_player.name_lower = returning_player.name.to_lowercase();
if !returning_player.ips.contains(&ip) {
Expand All @@ -38,33 +37,43 @@ pub async fn prelogin(

let mut puns : Vec<Punishment> = state.database.get_active_player_punishments(&returning_player).await;
let ban_pun_optional = puns.iter().find(|pun| pun.action.is_ban());
let mut ip_punishments : Vec<Punishment> = match state.database.punishments.find(doc! {
"targetIps": &ip,
"action.kind": PunishmentKind::IpBan.to_string()
}, None).await {
Ok(cursor) => Database::consume_cursor_into_owning_vec(cursor).await,
Err(_) => return Err(ApiErrorResponder::validation_error_with_message("Could not load punishments for player"))
let banned = {
let is_player_banned = ban_pun_optional.is_some();
if is_player_banned {
true
} else {
let mut ip_punishments : Vec<Punishment> = match state.database.punishments.find(doc! {
"targetIps": &ip,
"action.kind": PunishmentKind::IpBan.to_string()
}, None).await {
Ok(cursor) => Database::consume_cursor_into_owning_vec(cursor).await,
Err(_) => return Err(ApiErrorResponder::validation_error_with_message("Could not load punishments for player"))
};
let ip_ban = ip_punishments.first();
let is_ip_banned = match &ip_ban {
None => false,
Some(ip_ban_unwrapped) => ip_ban_unwrapped.is_active()
};
if is_ip_banned {
// move ip puns into main punishment vector
puns.append(&mut ip_punishments);
}
is_ip_banned
}
};
let ip_ban = ip_punishments.first();

let banned = ban_pun_optional.is_some() || ip_ban.is_some();

// move ip puns into main punishment vector
puns.append(&mut ip_punishments);

state.player_cache.set(&state.database, &returning_player.name, &returning_player, true).await;
state.database.ensure_player_name_uniqueness(&data.player.name, &data.player.id).await;

Ok(PlayerPreLoginResponder {
response: PlayerPreLoginResponse {
new: false,
allowed: !banned,
allowed: !banned,
player: returning_player,
active_punishments: puns
}
})
} else {
println!("Could not find player in database!");
println!("Could not find player {} in database!", player_id);
let time_millis : f64 = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as f64;
let player = Player {
id: data.player.id.clone(),
Expand Down

0 comments on commit 1887882

Please sign in to comment.