🚧 swich to the new routing func

main
Trisha 2022-03-25 16:04:51 -05:00
parent de5338f4f2
commit 436adb98ef
2 changed files with 19 additions and 15 deletions

View File

@ -252,7 +252,7 @@ pub struct FileRoots <'a> {
struct RoutedPath <'a> { struct RoutedPath <'a> {
root: &'a Path, root: &'a Path,
path: std::borrow::Cow <'a, str>, path_s: std::borrow::Cow <'a, str>,
} }
impl <'a> FileRoots <'a> { impl <'a> FileRoots <'a> {
@ -266,11 +266,11 @@ impl <'a> FileRoots <'a> {
let encoded_path = &path [0..]; let encoded_path = &path [0..];
let path = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?; let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?;
Ok (Some (RoutedPath { Ok (Some (RoutedPath {
root: self.files, root: self.files,
path, path_s,
})) }))
} }
} }
@ -317,19 +317,17 @@ pub async fn serve_all (
return serve_api (roots.files, &uri, hidden_path, path).await; return serve_api (roots.files, &uri, hidden_path, path).await;
} }
let path = match path.strip_prefix ("/files/") { let RoutedPath {
Some (x) => x, root,
path_s,
} = match roots.route (path)? {
None => return Ok (NotFound), None => return Ok (NotFound),
Some (x) => x,
}; };
// TODO: There is totally a dir traversal attack in here somewhere
let encoded_path = &path [0..];
let path_s = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().map_err (FileServerError::PathNotUtf8)?;
let path = Path::new (&*path_s); let path = Path::new (&*path_s);
let full_path = roots.files.join (path); let full_path = root.join (path);
trace! ("full_path = {:?}", full_path); trace! ("full_path = {:?}", full_path);

View File

@ -56,14 +56,20 @@ fn main ()
Some (Message::RunServer) => { Some (Message::RunServer) => {
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel (); let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel ();
let roots = match &config_file_opt {
None => Default::default (),
Some (cf) => match &cf.file_server_roots {
None => Default::default (),
Some (x) => x.clone (),
},
};
let config_file = ptth_server::ConfigFile { let config_file = ptth_server::ConfigFile {
name: gui.input_name.value ().to_string (), name: gui.input_name.value ().to_string (),
api_key: gui.input_api_key.value ().to_string (), api_key: gui.input_api_key.value ().to_string (),
relay_url: gui.input_relay_url.value ().to_string (), relay_url: gui.input_relay_url.value ().to_string (),
file_server_root: PathBuf::from (gui.input_file_server_root.value ()), file_server_root: PathBuf::from (gui.input_file_server_root.value ()),
file_server_roots: config_file_opt.as_ref () file_server_roots: roots,
.map (|cf| cf.file_server_roots.clone ())
.unwrap_or_else (|| Default::default ()),
throttle_upload: false, throttle_upload: false,
client_keys: Default::default (), client_keys: Default::default (),
allow_any_client: true, allow_any_client: true,
@ -114,7 +120,7 @@ pub struct ConfigFile {
pub api_key: String, pub api_key: String,
pub relay_url: Option <String>, pub relay_url: Option <String>,
pub file_server_root: Option <PathBuf>, pub file_server_root: Option <PathBuf>,
pub file_server_roots: BTreeMap <String, PathBuf>, pub file_server_roots: Option <BTreeMap <String, PathBuf>>,
} }
impl Gui { impl Gui {