diff --git a/Cargo.lock b/Cargo.lock index b623121..c3204fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,6 +71,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd" + +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", +] + [[package]] name = "autocfg" version = "0.1.8" @@ -113,6 +162,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630be753d4e58660abd17930c71b647fe46c27ea6b63cc59e1e3851406972e42" + [[package]] name = "block-buffer" version = "0.9.0" @@ -186,15 +241,62 @@ dependencies = [ "generic-array", ] +[[package]] +name = "clap" +version = "4.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0827b011f6f8ab38590295339817b0d26f344aa4932c3ced71b45b0c54b4a9" +dependencies = [ + "clap_builder", + "clap_derive", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9441b403be87be858db6a23edb493e7f694761acdc3343d5a0fcaafd304cbc9e" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" + [[package]] name = "cloudabi" version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -223,9 +325,9 @@ checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" [[package]] name = "cpufeatures" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03e69e28e9f7f77debdedbaafa2866e1de9ba56df55a8bd7cfc724c25a09987c" +checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1" dependencies = [ "libc", ] @@ -263,9 +365,9 @@ dependencies = [ [[package]] name = "curve25519-dalek" -version = "3.2.1" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90f9d052967f590a76e62eb387bd0bbb1b000182c3cefe5364db6b7211651bc0" +checksum = "0b9fdf9972b2bd6af2d913799d9ebc165ea4d2e65878e329d9c6b372c4491b61" dependencies = [ "byteorder", "digest 0.9.0", @@ -601,6 +703,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.2.6" @@ -775,6 +883,17 @@ version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12b6ee2129af8d4fb011108c73d99a1b83a85977f23b82460c0ae2e25bb4b57f" +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi 0.3.1", + "rustix 0.38.4", + "windows-sys 0.48.0", +] + [[package]] name = "itoa" version = "1.0.6" @@ -816,9 +935,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.146" +version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f92be4933c13fd498862a9e02a3055f8a8d9c039ce33db97306fd5a6caa7f29b" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "libm" @@ -832,6 +951,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" +[[package]] +name = "linux-raw-sys" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09fc20d2ca12cb9f044c93e3bd6d32d523e6e2ec3db4f7b2939cd99026ecd3f0" + [[package]] name = "lock_api" version = "0.4.10" @@ -854,6 +979,7 @@ version = "0.1.0" dependencies = [ "base64 0.13.1", "chrono", + "clap", "futures", "hex_fmt", "hyper", @@ -1079,7 +1205,7 @@ version = "0.10.55" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "345df152bc43501c5eb9e4654ff05f794effb78d4efe3d53abc158baddc0703d" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cfg-if", "foreign-types", "libc", @@ -1420,7 +1546,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -1468,11 +1594,24 @@ version = "0.37.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b96e891d04aa506a6d1f318d2771bcb1c7dfda84e126660ace067c9b474bb2c0" dependencies = [ - "bitflags", + "bitflags 1.3.2", "errno", "io-lifetimes", "libc", - "linux-raw-sys", + "linux-raw-sys 0.3.8", + "windows-sys 0.48.0", +] + +[[package]] +name = "rustix" +version = "0.38.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a962918ea88d644592894bc6dc55acc6c0956488adcebbfb6e273506b7fd6e5" +dependencies = [ + "bitflags 2.3.3", + "errno", + "libc", + "linux-raw-sys 0.4.3", "windows-sys 0.48.0", ] @@ -1503,7 +1642,7 @@ version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fc758eb7bffce5b308734e9b0c1468893cae9ff70ebf13e7090be8dcbcc83a8" dependencies = [ - "bitflags", + "bitflags 1.3.2", "core-foundation", "core-foundation-sys", "libc", @@ -1713,7 +1852,7 @@ dependencies = [ "cfg-if", "fastrand", "redox_syscall", - "rustix", + "rustix 0.37.20", "windows-sys 0.48.0", ] @@ -1935,6 +2074,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "vcpkg" version = "0.2.15" @@ -2228,9 +2373,9 @@ dependencies = [ [[package]] name = "zeroize" -version = "1.3.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4756f7db3f7b5574938c3eb1c117038b8e07f95ee6718c0efad4ac21508f1efd" +checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" dependencies = [ "zeroize_derive", ] diff --git a/crates/lox-distributor/Cargo.toml b/crates/lox-distributor/Cargo.toml index f2c6517..6abdbf8 100644 --- a/crates/lox-distributor/Cargo.toml +++ b/crates/lox-distributor/Cargo.toml @@ -19,7 +19,7 @@ futures = "0.3.26" time = "0.3.21" tokio = { version = "1", features = ["full", "macros", "signal"] } rand = "0.8.5" -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1.0", features = ["derive", "rc"] } serde_with = "3.0.0" serde_json = "1.0.87" zkp = "0.8.0" @@ -27,6 +27,7 @@ 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"] } [dependencies.chrono] version = "0.4.19" diff --git a/crates/lox-distributor/src/lox_context.rs b/crates/lox-distributor/src/lox_context.rs index c2b1bce..5f6a3b9 100644 --- a/crates/lox-distributor/src/lox_context.rs +++ b/crates/lox-distributor/src/lox_context.rs @@ -8,18 +8,20 @@ use lox_library::{ }, BridgeAuth, BridgeDb, IssuerPubKey, }; +use serde::{Deserialize, Serialize}; + use std::{ collections::HashMap, sync::{Arc, Mutex}, }; use zkp::ProofError; -#[derive(Clone)] +#[derive(Clone, Serialize, Deserialize)] pub struct LoxServerContext { pub db: Arc>, pub ba: Arc>, pub extra_bridges: Arc>>, - pub unreplaced_bridges: Arc>>, + pub to_be_replaced_bridges: Arc>>, } impl LoxServerContext { @@ -45,9 +47,9 @@ impl LoxServerContext { _ = extra_bridges.remove(length - 1) } - pub fn new_unreplaced_bridge(&self, bridge: BridgeLine) { - let mut unreplaced_bridges = self.unreplaced_bridges.lock().unwrap(); - unreplaced_bridges.push(bridge); + pub fn new_to_be_replaced_bridge(&self, bridge: BridgeLine) { + let mut to_be_replaced_bridges = self.to_be_replaced_bridges.lock().unwrap(); + to_be_replaced_bridges.push(bridge); } pub fn allocate_leftover_bridges(&self) { diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs index 45bde1c..c44b2a2 100644 --- a/crates/lox-distributor/src/main.rs +++ b/crates/lox-distributor/src/main.rs @@ -1,4 +1,5 @@ -use chrono::Utc; +use chrono::{prelude::*, Utc}; +use clap::Parser; use futures::future; use futures::StreamExt; use hyper::{ @@ -13,10 +14,10 @@ use rdsys_backend::{proto::ResourceDiff, start_stream}; use serde::Deserialize; use std::{ convert::Infallible, - env, fs::File, io::BufReader, net::SocketAddr, + path::PathBuf, sync::{Arc, Mutex}, time::Duration, }; @@ -40,6 +41,18 @@ async fn shutdown_signal() { println!("Shut down Lox Server"); } +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + /// Name of the person to greet + #[arg(short, long, default_value = "config.json")] + config: PathBuf, + + /// Number of times to greet + #[arg(short, long)] + db: Option, +} + #[derive(Debug, Deserialize)] struct ResourceInfo { endpoint: String, @@ -97,11 +110,12 @@ async fn parse_bridges(rdsys_tx: mpsc::Sender, mut rx: mpsc::Receiver, context_rx: mpsc::Receiver, mut kill: broadcast::Receiver<()>, ) { tokio::select! { - create_context = context_manager(context_rx) => create_context, + create_context = context_manager(db_path, context_rx) => create_context, _ = kill.recv() => {println!("Shut down context_manager");}, } } @@ -109,16 +123,24 @@ async fn create_context_manager( // 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(mut context_rx: mpsc::Receiver) { - let bridgedb = BridgeDb::new(); - let lox_auth = BridgeAuth::new(bridgedb.pubkey); - - let context = lox_context::LoxServerContext { - db: Arc::new(Mutex::new(bridgedb)), - ba: Arc::new(Mutex::new(lox_auth)), - extra_bridges: Arc::new(Mutex::new(Vec::new())), - unreplaced_bridges: Arc::new(Mutex::new(Vec::new())), - }; +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."); + } 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 { use Command::*; @@ -133,7 +155,7 @@ async fn context_manager(mut context_rx: mpsc::Receiver) { for resource in resources { let bridgeline = parse_resource(resource); println!("Now it's a bridgeline: {:?}", bridgeline); - if context.unreplaced_bridges.lock().unwrap().len() > 0 { + if context.to_be_replaced_bridges.lock().unwrap().len() > 0 { println!("BridgeLine to be replaced: {:?}", bridgeline); let res = context.replace_with_new(bridgeline); if res == lox_library::ReplaceSuccess::NotFound { @@ -151,10 +173,10 @@ async fn context_manager(mut context_rx: mpsc::Receiver) { res == lox_library::ReplaceSuccess::NotReplaced, "ReplaceSuccess incorrectly set somehow" ); - // Add the bridge to the list of unreplaced bridges in the Lox context and try + // Add the bridge to the list of to_be_replaced bridges in the Lox context and try // again to replace at the next update (nothing changes in the Lox Authority) println!("'Gone' BridgeLine NOT replaced, saved for next update! : {:?}", bridgeline); - context.new_unreplaced_bridge(bridgeline); + context.new_to_be_replaced_bridge(bridgeline); } } else if count < MAX_BRIDGES_PER_BUCKET { bucket[count] = bridgeline; @@ -234,13 +256,13 @@ async fn context_manager(mut context_rx: mpsc::Receiver) { bridgeline ); } else if res == lox_library::ReplaceSuccess::NotReplaced { - // Add the bridge to the list of unreplaced bridges in the Lox context and try + // Add the bridge to the list of to_be_replaced bridges in the Lox context and try // again to replace at the next update (nothing changes in the Lox Authority) println!( "'Gone' BridgeLine NOT replaced, saved for next update! : {:?}", bridgeline ); - context.new_unreplaced_bridge(bridgeline); + context.new_to_be_replaced_bridge(bridgeline); } } } @@ -265,6 +287,12 @@ async fn context_manager(mut context_rx: mpsc::Receiver) { */ context.allocate_leftover_bridges(); context.encrypt_table(); + let mut date = Local::now().format("%Y-%m-%d").to_string(); + let path = "_lox.json"; + date.push_str(path); + let file = File::create(&date).expect("Unable to write to file!"); + let _ = serde_json::to_writer(file, &context); + sleep(Duration::from_millis(1)).await; } Request { req, sender } => { @@ -300,8 +328,9 @@ enum Command { #[tokio::main] async fn main() { - let args: Vec = env::args().collect(); - let file = File::open(&args[1]).expect("Should have been able to read config.json file"); + let args: Args = Args::parse(); + + let file = File::open(&args.config).expect("Should have been able to read config.json file"); let reader = BufReader::new(file); // Read the JSON contents of the file as a ResourceInfo let rtype: ResourceInfo = @@ -333,7 +362,7 @@ async fn main() { }); let context_manager = - spawn(async move { create_context_manager(context_rx, kill_context).await }); + spawn(async move { create_context_manager(args.db, context_rx, kill_context).await }); let (tx, rx) = mpsc::channel(32); let rdsys_stream_handler = spawn(async { rdsys_stream(rtype, tx, kill_stream).await }); diff --git a/crates/lox-distributor/src/request_handler.rs b/crates/lox-distributor/src/request_handler.rs index d3a44fa..f47ea3c 100644 --- a/crates/lox-distributor/src/request_handler.rs +++ b/crates/lox-distributor/src/request_handler.rs @@ -240,7 +240,7 @@ mod tests { db: Arc::new(Mutex::new(bridgedb)), ba: Arc::new(Mutex::new(lox_auth)), extra_bridges: Arc::new(Mutex::new(Vec::new())), - unreplaced_bridges: Arc::new(Mutex::new(Vec::new())), + to_be_replaced_bridges: Arc::new(Mutex::new(Vec::new())), }; Self { context } }