mirror of
https://github.com/zyllian/classics.git
synced 2025-01-18 03:32:41 -08:00
add per-user server passwords
This commit is contained in:
parent
38164a6cc5
commit
d56bf8b7e4
2 changed files with 34 additions and 6 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::level::generation::LevelGeneration;
|
use crate::level::generation::LevelGeneration;
|
||||||
|
@ -9,8 +11,9 @@ pub struct ServerConfig {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
/// the server's motd
|
/// the server's motd
|
||||||
pub motd: String,
|
pub motd: String,
|
||||||
/// the server's password, if any
|
/// the server's protection mode
|
||||||
pub password: Option<String>,
|
#[serde(rename = "password")]
|
||||||
|
pub protection_mode: ServerProtectionMode,
|
||||||
/// the level's size
|
/// the level's size
|
||||||
pub level_size: Option<ConfigCoordinates>,
|
pub level_size: Option<ConfigCoordinates>,
|
||||||
/// the level's spawn point
|
/// the level's spawn point
|
||||||
|
@ -24,7 +27,7 @@ impl Default for ServerConfig {
|
||||||
Self {
|
Self {
|
||||||
name: "classic server wowie".to_string(),
|
name: "classic server wowie".to_string(),
|
||||||
motd: "here's the default server motd".to_string(),
|
motd: "here's the default server motd".to_string(),
|
||||||
password: None,
|
protection_mode: ServerProtectionMode::None,
|
||||||
level_size: None,
|
level_size: None,
|
||||||
spawn: None,
|
spawn: None,
|
||||||
generation: LevelGeneration::Empty,
|
generation: LevelGeneration::Empty,
|
||||||
|
@ -42,3 +45,15 @@ pub struct ConfigCoordinates {
|
||||||
/// the Z coordinate
|
/// the Z coordinate
|
||||||
pub z: usize,
|
pub z: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// enum for the different kinds of server protection
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum ServerProtectionMode {
|
||||||
|
/// the server is unprotected and anyone can join with any username
|
||||||
|
None,
|
||||||
|
/// the server requires a password to join, but you can use any username if you know the password
|
||||||
|
Password(String),
|
||||||
|
/// the server requires a password to join and the password is checked against each username
|
||||||
|
PasswordsByUser(BTreeMap<String, String>),
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ use crate::{
|
||||||
client::ClientPacket, server::ServerPacket, PacketReader, PacketWriter, ARRAY_LENGTH,
|
client::ClientPacket, server::ServerPacket, PacketReader, PacketWriter, ARRAY_LENGTH,
|
||||||
},
|
},
|
||||||
player::{Player, PlayerType},
|
player::{Player, PlayerType},
|
||||||
|
server::config::ServerProtectionMode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::ServerData;
|
use super::ServerData;
|
||||||
|
@ -113,9 +114,21 @@ async fn handle_stream_inner(
|
||||||
|
|
||||||
let mut data = data.write().await;
|
let mut data = data.write().await;
|
||||||
|
|
||||||
if let Some(password) = &data.config.password {
|
match &data.config.protection_mode {
|
||||||
if verification_key != *password {
|
ServerProtectionMode::None => {}
|
||||||
return Ok(Some("Incorrect password!".to_string()));
|
ServerProtectionMode::Password(password) => {
|
||||||
|
if verification_key != *password {
|
||||||
|
return Ok(Some("Incorrect password!".to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ServerProtectionMode::PasswordsByUser(passwords) => {
|
||||||
|
if !passwords
|
||||||
|
.get(&username)
|
||||||
|
.map(|password| verification_key == *password)
|
||||||
|
.unwrap_or_default()
|
||||||
|
{
|
||||||
|
return Ok(Some("Incorrect password!".to_string()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue