Read json config from commandline file, progress on channels

This commit is contained in:
onyinyang 2023-03-03 17:25:43 -05:00
parent 1018dd827f
commit a7cf09f812
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
3 changed files with 60 additions and 23 deletions

View File

@ -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"

View File

@ -0,0 +1,9 @@
{
"endpoint": "http://127.0.0.1:7100/resource-stream",
"name": "https",
"token": "HttpsApiTokenPlaceholder",
"types": [
"obfs2",
"scramblesuit"
]
}

View File

@ -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<String>,
}
// 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<String> = 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()];