unlock with PIN (#8977)

* add custom password to unlock settings

* If not set, use admin password; if set, use custom settings password.
* At least 4 characters.
* Set with gui or command line.

Signed-off-by: 21pages <sunboeasy@gmail.com>

* Update cn.rs

---------

Signed-off-by: 21pages <sunboeasy@gmail.com>
Co-authored-by: RustDesk <71636191+rustdesk@users.noreply.github.com>
This commit is contained in:
21pages
2024-08-07 16:21:38 +08:00
committed by GitHub
Unverified
parent bc6ce6c7ee
commit 76d5a8b205
51 changed files with 425 additions and 9 deletions

View File

@@ -208,6 +208,8 @@ pub struct Config2 {
nat_type: i32,
#[serde(default, deserialize_with = "deserialize_i32")]
serial: i32,
#[serde(default, deserialize_with = "deserialize_string")]
unlock_pin: String,
#[serde(default)]
socks: Option<Socks5Server>,
@@ -427,14 +429,20 @@ fn patch(path: PathBuf) -> PathBuf {
impl Config2 {
fn load() -> Config2 {
let mut config = Config::load_::<Config2>("2");
let mut store = false;
if let Some(mut socks) = config.socks {
let (password, _, store) =
let (password, _, store2) =
decrypt_str_or_original(&socks.password, PASSWORD_ENC_VERSION);
socks.password = password;
config.socks = Some(socks);
if store {
config.store();
}
store |= store2;
}
let (unlock_pin, _, store2) =
decrypt_str_or_original(&config.unlock_pin, PASSWORD_ENC_VERSION);
config.unlock_pin = unlock_pin;
store |= store2;
if store {
config.store();
}
config
}
@@ -450,6 +458,8 @@ impl Config2 {
encrypt_str_or_original(&socks.password, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
config.socks = Some(socks);
}
config.unlock_pin =
encrypt_str_or_original(&config.unlock_pin, PASSWORD_ENC_VERSION, ENCRYPT_MAX_LEN);
Config::store_(&config, "2");
}
@@ -1081,6 +1091,19 @@ impl Config {
NetworkType::Direct
}
pub fn get_unlock_pin() -> String {
CONFIG2.read().unwrap().unlock_pin.clone()
}
pub fn set_unlock_pin(pin: &str) {
let mut config = CONFIG2.write().unwrap();
if pin == config.unlock_pin {
return;
}
config.unlock_pin = pin.to_string();
config.store();
}
pub fn get() -> Config {
return CONFIG.read().unwrap().clone();
}