From da372f276d3813d427df678b5c3bf3a44dc67bc1 Mon Sep 17 00:00:00 2001 From: opentrade Date: Fri, 9 Apr 2021 12:27:22 +0800 Subject: [PATCH] check_email --- Cargo.lock | 1 + Cargo.toml | 2 +- src/lic.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 89b72c2..83271c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1426,6 +1426,7 @@ dependencies = [ "percent-encoding", "pin-project-lite 0.2.0", "serde", + "serde_json", "serde_urlencoded", "tokio", "tokio-tls", diff --git a/Cargo.toml b/Cargo.toml index cb543ee..60b39f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ serde_json = "1.0" lazy_static = "1.4" clap = "2.33" rust-ini = "0.16" -reqwest = "0.10" +reqwest = { version = "0.10", features = ["json", "blocking"] } machine-uid = "0.2" mac_address = "1.1" whoami = "0.9" diff --git a/src/lic.rs b/src/lic.rs index 67a8745..85a2cad 100644 --- a/src/lic.rs +++ b/src/lic.rs @@ -6,11 +6,21 @@ use std::path::Path; #[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)] pub struct License { #[serde(default)] - pub hostname: String, + hostname: String, #[serde(default)] - pub uid: String, + uid: String, #[serde(default)] - pub mac: String, + mac: String, +} + +#[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)] +pub struct Post { + #[serde(default)] + lic: License, + #[serde(default)] + email: String, + #[serde(default)] + status: String, } const LICENSE_FILE: &'static str = ".license.txt"; @@ -32,16 +42,47 @@ pub fn check_lic(email: &str) -> bool { return false; } + match check_email(lic.clone(), email.to_owned()) { + Ok(v) => { + if v { + write_lic(&lic); + } + return v; + } + Err(err) => { + log::error!("{}", err); + return false; + } + } +} + +fn write_lic(lic: &License) { if let Ok(s) = enc_lic(&lic) { - if let Ok(mut f) = std::fs::File::create(path) { + if let Ok(mut f) = std::fs::File::create(LICENSE_FILE) { f.write_all(s.as_bytes()).ok(); f.sync_all().ok(); } } - true } -pub fn get_lic() -> License { +fn check_email(lic: License, email: String) -> ResultType { + use reqwest::blocking::Client; + let p: Post = Client::new() + .post("http://rustdesk.com/api/check-email") + .json(&Post { + lic, + email, + ..Default::default() + }) + .send()? + .json()?; + if !p.status.is_empty() { + bail!("{}", p.status); + } + Ok(true) +} + +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() { @@ -52,7 +93,7 @@ pub fn get_lic() -> License { License { hostname, uid, mac } } -pub fn enc_lic(lic: &License) -> ResultType { +fn enc_lic(lic: &License) -> ResultType { let tmp = serde_json::to_vec::(lic)?; const SK: &[u64] = &[ 139, 164, 88, 86, 6, 123, 221, 248, 96, 36, 106, 207, 99, 124, 27, 196, 5, 159, 58, 253, @@ -69,7 +110,7 @@ pub fn enc_lic(lic: &License) -> ResultType { Ok(tmp) } -pub fn dec_lic(s: &str) -> ResultType { +fn dec_lic(s: &str) -> ResultType { 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,