diff --git a/src/main.rs b/src/main.rs index 6d308ec..04fdb06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,10 +32,16 @@ fn main() { args.next(); let address = args.next().unwrap_or("127.0.0.1:8080".to_string()); let password = args.next(); - start_server(path, &address, password); + let partial_password = args.next(); + start_server(path, &address, password, partial_password); } -fn start_server(path: PathBuf, address: &str, password: Option) { +fn start_server( + path: PathBuf, + address: &str, + password: Option, + partial_password: Option, +) { let listener = TcpListener::bind(address).expect("Invalid bind address!"); eprintln!("Strated server!"); @@ -60,7 +66,16 @@ fn start_server(path: PathBuf, address: &str, password: Option) { let context = context.clone(); let path = path.clone(); let password = password.clone(); - pool.execute(move || handle_connection(context, path, stream, password.as_deref())); + let hidden_password = partial_password.clone(); + pool.execute(move || { + handle_connection( + context, + path, + stream, + password.as_deref(), + hidden_password.as_deref(), + ) + }); } } @@ -146,6 +161,7 @@ fn handle_connection( path: PathBuf, mut stream: TcpStream, password: Option<&str>, + partial_password: Option<&str>, ) { let Some(request) = Request::from(&stream) else { eprintln!("Invalid request!"); @@ -167,6 +183,7 @@ fn handle_connection( #[derive(PartialEq, Eq)] enum Access { None, + Partial, Full, } @@ -180,9 +197,14 @@ fn handle_connection( continue; }; - if key == "password" && input == password { - access = Access::Full; - cookie = Some(password); + if key == "password" { + if input == password { + access = Access::Full; + cookie = Some(password); + } else if Some(input) == partial_password { + access = Access::Partial; + cookie = partial_password; + } break; } } @@ -207,13 +229,13 @@ fn handle_connection( continue; } - if state != password { - continue; + if state == password { + access = Access::Full; + break; + } else if Some(state) == partial_password { + access = Access::Partial; + break; } - - access = Access::Full; - - break; } } } @@ -256,8 +278,13 @@ fn handle_connection( let mut data_path = path.to_path_buf(); let (pki_path, start_level, relative_path, partial) = if relative_path.is_empty() { - pk_path.push("index.pk"); - data_path.push("index.dat"); + if access == Access::Full { + pk_path.push("index.pk"); + data_path.push("index.dat"); + } else { + pk_path.push("partial.pk"); + data_path.push("partial.dat"); + } (None, 0, String::new(), None) } else { @@ -278,7 +305,11 @@ fn handle_connection( }; pk_path.push(format!("{path}.pk")); - pki_path.push(format!("{path}.pki")); + if access == Access::Full { + pki_path.push(format!("{path}.pki")); + } else { + pki_path.push(format!("{path}.pkc")); + } data_path.push(format!("{path}.dat")); (Some(pki_path), 1, path.to_string(), num) @@ -343,6 +374,7 @@ fn handle_connection( partial, start_level, cookie, + access != Access::Full, ) } @@ -384,6 +416,7 @@ fn handle_relative_connection( partial: Option, start_level: usize, cookie: Option<&str>, + censored: bool, ) { let mut name = None; let mut text = None; @@ -492,10 +525,11 @@ fn handle_relative_connection( if let Some((real_entry, _)) = entry.split_once(':') { entry = real_entry } + let pki_extension = if censored { "pkc" } else { "pki" }; path.push(if relative_path.is_empty() { - format!("{entry}.pki") + format!("{entry}.{pki_extension}") } else { - format!("{relative_path}/{entry}.pki") + format!("{relative_path}/{entry}.{pki_extension}") }); let Ok(file) = File::open(path) else {