main.rs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. //#[cfg(test)]
  39. opts.optopt(
  40. "A",
  41. "advance-days",
  42. "increase server days by NUM_DAYS",
  43. "NUM_DAYS",
  44. );
  45. opts.optflag("I", "invite", "generate invitation for a friend");
  46. opts.optflag("L", "level-up", "increase trust level");
  47. opts.optflag("N", "new-lox-cred", "get a new Lox Credential");
  48. opts.optopt("R", "redeem", "redeem invitation", "INVITE_FILE");
  49. opts.optopt(
  50. "",
  51. "server",
  52. "Lox Auth server address [http://localhost:8001]",
  53. "ADDR",
  54. );
  55. let matches = match opts.parse(&args[1..]) {
  56. Ok(m) => m,
  57. Err(f) => {
  58. panic!("{}", f.to_string())
  59. }
  60. };
  61. if matches.opt_present("h") {
  62. print_usage(&args[0], opts);
  63. return;
  64. }
  65. let net = if matches.opt_present("server") {
  66. HyperNet {
  67. hostname: matches.opt_str("server").unwrap(),
  68. }
  69. } else {
  70. HyperNet {
  71. hostname: "http://localhost:8001".to_string(),
  72. }
  73. };
  74. // Advance days on server (TESTING ONLY)
  75. //#[cfg(test)]
  76. if matches.opt_present("A") {
  77. let days: u16 = u16::from_str(matches.opt_str("A").unwrap().as_str()).unwrap();
  78. let today: u32 = advance_days(&net, days).await;
  79. println!("Today's date according to the server: {}", today);
  80. }
  81. // Get Lox Authority public keys
  82. let lox_auth_pubkeys: Vec<IssuerPubKey> = if Path::new(lox_auth_pubkeys_filename).exists() {
  83. // read in file
  84. let lox_auth_pubkeys_infile = File::open(lox_auth_pubkeys_filename).unwrap();
  85. serde_json::from_reader(lox_auth_pubkeys_infile).unwrap()
  86. } else {
  87. // download from Lox Auth
  88. let pubkeys = get_lox_auth_keys(&net).await;
  89. // save to file for next time
  90. save_object(&pubkeys, &lox_auth_pubkeys_filename);
  91. pubkeys
  92. };
  93. // Get Lox Credential and Bucket
  94. let (lox_cred, bucket) = if matches.opt_present("R") && Path::new(invite_filename).exists() {
  95. // redeem invite
  96. let invite_infile = File::open(invite_filename).unwrap();
  97. let invite = serde_json::from_reader(invite_infile).unwrap();
  98. let (cred, bucket) = redeem_invite(
  99. &net,
  100. &invite,
  101. get_lox_pub(&lox_auth_pubkeys),
  102. get_invitation_pub(&lox_auth_pubkeys),
  103. )
  104. .await;
  105. // save to files for future use
  106. save_object(&cred, &lox_cred_filename);
  107. save_object(&bucket, &bucket_filename);
  108. (cred, bucket)
  109. } else if matches.opt_present("N")
  110. || !Path::new(lox_cred_filename).exists()
  111. || !Path::new(bucket_filename).exists()
  112. {
  113. // get new Lox Credential
  114. let open_invite = get_open_invitation(&net).await;
  115. let (cred, bl) =
  116. get_lox_credential(&net, &open_invite, get_lox_pub(&lox_auth_pubkeys)).await;
  117. let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET];
  118. // note: this is a bucket with one real bridgeline and n-1
  119. // default (zeroed out) bridgelines
  120. bucket[0] = bl;
  121. // save to files for future use
  122. save_object(&cred, &lox_cred_filename);
  123. save_object(&bucket, &bucket_filename);
  124. (cred, bucket)
  125. } else {
  126. // Read existing Lox Credential and BridgeLine from files
  127. let cred = serde_json::from_reader(File::open(lox_cred_filename).unwrap()).unwrap();
  128. let bucket = serde_json::from_reader(File::open(bucket_filename).unwrap()).unwrap();
  129. (cred, bucket)
  130. };
  131. let (lox_cred, bucket) = if matches.opt_present("L") {
  132. let old_level = get_cred_trust_level(&lox_cred);
  133. // If trust level is 0, do trust promotion, otherwise level up.
  134. let (cred, bucket) = if old_level == 0 {
  135. if eligible_for_trust_promotion(&net, &lox_cred).await {
  136. let migration_cred =
  137. trust_promotion(&net, &lox_cred, get_lox_pub(&lox_auth_pubkeys)).await;
  138. let cred = trust_migration(
  139. &net,
  140. &lox_cred,
  141. &migration_cred,
  142. get_lox_pub(&lox_auth_pubkeys),
  143. get_migration_pub(&lox_auth_pubkeys),
  144. )
  145. .await;
  146. let bucket = get_bucket(&net, &cred).await;
  147. (cred, bucket)
  148. } else {
  149. (lox_cred, bucket)
  150. }
  151. } else {
  152. if eligible_for_level_up(&net, &lox_cred).await {
  153. let encbuckets = get_reachability_credential(&net).await;
  154. let (cred, bucket) = level_up(
  155. &net,
  156. &lox_cred,
  157. &encbuckets,
  158. get_lox_pub(&lox_auth_pubkeys),
  159. get_reachability_pub(&lox_auth_pubkeys),
  160. )
  161. .await;
  162. (cred, bucket)
  163. } else {
  164. (lox_cred, bucket)
  165. }
  166. };
  167. save_object(&cred, &lox_cred_filename);
  168. save_object(&bucket, &bucket_filename);
  169. let new_level = get_cred_trust_level(&cred);
  170. if new_level > old_level {
  171. println!("Old level: {}\nNew level: {}", old_level, new_level);
  172. } else if new_level == old_level {
  173. println!("Unable to level up. Current level: {}", new_level);
  174. }
  175. (cred, bucket)
  176. } else {
  177. (lox_cred, bucket)
  178. };
  179. // Invite a friend
  180. let lox_cred = if matches.opt_present("I") {
  181. if scalar_u32(&lox_cred.invites_remaining).unwrap() > 0 {
  182. let (cred, invite) = issue_invite(
  183. &net,
  184. &lox_cred,
  185. &get_reachability_credential(&net).await,
  186. get_lox_pub(&lox_auth_pubkeys),
  187. get_reachability_pub(&lox_auth_pubkeys),
  188. get_invitation_pub(&lox_auth_pubkeys),
  189. )
  190. .await;
  191. // TODO: Make this unique per-run (e.g., add timestamp)
  192. save_object(&invite, &invite_filename);
  193. save_object(&cred, &lox_cred_filename);
  194. println!("Invite saved in {}", &invite_filename);
  195. println!(
  196. "Invites left: {}",
  197. scalar_u32(&cred.invites_remaining).unwrap()
  198. );
  199. cred
  200. } else {
  201. println!("No invites left");
  202. lox_cred
  203. }
  204. } else {
  205. lox_cred
  206. };
  207. }