diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs index a80cc2c..084558e 100644 --- a/crates/lox-distributor/src/main.rs +++ b/crates/lox-distributor/src/main.rs @@ -6,7 +6,7 @@ use hyper::{ service::{make_service_fn, service_fn}, Body, Method, Request, Response, Server, StatusCode, }; -use lox::bridge_table::{BridgeLine, ENC_BUCKET_BYTES, BRIDGE_BYTES}; +use lox::bridge_table::{BridgeLine, BRIDGE_BYTES, ENC_BUCKET_BYTES}; use lox::proto; use lox::{BridgeAuth, BridgeDb, OPENINV_LENGTH}; use rand::RngCore; @@ -331,8 +331,8 @@ async fn main() { let (mut tx, mut rx) = async_channel::bounded(3); // to populate the bridge db let rstream = start_stream(rtype.endpoint, rtype.name, rtype.token, rtype.types) - .await - .unwrap(); + .await + .unwrap(); spawn(async move { for diff in rstream { println!("Received diff: {:?}", diff); //send this through a channel @@ -340,56 +340,92 @@ async fn main() { } }); - while let resourcediff = rx.recv().await.unwrap() { - spawn(async move { - for new_resource in resourcediff.new.unwrap(){ - println!("A NEW RESOURCE: {:?}", new_resource); - match new_resource.0.as_str() { - "obfs2" => { - println!("Obfs2!"); - let count = 0; - for resource in new_resource.1 { - let mut ip_bytes: [u8; 16] = [0; 16]; - ip_bytes[..resource.address.len()].copy_from_slice(resource.address.as_bytes()); - //let params = resource.params.unwrap(); I guess this should be a cert but I will fix this later - let infostr: String = format!( - "obfs4 {} fingerprint={} cert={} iat-mode=0", - resource.r#type, - resource.fingerprint, - base64::encode_config("super secret password", base64::STANDARD_NO_PAD), - ); - let mut info_bytes: [u8; BRIDGE_BYTES - 18] = [0; BRIDGE_BYTES-18]; + while let resourcediff = rx.recv().await.unwrap() { + // spawn(async move { + for new_resource in resourcediff.new { + for pt in new_resource { + println!("A NEW RESOURCE: {:?}", pt); + let mut bucket = [ + BridgeLine::default(), + BridgeLine::default(), + BridgeLine::default(), + ]; + let mut count = 0; + for resource in pt.1 { + let mut ip_bytes: [u8; 16] = [0; 16]; + ip_bytes[..resource.address.len()].copy_from_slice(resource.address.as_bytes()); + let infostr: String = format!( + "type={} blocked_in={:?} protocol={} fingerprint={} or_addresses={:?} distribution={} flags={:?} params={:?}", + resource.r#type, + resource.blocked_in, + resource.protocol, + resource.fingerprint, + resource.or_addresses, + resource.distribution, + resource.flags, + resource.params, + ); + let mut info_bytes: [u8; BRIDGE_BYTES - 18] = [0; BRIDGE_BYTES - 18]; - info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes()); - let bline = BridgeLine { - addr: ip_bytes, - port: resource.port, - info: info_bytes, - }; - println!("Now it's a bridgeline: {:?}", bline); - } - }, - "scramblesuit" => println!("Scramblesuit!"), - "obfs4" => println!("Obfs4!"), - "meek" => println!("Meek!"), - "snowflake" => println!("Meek!"), - _=> println!("Other unknown"), + info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes()); + let bridgeline = BridgeLine { + addr: ip_bytes, + port: resource.port, + info: info_bytes, + }; + + println!("Now it's a bridgeline: {:?}", bridgeline); + if count < 2 { + bucket[count] = bridgeline; + count += 1; + } else { + lox_auth.add_openinv_bridges(bucket, &mut bridgedb); + count = 0; + bucket = [ + BridgeLine::default(), + BridgeLine::default(), + BridgeLine::default(), + ]; + } } - - - } - for changed_resource in resourcediff.changed{ - println!("A NEW CHANGED RESOURCE: {:?}", changed_resource); - } - for gone_resource in resourcediff.gone{ - println!("A NEW GONE RESOURCE: {:?}", gone_resource); - } - //parse resource diff into Bridgeline - //add open inv bridges - // users.push(user); - }); } + for changed_resource in resourcediff.changed { + println!("A NEW CHANGED RESOURCE: {:?}", changed_resource); + } + for gone_resource in resourcediff.gone { + for pt in gone_resource { + println!("A NEW GONE RESOURCE: {:?}", pt); + let mut unreachable_bridge = BridgeLine::default(); + for resource in pt.1 { + let mut ip_bytes: [u8; 16] = [0; 16]; + ip_bytes[..resource.address.len()].copy_from_slice(resource.address.as_bytes()); + let infostr: String = format!( + "type={} blocked_in={:?} protocol={} fingerprint={} or_addresses={:?} distribution={} flags={:?} params={:?}", + resource.r#type, + resource.blocked_in, + resource.protocol, + resource.fingerprint, + resource.or_addresses, + resource.distribution, + resource.flags, + resource.params, + ); + let mut info_bytes: [u8; BRIDGE_BYTES - 18] = [0; BRIDGE_BYTES - 18]; + + info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes()); + let bridgeline = BridgeLine { + addr: ip_bytes, + port: resource.port, + info: info_bytes, + }; + + println!("Now it's a bridgeline: {:?}", bridgeline); + lox_auth.bridge_unreachable(&unreachable_bridge, &mut bridgedb); + } + } + } + } // 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