Browse Source

better error management

Paolo Asperti 3 years ago
parent
commit
26549d7e7e
1 changed files with 29 additions and 42 deletions
  1. 29 42
      src/utils.rs

+ 29 - 42
src/utils.rs

@@ -1,14 +1,17 @@
1
+use hbb_common::{bail, ResultType};
1 2
 use sodiumoxide::crypto::sign;
2 3
 use std::env;
3 4
 use std::process;
4 5
 use std::str;
5 6
 
6 7
 fn print_help() {
7
-    println!("Usage:");
8
-    println!("  rustdesk-util [command]\n");
9
-    println!("Available Commands:");
10
-    println!("  genkeypair                                   Generate a new keypair");
11
-    println!("  validatekeypair [public key] [secret key]    Validate an existing keypair");
8
+    println!(
9
+        "Usage:
10
+    rustdesk-util [command]\n
11
+Available Commands:
12
+    genkeypair                                   Generate a new keypair
13
+    validatekeypair [public key] [secret key]    Validate an existing keypair"
14
+    );
12 15
     process::exit(0x0001);
13 16
 }
14 17
 
@@ -25,65 +28,44 @@ fn gen_keypair() {
25 28
     println!("Secret Key:  {secret_key}");
26 29
 }
27 30
 
28
-fn validate_keypair(pk: &str, sk: &str) {
31
+fn validate_keypair(pk: &str, sk: &str) -> ResultType<()> {
29 32
     let sk1 = base64::decode(&sk);
30
-    match sk1 {
31
-        Ok(_) => {}
32
-        Err(_) => {
33
-            println!("Invalid secret key");
34
-            process::exit(0x0001);
35
-        }
33
+    if sk1.is_err() {
34
+        bail!("Invalid secret key");
36 35
     }
37 36
     let sk1 = sk1.unwrap();
38 37
 
39 38
     let secret_key = sign::SecretKey::from_slice(sk1.as_slice());
40
-    match secret_key {
41
-        Some(_) => {}
42
-        None => {
43
-            println!("Invalid Secret key");
44
-            process::exit(0x0001);
45
-        }
39
+    if secret_key.is_none() {
40
+        bail!("Invalid Secret key");
46 41
     }
47 42
     let secret_key = secret_key.unwrap();
48 43
 
49 44
     let pk1 = base64::decode(&pk);
50
-    match pk1 {
51
-        Ok(_) => {}
52
-        Err(_) => {
53
-            println!("Invalid public key");
54
-            process::exit(0x0001);
55
-        }
45
+    if pk1.is_err() {
46
+        bail!("Invalid public key");
56 47
     }
57 48
     let pk1 = pk1.unwrap();
58 49
 
59 50
     let public_key = sign::PublicKey::from_slice(pk1.as_slice());
60
-    match public_key {
61
-        Some(_) => {}
62
-        None => {
63
-            println!("Invalid Public key");
64
-            process::exit(0x0001);
65
-        }
51
+    if public_key.is_none() {
52
+        bail!("Invalid Public key");
66 53
     }
67 54
     let public_key = public_key.unwrap();
68 55
 
69 56
     let random_data_to_test = b"This is meh.";
70 57
     let signed_data = sign::sign(random_data_to_test, &secret_key);
71 58
     let verified_data = sign::verify(&signed_data, &public_key);
72
-    match verified_data {
73
-        Ok(_) => {}
74
-        Err(_) => {
75
-            println!("Key pair is INVALID");
76
-            process::exit(0x0001);
77
-        }
59
+    if verified_data.is_err() {
60
+        bail!("Key pair is INVALID");
78 61
     }
79 62
     let verified_data = verified_data.unwrap();
80 63
 
81
-    if random_data_to_test == &verified_data[..] {
82
-        println!("Key pair is VALID");
83
-    } else {
84
-        println!("Key pair is INVALID");
85
-        process::exit(0x0001);
64
+    if random_data_to_test != &verified_data[..] {
65
+        bail!("Key pair is INVALID");
86 66
     }
67
+
68
+    Ok(())
87 69
 }
88 70
 
89 71
 fn main() {
@@ -99,7 +81,12 @@ fn main() {
99 81
             if args.len() <= 3 {
100 82
                 error_then_help("You must supply both the public and the secret key");
101 83
             }
102
-            validate_keypair(args[2].as_str(), args[3].as_str());
84
+            let res = validate_keypair(args[2].as_str(), args[3].as_str());
85
+            if let Err(e) = res {
86
+                println!("{}", e);
87
+                process::exit(0x0001);
88
+            }
89
+            println!("Key pair is VALID");
103 90
         }
104 91
         _ => print_help(),
105 92
     }