opentrade лет назад: 4
Родитель
Сommit
1282e730af
5 измененных файлов с 120 добавлено и 3 удалено
  1. 29 1
      Cargo.lock
  2. 4 0
      Cargo.toml
  3. 1 1
      libs/hbb_common
  4. 84 0
      src/lic.rs
  5. 2 1
      src/main.rs

+ 29 - 1
Cargo.lock

@@ -618,15 +618,19 @@ dependencies = [
618
 name = "hbbs"
618
 name = "hbbs"
619
 version = "1.1.3"
619
 version = "1.1.3"
620
 dependencies = [
620
 dependencies = [
621
+ "base64 0.13.0",
621
  "clap",
622
  "clap",
622
  "hbb_common",
623
  "hbb_common",
623
  "lazy_static",
624
  "lazy_static",
625
+ "mac_address",
626
+ "machine-uid",
624
  "reqwest",
627
  "reqwest",
625
  "rocksdb",
628
  "rocksdb",
626
  "rust-ini",
629
  "rust-ini",
627
  "serde",
630
  "serde",
628
  "serde_derive",
631
  "serde_derive",
629
  "serde_json",
632
  "serde_json",
633
+ "whoami",
630
 ]
634
 ]
631
 
635
 
632
 [[package]]
636
 [[package]]
@@ -872,6 +876,15 @@ dependencies = [
872
  "winapi 0.3.9",
876
  "winapi 0.3.9",
873
 ]
877
 ]
874
 
878
 
879
+[[package]]
880
+name = "machine-uid"
881
+version = "0.2.0"
882
+source = "registry+https://github.com/rust-lang/crates.io-index"
883
+checksum = "1f1595709b0a7386bcd56ba34d250d626e5503917d05d32cdccddcd68603e212"
884
+dependencies = [
885
+ "winreg 0.6.2",
886
+]
887
+
875
 [[package]]
888
 [[package]]
876
 name = "matches"
889
 name = "matches"
877
 version = "0.1.8"
890
 version = "0.1.8"
@@ -1420,7 +1433,7 @@ dependencies = [
1420
  "wasm-bindgen",
1433
  "wasm-bindgen",
1421
  "wasm-bindgen-futures",
1434
  "wasm-bindgen-futures",
1422
  "web-sys",
1435
  "web-sys",
1423
- "winreg",
1436
+ "winreg 0.7.0",
1424
 ]
1437
 ]
1425
 
1438
 
1426
 [[package]]
1439
 [[package]]
@@ -2072,6 +2085,12 @@ dependencies = [
2072
  "libc",
2085
  "libc",
2073
 ]
2086
 ]
2074
 
2087
 
2088
+[[package]]
2089
+name = "whoami"
2090
+version = "0.9.0"
2091
+source = "registry+https://github.com/rust-lang/crates.io-index"
2092
+checksum = "7884773ab69074615cb8f8425d0e53f11710786158704fca70f53e71b0e05504"
2093
+
2075
 [[package]]
2094
 [[package]]
2076
 name = "winapi"
2095
 name = "winapi"
2077
 version = "0.2.8"
2096
 version = "0.2.8"
@@ -2115,6 +2134,15 @@ version = "0.4.0"
2115
 source = "registry+https://github.com/rust-lang/crates.io-index"
2134
 source = "registry+https://github.com/rust-lang/crates.io-index"
2116
 checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
2135
 checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
2117
 
2136
 
2137
+[[package]]
2138
+name = "winreg"
2139
+version = "0.6.2"
2140
+source = "registry+https://github.com/rust-lang/crates.io-index"
2141
+checksum = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9"
2142
+dependencies = [
2143
+ "winapi 0.3.9",
2144
+]
2145
+
2118
 [[package]]
2146
 [[package]]
2119
 name = "winreg"
2147
 name = "winreg"
2120
 version = "0.7.0"
2148
 version = "0.7.0"

+ 4 - 0
Cargo.toml

@@ -20,6 +20,10 @@ lazy_static = "1.4"
20
 clap = "2.33"
20
 clap = "2.33"
21
 rust-ini = "0.16"
21
 rust-ini = "0.16"
22
 reqwest = "0.10"
22
 reqwest = "0.10"
23
+machine-uid = "0.2"
24
+mac_address = "1.1"
25
+whoami = "0.9"
26
+base64 = "0.13"
23
 
27
 
24
 [build-dependencies]
28
 [build-dependencies]
25
 hbb_common = { path = "libs/hbb_common" }
29
 hbb_common = { path = "libs/hbb_common" }

+ 1 - 1
libs/hbb_common

@@ -1 +1 @@
1
-Subproject commit 20bea85903acbd701aed45d195e9206f2ea09edf
1
+Subproject commit 98257ca5b799ecae53e2fc3ec20bb997a2f6206c

+ 84 - 0
src/lic.rs

