main.rs 7.2 KB

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