diff --git a/src/main.rs b/src/main.rs index 054a192..6078d51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,16 @@ mod server; mod config; mod heartbeat; +const USE_TUI_CONSOLE_UI: bool = true; // true = uses tui.rs. false = uses pretty_env_logger (useful when developing) + #[tokio::main] async fn main() { - // pretty_env_logger::formatted_timed_builder().filter_level(log::LevelFilter::max()).init(); - // pretty_env_logger::formatted_timed_builder().filter_level(log::LevelFilter::Debug).init(); - logger::init(log::LevelFilter::max()).expect("Failed to enable logger!"); + if USE_TUI_CONSOLE_UI { + logger::init(log::LevelFilter::max()).expect("Failed to enable logger!"); + } else { + // pretty_env_logger::formatted_timed_builder().filter_level(log::LevelFilter::max()).init(); + pretty_env_logger::formatted_timed_builder().filter_level(log::LevelFilter::Debug).init(); + } let mut user_config: config::Config = toml::from_str( &std::fs::read_to_string("ServerConfig.toml") @@ -48,7 +53,9 @@ async fn main() { let (cmd_tx, cmd_rx) = mpsc::channel(100); - tokio::spawn(tui::tui_main(user_config.clone(), cmd_tx)); + if USE_TUI_CONSOLE_UI { + tokio::spawn(tui::tui_main(user_config.clone(), cmd_tx)); + } server_main(user_config, cmd_rx).await; } diff --git a/src/server/client.rs b/src/server/client.rs index cd52eb7..6e9f1aa 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -206,13 +206,25 @@ impl Client { mod_name.remove(0); // Remove f debug!("Client requested file {}", mod_name); - self.write_packet(Packet::Raw(RawPacket::from_str("AG"))).await?; - - // Send the first half of the file - if mod_name.starts_with("/") == false { - mod_name = format!("/{mod_name}"); + // making sure that the requested file cant point to files outside of Resources/Client/* + let path = std::path::Path::new(&mod_name); + let mod_name = match path.file_name() { + Some(v) => "/".to_string() + v.to_str().unwrap(), + None => { + error!("Client requests invalid mod. Disconnecting"); + self.kick("Invalid mod request"); + return Ok(()); + }, // client requested path (fResources/Client/) or nothing at all (f) - invalid + }; + let mod_path = format!("Resources/Client{mod_name}"); + if !std::path::Path::new(&mod_path).exists() { + error!("Client requests inexistent mod. Disconnecting"); + self.kick("Invalid mod request"); + return Ok(()) // client requested mod that doesnt exists within "Resources/Client/*" } + self.write_packet(Packet::Raw(RawPacket::from_str("AG"))).await?; + let mut mod_id = 0; for (i, (bmod_name, _bmod_size)) in config.mods.iter().enumerate() { if bmod_name == &mod_name { @@ -220,9 +232,8 @@ impl Client { } } - let mod_path = format!("Resources/Client{mod_name}"); + // Send the first half of the file over this socket and the other over the DSocket let file_data = std::fs::read(mod_path)?; - { let mut lock = self.write_half.lock().await; lock.writable().await?;