main.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. use lox_cli::{self, networking::HyperNet, *};
  2. use getopts::Options;
  3. use lox_library::{
  4. bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET},
  5. scalar_u32, IssuerPubKey,
  6. };
  7. use serde::Serialize;
  8. use std::{env::args, fs::File, io::Write, path::Path};
  9. // Prints the argument details for this program
  10. fn print_usage(program: &str, opts: Options) {
  11. let brief = format!("Usage: {} [options]", program);
  12. print!("{}", opts.usage(&brief));
  13. }
  14. // Helper function to save serializable objects to files
  15. fn save_object<T: Serialize>(obj: T, filename: &str) {
  16. let mut outfile = File::create(filename).expect(&("Failed to create ".to_string() + filename));
  17. write!(outfile, "{}", serde_json::to_string(&obj).unwrap())
  18. .expect(&("Failed to write to ".to_string() + filename));
  19. }
  20. #[tokio::main]
  21. async fn main() {
  22. let args: Vec<String> = args().collect();
  23. // files used to store various data
  24. let bucket_filename = "bucket.json";
  25. let invite_filename = "invite.json";
  26. let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
  27. let lox_cred_filename = "lox_cred.json";
  28. let mut opts = Options::new();
  29. opts.optflag("h", "help", "print this help menu");
  30. opts.optflag("I", "invite", "generate invitation for a friend");
  31. opts.optflag("L", "level-up", "increase trust level");
  32. opts.optflag("N", "new-lox-cred", "get a new Lox Credential");
  33. opts.optopt("R", "redeem", "redeem invitation", "INVITE_FILE");
  34. opts.optopt(
  35. "",
  36. "server",
  37. "Lox Auth server address [http://localhost:8001]",
  38. "ADDR",
  39. );
  40. let matches = match opts.parse(&args[1..]) {
  41. Ok(m) => m,
  42. Err(f) => {
  43. panic!("{}", f.to_string())
  44. }
  45. };
  46. if matches.opt_present("h") {
  47. print_usage(&args[0], opts);
  48. return;
  49. }
  50. let net = if matches.opt_present("server") {
  51. HyperNet {
  52. hostname: matches.opt_str("server").unwrap(),
  53. }
  54. } else {
  55. HyperNet {
  56. hostname: "http://localhost:8001".to_string(),
  57. }
  58. };
  59. // Get Lox Authority public keys
  60. let lox_auth_pubkeys: Vec<IssuerPubKey> = if Path::new(lox_auth_pubkeys_filename).exists() {
  61. // read in file
  62. let lox_auth_pubkeys_infile = File::open(lox_auth_pubkeys_filename).unwrap();
  63. serde_json::from_reader(lox_auth_pubkeys_infile).unwrap()
  64. } else {
  65. // download from Lox Auth
  66. let pubkeys = get_lox_auth_keys(&net).await;
  67. // save to file for next time
  68. save_object(&pubkeys, &lox_auth_pubkeys_filename);
  69. pubkeys
  70. };
  71. // Get Lox Credential and Bucket
  72. let (lox_cred, bucket) = if matches.opt_present("R") && Path::new(invite_filename).exists() {
  73. // redeem invite
  74. let invite_infile = File::open(invite_filename).unwrap();
  75. let invite = serde_json::from_reader(invite_infile).unwrap();
  76. let (cred, bucket) = redeem_invite(
  77. &net,
  78. &invite,
  79. get_lox_pub(&lox_auth_pubkeys),
  80. get_invitation_pub(&lox_auth_pubkeys),
  81. )
  82. .await;
  83. // save to files for future use
  84. save_object(&cred, &lox_cred_filename);
  85. save_object(&bucket, &bucket_filename);
  86. (cred, bucket)
  87. } else if matches.opt_present("N")
  88. || !Path::new(lox_cred_filename).exists()
  89. || !Path::new(bucket_filename).exists()
  90. {
  91. // get new Lox Credential
  92. let open_invite = get_open_invitation(&net).await;
  93. let (cred, bl) =
  94. get_lox_credential(&net, &open_invite, get_lox_pub(&lox_auth_pubkeys)).await;
  95. let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET];
  96. // note: this is a bucket with one real bridgeline and n-1
  97. // default (zeroed out) bridgelines
  98. bucket[0] = bl;
  99. // save to files for future use
  100. save_object(&cred, &lox_cred_filename);
  101. save_object(&bucket, &bucket_filename);
  102. (cred, bucket)
  103. } else {
  104. // Read existing Lox Credential and BridgeLine from files
  105. let cred = serde_json::from_reader(File::open(lox_cred_filename).unwrap()).unwrap();
  106. let bucket = serde_json::from_reader(File::open(bucket_filename).unwrap()).unwrap();
  107. (cred, bucket)
  108. };
  109. let (lox_cred, bucket) = if matches.opt_present("L") {
  110. let old_level = scalar_u32(&lox_cred.trust_level).unwrap();
  111. // If trust level is 0, do trust promotion, otherwise level up.
  112. let (cred, bucket) = if old_level == 0 {
  113. if eligible_for_trust_promotion(&net, &lox_cred).await {
  114. let migration_cred =
  115. trust_promotion(&net, &lox_cred, get_lox_pub(&lox_auth_pubkeys)).await;
  116. let cred = trust_migration(
  117. &net,
  118. &lox_cred,
  119. &migration_cred,
  120. get_lox_pub(&lox_auth_pubkeys),
  121. get_migration_pub(&lox_auth_pubkeys),
  122. )
  123. .await;
  124. let bucket = get_bucket(&net, &cred).await.0;
  125. (cred, bucket)
  126. } else {
  127. (lox_cred, bucket)
  128. }
  129. } else {
  130. if eligible_for_level_up(&net, &lox_cred).await {
  131. let (bucket, reachcred) = get_bucket(&net, &lox_cred).await;
  132. let cred = level_up(
  133. &net,
  134. &lox_cred,
  135. &reachcred.unwrap(),
  136. get_lox_pub(&lox_auth_pubkeys),
  137. get_reachability_pub(&lox_auth_pubkeys),
  138. )
  139. .await;
  140. let bucket = get_bucket(&net, &lox_cred).await.0;
  141. (cred, bucket)
  142. } else {
  143. (lox_cred, bucket)
  144. }
  145. };
  146. save_object(&cred, &lox_cred_filename);
  147. save_object(&bucket, &bucket_filename);
  148. let new_level = scalar_u32(&cred.trust_level).unwrap();
  149. if new_level > old_level {
  150. println!("Old level: {}\nNew level: {}", old_level, new_level);
  151. } else if new_level == old_level {
  152. println!("Unable to level up. Current level: {}", new_level);
  153. }
  154. (cred, bucket)
  155. } else {
  156. (lox_cred, bucket)
  157. };
  158. // Invite a friend
  159. let lox_cred = if matches.opt_present("I") {
  160. if scalar_u32(&lox_cred.invites_remaining).unwrap() > 0 {
  161. let (cred, invite) = issue_invite(
  162. &net,
  163. &lox_cred,
  164. &get_reachability_credential(&net).await,
  165. get_lox_pub(&lox_auth_pubkeys),
  166. get_reachability_pub(&lox_auth_pubkeys),
  167. get_invitation_pub(&lox_auth_pubkeys),
  168. )
  169. .await;
  170. // TODO: Make this unique per-run (e.g., add timestamp)
  171. save_object(&invite, &invite_filename);
  172. save_object(&cred, &lox_cred_filename);
  173. println!("Invite saved in {}", &invite_filename);
  174. println!(
  175. "Invites left: {}",
  176. scalar_u32(&cred.invites_remaining).unwrap()
  177. );
  178. cred
  179. } else {
  180. println!("No invites left");
  181. lox_cred
  182. }
  183. } else {
  184. lox_cred
  185. };
  186. }