working on lic

This commit is contained in:
opentrade 2021-04-08 18:59:57 +08:00
parent 918dfc3bdd
commit 1282e730af
5 changed files with 120 additions and 3 deletions

30
Cargo.lock generated
View File

@ -618,15 +618,19 @@ dependencies = [
name = "hbbs"
version = "1.1.3"
dependencies = [
"base64 0.13.0",
"clap",
"hbb_common",
"lazy_static",
"mac_address",
"machine-uid",
"reqwest",
"rocksdb",
"rust-ini",
"serde",
"serde_derive",
"serde_json",
"whoami",
]
[[package]]
@ -872,6 +876,15 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "machine-uid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f1595709b0a7386bcd56ba34d250d626e5503917d05d32cdccddcd68603e212"
dependencies = [
"winreg 0.6.2",
]
[[package]]
name = "matches"
version = "0.1.8"
@ -1420,7 +1433,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"winreg",
"winreg 0.7.0",
]
[[package]]
@ -2072,6 +2085,12 @@ dependencies = [
"libc",
]
[[package]]
name = "whoami"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7884773ab69074615cb8f8425d0e53f11710786158704fca70f53e71b0e05504"
[[package]]
name = "winapi"
version = "0.2.8"
@ -2115,6 +2134,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "winreg"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "winreg"
version = "0.7.0"

View File

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

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

View File

@ -1,6 +1,90 @@
use hbb_common::{bail, log, sodiumoxide::crypto::sign, ResultType};
use serde_derive::{Deserialize, Serialize};
use std::io::prelude::*;
use std::path::Path;
#[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)]
pub struct License {
#[serde(default)]
pub hostname: String,
#[serde(default)]
pub uid: String,
#[serde(default)]
pub mac: String,
}
const LICENSE_FILE: &'static str = ".license.txt";
pub fn check_lic(email: &str) -> bool {
let lic = get_lic();
let path = Path::new(LICENSE_FILE);
if Path::is_file(&path) {
let contents = std::fs::read_to_string(&path).unwrap_or("".to_owned());
if let Ok(old_lic) = dec_lic(&contents) {
if lic == old_lic {
return true;
}
}
}
if email.is_empty() {
log::error!("Registered email required.");
return false;
}
if let Ok(s) = enc_lic(&lic) {
if let Ok(mut f) = std::fs::File::create(path) {
f.write_all(s.as_bytes()).ok();
f.sync_all().ok();
}
}
true
}
pub fn get_lic() -> License {
let hostname = whoami::hostname();
let uid = machine_uid::get().unwrap_or("".to_owned());
let mac = if let Ok(Some(ma)) = mac_address::get_mac_address() {
base64::encode_config(ma.bytes(), base64::URL_SAFE_NO_PAD)
} else {
"".to_owned()
};
License { hostname, uid, mac }
}
pub fn enc_lic(lic: &License) -> ResultType<String> {
let tmp = serde_json::to_vec::<License>(lic)?;
const SK: &[u64] = &[
139, 164, 88, 86, 6, 123, 221, 248, 96, 36, 106, 207, 99, 124, 27, 196, 5, 159, 58, 253,
238, 94, 3, 184, 237, 236, 122, 59, 205, 95, 6, 189, 88, 168, 68, 104, 60, 5, 163, 198,
165, 38, 12, 85, 114, 203, 96, 163, 70, 48, 0, 131, 57, 12, 46, 129, 83, 17, 84, 193, 119,
197, 130, 103,
];
let sk: Vec<u8> = SK.iter().map(|x| *x as u8).collect();
let mut sk_ = [0u8; sign::SECRETKEYBYTES];
sk_[..].copy_from_slice(&sk);
let sk = sign::SecretKey(sk_);
let tmp = base64::encode_config(sign::sign(&tmp, &sk), base64::URL_SAFE_NO_PAD);
let tmp: String = tmp.chars().rev().collect();
Ok(tmp)
}
pub fn dec_lic(s: &str) -> ResultType<License> {
let tmp: String = s.chars().rev().collect();
const PK: &[u64] = &[
88, 168, 68, 104, 60, 5, 163, 198, 165, 38, 12, 85, 114, 203, 96, 163, 70, 48, 0, 131, 57,
12, 46, 129, 83, 17, 84, 193, 119, 197, 130, 103,
];
let pk: Vec<u8> = PK.iter().map(|x| *x as u8).collect();
let mut pk_ = [0u8; sign::PUBLICKEYBYTES];
pk_[..].copy_from_slice(&pk);
let pk = sign::PublicKey(pk_);
if let Ok(data) = sign::verify(&base64::decode_config(tmp, base64::URL_SAFE_NO_PAD)?, &pk) {
Ok(serde_json::from_slice::<License>(&data)?)
} else {
bail!("sign:verify failed");
}
}
pub const EMAIL_ARG: &'static str =
"-m, --email=[EMAIL] 'Sets your email address registered with RustDesk'";

View File

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