🐛 Don't create new rendezvous points for unknown servers.

Also improve error reporting when loading config files
main
_ 2020-11-02 13:37:08 +00:00
parent b1b37ee696
commit 189ae7abd7
6 changed files with 38 additions and 17 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
/target
/ptth_server.toml
/ptth_relay.toml

View File

@ -7,13 +7,17 @@ use std::{
async fn main () -> Result <(), Box <dyn Error>> {
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
}

View File

@ -18,13 +18,17 @@ struct Opt {
async fn main () -> Result <(), Box <dyn Error>> {
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 ();

View File

@ -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);
});
}
}

View File

@ -228,7 +228,10 @@ async fn handle_http_request (
)
-> Response <Body>
{
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);
{

View File

@ -1,4 +1,3 @@
- Don't turn client-side typos into new rendezvous points
- ETag cache
- Server-side hash?
- Log / audit log?