Added a second password for partial access

This commit is contained in:
p11 2024-02-10 00:57:14 +01:00
parent ef1afa42fa
commit 5bd4ec7097

View File

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