Skip to content

Commit f3cbfb4

Browse files
committed
fix: IP bans
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
1 parent 33ac800 commit f3cbfb4

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/http/player/mod.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub async fn prelogin(
2929
let ip = hash_ip(&state, &data.ip);
3030
let player_optional = Database::find_by_id(&state.database.players, &data.player.id).await;
3131
if let Some(mut returning_player) = player_optional {
32-
println!("the player was found!");
3332
returning_player.name = data.player.name.clone();
3433
returning_player.name_lower = returning_player.name.to_lowercase();
3534
if !returning_player.ips.contains(&ip) {
@@ -38,33 +37,40 @@ pub async fn prelogin(
3837

3938
let mut puns : Vec<Punishment> = state.database.get_active_player_punishments(&returning_player).await;
4039
let ban_pun_optional = puns.iter().find(|pun| pun.action.is_ban());
41-
let mut ip_punishments : Vec<Punishment> = match state.database.punishments.find(doc! {
42-
"targetIps": &ip,
43-
"action.kind": PunishmentKind::IpBan.to_string()
44-
}, None).await {
45-
Ok(cursor) => Database::consume_cursor_into_owning_vec(cursor).await,
46-
Err(_) => return Err(ApiErrorResponder::validation_error_with_message("Could not load punishments for player"))
40+
let banned = {
41+
let is_player_banned = ban_pun_optional.is_some();
42+
if is_player_banned {
43+
true
44+
} else {
45+
let mut ip_punishments : Vec<Punishment> = match state.database.punishments.find(doc! {
46+
"targetIps": &ip,
47+
"action.kind": PunishmentKind::IpBan.to_string()
48+
}, None).await {
49+
Ok(cursor) => Database::consume_cursor_into_owning_vec(cursor).await,
50+
Err(_) => return Err(ApiErrorResponder::validation_error_with_message("Could not load punishments for player"))
51+
};
52+
ip_punishments.retain(|p| p.is_active());
53+
let is_ip_banned = !ip_punishments.is_empty();
54+
if is_ip_banned {
55+
// move ip puns into main punishment vector
56+
puns.append(&mut ip_punishments);
57+
}
58+
is_ip_banned
59+
}
4760
};
48-
let ip_ban = ip_punishments.first();
49-
50-
let banned = ban_pun_optional.is_some() || ip_ban.is_some();
51-
52-
// move ip puns into main punishment vector
53-
puns.append(&mut ip_punishments);
54-
5561
state.player_cache.set(&state.database, &returning_player.name, &returning_player, true).await;
5662
state.database.ensure_player_name_uniqueness(&data.player.name, &data.player.id).await;
5763

5864
Ok(PlayerPreLoginResponder {
5965
response: PlayerPreLoginResponse {
6066
new: false,
61-
allowed: !banned,
67+
allowed: !banned,
6268
player: returning_player,
6369
active_punishments: puns
6470
}
6571
})
6672
} else {
67-
println!("Could not find player in database!");
73+
println!("Could not find player {} in database!", player_id);
6874
let time_millis : f64 = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as f64;
6975
let player = Player {
7076
id: data.player.id.clone(),

0 commit comments

Comments
 (0)