lox_client.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // This seems like probably not the best way to do this, but it works.
  2. #[path = "../client_lib.rs"]
  3. mod client_lib;
  4. use client_lib::*;
  5. use curve25519_dalek::scalar::Scalar;
  6. use getopts::Options;
  7. use lox::bridge_table::BridgeLine;
  8. use lox::IssuerPubKey;
  9. use serde::Serialize;
  10. use std::env::args;
  11. use std::fs::File;
  12. use std::io::Write;
  13. use std::path::Path;
  14. // Prints the argument details for this program
  15. fn print_usage(program: &str, opts: Options) {
  16. let brief = format!("Usage: {} [options]", program);
  17. print!("{}", opts.usage(&brief));
  18. }
  19. // Helper function to save serializable objects to files
  20. fn save_object<T: Serialize>(obj: T, filename: &str) {
  21. let mut outfile = File::create(filename).expect(&("Failed to create ".to_string() + filename));
  22. write!(outfile, "{}", serde_json::to_string(&obj).unwrap())
  23. .expect(&("Failed to write to ".to_string() + filename));
  24. }
  25. #[tokio::main]
  26. async fn main() {
  27. let args: Vec<String> = args().collect();
  28. let mut opts = Options::new();
  29. opts.optflag("h", "help", "print this help menu");
  30. opts.optflag("L", "level-up", "increase trust level");
  31. opts.optflag("N", "new-lox-cred", "get a new Lox Credential");
  32. opts.optopt(
  33. "",
  34. "server",
  35. "Lox Auth server address [http://localhost:8001]",
  36. "ADDR",
  37. );
  38. let matches = match opts.parse(&args[1..]) {
  39. Ok(m) => m,
  40. Err(f) => {
  41. panic!("{}", f.to_string())
  42. }
  43. };
  44. if matches.opt_present("h") {
  45. print_usage(&args[0], opts);
  46. return;
  47. }
  48. let server_addr = if matches.opt_present("server") {
  49. matches.opt_str("server").unwrap()
  50. } else {
  51. "http://localhost:8001".to_string()
  52. };
  53. // Get Lox Authority public keys
  54. // TODO: Make this filename configurable
  55. let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
  56. let lox_auth_pubkeys: Vec<IssuerPubKey> = if Path::new(lox_auth_pubkeys_filename).exists() {
  57. // read in file
  58. let lox_auth_pubkeys_infile = File::open(lox_auth_pubkeys_filename).unwrap();
  59. serde_json::from_reader(lox_auth_pubkeys_infile).unwrap()
  60. } else {
  61. // download from Lox Auth
  62. let pubkeys = get_lox_auth_keys(&server_addr).await;
  63. // save to file for next time
  64. save_object(&pubkeys, &lox_auth_pubkeys_filename);
  65. pubkeys
  66. };
  67. // Get Lox Credential and BridgeLine
  68. let lox_cred_filename = "lox_cred.json";
  69. let bridgeline_filename = "bridgeline.json";
  70. let (lox_cred, bridgeline) = if matches.opt_present("N")
  71. || !Path::new(lox_cred_filename).exists()
  72. || !Path::new(bridgeline_filename).exists()
  73. {
  74. // get new Lox Credential
  75. let open_invite = get_open_invitation(&server_addr).await;
  76. let (cred, bl) =
  77. get_lox_credential(&server_addr, &open_invite, get_lox_pub(&lox_auth_pubkeys)).await;
  78. // save to files for next time
  79. save_object(&cred, &lox_cred_filename);
  80. save_object(&bl, &bridgeline_filename);
  81. (cred, bl)
  82. } else {
  83. // Read existing Lox Credential and BridgeLine from files
  84. let cred = serde_json::from_reader(File::open(lox_cred_filename).unwrap()).unwrap();
  85. let bl = serde_json::from_reader(File::open(bridgeline_filename).unwrap()).unwrap();
  86. (cred, bl)
  87. };
  88. if matches.opt_present("L") {
  89. // If trust level is 0, do trust promotion, otherwise level up.
  90. if lox_cred.trust_level == Scalar::zero() {
  91. let migration_cred =
  92. trust_promotion(&server_addr, &lox_cred, get_lox_pub(&lox_auth_pubkeys)).await;
  93. let cred = trust_migration(
  94. &server_addr,
  95. &lox_cred,
  96. &migration_cred,
  97. get_lox_pub(&lox_auth_pubkeys),
  98. get_migration_pub(&lox_auth_pubkeys),
  99. )
  100. .await;
  101. } else {
  102. let encbuckets = get_reachability_credential(&server_addr).await;
  103. let cred = level_up(
  104. &server_addr,
  105. &lox_cred,
  106. &encbuckets,
  107. get_lox_pub(&lox_auth_pubkeys),
  108. get_reachability_pub(&lox_auth_pubkeys),
  109. )
  110. .await;
  111. }
  112. }
  113. }