This commit is contained in:
opentrade 2021-04-10 01:16:02 +08:00
parent 4662b05d73
commit 321bccc19e
3 changed files with 21 additions and 21 deletions

@ -1 +1 @@
Subproject commit 98257ca5b799ecae53e2fc3ec20bb997a2f6206c Subproject commit 99487187a6b25380b9a412f040f43f319ece7545

View File

@ -12,7 +12,7 @@ use std::io::prelude::*;
use std::path::Path; use std::path::Path;
#[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)]
pub struct License { pub struct Machine {
#[serde(default)] #[serde(default)]
hostname: String, hostname: String,
#[serde(default)] #[serde(default)]
@ -24,7 +24,7 @@ pub struct License {
#[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)]
pub struct Post { pub struct Post {
#[serde(default)] #[serde(default)]
lic: License, machine: Machine,
#[serde(default)] #[serde(default)]
email: String, email: String,
#[serde(default)] #[serde(default)]
@ -36,12 +36,12 @@ pub struct Post {
const LICENSE_FILE: &'static str = ".license.txt"; const LICENSE_FILE: &'static str = ".license.txt";
pub fn check_lic(email: &str) -> bool { pub fn check_lic(email: &str) -> bool {
let lic = get_lic(); let machine = get_lic();
let path = Path::new(LICENSE_FILE); let path = Path::new(LICENSE_FILE);
if Path::is_file(&path) { if Path::is_file(&path) {
let contents = std::fs::read_to_string(&path).unwrap_or("".to_owned()); let contents = std::fs::read_to_string(&path).unwrap_or("".to_owned());
if let Ok(old_lic) = dec_lic(&contents) { if let Ok(old_lic) = dec_machine(&contents) {
if lic == old_lic { if machine == old_lic {
return true; return true;
} }
} }
@ -52,10 +52,10 @@ pub fn check_lic(email: &str) -> bool {
return false; return false;
} }
match check_email(lic.clone(), email.to_owned()) { match check_email(machine.clone(), email.to_owned()) {
Ok(v) => { Ok(v) => {
if v { if v {
write_lic(&lic); write_lic(&machine);
} }
return v; return v;
} }
@ -66,8 +66,8 @@ pub fn check_lic(email: &str) -> bool {
} }
} }
fn write_lic(lic: &License) { fn write_lic(machine: &Machine) {
if let Ok(s) = enc_lic(&lic) { if let Ok(s) = enc_machine(&machine) {
if let Ok(mut f) = std::fs::File::create(LICENSE_FILE) { if let Ok(mut f) = std::fs::File::create(LICENSE_FILE) {
f.write_all(s.as_bytes()).ok(); f.write_all(s.as_bytes()).ok();
f.sync_all().ok(); f.sync_all().ok();
@ -75,7 +75,7 @@ fn write_lic(lic: &License) {
} }
} }
fn check_email(lic: License, email: String) -> ResultType<bool> { fn check_email(machine: Machine, email: String) -> ResultType<bool> {
log::info!("Checking email with the server ..."); log::info!("Checking email with the server ...");
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let nonce: usize = rng.gen(); let nonce: usize = rng.gen();
@ -83,7 +83,7 @@ fn check_email(lic: License, email: String) -> ResultType<bool> {
let resp = Client::new() let resp = Client::new()
.post("http://rustdesk.com/api/check-email") .post("http://rustdesk.com/api/check-email")
.json(&Post { .json(&Post {
lic, machine,
email, email,
nonce, nonce,
..Default::default() ..Default::default()
@ -101,7 +101,7 @@ fn check_email(lic: License, email: String) -> ResultType<bool> {
Ok(true) Ok(true)
} }
fn get_lic() -> License { fn get_lic() -> Machine {
let hostname = whoami::hostname(); let hostname = whoami::hostname();
let uid = machine_uid::get().unwrap_or("".to_owned()); let uid = machine_uid::get().unwrap_or("".to_owned());
let mac = if let Ok(Some(ma)) = mac_address::get_mac_address() { let mac = if let Ok(Some(ma)) = mac_address::get_mac_address() {
@ -109,11 +109,11 @@ fn get_lic() -> License {
} else { } else {
"".to_owned() "".to_owned()
}; };
License { hostname, uid, mac } Machine { hostname, uid, mac }
} }
fn enc_lic(lic: &License) -> ResultType<String> { fn enc_machine(machine: &Machine) -> ResultType<String> {
let tmp = serde_json::to_vec::<License>(lic)?; let tmp = serde_json::to_vec::<Machine>(machine)?;
const SK: &[u64] = &[ const SK: &[u64] = &[
139, 164, 88, 86, 6, 123, 221, 248, 96, 36, 106, 207, 99, 124, 27, 196, 5, 159, 58, 253, 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, 238, 94, 3, 184, 237, 236, 122, 59, 205, 95, 6, 189, 88, 168, 68, 104, 60, 5, 163, 198,
@ -129,7 +129,7 @@ fn enc_lic(lic: &License) -> ResultType<String> {
Ok(tmp) Ok(tmp)
} }
fn dec_lic(s: &str) -> ResultType<License> { fn dec_machine(s: &str) -> ResultType<Machine> {
let tmp: String = s.chars().rev().collect(); let tmp: String = s.chars().rev().collect();
const PK: &[u64] = &[ const PK: &[u64] = &[
88, 168, 68, 104, 60, 5, 163, 198, 165, 38, 12, 85, 114, 203, 96, 163, 70, 48, 0, 131, 57, 88, 168, 68, 104, 60, 5, 163, 198, 165, 38, 12, 85, 114, 203, 96, 163, 70, 48, 0, 131, 57,
@ -140,7 +140,7 @@ fn dec_lic(s: &str) -> ResultType<License> {
pk_[..].copy_from_slice(&pk); pk_[..].copy_from_slice(&pk);
let pk = sign::PublicKey(pk_); let pk = sign::PublicKey(pk_);
if let Ok(data) = sign::verify(&base64::decode_config(tmp, base64::URL_SAFE_NO_PAD)?, &pk) { if let Ok(data) = sign::verify(&base64::decode_config(tmp, base64::URL_SAFE_NO_PAD)?, &pk) {
Ok(serde_json::from_slice::<License>(&data)?) Ok(serde_json::from_slice::<Machine>(&data)?)
} else { } else {
bail!("sign:verify failed"); bail!("sign:verify failed");
} }

View File

@ -47,6 +47,9 @@ fn main() -> ResultType<()> {
} }
return default.to_owned(); return default.to_owned();
}; };
if !lic::check_lic(&get_arg("email", "")) {
return Ok(());
}
let port = get_arg("port", DEFAULT_PORT); let port = get_arg("port", DEFAULT_PORT);
let relay_servers: Vec<String> = get_arg("relay-servers", "") let relay_servers: Vec<String> = get_arg("relay-servers", "")
.split(",") .split(",")
@ -65,9 +68,6 @@ fn main() -> ResultType<()> {
log::info!("serial={}", serial); log::info!("serial={}", serial);
log::info!("rendezvous-servers={:?}", rendezvous_servers); log::info!("rendezvous-servers={:?}", rendezvous_servers);
let stop: Arc<Mutex<bool>> = Default::default(); let stop: Arc<Mutex<bool>> = Default::default();
if !lic::check_lic(&get_arg("email", "")) {
return Ok(());
}
RendezvousServer::start( RendezvousServer::start(
&addr, &addr,
&addr2, &addr2,