Read json config from commandline file, progress on channels
This commit is contained in:
parent
1018dd827f
commit
a7cf09f812
|
@ -9,7 +9,7 @@ edition = "2021"
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
hyper = { version = "0.14.24", features = ["server"] }
|
hyper = { version = "0.14.24", features = ["server"] }
|
||||||
hex_fmt = "0.3"
|
hex_fmt = "0.3"
|
||||||
tokio = { version = "1", features = ["full", "macros", "signal"] }
|
tokio = { version = "1", features = ["full", "macros", "signal", "sync"] }
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_with = "1.9.1"
|
serde_with = "1.9.1"
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"endpoint": "http://127.0.0.1:7100/resource-stream",
|
||||||
|
"name": "https",
|
||||||
|
"token": "HttpsApiTokenPlaceholder",
|
||||||
|
"types": [
|
||||||
|
"obfs2",
|
||||||
|
"scramblesuit"
|
||||||
|
]
|
||||||
|
}
|
|
@ -1,9 +1,3 @@
|
||||||
use std::{
|
|
||||||
convert::Infallible,
|
|
||||||
net::SocketAddr,
|
|
||||||
sync::{Arc, Mutex},
|
|
||||||
};
|
|
||||||
|
|
||||||
use hyper::{
|
use hyper::{
|
||||||
body,
|
body,
|
||||||
body::Bytes,
|
body::Bytes,
|
||||||
|
@ -16,11 +10,20 @@ use lox::bridge_table::{BridgeLine, ENC_BUCKET_BYTES};
|
||||||
use lox::proto;
|
use lox::proto;
|
||||||
use lox::{BridgeAuth, BridgeDb, OPENINV_LENGTH};
|
use lox::{BridgeAuth, BridgeDb, OPENINV_LENGTH};
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use rdsys_backend::start_stream;
|
use rdsys_backend::{proto::ResourceDiff, start_stream, ResourceStream};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{
|
||||||
|
convert::Infallible,
|
||||||
|
env,
|
||||||
|
fs::File,
|
||||||
|
io::BufReader,
|
||||||
|
net::SocketAddr,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use serde_with::serde_as;
|
use serde_with::serde_as;
|
||||||
|
use tokio::{spawn, sync::mpsc};
|
||||||
|
|
||||||
#[serde_as]
|
#[serde_as]
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
|
@ -36,6 +39,13 @@ pub struct EncBridgeTable {
|
||||||
etable: Vec<[u8; ENC_BUCKET_BYTES]>,
|
etable: Vec<[u8; ENC_BUCKET_BYTES]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct ResourceInfo {
|
||||||
|
endpoint: String,
|
||||||
|
name: String,
|
||||||
|
token: String,
|
||||||
|
types: Vec<String>,
|
||||||
|
}
|
||||||
// Populate Bridgedb from rdsys
|
// Populate Bridgedb from rdsys
|
||||||
|
|
||||||
/// Create a random BridgeLine for testing ONLY. Do not use in production!
|
/// Create a random BridgeLine for testing ONLY. Do not use in production!
|
||||||
|
@ -300,28 +310,46 @@ async fn shutdown_signal() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial bridgedb setup then:
|
// Initial bridgedb setup then:
|
||||||
// Listen for updates and return new bridges to be added to the bridgedb
|
// Listen for updates and return new bridges to be added to the bridged
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Run with cargo run -- config.json
|
||||||
#[tokio::main(worker_threads = 2)]
|
#[tokio::main(worker_threads = 2)]
|
||||||
async fn main() {
|
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();
|
||||||
|
|
||||||
//
|
// pass in distribution of open invite vs. hot spare buckets?
|
||||||
tokio::spawn(async move { load_bridges().await; });
|
|
||||||
// let new_bridgedb = task::spawn(load_bridges());
|
|
||||||
let num_buckets = 5;
|
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 bridgedb = BridgeDb::new();
|
||||||
let mut lox_auth = BridgeAuth::new(bridgedb.pubkey);
|
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
|
// Make 3 x num_buckets open invitation bridges, in sets of 3
|
||||||
for _ in 0..num_buckets {
|
for _ in 0..num_buckets {
|
||||||
let bucket = [random(), random(), random()];
|
let bucket = [random(), random(), random()];
|
||||||
|
|
Loading…
Reference in New Issue