diff --git a/src/bin/bridgedb.rs b/src/bin/bridgedb.rs deleted file mode 100644 index 4b19adb..0000000 --- a/src/bin/bridgedb.rs +++ /dev/null @@ -1,62 +0,0 @@ -// This seems like probably not the best way to do this, but it works. -#[path = "../server_net.rs"] -mod server_net; -use crate::server_net::listen; - -use lox::BridgeDb; -use std::env::args; -use std::fs::File; -use std::io::Write; -use std::path::Path; - -#[tokio::main] -async fn main() { - let bridgedb_filename = "bridgedb.json"; - let bridgedb_pubkey_filename = "bridgedb_pubkey.json"; - - // network address to listen on, e.g., localhost:8181 - let addr = args().nth(1).unwrap(); - - // If bridgedb has already been created, recreate it from file. - // Otherwise, create new bridgedb. - let mut bridgedb = if Path::new(bridgedb_filename).exists() { - // read in file - let bridgedb_infile = File::open(bridgedb_filename).unwrap(); - serde_json::from_reader(bridgedb_infile).unwrap() - } else { - // create new bridgedb (implicitly generates keys) - BridgeDb::new() - }; - - // output full serialized bridgedb if file doesn't already exist - if !Path::new(bridgedb_filename).exists() { - let mut bridgedb_outfile = - File::create(bridgedb_filename).expect("Failed to create bridgedb privkey file"); - let bridgedb_outfile_json = serde_json::to_string(&bridgedb).unwrap(); - write!(bridgedb_outfile, "{}", bridgedb_outfile_json) - .expect("Failed to write to bridgedb.json"); - } - - // output bridgedb public key if file doesn't already exist - if !Path::new(bridgedb_pubkey_filename).exists() { - let mut bridgedb_pubkey_outfile = - File::create(bridgedb_pubkey_filename).expect("Failed to create bridgedb pubkey file"); - write!( - bridgedb_pubkey_outfile, - "{}", - serde_json::to_string(&bridgedb.pubkey).unwrap() - ) - .expect("Failed to write to bridgedb pubkey file"); - } - - listen(addr, &mut |request: Vec| handle_request(request, &mut bridgedb)).await; -} - -fn handle_request(request: Vec, bridgedb: &mut BridgeDb) -> Vec { - if request[0] == 1 { - // note, the bridgedb currently has no buckets - return bridgedb.invite().to_vec(); - } - // default response, to deal with later - [].to_vec() -} diff --git a/src/bin/lox_auth.rs b/src/bin/lox_auth.rs deleted file mode 100644 index 2b34085..0000000 --- a/src/bin/lox_auth.rs +++ /dev/null @@ -1,119 +0,0 @@ -// This seems like probably not the best way to do this, but it works. -#[path = "../server_net.rs"] -mod server_net; -use crate::server_net::listen; - -#[path = "../request_type.rs"] -mod request_type; -use crate::request_type::RequestType; - -use lox::BridgeAuth; -use lox::proto::*; -use std::env::args; -use std::fs::File; -use std::io::Write; -use std::path::Path; - -#[tokio::main] -async fn main() { - let bridgedb_pubkey_filename = "bridgedb_pubkey.json"; - let lox_auth_filename = "lox_auth.json"; - let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json"; - - // network address to listen on, e.g., localhost:8080 - let addr = args().nth(1).unwrap(); - - // import bridgedb pubkey - // note: currently no checks for valid data - let bridgedb_pubkey_infile = File::open(bridgedb_pubkey_filename).unwrap(); - let bridgedb_pubkey = serde_json::from_reader(bridgedb_pubkey_infile).unwrap(); - - // If lox_auth has already been created, recreate it from file. - // Otherwise, create new lox authority. - let mut lox_auth = if Path::new(lox_auth_filename).exists() { - // read in file - let lox_auth_infile = File::open(lox_auth_filename).unwrap(); - serde_json::from_reader(lox_auth_infile).unwrap() - } else { - // create new lox_auth (implicitly generates keys) - BridgeAuth::new(bridgedb_pubkey) - }; - - // output full serialized lox_auth if it doesn't exist - if !Path::new(lox_auth_filename).exists() { - let mut lox_auth_outfile = - File::create(lox_auth_filename).expect("Failed to create lox_auth file"); - let lox_auth_outfile_json = serde_json::to_string(&lox_auth).unwrap(); - write!(lox_auth_outfile, "{}", lox_auth_outfile_json) - .expect("Failed to write to lox_auth file"); - } - - // output lox_auth pubkeys if the file doesn't exist - if !Path::new(lox_auth_pubkeys_filename).exists() { - // vector of public keys (to serialize) - let lox_auth_pubkeys = vec![ - &lox_auth.lox_pub, - &lox_auth.migration_pub, - &lox_auth.migrationkey_pub, - &lox_auth.reachability_pub, - &lox_auth.invitation_pub, - ]; - - // output lox_auth public keys - let mut lox_auth_pubkeys_outfile = File::create(lox_auth_pubkeys_filename) - .expect("Failed to create lox_auth pubkeys file"); - write!( - lox_auth_pubkeys_outfile, - "{}", - serde_json::to_string(&lox_auth_pubkeys).unwrap() - ) - .expect("Failed to write to lox_auth pubkeys file"); - } - - listen(addr, &mut |request: Vec| handle_request(request, &mut lox_auth)).await; -} - -fn handle_request(request: Vec, lox_auth: &mut BridgeAuth) -> Vec { - match RequestType::try_from(request[0]).unwrap() { - RequestType::BlockageMigration => { - let decoded: blockage_migration::Request = bincode::deserialize(&request[1..]).unwrap(); - let resp = lox_auth.handle_blockage_migration(decoded).unwrap(); - bincode::serialize(&resp).unwrap() - }, - RequestType::CheckBlockage => { - let decoded: check_blockage::Request = bincode::deserialize(&request[1..]).unwrap(); - let resp = lox_auth.handle_check_blockage(decoded).unwrap(); - bincode::serialize(&resp).unwrap() - }, - RequestType::IssueInvite => { - let decoded: issue_invite::Request = bincode::deserialize(&request[1..]).unwrap(); - let resp = lox_auth.handle_issue_invite(decoded).unwrap(); - bincode::serialize(&resp).unwrap() - }, - RequestType::LevelUp => { - let decoded: level_up::Request = bincode::deserialize(&request[1..]).unwrap(); - let resp = lox_auth.handle_level_up(decoded).unwrap(); - bincode::serialize(&resp).unwrap() - }, - RequestType::Migration => { - let decoded: migration::Request = bincode::deserialize(&request[1..]).unwrap(); - let resp = lox_auth.handle_migration(decoded).unwrap(); - bincode::serialize(&resp).unwrap() - }, - RequestType::OpenInvite => { - let decoded: open_invite::Request = bincode::deserialize(&request[1..]).unwrap(); - let resp = lox_auth.handle_open_invite(decoded).unwrap(); - bincode::serialize(&resp).unwrap() - }, - RequestType::RedeemInvite => { - let decoded: redeem_invite::Request = bincode::deserialize(&request[1..]).unwrap(); - let resp = lox_auth.handle_redeem_invite(decoded).unwrap(); - bincode::serialize(&resp).unwrap() - }, - RequestType::TrustPromotion => { - let decoded: trust_promotion::Request = bincode::deserialize(&request[1..]).unwrap(); - let resp = lox_auth.handle_trust_promotion(decoded).unwrap(); - bincode::serialize(&resp).unwrap() - }, - } -} diff --git a/src/request_type.rs b/src/request_type.rs deleted file mode 100644 index 1a7e993..0000000 --- a/src/request_type.rs +++ /dev/null @@ -1,29 +0,0 @@ -/*! Types of requests we might encounter. */ -pub enum RequestType { - BlockageMigration, - CheckBlockage, - IssueInvite, - LevelUp, - Migration, - OpenInvite, - RedeemInvite, - TrustPromotion, -} - -impl TryFrom for RequestType { - type Error = &'static str; - - fn try_from(value: u8) -> Result { - match value { - 1 => Ok(RequestType::BlockageMigration), - 2 => Ok(RequestType::CheckBlockage), - 3 => Ok(RequestType::IssueInvite), - 4 => Ok(RequestType::LevelUp), - 5 => Ok(RequestType::Migration), - 6 => Ok(RequestType::OpenInvite), - 7 => Ok(RequestType::RedeemInvite), - 8 => Ok(RequestType::TrustPromotion), - _ => Err("Byte given does not represent a valid request type."), - } - } -} diff --git a/src/server_net.rs b/src/server_net.rs deleted file mode 100644 index 8f7f345..0000000 --- a/src/server_net.rs +++ /dev/null @@ -1,67 +0,0 @@ -/*! The networking methods for our server components to call. In -particular, this file provides a listen() methods to handle creating -a server process so the other code doesn't need to care about how -these work. */ - -// This file is based on tokio's echo.rs example: -// https://github.com/tokio-rs/tokio/blob/master/examples/echo.rs - -use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use tokio::net::TcpListener; - -pub async fn listen(addr: String, fun: &mut dyn FnMut(Vec) -> Vec) { - let listener = TcpListener::bind(&addr) - .await - .expect("Failed to create TcpListener"); - println!("Listening on: {}", addr); - - loop { - // asynchronously wait for an inbound socket - let (mut socket, _) = listener.accept().await.expect("Failed to create socket"); - -// tokio::spawn(async move { - loop { - // get number of bytes to receive - let mut nbuf: [u8; 4] = [0; 4]; - socket - .read(&mut nbuf) - .await - .expect("Failed to get number of bytes to read"); - let n = u32::from_be_bytes(nbuf); - - if n == 0 { - break; -// return; - } - - let mut buf = vec![0; n.try_into().unwrap()]; - - // receive data - socket - .read(&mut buf) - .await - .expect("Failed to read data from socket"); - - if n == 0 { - break; - // return; - } - - let response = fun(buf); - - // send number of bytes in response - let response_size = u32::to_be_bytes(response.len().try_into().unwrap()); - socket - .write_all(&response_size) - .await - .expect("Failed to write number of bytes to listen for"); - - // send response - socket - .write_all(&response) - .await - .expect("Failed to write data to socket"); - } -// }); - } -}