main.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.unwrap();
  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. .unwrap();
  84. // save to files for future use
  85. save_object(&cred, &lox_cred_filename);
  86. save_object(&bucket, &bucket_filename);
  87. (cred, bucket)
  88. } else if matches.opt_present("N")
  89. || !Path::new(lox_cred_filename).exists()
  90. || !Path::new(bucket_filename).exists()
  91. {
  92. // get new Lox Credential
  93. let open_invite = get_open_invitation(&net).await.unwrap();
  94. let (cred, bl) = get_lox_credential(&net, &open_invite, get_lox_pub(&lox_auth_pubkeys))
  95. .await
  96. .unwrap();
  97. let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET];
  98. // note: this is a bucket with one real bridgeline and n-1
  99. // default (zeroed out) bridgelines
  100. bucket[0] = bl;
  101. // save to files for future use
  102. save_object(&cred, &lox_cred_filename);
  103. save_object(&bucket, &bucket_filename);
  104. (cred, bucket)
  105. } else {
  106. // Read existing Lox Credential and BridgeLine from files
  107. let cred = serde_json::from_reader(File::open(lox_cred_filename).unwrap()).unwrap();
  108. let bucket = serde_json::from_reader(File::open(bucket_filename).unwrap()).unwrap();
  109. (cred, bucket)
  110. };
  111. let (lox_cred, bucket) = if matches.opt_present("L") {
  112. let old_level = scalar_u32(&lox_cred.trust_level).unwrap();
  113. // If trust level is 0, do trust promotion, otherwise level up.
  114. let (cred, bucket) = if old_level == 0 {
  115. if eligible_for_trust_promotion(&net, &lox_cred).await {
  116. let migration_cred =
  117. trust_promotion(&net, &lox_cred, get_lox_pub(&lox_auth_pubkeys))
  118. .await
  119. .unwrap();
  120. let cred = trust_migration(
  121. &net,
  122. &lox_cred,
  123. &migration_cred,
  124. get_lox_pub(&lox_auth_pubkeys),
  125. get_migration_pub(&lox_auth_pubkeys),
  126. )
  127. .await
  128. .unwrap();
  129. let bucket = get_bucket(&net, &cred).await.unwrap().0;
  130. (cred, bucket)
  131. } else {
  132. (lox_cred, bucket)
  133. }
  134. } else {
  135. if eligible_for_level_up(&net, &lox_cred).await {
  136. let (bucket, reachcred) = get_bucket(&net, &lox_cred).await.unwrap();
  137. let cred = level_up(
  138. &net,
  139. &lox_cred,
  140. &reachcred.unwrap(),
  141. get_lox_pub(&lox_auth_pubkeys),
  142. get_reachability_pub(&lox_auth_pubkeys),
  143. )
  144. .await
  145. .unwrap();
  146. let bucket = get_bucket(&net, &lox_cred).await.unwrap().0;
  147. (cred, bucket)
  148. } else {
  149. (lox_cred, bucket)
  150. }
  151. };
  152. save_object(&cred, &lox_cred_filename);
  153. save_object(&bucket, &bucket_filename);
  154. let new_level = scalar_u32(&cred.trust_level).unwrap();
  155. if new_level > old_level {
  156. println!("Old level: {}\nNew level: {}", old_level, new_level);
  157. } else if new_level == old_level {
  158. println!("Unable to level up. Current level: {}", new_level);
  159. }
  160. (cred, bucket)
  161. } else {
  162. (lox_cred, bucket)
  163. };
  164. // Invite a friend
  165. let lox_cred = if matches.opt_present("I") {
  166. if scalar_u32(&lox_cred.invites_remaining).unwrap() > 0 {
  167. let (cred, invite) = issue_invite(
  168. &net,
  169. &lox_cred,
  170. &get_reachability_credential(&net).await.unwrap(),
  171. get_lox_pub(&lox_auth_pubkeys),
  172. get_reachability_pub(&lox_auth_pubkeys),
  173. get_invitation_pub(&lox_auth_pubkeys),
  174. )
  175. .await
  176. .unwrap();
  177. // TODO: Make this unique per-run (e.g., add timestamp)
  178. save_object(&invite, &invite_filename);
  179. save_object(&cred, &lox_cred_filename);
  180. println!("Invite saved in {}", &invite_filename);
  181. println!(
  182. "Invites left: {}",
  183. scalar_u32(&cred.invites_remaining).unwrap()
  184. );
  185. cred
  186. } else {
  187. println!("No invites left");
  188. lox_cred
  189. }
  190. } else {
  191. lox_cred
  192. };
  193. }