Browse Source

dec not working yet

opentrade 4 years ago
parent
commit
4662b05d73
3 changed files with 38 additions and 6 deletions
  1. 1 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 36 6
      src/lic.rs

+ 1 - 0
Cargo.lock

@@ -624,6 +624,7 @@ dependencies = [
624 624
  "lazy_static",
625 625
  "mac_address",
626 626
  "machine-uid",
627
+ "rand 0.8.3",
627 628
  "reqwest",
628 629
  "rocksdb",
629 630
  "rust-ini",

+ 1 - 0
Cargo.toml

@@ -24,6 +24,7 @@ machine-uid = "0.2"
24 24
 mac_address = "1.1"
25 25
 whoami = "0.9"
26 26
 base64 = "0.13"
27
+rand = "0.8"
27 28
 
28 29
 [build-dependencies]
29 30
 hbb_common = { path = "libs/hbb_common" }

+ 36 - 6
src/lic.rs

@@ -1,4 +1,12 @@
1
-use hbb_common::{bail, log, sodiumoxide::crypto::sign, ResultType};
1
+use hbb_common::{
2
+    bail, log,
3
+    sodiumoxide::crypto::{
4
+        secretbox::{self, Key, Nonce},
5
+        sign,
6
+    },
7
+    ResultType,
8
+};
9
+use rand::Rng;
2 10
 use serde_derive::{Deserialize, Serialize};
3 11
 use std::io::prelude::*;
4 12
 use std::path::Path;
@@ -21,6 +29,8 @@ pub struct Post {
21 29
     email: String,
22 30
     #[serde(default)]
23 31
     status: String,
32
+    #[serde(default)]
33
+    nonce: usize,
24 34
 }
25 35
 
26 36
 const LICENSE_FILE: &'static str = ".license.txt";
@@ -66,18 +76,27 @@ fn write_lic(lic: &License) {
66 76
 }
67 77
 
68 78
 fn check_email(lic: License, email: String) -> ResultType<bool> {
79
+    log::info!("Checking email with the server ...");
80
+    let mut rng = rand::thread_rng();
81
+    let nonce: usize = rng.gen();
69 82
     use reqwest::blocking::Client;
70
-    let p: Post = Client::new()
83
+    let resp = Client::new()
71 84
         .post("http://rustdesk.com/api/check-email")
72 85
         .json(&Post {
73 86
             lic,
74 87
             email,
88
+            nonce,
75 89
             ..Default::default()
76 90
         })
77
-        .send()?
78
-        .json()?;
79
-    if !p.status.is_empty() {
80
-        bail!("{}", p.status);
91
+        .send()?;
92
+    if resp.status().is_success() {
93
+        let text = base64::decode(resp.text()?)?;
94
+        let p = dec_data(&text, nonce)?;
95
+        if !p.status.is_empty() {
96
+            bail!("{}", p.status);
97
+        }
98
+    } else {
99
+        bail!("Server error: {}", resp.status());
81 100
     }
82 101
     Ok(true)
83 102
 }
@@ -127,5 +146,16 @@ fn dec_lic(s: &str) -> ResultType<License> {
127 146
     }
128 147
 }
129 148
 
149
+fn dec_data(data: &[u8], n: usize) -> ResultType<Post> {
150
+    let key = b"\xa94\xb4\xb4\xda\xf82\x96\x8b\xb0\x9d\x04d\"\x94T\xa6\xdb\xf6\xd5i=Y.\xf5\xf5i\xa9\x14\x91\xa7\xa9";
151
+    let mut nonce = Nonce([0u8; secretbox::NONCEBYTES]);
152
+    nonce.0[..std::mem::size_of_val(&n)].copy_from_slice(&n.to_le_bytes());
153
+    let key = secretbox::Key(*key);
154
+    if let Ok(res) = secretbox::open(&data, &nonce, &key) {
155
+        return Ok(serde_json::from_slice::<Post>(&res)?);
156
+    }
157
+    bail!("Encryption error");
158
+}
159
+
130 160
 pub const EMAIL_ARG: &'static str =
131 161
     "-m, --email=[EMAIL] 'Sets your email address registered with RustDesk'";