lox_client_2.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // During a later cleanup, this file will replace lox_client.rs.
  2. // This seems like probably not the best way to do this, but it works.
  3. #[path = "../client_lib.rs"]
  4. mod client_lib;
  5. use client_lib::*;
  6. use lox::IssuerPubKey;
  7. use std::env::args;
  8. use std::fs::File;
  9. use std::io::Write;
  10. use std::path::Path;
  11. #[tokio::main]
  12. async fn main() {
  13. // TODO: Do proper argument handling
  14. let server_addr = args().nth(1).unwrap(); // must include http://
  15. // Get Lox Authority public keys
  16. // TODO: Make this filename configurable
  17. let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
  18. let lox_auth_pubkeys: Vec<IssuerPubKey> = if Path::new(lox_auth_pubkeys_filename).exists() {
  19. // read in file
  20. let lox_auth_pubkeys_infile = File::open(lox_auth_pubkeys_filename).unwrap();
  21. serde_json::from_reader(lox_auth_pubkeys_infile).unwrap()
  22. } else {
  23. // download from Lox Auth
  24. let pubkeys = get_lox_auth_keys(server_addr.clone()).await;
  25. // save to file for next time
  26. let mut lox_auth_pubkeys_outfile = File::create(lox_auth_pubkeys_filename).expect("Failed to create lox_auth pubkeys file");
  27. write!(
  28. lox_auth_pubkeys_outfile,
  29. "{}",
  30. serde_json::to_string(&pubkeys).unwrap()
  31. ).expect("Failed to write to lox_auth pubkeys file");
  32. pubkeys
  33. };
  34. let lox_pub = lox_auth_pubkeys[0].clone();
  35. let migration_pub = lox_auth_pubkeys[1].clone();
  36. let migrationkey_pub = lox_auth_pubkeys[2].clone();
  37. let reachability_pub = lox_auth_pubkeys[3].clone();
  38. let invitation_pub = lox_auth_pubkeys[4].clone();
  39. // Get Lox Credential
  40. // TODO: Make this filename configurable
  41. let lox_cred_filename = "lox_cred.json";
  42. let lox_cred = if Path::new(lox_cred_filename).exists() {
  43. let lox_cred_infile = File::open(lox_cred_filename).unwrap();
  44. serde_json::from_reader(lox_cred_infile).unwrap()
  45. } else {
  46. // get new credential based on an open invite
  47. let open_invite = get_open_invitation(server_addr.clone()).await;
  48. let cred = get_lox_credential(server_addr.clone(), open_invite, lox_pub).await;
  49. // save to file for next time
  50. let mut lox_cred_outfile = File::create(lox_cred_filename).expect("Failed to create lox credential file");
  51. write!(
  52. lox_cred_outfile,
  53. "{}",
  54. serde_json::to_string(&cred).unwrap()
  55. ).expect("Failed to write to lox credential file");
  56. cred
  57. };
  58. }