fix slow connection, '/' in pub key, and hbbr wait for key, and possible

solution for https://github.com/rustdesk/rustdesk-server/issues/24
This commit is contained in:
rustdesk
2022-07-13 00:22:45 +08:00
parent 57cbac7079
commit 39153ce147
14 changed files with 269 additions and 178 deletions

View File

@@ -95,8 +95,11 @@ pub fn now() -> u64 {
.unwrap_or_default()
}
pub fn gen_sk() -> (String, Option<sign::SecretKey>) {
pub fn gen_sk(wait: u64) -> (String, Option<sign::SecretKey>) {
let sk_file = "id_ed25519";
if wait > 0 && !std::path::Path::new(sk_file).exists() {
std::thread::sleep(std::time::Duration::from_millis(wait));
}
if let Ok(mut file) = std::fs::File::open(sk_file) {
let mut contents = String::new();
if file.read_to_string(&mut contents).is_ok() {
@@ -110,16 +113,26 @@ pub fn gen_sk() -> (String, Option<sign::SecretKey>) {
}
}
} else {
let (pk, sk) = sign::gen_keypair();
let gen_func = || {
let (tmp, sk) = sign::gen_keypair();
(base64::encode(tmp), sk)
};
let (mut pk, mut sk) = gen_func();
for _ in 0..300 {
if !pk.contains("/") {
break;
}
(pk, sk) = gen_func();
}
let pub_file = format!("{}.pub", sk_file);
if let Ok(mut f) = std::fs::File::create(&pub_file) {
f.write_all(base64::encode(pk).as_bytes()).ok();
f.write_all(pk.as_bytes()).ok();
if let Ok(mut f) = std::fs::File::create(sk_file) {
let s = base64::encode(&sk);
if f.write_all(s.as_bytes()).is_ok() {
log::info!("Private/public key written to {}/{}", sk_file, pub_file);
log::debug!("Public key: {:?}", pk);
return (base64::encode(pk), Some(sk));
log::debug!("Public key: {}", pk);
return (pk, Some(sk));
}
}
}