diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 11eb4c29d4..704b290d9e 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -1855,7 +1855,7 @@ fn handle_whitelist( server .editable_settings_mut() .whitelist - .edit(server.data_dir().as_ref(), |w| w.push(uuid)); + .edit(server.data_dir().as_ref(), |w| w.insert(uuid)); server.notify_client( client, ChatType::CommandInfo @@ -1867,7 +1867,7 @@ fn handle_whitelist( server .editable_settings_mut() .whitelist - .edit(server.data_dir().as_ref(), |w| w.retain(|x| x != &uuid)); + .edit(server.data_dir().as_ref(), |w| w.remove(&uuid)); server.notify_client( client, ChatType::CommandInfo diff --git a/server/src/lib.rs b/server/src/lib.rs index a9a40b1ed3..e1ec27bab5 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -116,6 +116,9 @@ impl Server { data_dir: &std::path::Path, ) -> Result { info!("Server is data dir is: {}", data_dir.display()); + if settings.auth_server_address.is_none() { + info!("Authentication is disabled"); + } // persistence_db_dir is relative to data_dir let persistence_db_dir = data_dir.join(&settings.persistence_db_dir); diff --git a/server/src/login_provider.rs b/server/src/login_provider.rs index 1b061b1a1f..47c46698e1 100644 --- a/server/src/login_provider.rs +++ b/server/src/login_provider.rs @@ -1,7 +1,7 @@ use crate::settings::BanRecord; use authc::{AuthClient, AuthClientError, AuthToken, Uuid}; use common::msg::RegisterError; -use hashbrown::HashMap; +use hashbrown::{HashMap, HashSet}; use std::str::FromStr; use tracing::{error, info}; @@ -53,7 +53,8 @@ impl LoginProvider { pub fn try_login( &mut self, username_or_token: &str, - whitelist: &[Uuid], + admins: &[String], + whitelist: &HashSet, banlist: &HashMap, ) -> Result<(String, Uuid), RegisterError> { self @@ -69,7 +70,7 @@ impl LoginProvider { // user can only join if he is admin, the whitelist is empty (everyone can join) // or his name is in the whitelist - if !whitelist.is_empty() && !whitelist.contains(&uuid) { + if !whitelist.is_empty() && !whitelist.contains(&uuid) && !admins.contains(&username) { return Err(RegisterError::NotOnWhitelist); } diff --git a/server/src/persistence/mod.rs b/server/src/persistence/mod.rs index 2c01dc38ce..797b4eefb0 100644 --- a/server/src/persistence/mod.rs +++ b/server/src/persistence/mod.rs @@ -14,16 +14,11 @@ mod json_models; mod models; mod schema; -extern crate diesel; - use common::comp; use diesel::{connection::SimpleConnection, prelude::*}; use diesel_migrations::embed_migrations; -use std::{ - env, fs, - path::{Path, PathBuf}, -}; -use tracing::{info, warn}; +use std::{fs, path::Path}; +use tracing::info; /// A tuple of the components that are persisted to the DB for each character pub type PersistedComponents = (comp::Body, comp::Stats, comp::Inventory, comp::Loadout); @@ -49,7 +44,6 @@ impl std::io::Write for TracingOut { /// Runs any pending database migrations. This is executed during server startup pub fn run_migrations(db_dir: &Path) -> Result<(), diesel_migrations::RunMigrationsError> { - let db_dir = &apply_saves_dir_override(db_dir); let _ = fs::create_dir(format!("{}/", db_dir.display())); embedded_migrations::run_with_output( @@ -95,7 +89,6 @@ impl<'a> core::ops::Deref for VelorenTransaction<'a> { } pub fn establish_connection(db_dir: &Path) -> QueryResult { - let db_dir = &apply_saves_dir_override(db_dir); let database_url = format!("{}/db.sqlite", db_dir.display()); let connection = SqliteConnection::establish(&database_url) @@ -119,14 +112,3 @@ pub fn establish_connection(db_dir: &Path) -> QueryResult { Ok(VelorenConnection(connection)) } - -fn apply_saves_dir_override(db_dir: &Path) -> PathBuf { - if let Some(saves_dir) = env::var_os("VELOREN_SAVES_DIR") { - let path = PathBuf::from(&saves_dir); - if path.exists() || path.parent().map(|x| x.exists()).unwrap_or(false) { - return path; - } - warn!(?saves_dir, "VELOREN_SAVES_DIR points to an invalid path."); - } - db_dir.to_owned() -} diff --git a/server/src/settings.rs b/server/src/settings.rs index c0b1cdbaab..92a3be8ecd 100644 --- a/server/src/settings.rs +++ b/server/src/settings.rs @@ -3,7 +3,7 @@ mod editable; pub use editable::EditableSetting; use authc::Uuid; -use hashbrown::HashMap; +use hashbrown::{HashMap, HashSet}; use portpicker::pick_unused_port; use serde::{Deserialize, Serialize}; use std::{ @@ -39,7 +39,7 @@ pub struct Settings { /// uses the value of the file options to decide how to proceed. pub map_file: Option, /// Relative paths are relative to the server data dir - pub persistence_db_dir: String, + pub persistence_db_dir: PathBuf, pub max_view_distance: Option, pub banned_words_files: Vec, pub max_player_group_size: u32, @@ -97,6 +97,7 @@ impl Settings { } default_settings } + .apply_saves_dir_override() } fn save_to_file(&self, path: &Path) -> std::io::Result<()> { @@ -149,6 +150,18 @@ impl Settings { path.push(SETTINGS_FILENAME); path } + + fn apply_saves_dir_override(mut self) -> Self { + if let Some(saves_dir) = std::env::var_os("VELOREN_SAVES_DIR") { + let path = PathBuf::from(&saves_dir); + if path.exists() || path.parent().map(|x| x.exists()).unwrap_or(false) { + self.persistence_db_dir = path; + } else { + warn!(?saves_dir, "VELOREN_SAVES_DIR points to an invalid path."); + } + } + self + } } fn with_config_dir(path: &Path) -> PathBuf { @@ -165,7 +178,7 @@ pub struct BanRecord { #[derive(Deserialize, Serialize, Default)] #[serde(transparent)] -pub struct Whitelist(Vec); +pub struct Whitelist(HashSet); #[derive(Deserialize, Serialize, Default)] #[serde(transparent)] pub struct Banlist(HashMap); @@ -215,7 +228,7 @@ impl EditableSetting for ServerDescription { } impl Deref for Whitelist { - type Target = Vec; + type Target = HashSet; fn deref(&self) -> &Self::Target { &self.0 } } diff --git a/server/src/sys/message.rs b/server/src/sys/message.rs index fd2e3bdae5..6f13bfa00b 100644 --- a/server/src/sys/message.rs +++ b/server/src/sys/message.rs @@ -99,6 +99,7 @@ impl Sys { } => { let (username, uuid) = match login_provider.try_login( &token_or_username, + &settings.admins, &*editable_settings.whitelist, &*editable_settings.banlist, ) {