|
|
@@ -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
|
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
|
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
|
89
|
pub const EMAIL_ARG: &'static str =
|
|
6
|
90
|
"-m, --email=[EMAIL] 'Sets your email address registered with RustDesk'";
|