|
@@ -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<u8>| handle_request(request, &mut lox_auth)).await;
|
|
|
-}
|
|
|
-
|
|
|
-fn handle_request(request: Vec<u8>, lox_auth: &mut BridgeAuth) -> Vec<u8> {
|
|
|
- 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()
|
|
|
- },
|
|
|
- }
|
|
|
-}
|