main.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. mod client_lib;
  2. use client_lib::*;
  3. mod client_net;
  4. use client_net::HyperNet;
  5. use curve25519_dalek::scalar::Scalar;
  6. use getopts::Options;
  7. use lox_library::bridge_table::BridgeLine;
  8. use lox_library::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. use std::str::FromStr;
  15. // Prints the argument details for this program
  16. fn print_usage(program: &str, opts: Options) {
  17. let brief = format!("Usage: {} [options]", program);
  18. print!("{}", opts.usage(&brief));
  19. }
  20. // Helper function to save serializable objects to files
  21. fn save_object<T: Serialize>(obj: T, filename: &str) {
  22. let mut outfile = File::create(filename).expect(&("Failed to create ".to_string() + filename));
  23. write!(outfile, "{}", serde_json::to_string(&obj).unwrap())
  24. .expect(&("Failed to write to ".to_string() + filename));
  25. }
  26. #[tokio::main]
  27. async fn main() {
  28. let args: Vec<String> = args().collect();
  29. let mut opts = Options::new();
  30. opts.optflag("h", "help", "print this help menu");
  31. //#[cfg(test)]
  32. opts.optopt(
  33. "A",
  34. "advance-days",
  35. "increase server days by NUM_DAYS",
  36. "NUM_DAYS",
  37. );
  38. opts.optflag("L", "level-up", "increase trust level");
  39. opts.optflag("N", "new-lox-cred", "get a new Lox Credential");
  40. opts.optopt(
  41. "",
  42. "server",
  43. "Lox Auth server address [http://localhost:8001]",
  44. "ADDR",
  45. );
  46. let matches = match opts.parse(&args[1..]) {
  47. Ok(m) => m,
  48. Err(f) => {
  49. panic!("{}", f.to_string())
  50. }
  51. };
  52. if matches.opt_present("h") {
  53. print_usage(&args[0], opts);
  54. return;
  55. }
  56. let net = if matches.opt_present("server") {
  57. HyperNet {
  58. hostname: matches.opt_str("server").unwrap(),
  59. }
  60. } else {
  61. HyperNet {
  62. hostname: "http://localhost:8001".to_string(),
  63. }
  64. };
  65. // Advance days on server (TESTING ONLY)
  66. //#[cfg(test)]
  67. if matches.opt_present("A") {
  68. let days: u16 = u16::from_str(matches.opt_str("A").unwrap().as_str()).unwrap();
  69. let today: u32 = advance_days(&net, days).await;
  70. println!("Today's date according to the server: {}", today);
  71. }
  72. // Get Lox Authority public keys
  73. // TODO: Make this filename configurable
  74. let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
  75. let lox_auth_pubkeys: Vec<IssuerPubKey> = if Path::new(lox_auth_pubkeys_filename).exists() {
  76. // read in file
  77. let lox_auth_pubkeys_infile = File::open(lox_auth_pubkeys_filename).unwrap();
  78. serde_json::from_reader(lox_auth_pubkeys_infile).unwrap()
  79. } else {
  80. // download from Lox Auth
  81. let pubkeys = get_lox_auth_keys(&net).await;
  82. // save to file for next time
  83. save_object(&pubkeys, &lox_auth_pubkeys_filename);
  84. pubkeys
  85. };
  86. // Get Lox Credential and BridgeLine
  87. let lox_cred_filename = "lox_cred.json";
  88. let bridgeline_filename = "bridgeline.json";
  89. let (lox_cred, bridgeline) = if matches.opt_present("N")
  90. || !Path::new(lox_cred_filename).exists()
  91. || !Path::new(bridgeline_filename).exists()
  92. {
  93. // get new Lox Credential
  94. let open_invite = get_open_invitation(&net).await;
  95. let (cred, bl) =
  96. get_lox_credential(&net, &open_invite, get_lox_pub(&lox_auth_pubkeys)).await;
  97. // save to files for next time
  98. save_object(&cred, &lox_cred_filename);
  99. save_object(&bl, &bridgeline_filename);
  100. (cred, bl)
  101. } else {
  102. // Read existing Lox Credential and BridgeLine from files
  103. let cred = serde_json::from_reader(File::open(lox_cred_filename).unwrap()).unwrap();
  104. let bl = serde_json::from_reader(File::open(bridgeline_filename).unwrap()).unwrap();
  105. (cred, bl)
  106. };
  107. let lox_cred = if matches.opt_present("L") {
  108. let old_level = get_cred_trust_level(&lox_cred);
  109. // If trust level is 0, do trust promotion, otherwise level up.
  110. let cred = if old_level == 0 {
  111. if eligible_for_trust_promotion(&net, &lox_cred).await {
  112. let migration_cred =
  113. trust_promotion(&net, &lox_cred, get_lox_pub(&lox_auth_pubkeys)).await;
  114. let cred = trust_migration(
  115. &net,
  116. &lox_cred,
  117. &migration_cred,
  118. get_lox_pub(&lox_auth_pubkeys),
  119. get_migration_pub(&lox_auth_pubkeys),
  120. )
  121. .await;
  122. cred
  123. } else {
  124. lox_cred
  125. }
  126. } else {
  127. if eligible_for_level_up(&net, &lox_cred).await {
  128. let encbuckets = get_reachability_credential(&net).await;
  129. let cred = level_up(
  130. &net,
  131. &lox_cred,
  132. &encbuckets,
  133. get_lox_pub(&lox_auth_pubkeys),
  134. get_reachability_pub(&lox_auth_pubkeys),
  135. )
  136. .await;
  137. cred
  138. } else {
  139. lox_cred
  140. }
  141. };
  142. save_object(&cred, &lox_cred_filename);
  143. let new_level = get_cred_trust_level(&cred);
  144. if new_level > old_level {
  145. println!("Old level: {}\nNew level: {}", old_level, new_level);
  146. } else if new_level == old_level {
  147. println!("Unable to level up. Current level: {}", new_level);
  148. }
  149. cred
  150. } else {
  151. lox_cred
  152. };
  153. }