2023-03-13 18:05:52 -04:00
|
|
|
use futures::future;
|
2023-03-22 19:14:56 -04:00
|
|
|
use futures::StreamExt;
|
2022-11-15 19:11:16 -05:00
|
|
|
use hyper::{
|
2023-02-01 00:44:18 -05:00
|
|
|
body,
|
2022-11-15 22:04:04 -05:00
|
|
|
header::HeaderValue,
|
2023-01-25 15:23:28 -05:00
|
|
|
server::conn::AddrStream,
|
2023-01-25 15:34:43 -05:00
|
|
|
service::{make_service_fn, service_fn},
|
2023-01-25 15:23:28 -05:00
|
|
|
Body, Method, Request, Response, Server, StatusCode,
|
2022-11-15 19:11:16 -05:00
|
|
|
};
|
2023-04-10 12:16:19 -04:00
|
|
|
use lox::bridge_table::MAX_BRIDGES_PER_BUCKET;
|
2023-04-04 19:34:35 -04:00
|
|
|
use lox::bridge_table::{BridgeLine, BRIDGE_BYTES};
|
|
|
|
|
|
|
|
use lox::{BridgeAuth, BridgeDb};
|
|
|
|
|
2023-03-17 10:41:32 -04:00
|
|
|
use rdsys_backend::{proto::ResourceDiff, start_stream};
|
2023-04-04 19:34:35 -04:00
|
|
|
use serde::{Deserialize};
|
2023-03-03 17:25:43 -05:00
|
|
|
use std::{
|
|
|
|
convert::Infallible,
|
|
|
|
env,
|
|
|
|
fs::File,
|
|
|
|
io::BufReader,
|
|
|
|
net::SocketAddr,
|
|
|
|
sync::{Arc, Mutex},
|
2023-03-13 18:05:52 -04:00
|
|
|
time::Duration,
|
2023-03-03 17:25:43 -05:00
|
|
|
};
|
2023-03-22 19:14:56 -04:00
|
|
|
|
2023-04-04 19:34:35 -04:00
|
|
|
mod lox_context;
|
|
|
|
use lox_context::LoxServerContext;
|
|
|
|
|
2023-03-15 15:21:03 -04:00
|
|
|
use tokio::{
|
2023-03-20 12:42:40 -04:00
|
|
|
signal, spawn,
|
2023-03-17 10:41:32 -04:00
|
|
|
sync::{broadcast, mpsc, oneshot},
|
2023-03-20 12:42:40 -04:00
|
|
|
time::sleep,
|
2023-03-15 15:21:03 -04:00
|
|
|
};
|
2022-11-15 19:11:16 -05:00
|
|
|
|
2023-03-20 11:53:10 -04:00
|
|
|
// Lox Request handling logic for each Lox request/protocol
|
2023-01-25 15:23:28 -05:00
|
|
|
async fn handle(
|
2023-03-14 23:23:40 -04:00
|
|
|
cloned_context: LoxServerContext,
|
2023-01-25 15:23:28 -05:00
|
|
|
req: Request<Body>,
|
|
|
|
) -> Result<Response<Body>, Infallible> {
|
2023-02-06 13:57:23 -05:00
|
|
|
println!("Request: {:?}", req);
|
2023-01-30 18:14:22 -05:00
|
|
|
match req.method() {
|
|
|
|
&Method::OPTIONS => Ok(Response::builder()
|
|
|
|
.header("Access-Control-Allow-Origin", HeaderValue::from_static("*"))
|
|
|
|
.header("Access-Control-Allow-Headers", "accept, content-type")
|
|
|
|
.header("Access-Control-Allow-Methods", "POST")
|
|
|
|
.status(200)
|
|
|
|
.body(Body::from("Allow POST"))
|
|
|
|
.unwrap()),
|
|
|
|
_ => match (req.method(), req.uri().path()) {
|
2023-04-04 19:34:35 -04:00
|
|
|
(&Method::POST, "/invite") => Ok::<_, Infallible>(lox_context::generate_invite(cloned_context)),
|
2023-03-24 13:16:26 -04:00
|
|
|
(&Method::POST, "/reachability") => {
|
2023-04-04 19:34:35 -04:00
|
|
|
Ok::<_, Infallible>(lox_context::send_reachability_cred(cloned_context))
|
2023-03-15 15:21:03 -04:00
|
|
|
}
|
2023-04-04 19:34:35 -04:00
|
|
|
(&Method::POST, "/pubkeys") => Ok::<_, Infallible>(lox_context::send_keys(cloned_context)),
|
2023-01-30 18:14:22 -05:00
|
|
|
(&Method::POST, "/openreq") => Ok::<_, Infallible>({
|
|
|
|
let bytes = body::to_bytes(req.into_body()).await.unwrap();
|
2023-04-04 19:34:35 -04:00
|
|
|
lox_context::verify_and_send_open_cred(bytes, cloned_context)
|
2023-01-30 18:14:22 -05:00
|
|
|
}),
|
2023-02-06 17:41:26 -05:00
|
|
|
(&Method::POST, "/trustpromo") => Ok::<_, Infallible>({
|
|
|
|
let bytes = body::to_bytes(req.into_body()).await.unwrap();
|
2023-04-04 19:34:35 -04:00
|
|
|
lox_context::verify_and_send_trust_promo(bytes, cloned_context)
|
2023-02-06 17:41:26 -05:00
|
|
|
}),
|
2023-02-13 23:58:38 -05:00
|
|
|
(&Method::POST, "/trustmig") => Ok::<_, Infallible>({
|
|
|
|
let bytes = body::to_bytes(req.into_body()).await.unwrap();
|
2023-04-04 19:34:35 -04:00
|
|
|
lox_context::verify_and_send_trust_migration(bytes, cloned_context)
|
2023-02-13 23:58:38 -05:00
|
|
|
}),
|
2023-02-23 01:00:53 -05:00
|
|
|
(&Method::POST, "/levelup") => Ok::<_, Infallible>({
|
|
|
|
let bytes = body::to_bytes(req.into_body()).await.unwrap();
|
2023-04-04 19:34:35 -04:00
|
|
|
lox_context::verify_and_send_level_up(bytes, cloned_context)
|
2023-02-23 01:00:53 -05:00
|
|
|
}),
|
2023-02-24 19:10:38 -05:00
|
|
|
(&Method::POST, "/issueinvite") => Ok::<_, Infallible>({
|
|
|
|
let bytes = body::to_bytes(req.into_body()).await.unwrap();
|
2023-04-04 19:34:35 -04:00
|
|
|
lox_context::verify_and_send_issue_invite(bytes, cloned_context)
|
2023-02-24 19:10:38 -05:00
|
|
|
}),
|
2023-02-27 14:17:07 -05:00
|
|
|
(&Method::POST, "/redeem") => Ok::<_, Infallible>({
|
|
|
|
let bytes = body::to_bytes(req.into_body()).await.unwrap();
|
2023-04-04 19:34:35 -04:00
|
|
|
lox_context::verify_and_send_redeem_invite(bytes, cloned_context)
|
2023-02-27 14:17:07 -05:00
|
|
|
}),
|
2023-03-03 14:09:13 -05:00
|
|
|
(&Method::POST, "/checkblockage") => Ok::<_, Infallible>({
|
|
|
|
let bytes = body::to_bytes(req.into_body()).await.unwrap();
|
|
|
|
// TEST ONLY: Block all existing bridges and add new ones for migration
|
2023-04-04 19:34:35 -04:00
|
|
|
lox_context::verify_and_send_check_blockage(bytes, cloned_context)
|
2023-03-03 14:09:13 -05:00
|
|
|
}),
|
|
|
|
(&Method::POST, "/blockagemigration") => Ok::<_, Infallible>({
|
|
|
|
let bytes = body::to_bytes(req.into_body()).await.unwrap();
|
2023-04-04 19:34:35 -04:00
|
|
|
lox_context::verify_and_send_blockage_migration(bytes, cloned_context)
|
2023-03-03 14:09:13 -05:00
|
|
|
}),
|
2023-01-30 18:14:22 -05:00
|
|
|
_ => {
|
|
|
|
// Return 404 not found response.
|
|
|
|
Ok(Response::builder()
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
.body(Body::from("Not found"))
|
|
|
|
.unwrap())
|
|
|
|
}
|
|
|
|
},
|
2023-01-25 15:23:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 18:14:22 -05:00
|
|
|
async fn shutdown_signal() {
|
|
|
|
tokio::signal::ctrl_c()
|
|
|
|
.await
|
|
|
|
.expect("failed to listen for ctrl+c signal");
|
2023-03-17 10:41:32 -04:00
|
|
|
println!("Shut down Lox Server");
|
|
|
|
}
|
|
|
|
|
2023-04-04 19:34:35 -04:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct ResourceInfo {
|
|
|
|
endpoint: String,
|
|
|
|
name: String,
|
|
|
|
token: String,
|
|
|
|
types: Vec<String>,
|
|
|
|
}
|
|
|
|
// Populate Bridgedb from rdsys
|
|
|
|
|
2023-03-24 11:31:01 -04:00
|
|
|
// Rdsys sender creates a ResourceStream with the api_endpoint, resource token and type specified
|
|
|
|
// in the config.json file.
|
|
|
|
// TODO: ensure this stream gracefully shutdowns on the ctrl_c command.
|
2023-03-20 12:42:40 -04:00
|
|
|
async fn rdsys_stream(
|
|
|
|
rtype: ResourceInfo,
|
|
|
|
tx: mpsc::Sender<ResourceDiff>,
|
|
|
|
mut kill: broadcast::Receiver<()>,
|
|
|
|
) {
|
2023-03-22 19:14:56 -04:00
|
|
|
let mut rstream = start_stream(rtype.endpoint, rtype.name, rtype.token, rtype.types)
|
2023-03-17 10:41:32 -04:00
|
|
|
.await
|
|
|
|
.expect("rdsys stream initialization failed. Start rdsys or check config.json");
|
2023-03-24 11:31:01 -04:00
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
res = rstream.next() => {
|
|
|
|
match res {
|
|
|
|
Some(diff) => tx.send(diff).await.unwrap(),
|
|
|
|
None => return,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ = kill.recv() => {println!("Shut down rdsys stream"); return},
|
|
|
|
|
|
|
|
}
|
2023-03-13 18:05:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 12:42:40 -04:00
|
|
|
async fn rdsys_bridge_parser(
|
|
|
|
rdsys_tx: mpsc::Sender<Command>,
|
|
|
|
rx: mpsc::Receiver<ResourceDiff>,
|
|
|
|
mut kill: broadcast::Receiver<()>,
|
|
|
|
) {
|
|
|
|
tokio::select! {
|
2023-03-17 10:41:32 -04:00
|
|
|
start_bridge_parser = parse_bridges(rdsys_tx, rx) => start_bridge_parser ,
|
2023-03-24 13:16:26 -04:00
|
|
|
_ = kill.recv() => {println!("Shut down bridge_parser");},
|
2023-03-17 10:41:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 11:53:10 -04:00
|
|
|
// Parse Bridges receives a ResourceDiff from rdsys_sender and sends it to the
|
|
|
|
// Context Manager to be parsed and added to the BridgeDB
|
|
|
|
async fn parse_bridges(rdsys_tx: mpsc::Sender<Command>, mut rx: mpsc::Receiver<ResourceDiff>) {
|
2023-03-13 18:05:52 -04:00
|
|
|
loop {
|
|
|
|
let resourcediff = rx.recv().await.unwrap();
|
2023-03-14 23:23:40 -04:00
|
|
|
let cmd = Command::Rdsys {
|
2023-03-24 13:16:26 -04:00
|
|
|
resourcediff,
|
2023-03-14 23:23:40 -04:00
|
|
|
};
|
2023-03-13 18:05:52 -04:00
|
|
|
rdsys_tx.send(cmd).await.unwrap();
|
2023-03-15 15:21:03 -04:00
|
|
|
sleep(Duration::from_secs(1)).await;
|
2023-03-13 18:05:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 12:42:40 -04:00
|
|
|
async fn create_context_manager(
|
|
|
|
context_rx: mpsc::Receiver<Command>,
|
|
|
|
mut kill: broadcast::Receiver<()>,
|
|
|
|
) {
|
|
|
|
tokio::select! {
|
2023-03-17 10:41:32 -04:00
|
|
|
create_context = context_manager(context_rx) => create_context,
|
2023-03-24 13:16:26 -04:00
|
|
|
_ = kill.recv() => {println!("Shut down context_manager");},
|
2023-03-17 10:41:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 11:53:10 -04:00
|
|
|
// 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
|
2023-03-17 10:41:32 -04:00
|
|
|
async fn context_manager(mut context_rx: mpsc::Receiver<Command>) {
|
2023-03-20 12:42:40 -04:00
|
|
|
let bridgedb = BridgeDb::new();
|
|
|
|
let lox_auth = BridgeAuth::new(bridgedb.pubkey);
|
2023-03-17 10:41:32 -04:00
|
|
|
|
2023-04-04 19:34:35 -04:00
|
|
|
let context = lox_context::LoxServerContext {
|
2023-03-20 12:42:40 -04:00
|
|
|
db: Arc::new(Mutex::new(bridgedb)),
|
|
|
|
ba: Arc::new(Mutex::new(lox_auth)),
|
2023-04-05 16:26:23 -04:00
|
|
|
extra_bridges: Arc::new(Mutex::new(Vec::new())),
|
2023-03-20 12:42:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
while let Some(cmd) = context_rx.recv().await {
|
|
|
|
use Command::*;
|
|
|
|
|
|
|
|
match cmd {
|
|
|
|
Rdsys { resourcediff } => {
|
2023-03-24 13:16:26 -04:00
|
|
|
if let Some(new_resources) = resourcediff.new {
|
2023-04-05 16:26:23 -04:00
|
|
|
let mut count = 0;
|
2023-04-10 12:16:19 -04:00
|
|
|
let mut bucket= [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET];
|
2023-03-24 13:16:26 -04:00
|
|
|
for pt in new_resources {
|
2023-03-20 12:42:40 -04:00
|
|
|
println!("A NEW RESOURCE: {:?}", pt);
|
|
|
|
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());
|
2023-04-04 19:34:35 -04:00
|
|
|
let resource_uid = resource.get_uid().expect("Unable to get Fingerprint UID of resource");
|
2023-03-20 12:42:40 -04:00
|
|
|
let infostr: String = format!(
|
2023-04-04 19:34:35 -04:00
|
|
|
"type={} blocked_in={:?} protocol={} fingerprint={:?} or_addresses={:?} distribution={} flags={:?} params={:?}",
|
2023-03-17 10:41:32 -04:00
|
|
|
resource.r#type,
|
|
|
|
resource.blocked_in,
|
|
|
|
resource.protocol,
|
|
|
|
resource.fingerprint,
|
|
|
|
resource.or_addresses,
|
|
|
|
resource.distribution,
|
|
|
|
resource.flags,
|
|
|
|
resource.params,
|
|
|
|
);
|
2023-04-04 19:34:35 -04:00
|
|
|
let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26];
|
2023-03-20 12:42:40 -04:00
|
|
|
|
|
|
|
info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes());
|
|
|
|
let bridgeline = BridgeLine {
|
|
|
|
addr: ip_bytes,
|
|
|
|
port: resource.port,
|
2023-04-04 19:34:35 -04:00
|
|
|
uid_fingerprint: resource_uid,
|
2023-03-20 12:42:40 -04:00
|
|
|
info: info_bytes,
|
|
|
|
};
|
|
|
|
println!("Now it's a bridgeline: {:?}", bridgeline);
|
2023-04-10 12:16:19 -04:00
|
|
|
if count < MAX_BRIDGES_PER_BUCKET-1 {
|
2023-03-20 12:42:40 -04:00
|
|
|
bucket[count] = bridgeline;
|
|
|
|
count += 1;
|
|
|
|
} else {
|
2023-04-05 16:26:23 -04:00
|
|
|
// TODO: Decide the circumstances under which a bridge is allocated to an open_inv or spare bucket,
|
|
|
|
// eventually also do some more fancy grouping of new resources, i.e., by type or region
|
2023-03-20 12:42:40 -04:00
|
|
|
context.add_openinv_bucket(bucket);
|
|
|
|
count = 0;
|
2023-04-10 12:16:19 -04:00
|
|
|
bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET];
|
2023-03-20 12:42:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 16:26:23 -04:00
|
|
|
// Handle the extra buckets that were not allocated already
|
|
|
|
if count != 0 {
|
|
|
|
for val in 0..count {
|
2023-04-10 12:16:19 -04:00
|
|
|
if context.extra_bridges.lock().unwrap().len() < (MAX_BRIDGES_PER_BUCKET-1) {
|
2023-04-05 16:26:23 -04:00
|
|
|
context.append_extra_bridges(bucket[val]);
|
|
|
|
} else {
|
|
|
|
bucket = context.remove_extra_bridges();
|
|
|
|
context.add_spare_bucket(bucket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-20 12:42:40 -04:00
|
|
|
}
|
2023-03-24 13:16:26 -04:00
|
|
|
if let Some(changed_resources) = resourcediff.changed {
|
|
|
|
for pt in changed_resources {
|
2023-03-20 12:42:40 -04:00
|
|
|
println!("A NEW CHANGED RESOURCE: {:?}", pt);
|
|
|
|
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());
|
2023-04-04 19:34:35 -04:00
|
|
|
let resource_uid = resource.get_uid().expect("Unable to get Fingerprint UID of resource");
|
2023-03-20 12:42:40 -04:00
|
|
|
let infostr: String = format!(
|
2023-04-04 19:34:35 -04:00
|
|
|
"type={} blocked_in={:?} protocol={} fingerprint= {:?} or_addresses={:?} distribution={} flags={:?} params={:?}",
|
2023-03-20 12:42:40 -04:00
|
|
|
resource.r#type,
|
|
|
|
resource.blocked_in,
|
|
|
|
resource.protocol,
|
|
|
|
resource.fingerprint,
|
|
|
|
resource.or_addresses,
|
|
|
|
resource.distribution,
|
|
|
|
resource.flags,
|
|
|
|
resource.params,
|
|
|
|
);
|
2023-04-04 19:34:35 -04:00
|
|
|
let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26];
|
2023-03-20 12:42:40 -04:00
|
|
|
|
|
|
|
info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes());
|
|
|
|
let bridgeline = BridgeLine {
|
|
|
|
addr: ip_bytes,
|
|
|
|
port: resource.port,
|
2023-04-04 19:34:35 -04:00
|
|
|
uid_fingerprint: resource_uid,
|
2023-03-20 12:42:40 -04:00
|
|
|
info: info_bytes,
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("BridgeLine to be changed: {:?}", bridgeline);
|
|
|
|
let res = context.update_bridge(bridgeline);
|
|
|
|
if res {
|
|
|
|
println!("BridgeLine successfully updated: {:?}", bridgeline);
|
|
|
|
} else {
|
2023-04-05 16:26:23 -04:00
|
|
|
println!("BridgeLine: {:?} not found in Lox's Bridgetable. Save it as a new resource for now!", bridgeline);
|
|
|
|
if context.extra_bridges.lock().unwrap().len() < 2 {
|
|
|
|
context.append_extra_bridges(bridgeline);
|
|
|
|
} else {
|
|
|
|
let bucket = context.remove_extra_bridges();
|
|
|
|
context.add_spare_bucket(bucket);
|
|
|
|
}
|
2023-03-20 12:42:40 -04:00
|
|
|
//TODO probably do something else here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 20:11:10 -04:00
|
|
|
// gone resources are not the same as blocked resources.
|
|
|
|
// Instead, these are bridges which have either failed to pass tests for some period
|
|
|
|
// or have expired bridge descriptors. In both cases, the bridge is unusable, but this
|
|
|
|
// is not likely due to censorship. Therefore, we replace gone resources with new resources
|
|
|
|
// TODO: create a notion of blocked resources from information collected through various means:
|
|
|
|
// https://gitlab.torproject.org/tpo/anti-censorship/censorship-analysis/-/issues/40035
|
2023-03-24 13:16:26 -04:00
|
|
|
if let Some(gone_resources) = resourcediff.gone {
|
|
|
|
for pt in gone_resources {
|
2023-03-20 12:42:40 -04:00
|
|
|
println!("A NEW GONE RESOURCE: {:?}", pt);
|
|
|
|
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());
|
2023-04-04 19:34:35 -04:00
|
|
|
let resource_uid = resource.get_uid().expect("Unable to get Fingerprint UID of resource");
|
2023-03-20 12:42:40 -04:00
|
|
|
let infostr: String = format!(
|
2023-04-04 19:34:35 -04:00
|
|
|
"type={} blocked_in={:?} protocol={} fingerprint={:?} or_addresses={:?} distribution={} flags={:?} params={:?}",
|
2023-03-17 10:41:32 -04:00
|
|
|
resource.r#type,
|
|
|
|
resource.blocked_in,
|
|
|
|
resource.protocol,
|
|
|
|
resource.fingerprint,
|
|
|
|
resource.or_addresses,
|
|
|
|
resource.distribution,
|
|
|
|
resource.flags,
|
|
|
|
resource.params,
|
|
|
|
);
|
2023-04-04 19:34:35 -04:00
|
|
|
let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26];
|
2023-03-20 12:42:40 -04:00
|
|
|
|
|
|
|
info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes());
|
|
|
|
let bridgeline = BridgeLine {
|
|
|
|
addr: ip_bytes,
|
|
|
|
port: resource.port,
|
2023-04-04 19:34:35 -04:00
|
|
|
uid_fingerprint: resource_uid,
|
2023-03-20 12:42:40 -04:00
|
|
|
info: info_bytes,
|
|
|
|
};
|
2023-05-03 20:11:10 -04:00
|
|
|
/* // Marking bridges as unreachable is reserved for blocked bridges
|
2023-03-20 12:42:40 -04:00
|
|
|
println!("BridgeLine to be removed: {:?}", bridgeline);
|
|
|
|
let res = context.add_unreachable(bridgeline);
|
|
|
|
if res {
|
|
|
|
println!(
|
|
|
|
"BridgeLine successfully marked unreachable: {:?}",
|
|
|
|
bridgeline
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!("'Gone' BridgeLine NOT REMOVED!! : {:?}", bridgeline);
|
|
|
|
//TODO probably do something else here
|
|
|
|
}
|
2023-05-03 20:11:10 -04:00
|
|
|
*/
|
|
|
|
println!("BridgeLine to be replaced: {:?}", bridgeline);
|
|
|
|
let res = context.replace_with_new(bridgeline);
|
|
|
|
if res {
|
|
|
|
println!(
|
|
|
|
"BridgeLine successfully replaced: {:?}",
|
|
|
|
bridgeline
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!("'Gone' BridgeLine NOT replaced, marked removed!! : {:?}", bridgeline);
|
|
|
|
//TODO probably do something else here
|
|
|
|
}
|
|
|
|
|
2023-03-20 12:42:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-08 20:30:30 -04:00
|
|
|
context.allocate_leftover_bridges();
|
2023-03-20 12:42:40 -04:00
|
|
|
context.encrypt_table();
|
|
|
|
sleep(Duration::from_millis(1)).await;
|
|
|
|
}
|
2023-05-03 20:11:10 -04:00
|
|
|
|
2023-03-20 12:42:40 -04:00
|
|
|
Request { req, sender } => {
|
|
|
|
let response = handle(context.clone(), req).await;
|
|
|
|
if let Err(e) = sender.send(response) {
|
|
|
|
eprintln!("Server Response Error: {:?}", e);
|
|
|
|
};
|
|
|
|
sleep(Duration::from_millis(1)).await;
|
|
|
|
}
|
|
|
|
Shutdown { shutdown_sig } => {
|
2023-03-20 11:53:10 -04:00
|
|
|
println!("Sending Shutdown Signal, all threads should shutdown.");
|
|
|
|
drop(shutdown_sig);
|
|
|
|
println!("Shutdown Sent.");
|
2023-03-20 12:42:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 10:41:32 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 11:53:10 -04:00
|
|
|
// Each of the commands that the Context Manager handles
|
2023-03-13 18:05:52 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum Command {
|
2023-03-15 15:21:03 -04:00
|
|
|
Rdsys {
|
|
|
|
resourcediff: ResourceDiff,
|
|
|
|
},
|
2023-03-13 18:05:52 -04:00
|
|
|
Request {
|
2023-03-14 23:23:40 -04:00
|
|
|
req: Request<Body>,
|
2023-03-15 15:21:03 -04:00
|
|
|
sender: oneshot::Sender<Result<Response<Body>, Infallible>>,
|
|
|
|
},
|
2023-03-20 11:53:10 -04:00
|
|
|
Shutdown {
|
|
|
|
shutdown_sig: broadcast::Sender<()>,
|
2023-03-20 12:42:40 -04:00
|
|
|
},
|
2023-03-13 18:05:52 -04:00
|
|
|
}
|
2023-03-03 14:09:13 -05:00
|
|
|
|
2023-03-14 23:23:40 -04:00
|
|
|
#[tokio::main]
|
2022-11-15 19:11:16 -05:00
|
|
|
async fn main() {
|
2023-03-03 17:25:43 -05:00
|
|
|
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
|
2023-03-20 12:42:40 -04:00
|
|
|
let rtype: ResourceInfo =
|
|
|
|
serde_json::from_reader(reader).expect("Reading ResourceInfo from JSON failed.");
|
2023-03-03 14:09:13 -05:00
|
|
|
|
2023-03-17 10:41:32 -04:00
|
|
|
let (rdsys_tx, context_rx) = mpsc::channel(32);
|
2023-03-15 15:21:03 -04:00
|
|
|
let request_tx = rdsys_tx.clone();
|
2023-03-20 11:53:10 -04:00
|
|
|
let shutdown_cmd_tx = rdsys_tx.clone();
|
2023-03-13 18:05:52 -04:00
|
|
|
|
2023-03-20 11:53:10 -04:00
|
|
|
// create the shutdown broadcast channel and clone for every thread
|
2023-03-20 12:42:40 -04:00
|
|
|
let (shutdown_tx, mut shutdown_rx) = broadcast::channel(16);
|
|
|
|
let kill_stream = shutdown_tx.subscribe();
|
|
|
|
let kill_parser = shutdown_tx.subscribe();
|
|
|
|
let kill_context = shutdown_tx.subscribe();
|
2023-03-17 10:41:32 -04:00
|
|
|
|
2023-03-20 12:42:40 -04:00
|
|
|
// Listen for ctrl_c, send signal to broadcast shutdown to all threads by dropping shutdown_tx
|
|
|
|
let shutdown_handler = spawn(async move {
|
2023-03-17 10:41:32 -04:00
|
|
|
tokio::select! {
|
|
|
|
_ = signal::ctrl_c() => {
|
2023-03-20 11:53:10 -04:00
|
|
|
let cmd = Command::Shutdown {
|
|
|
|
shutdown_sig: shutdown_tx,
|
|
|
|
};
|
|
|
|
shutdown_cmd_tx.send(cmd).await.unwrap();
|
|
|
|
sleep(Duration::from_secs(1)).await;
|
2023-03-17 10:41:32 -04:00
|
|
|
|
|
|
|
_ = shutdown_rx.recv().await;
|
2023-03-09 19:01:01 -05:00
|
|
|
}
|
2023-03-03 17:25:43 -05:00
|
|
|
}
|
2023-03-20 12:42:40 -04:00
|
|
|
});
|
2023-03-17 10:41:32 -04:00
|
|
|
|
2023-03-20 12:42:40 -04:00
|
|
|
let context_manager =
|
|
|
|
spawn(async move { create_context_manager(context_rx, kill_context).await });
|
2023-03-10 16:54:31 -05:00
|
|
|
|
2023-03-20 11:53:10 -04:00
|
|
|
let (tx, rx) = mpsc::channel(32);
|
2023-03-17 10:41:32 -04:00
|
|
|
let rdsys_stream_handler = spawn(async { rdsys_stream(rtype, tx, kill_stream).await });
|
2023-03-14 23:23:40 -04:00
|
|
|
|
2023-03-20 12:42:40 -04:00
|
|
|
let rdsys_resource_receiver =
|
|
|
|
spawn(async { rdsys_bridge_parser(rdsys_tx, rx, kill_parser).await });
|
2023-03-14 23:23:40 -04:00
|
|
|
|
2023-03-15 15:21:03 -04:00
|
|
|
let make_service = make_service_fn(move |_conn: &AddrStream| {
|
2023-03-14 23:23:40 -04:00
|
|
|
let request_tx = request_tx.clone();
|
2023-03-15 15:21:03 -04:00
|
|
|
let service = service_fn(move |req| {
|
2023-03-14 23:23:40 -04:00
|
|
|
let request_tx = request_tx.clone();
|
|
|
|
let (response_tx, response_rx) = oneshot::channel();
|
|
|
|
let cmd = Command::Request {
|
2023-03-24 13:16:26 -04:00
|
|
|
req,
|
2023-03-14 23:23:40 -04:00
|
|
|
sender: response_tx,
|
|
|
|
};
|
|
|
|
async move {
|
2023-03-15 15:21:03 -04:00
|
|
|
request_tx.send(cmd).await.unwrap();
|
|
|
|
response_rx.await.unwrap()
|
2023-03-14 23:23:40 -04:00
|
|
|
}
|
|
|
|
});
|
2023-01-25 15:23:28 -05:00
|
|
|
async move { Ok::<_, Infallible>(service) }
|
2022-11-15 19:11:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 8001));
|
2023-03-14 23:23:40 -04:00
|
|
|
let server = Server::bind(&addr).serve(make_service);
|
2023-03-17 10:41:32 -04:00
|
|
|
let graceful = server.with_graceful_shutdown(shutdown_signal());
|
|
|
|
println!("Listening on {}", addr);
|
|
|
|
if let Err(e) = graceful.await {
|
|
|
|
eprintln!("server error: {}", e);
|
|
|
|
}
|
2023-03-20 12:42:40 -04:00
|
|
|
future::join_all([
|
2023-03-17 10:41:32 -04:00
|
|
|
rdsys_stream_handler,
|
|
|
|
rdsys_resource_receiver,
|
|
|
|
context_manager,
|
|
|
|
shutdown_handler,
|
|
|
|
])
|
|
|
|
.await;
|
2022-11-15 19:11:16 -05:00
|
|
|
}
|