Merge pull request #8 from OfficialLambdax/mod-request-check

Mod request check + ui toggle
This commit is contained in:
Luuk van Oijen 2023-11-22 10:23:32 +01:00 committed by GitHub
commit 669e061fdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 11 deletions

View File

@ -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();
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);
if USE_TUI_CONSOLE_UI {
tokio::spawn(tui::tui_main(user_config.clone(), cmd_tx));
}
server_main(user_config, cmd_rx).await;
}

View File

@ -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?;