lox_cli/src/bin/lox_client.rs

77 lines
2.8 KiB
Rust
Raw Normal View History

// This seems like probably not the best way to do this, but it works.
2023-03-19 14:47:08 -04:00
#[path = "../client_lib.rs"]
mod client_lib;
use client_lib::*;
use lox::IssuerPubKey;
use std::env::args;
use std::fs::File;
2023-03-19 14:47:08 -04:00
use std::io::Write;
use std::path::Path;
#[tokio::main]
async fn main() {
2023-03-19 14:47:08 -04:00
// TODO: Do proper argument handling
let server_addr = args().nth(1).unwrap(); // must include http://
2023-03-19 14:47:08 -04:00
// Get Lox Authority public keys
// TODO: Make this filename configurable
let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
2023-03-19 14:47:08 -04:00
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
};
2023-03-19 14:47:08 -04:00
// Get Lox Credential and BridgeLine
// TODO: Make these filenames configurable
let lox_cred_filename = "lox_cred.json";
let bridgeline_filename = "bridgeline.json";
let (lox_cred, bridgeline) = if Path::new(lox_cred_filename).exists()
&& Path::new(bridgeline_filename).exists()
{
let lox_cred_infile = File::open(lox_cred_filename).unwrap();
let lox_cred = serde_json::from_reader(lox_cred_infile).unwrap();
let bridgeline_infile = File::open(bridgeline_filename).unwrap();
let bridgeline = serde_json::from_reader(bridgeline_infile).unwrap();
(lox_cred, bridgeline)
} else {
// get new credential based on an open invite
let open_invite = get_open_invitation(&server_addr).await;
let (cred, bridgeline) =
get_lox_credential(&server_addr, &open_invite, get_lox_pub(&lox_auth_pubkeys)).await;
// save to files 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");
let mut bridgeline_outfile =
File::create(bridgeline_filename).expect("Failed to create bridgeline file");
write!(
bridgeline_outfile,
"{}",
serde_json::to_string(&bridgeline).unwrap()
)
.expect("Failed to write to bridgeline file");
(cred, bridgeline)
};
}