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();
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<String>) {
fn start_server(
path: PathBuf,
address: &str,
password: Option<String>,
partial_password: Option<String>,
) {
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<String>) {
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<usize>,
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 {