diff --git a/crates/lox-distributor/Cargo.toml b/crates/lox-distributor/Cargo.toml index 1e9f513..3050f2b 100644 --- a/crates/lox-distributor/Cargo.toml +++ b/crates/lox-distributor/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" base64 = "0.13" hyper = { version = "0.14.24", features = ["server"] } hex_fmt = "0.3" -tokio = { version = "1", features = ["full", "macros", "signal"] } +tokio = { version = "1", features = ["full", "macros", "signal", "sync"] } rand = "0.7" serde = { version = "1.0", features = ["derive"] } serde_with = "1.9.1" diff --git a/crates/lox-distributor/config.json b/crates/lox-distributor/config.json new file mode 100644 index 0000000..177e5a2 --- /dev/null +++ b/crates/lox-distributor/config.json @@ -0,0 +1,9 @@ +{ + "endpoint": "http://127.0.0.1:7100/resource-stream", + "name": "https", + "token": "HttpsApiTokenPlaceholder", + "types": [ + "obfs2", + "scramblesuit" + ] +} \ No newline at end of file diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs index 3932211..e3bbd23 100644 --- a/crates/lox-distributor/src/main.rs +++ b/crates/lox-distributor/src/main.rs @@ -1,9 +1,3 @@ -use std::{ - convert::Infallible, - net::SocketAddr, - sync::{Arc, Mutex}, -}; - use hyper::{ body, body::Bytes, @@ -16,11 +10,20 @@ use lox::bridge_table::{BridgeLine, ENC_BUCKET_BYTES}; use lox::proto; use lox::{BridgeAuth, BridgeDb, OPENINV_LENGTH}; use rand::RngCore; -use rdsys_backend::start_stream; +use rdsys_backend::{proto::ResourceDiff, start_stream, ResourceStream}; use serde::{Deserialize, Serialize}; +use std::{ + convert::Infallible, + env, + fs::File, + io::BufReader, + net::SocketAddr, + sync::{Arc, Mutex}, +}; use serde_json; use serde_with::serde_as; +use tokio::{spawn, sync::mpsc}; #[serde_as] #[derive(Serialize, Deserialize)] @@ -36,6 +39,13 @@ pub struct EncBridgeTable { etable: Vec<[u8; ENC_BUCKET_BYTES]>, } +#[derive(Debug, Deserialize)] +struct ResourceInfo { + endpoint: String, + name: String, + token: String, + types: Vec, +} // Populate Bridgedb from rdsys /// Create a random BridgeLine for testing ONLY. Do not use in production! @@ -300,28 +310,46 @@ async fn shutdown_signal() { } // Initial bridgedb setup then: -// Listen for updates and return new bridges to be added to the bridgedb -async fn load_bridges() { - let endpoint = String::from("http://127.0.0.1:7100/resource-stream"); - let name = String::from("https"); - let token = String::from("HttpsApiTokenPlaceholder"); //Bring in from commmand line - let types = vec![String::from("obfs2"), String::from("scramblesuit")]; - let rx = start_stream(endpoint, name, token, types).await.unwrap(); - for diff in rx { - println!("Received diff: {:?}", diff); //send this through a channel - } -} +// Listen for updates and return new bridges to be added to the bridged +// Run with cargo run -- config.json #[tokio::main(worker_threads = 2)] 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 reader = BufReader::new(file); + // Read the JSON contents of the file as a ResourceInfo + let rtype: ResourceInfo = serde_json::from_reader(reader).unwrap(); -// - tokio::spawn(async move { load_bridges().await; }); - // let new_bridgedb = task::spawn(load_bridges()); + // pass in distribution of open invite vs. hot spare buckets? let num_buckets = 5; - // Create and initialize a new db and lox_auth + let hot_spare_buckets = 5; let mut bridgedb = BridgeDb::new(); let mut lox_auth = BridgeAuth::new(bridgedb.pubkey); + + //Sender is resource stream and receiver is bridgedb function (add_openinv_bridges) + let (mut tx, mut rx) = mpsc::channel(100); + // to populate the bridge db + spawn(async move { + let rstream = start_stream(rtype.endpoint, rtype.name, rtype.token, rtype.types) + .await + .unwrap(); + for diff in rstream { + println!("Received diff: {:?}", diff); //send this through a channel + tx.send(diff).await.expect("can not add to bridgedb)") + } + }); + + spawn(async move { + while let Some(resourcediff) = rx.recv().await { + println!("received: {:?}", resourcediff); + //parse resource diff into Bridgeline + //add open inv bridges + // users.push(user); + } + }); + // let new_bridgedb = task::spawn(load_bridges()); + // Create and initialize a new db and lox_auth // Make 3 x num_buckets open invitation bridges, in sets of 3 for _ in 0..num_buckets { let bucket = [random(), random(), random()];