lox_cli/src/bin/lox_client_2.rs

66 lines
2.4 KiB
Rust

// During a later cleanup, this file will replace lox_client.rs.
// This seems like probably not the best way to do this, but it works.
#[path = "../client_lib.rs"]
mod client_lib;
use client_lib::*;
use lox::IssuerPubKey;
use std::env::args;
use std::fs::File;
use std::io::Write;
use std::path::Path;
#[tokio::main]
async fn main() {
// TODO: Do proper argument handling
let server_addr = args().nth(1).unwrap(); // must include http://
// Get Lox Authority public keys
// TODO: Make this filename configurable
let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
let lox_auth_pubkeys: Vec<IssuerPubKey> = if Path::new(lox_auth_pubkeys_filename).exists() {
// read in file
let lox_auth_pubkeys_infile = File::open(lox_auth_pubkeys_filename).unwrap();
serde_json::from_reader(lox_auth_pubkeys_infile).unwrap()
} else {
// download from Lox Auth
let pubkeys = get_lox_auth_keys(&server_addr).await;
// save to file for next time
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(&pubkeys).unwrap()
).expect("Failed to write to lox_auth pubkeys file");
pubkeys
};
let lox_pub = lox_auth_pubkeys[0].clone();
let migration_pub = lox_auth_pubkeys[1].clone();
let migrationkey_pub = lox_auth_pubkeys[2].clone();
let reachability_pub = lox_auth_pubkeys[3].clone();
let invitation_pub = lox_auth_pubkeys[4].clone();
// Get Lox Credential
// TODO: Make this filename configurable
let lox_cred_filename = "lox_cred.json";
let lox_cred = if Path::new(lox_cred_filename).exists() {
let lox_cred_infile = File::open(lox_cred_filename).unwrap();
serde_json::from_reader(lox_cred_infile).unwrap()
} else {
// get new credential based on an open invite
let open_invite = get_open_invitation(&server_addr).await;
let cred = get_lox_credential(&server_addr, open_invite, lox_pub).await;
// save to file for next time
let mut lox_cred_outfile = File::create(lox_cred_filename).expect("Failed to create lox credential file");
write!(
lox_cred_outfile,
"{}",
serde_json::to_string(&cred).unwrap()
).expect("Failed to write to lox credential file");
cred
};
}