diff --git a/Cargo.lock b/Cargo.lock index c3204fa..9ab3295 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1319,9 +1319,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.60" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] @@ -1661,9 +1661,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.164" +version = "1.0.174" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" +checksum = "3b88756493a5bd5e5395d53baa70b194b05764ab85b59e43e4b8f4e1192fa9b1" dependencies = [ "serde_derive", ] @@ -1679,9 +1679,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.164" +version = "1.0.174" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" +checksum = "6e5c3a298c7f978e53536f95a63bdc4c4a64550582f31a0359a9afda6aede62e" dependencies = [ "proc-macro2", "quote", @@ -1690,9 +1690,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.97" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdf3bf93142acad5821c99197022e170842cdbc1c30482b98750c688c640842a" +checksum = "d03b412469450d4404fe8499a268edd7f8b79fecb074b0d812ad64ca21f4031b" dependencies = [ "itoa", "ryu", @@ -1833,9 +1833,9 @@ checksum = "734676eb262c623cec13c3155096e08d1f8f29adce39ba17948b18dad1e54142" [[package]] name = "syn" -version = "2.0.18" +version = "2.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" dependencies = [ "proc-macro2", "quote", diff --git a/crates/lox-distributor/Cargo.toml b/crates/lox-distributor/Cargo.toml index 6abdbf8..cf7ad5d 100644 --- a/crates/lox-distributor/Cargo.toml +++ b/crates/lox-distributor/Cargo.toml @@ -21,13 +21,13 @@ tokio = { version = "1", features = ["full", "macros", "signal"] } rand = "0.8.5" serde = { version = "1.0", features = ["derive", "rc"] } serde_with = "3.0.0" -serde_json = "1.0.87" zkp = "0.8.0" lox-library = { path = "../lox-library", version = "0.1.0"} lox_utils = { path = "../lox-utils", version = "0.1.0"} rdsys_backend = { path = "../rdsys-backend-api", version = "0.2"} clap = { version = "4.3.17", features = ["derive"] } +serde_json = "1.0.103" [dependencies.chrono] version = "0.4.19" diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs index c44b2a2..ece2d32 100644 --- a/crates/lox-distributor/src/main.rs +++ b/crates/lox-distributor/src/main.rs @@ -12,12 +12,14 @@ use lox_library::{BridgeAuth, BridgeDb}; use rdsys_backend::{proto::ResourceDiff, start_stream}; use serde::Deserialize; +use std::env; use std::{ convert::Infallible, + error::Error, fs::File, io::BufReader, net::SocketAddr, - path::PathBuf, + path::{Path, PathBuf}, sync::{Arc, Mutex}, time::Duration, }; @@ -44,11 +46,11 @@ async fn shutdown_signal() { #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] struct Args { - /// Name of the person to greet + /// Name/path of the configuration file #[arg(short, long, default_value = "config.json")] config: PathBuf, - /// Number of times to greet + /// Optional name/path of the db file #[arg(short, long)] db: Option, } @@ -120,26 +122,46 @@ async fn create_context_manager( } } +fn read_context_from_file>( + path: P, +) -> Result> { + let file = File::open(path)?; + let reader = BufReader::new(file); + let context = serde_json::from_reader(reader)?; + Ok(context) +} + // Context Manager handles the Lox BridgeDB and Bridge Authority, ensuring // that the DB can be updated from the rdsys stream and client requests // can be responded to with an updated BridgeDB state async fn context_manager(db_path: Option, mut context_rx: mpsc::Receiver) { let context: lox_context::LoxServerContext; if let Some(existing_db) = db_path.as_deref() { - let file = File::open(existing_db).expect("Should have been able to read config.json file"); - let reader = BufReader::new(file); - // Read the JSON contents of the file as a ResourceIn - context = - serde_json::from_reader(reader).expect("Reading SerializableContext from JSON failed."); + context = read_context_from_file(existing_db).unwrap(); } else { - let new_db = BridgeDb::new(); - let new_ba = BridgeAuth::new(new_db.pubkey); - context = lox_context::LoxServerContext { - db: Arc::new(Mutex::new(new_db)), - ba: Arc::new(Mutex::new(new_ba)), - extra_bridges: Arc::new(Mutex::new(Vec::new())), - to_be_replaced_bridges: Arc::new(Mutex::new(Vec::new())), - }; + let current_path = env::current_dir().expect("Unable to access current dir"); + if let Some(last_modified_file) = std::fs::read_dir(current_path) + .expect("Couldn't read local directory") + .flatten() // Remove failed + .filter(|f| { + f.metadata().unwrap().is_file() + && (f.file_name().into_string().unwrap().contains("_lox.json")) + }) // Filter out directories (only consider files) + .max_by_key(|x| x.metadata().unwrap().modified().unwrap()) + // Get the most recently modified file + { + println!("Reading from file {:?}", last_modified_file); + context = read_context_from_file(&last_modified_file.path()).unwrap(); + } else { + let new_db = BridgeDb::new(); + let new_ba = BridgeAuth::new(new_db.pubkey); + context = lox_context::LoxServerContext { + db: Arc::new(Mutex::new(new_db)), + ba: Arc::new(Mutex::new(new_ba)), + extra_bridges: Arc::new(Mutex::new(Vec::new())), + to_be_replaced_bridges: Arc::new(Mutex::new(Vec::new())), + }; + } } while let Some(cmd) = context_rx.recv().await { @@ -287,7 +309,7 @@ async fn context_manager(db_path: Option, mut context_rx: mpsc::Receive */ context.allocate_leftover_bridges(); context.encrypt_table(); - let mut date = Local::now().format("%Y-%m-%d").to_string(); + let mut date = Local::now().format("%Y-%m-%d_%H:%M:%S").to_string(); let path = "_lox.json"; date.push_str(path); let file = File::create(&date).expect("Unable to write to file!"); diff --git a/crates/lox-library/Cargo.toml b/crates/lox-library/Cargo.toml index a38c31c..77c1844 100644 --- a/crates/lox-library/Cargo.toml +++ b/crates/lox-library/Cargo.toml @@ -17,7 +17,7 @@ bincode = "1" chrono = "0.4" rand = "0.7" serde = "1.0.164" -serde_with = "3.0.0" +serde_with = {version = "3.0.0", features = ["json"]} sha2 = "0.9" statistical = "1.0.0" lazy_static = "1" diff --git a/crates/lox-library/src/bridge_table.rs b/crates/lox-library/src/bridge_table.rs index 36efe76..d820bd0 100644 --- a/crates/lox-library/src/bridge_table.rs +++ b/crates/lox-library/src/bridge_table.rs @@ -237,7 +237,7 @@ struct K { /// the encrypted buckets. The encrypted buckets will be exposed to the /// users of the system, and each user credential will contain the /// decryption key for one bucket. -//#[serde_as] +#[serde_as] #[derive(Debug, Default, Serialize, Deserialize)] pub struct BridgeTable { // All structures in the bridgetable are indexed by counter @@ -246,6 +246,7 @@ pub struct BridgeTable { pub buckets: HashMap, pub encbuckets: HashMap, /// Individual bridges that are reachable + #[serde_as(as = "HashMap")] pub reachable: HashMap>, /// bucket ids of "hot spare" buckets. These buckets are not handed /// to users, nor do they have any Migration credentials pointing to