@@ -1,6 +1,90 @@
1
+use hbb_common::{bail, log, sodiumoxide::crypto::sign, ResultType};
2
+use serde_derive::{Deserialize, Serialize};
3
+use std::io::prelude::*;
4
+use std::path::Path;
5
+
6
+#[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)]
7
+pub struct License {
8
+    #[serde(default)]
9
+    pub hostname: String,
10
+    #[serde(default)]
11
+    pub uid: String,
12
+    #[serde(default)]
13
+    pub mac: String,
14
+}
15
+
16
+const LICENSE_FILE: &'static str = ".license.txt";
17
+
1
 pub fn check_lic(email: &str) -> bool {
18
 pub fn check_lic(email: &str) -> bool {
19
+    let lic = get_lic();
20
+    let path = Path::new(LICENSE_FILE);
21
+    if Path::is_file(&path) {
22
+        let contents = std::fs::read_to_string(&path).unwrap_or("".to_owned());
23
+        if let Ok(old_lic) = dec_lic(&contents) {
24
+            if lic == old_lic {
25
+                return true;
26
+            }
27
+        }
28
+    }
29
+
30
+    if email.is_empty() {
31
+        log::error!("Registered email required.");
32
+        return false;
33
+    }
34
+
35
+    if let Ok(s) = enc_lic(&lic) {
36
+        if let Ok(mut f) = std::fs::File::create(path) {
37
+            f.write_all(s.as_bytes()).ok();
38
+            f.sync_all().ok();
39
+        }
40
+    }
2
     true
41
     true
3
 }
42
 }
4
 
43
 
44
+pub fn get_lic() -> License {
45
+    let hostname = whoami::hostname();
46
+    let uid = machine_uid::get().unwrap_or("".to_owned());
47
+    let mac = if let Ok(Some(ma)) = mac_address::get_mac_address() {
48
+        base64::encode_config(ma.bytes(), base64::URL_SAFE_NO_PAD)
49
+    } else {
50
+        "".to_owned()
51
+    };
52
+    License { hostname, uid, mac }
53
+}
54
+
55
+pub fn enc_lic(lic: &License) -> ResultType<String> {
56
+    let tmp = serde_json::to_vec::<License>(lic)?;
57
+    const SK: &[u64] = &[
58
+        139, 164, 88, 86, 6, 123, 221, 248, 96, 36, 106, 207, 99, 124, 27, 196, 5, 159, 58, 253,
59
+        238, 94, 3, 184, 237, 236, 122, 59, 205, 95, 6, 189, 88, 168, 68, 104, 60, 5, 163, 198,
60
+        165, 38, 12, 85, 114, 203, 96, 163, 70, 48, 0, 131, 57, 12, 46, 129, 83, 17, 84, 193, 119,
61
+        197, 130, 103,
62
+    ];
63
+    let sk: Vec<u8> = SK.iter().map(|x| *x as u8).collect();
64
+    let mut sk_ = [0u8; sign::SECRETKEYBYTES];
65
+    sk_[..].copy_from_slice(&sk);
66
+    let sk = sign::SecretKey(sk_);
67
+    let tmp = base64::encode_config(sign::sign(&tmp, &sk), base64::URL_SAFE_NO_PAD);
68
+    let tmp: String = tmp.chars().rev().collect();
69
+    Ok(tmp)
70
+}
71
+
72
+pub fn dec_lic(s: &str) -> ResultType<License> {
73
+    let tmp: String = s.chars().rev().collect();
74
+    const PK: &[u64] = &[
75
+        88, 168, 68, 104, 60, 5, 163, 198, 165, 38, 12, 85, 114, 203, 96, 163, 70, 48, 0, 131, 57,
76
+        12, 46, 129, 83, 17, 84, 193, 119, 197, 130, 103,
77
+    ];
78
+    let pk: Vec<u8> = PK.iter().map(|x| *x as u8).collect();
79
+    let mut pk_ = [0u8; sign::PUBLICKEYBYTES];
80
+    pk_[..].copy_from_slice(&pk);
81
+    let pk = sign::PublicKey(pk_);
82
+    if let Ok(data) = sign::verify(&base64::decode_config(tmp, base64::URL_SAFE_NO_PAD)?, &pk) {
83
+        Ok(serde_json::from_slice::<License>(&data)?)
84
+    } else {
85
+        bail!("sign:verify failed");
86
+    }
87
+}
88
+
5
 pub const EMAIL_ARG: &'static str =
89
 pub const EMAIL_ARG: &'static str =
6
     "-m, --email=[EMAIL] 'Sets your email address registered with RustDesk'";
90
     "-m, --email=[EMAIL] 'Sets your email address registered with RustDesk'";

+ 2 - 1
src/main.rs

@@ -20,7 +20,8 @@ fn main() -> ResultType<()> {
20
         -C, --change-id=[BOOL(default=Y)] 'Sets if support to change id'
20
         -C, --change-id=[BOOL(default=Y)] 'Sets if support to change id'
21
         {}
21
         {}
22
         -k, --key=[KEY] 'Only allow the client with the same key'",
22
         -k, --key=[KEY] 'Only allow the client with the same key'",
23
-        DEFAULT_PORT, lic::EMAIL_ARG
23
+        DEFAULT_PORT,
24
+        lic::EMAIL_ARG
24
     );
25
     );
25
     let matches = App::new("hbbs")
26
     let matches = App::new("hbbs")
26
         .version(crate::VERSION)
27
         .version(crate::VERSION)