From 189ae7abd79e0099280daa800c2a5e5408a27a38 Mon Sep 17 00:00:00 2001 From: _ <> Date: Mon, 2 Nov 2020 13:37:08 +0000 Subject: [PATCH] :bug: Don't create new rendezvous points for unknown servers. Also improve error reporting when loading config files --- .gitignore | 2 ++ src/bin/relay.rs | 18 +++++++++++------- src/bin/server.rs | 18 +++++++++++------- src/lib.rs | 10 +++++++++- src/relay/mod.rs | 6 +++++- todo.md | 1 - 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..2e2a568 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +/ptth_server.toml +/ptth_relay.toml diff --git a/src/bin/relay.rs b/src/bin/relay.rs index 71e3c76..ef43258 100644 --- a/src/bin/relay.rs +++ b/src/bin/relay.rs @@ -7,13 +7,17 @@ use std::{ async fn main () -> Result <(), Box > { use std::io::Read; - let mut f = File::open ("ptth_relay.toml").unwrap (); - let mut buffer = vec! [0u8; 4096]; - let bytes_read = f.read (&mut buffer).unwrap (); - buffer.truncate (bytes_read); - - let config_s = String::from_utf8 (buffer).unwrap (); - let config_file: ptth::relay::ConfigFile = toml::from_str (&config_s).unwrap (); + let config_file = { + let config_file_path = "ptth_relay.toml"; + + let mut f = File::open (config_file_path).expect (&format! ("Can't open {:?}", config_file_path)); + let mut buffer = vec! [0u8; 4096]; + let bytes_read = f.read (&mut buffer).expect (&format! ("Can't read {:?}", config_file_path)); + buffer.truncate (bytes_read); + + let config_s = String::from_utf8 (buffer).expect (&format! ("Can't parse {:?} as UTF-8", config_file_path)); + toml::from_str (&config_s).expect (&format! ("Can't parse {:?} as TOML", config_file_path)) + }; ptth::relay::main (config_file).await } diff --git a/src/bin/server.rs b/src/bin/server.rs index 7b71449..1925ad8 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -18,13 +18,17 @@ struct Opt { async fn main () -> Result <(), Box > { use std::io::Read; - let mut f = std::fs::File::open ("ptth_server.toml").unwrap (); - let mut buffer = vec! [0u8; 4096]; - let bytes_read = f.read (&mut buffer).unwrap (); - buffer.truncate (bytes_read); - - let config_s = String::from_utf8 (buffer).unwrap (); - let config_file: ptth::server::ConfigFile = toml::from_str (&config_s).unwrap (); + let config_file = { + let config_file_path = "ptth_server.toml"; + + let mut f = std::fs::File::open (config_file_path).expect (&format! ("Can't open {:?}", config_file_path)); + let mut buffer = vec! [0u8; 4096]; + let bytes_read = f.read (&mut buffer).expect (&format! ("Can't read {:?}", config_file_path)); + buffer.truncate (bytes_read); + + let config_s = String::from_utf8 (buffer).expect (&format! ("Can't parse {:?} as UTF-8", config_file_path)); + toml::from_str (&config_s).expect (&format! ("Can't parse {:?} as TOML", config_file_path)) + }; let opt = Opt::from_args (); diff --git a/src/lib.rs b/src/lib.rs index 369d97b..60e5807 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,7 +49,7 @@ mod tests { // Spawn the root task rt.block_on (async { - let server_name = "alien_wildlands"; + let server_name = "aliens_wildland"; let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate"; let tripcode = base64::encode (blake3::hash (api_key.as_bytes ()).as_bytes ()); println! ("Relay is expecting tripcode {}", tripcode); @@ -116,6 +116,14 @@ mod tests { ]) { panic! ("{}", String::from_utf8 (resp.to_vec ()).unwrap ()); } + + // Requesting a file from a server that isn't registered + // will error out + + let resp = client.get (&format! ("{}/servers/obviously_this_server_does_not_exist/files/COPYING", relay_url)) + .send ().await.unwrap (); + + assert_eq! (resp.status (), reqwest::StatusCode::NOT_FOUND); }); } } diff --git a/src/relay/mod.rs b/src/relay/mod.rs index a4d3693..654aa66 100644 --- a/src/relay/mod.rs +++ b/src/relay/mod.rs @@ -228,7 +228,10 @@ async fn handle_http_request ( ) -> Response { - let id = ulid::Ulid::new ().to_string (); + if ! state.config.server_tripcodes.contains_key (&watcher_code) { + return status_reply (StatusCode::NOT_FOUND, "Unknown server"); + } + let req = match http_serde::RequestParts::from_hyper (req.method, uri, req.headers) { Ok (x) => x, _ => return status_reply (StatusCode::BAD_REQUEST, "Bad request"), @@ -236,6 +239,7 @@ async fn handle_http_request ( let (tx, rx) = oneshot::channel (); + let id = ulid::Ulid::new ().to_string (); state.response_rendezvous.insert (id.clone (), tx); { diff --git a/todo.md b/todo.md index 5576d7c..9987cc8 100644 --- a/todo.md +++ b/todo.md @@ -1,4 +1,3 @@ -- Don't turn client-side typos into new rendezvous points - ETag cache - Server-side hash? - Log / audit log?