From 9f0d0203a0d80989998ae987035f1d200b144ccb Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 6 Feb 2023 13:57:23 -0500 Subject: [PATCH] Fix issue with public key request --- crates/lox-distributor/src/main.rs | 51 ++++++++---------------------- 1 file changed, 14 insertions(+), 37 deletions(-) diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs index 823d85a..7bba4af 100644 --- a/crates/lox-distributor/src/main.rs +++ b/crates/lox-distributor/src/main.rs @@ -1,4 +1,3 @@ -use core::slice; use std::{ convert::Infallible, net::SocketAddr, @@ -20,10 +19,6 @@ use rand::RngCore; use serde::{Deserialize, Serialize}; use serde_json; use serde_with::serde_as; -use std::fs; -use std::fs::File; -use std::io::Write; -use std::path::Path; #[serde_as] #[derive(Serialize, Deserialize)] @@ -75,7 +70,6 @@ pub fn random() -> BridgeLine { #[derive(Clone)] struct LoxServerContext { - pubkey_filename: String, db: Arc>, ba: Arc>, } @@ -85,9 +79,7 @@ async fn handle( // addr: SocketAddr, req: Request, ) -> Result, Infallible> { - println!("Reqs on {}, {}", req.method(), req.uri().path()); - println!("Whole req: {:?}", req); - + println!("Request: {:?}", req); match req.method() { &Method::OPTIONS => Ok(Response::builder() .header("Access-Control-Allow-Origin", HeaderValue::from_static("*")) @@ -98,7 +90,7 @@ async fn handle( .unwrap()), _ => match (req.method(), req.uri().path()) { (&Method::GET, "/invite") => Ok::<_, Infallible>(generate_invite(context.db)), - (&Method::GET, "/pubkeys") => Ok::<_, Infallible>(send_keys(&context.pubkey_filename)), + (&Method::GET, "/pubkeys") => Ok::<_, Infallible>(send_keys(context.ba)), //TODO: figure out the format of the request and parse it? (&Method::POST, "/openreq") => Ok::<_, Infallible>({ let bytes = body::to_bytes(req.into_body()).await.unwrap(); @@ -129,9 +121,18 @@ fn generate_invite(db: Arc>) -> Response { resp } -fn send_keys(pubkeys_filename: &str) -> Response { - let data = fs::read_to_string(pubkeys_filename).expect("Unable to read file"); - let mut resp = Response::new(Body::from(serde_json::to_string(&data).unwrap())); +fn send_keys(ba: Arc>) -> Response { + let ba_obj = ba.lock().unwrap(); + // vector of public keys (to serialize) + let ba_obj_pubkeys = vec![ + &ba_obj.lox_pub, + &ba_obj.migration_pub, + &ba_obj.migrationkey_pub, + &ba_obj.reachability_pub, + &ba_obj.invitation_pub, + ]; + + let mut resp = Response::new(Body::from(serde_json::to_string(&ba_obj_pubkeys).unwrap())); resp.headers_mut() .insert("Access-Control-Allow-Origin", HeaderValue::from_static("*")); resp @@ -156,7 +157,6 @@ async fn shutdown_signal() { #[tokio::main] async fn main() { - let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json"; let num_buckets = 5; // Create and initialize a new db and lox_auth let mut bridgedb = BridgeDb::new(); @@ -170,30 +170,7 @@ async fn main() { // Create the encrypted bridge table lox_auth.enc_bridge_table(); - // output lox_auth pubkeys if the file doesn't exist - if !Path::new(lox_auth_pubkeys_filename).exists() { - // vector of public keys (to serialize) - let lox_auth_pubkeys = vec![ - &lox_auth.lox_pub, - &lox_auth.migration_pub, - &lox_auth.migrationkey_pub, - &lox_auth.reachability_pub, - &lox_auth.invitation_pub, - ]; - - // output lox_auth public keys - let mut lox_auth_pubkeys_outfile = File::create(lox_auth_pubkeys_filename) - .expect("Failed to create lox_auth pubkeys file"); - write!( - lox_auth_pubkeys_outfile, - "{}", - serde_json::to_string(&lox_auth_pubkeys).unwrap() - ) - .expect("Failed to write to lox_auth pubkeys file"); - } - let context = LoxServerContext { - pubkey_filename: lox_auth_pubkeys_filename.into(), db: Arc::new(Mutex::new(bridgedb)), ba: Arc::new(Mutex::new(lox_auth)), };