make config in exe name has highest priority, also overwrite config if

installation,
https://github.com/rustdesk/rustdesk-server-pro/issues/21#issuecomment-1638259580,
not tested yet
This commit is contained in:
rustdesk 2023-07-19 14:45:11 +08:00
parent b40c3aae52
commit aacffd979b
6 changed files with 35 additions and 84 deletions

View File

@ -49,6 +49,7 @@ lazy_static::lazy_static! {
Some(key) if !key.is_empty() => key, Some(key) if !key.is_empty() => key,
_ => "", _ => "",
}.to_owned())); }.to_owned()));
pub static ref EXE_RENDEZVOUS_SERVER: Arc<RwLock<String>> = Default::default();
pub static ref APP_NAME: Arc<RwLock<String>> = Arc::new(RwLock::new("RustDesk".to_owned())); pub static ref APP_NAME: Arc<RwLock<String>> = Arc::new(RwLock::new("RustDesk".to_owned()));
static ref KEY_PAIR: Arc<Mutex<Option<KeyPair>>> = Default::default(); static ref KEY_PAIR: Arc<Mutex<Option<KeyPair>>> = Default::default();
static ref HW_CODEC_CONFIG: Arc<RwLock<HwCodecConfig>> = Arc::new(RwLock::new(HwCodecConfig::load())); static ref HW_CODEC_CONFIG: Arc<RwLock<HwCodecConfig>> = Arc::new(RwLock::new(HwCodecConfig::load()));
@ -604,7 +605,10 @@ impl Config {
} }
pub fn get_rendezvous_server() -> String { pub fn get_rendezvous_server() -> String {
let mut rendezvous_server = Self::get_option("custom-rendezvous-server"); let mut rendezvous_server = EXE_RENDEZVOUS_SERVER.read().unwrap().clone();
if rendezvous_server.is_empty() {
rendezvous_server = Self::get_option("custom-rendezvous-server");
}
if rendezvous_server.is_empty() { if rendezvous_server.is_empty() {
rendezvous_server = PROD_RENDEZVOUS_SERVER.read().unwrap().clone(); rendezvous_server = PROD_RENDEZVOUS_SERVER.read().unwrap().clone();
} }
@ -624,6 +628,10 @@ impl Config {
} }
pub fn get_rendezvous_servers() -> Vec<String> { pub fn get_rendezvous_servers() -> Vec<String> {
let s = EXE_RENDEZVOUS_SERVER.read().unwrap().clone();
if !s.is_empty() {
return vec![s];
}
let s = Self::get_option("custom-rendezvous-server"); let s = Self::get_option("custom-rendezvous-server");
if !s.is_empty() { if !s.is_empty() {
return vec![s]; return vec![s];

View File

@ -833,15 +833,15 @@ pub fn is_setup(name: &str) -> bool {
} }
pub fn get_custom_rendezvous_server(custom: String) -> String { pub fn get_custom_rendezvous_server(custom: String) -> String {
if !custom.is_empty() {
return custom;
}
#[cfg(windows)] #[cfg(windows)]
if let Some(lic) = crate::platform::windows::get_license() { if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() {
if !lic.host.is_empty() { if !lic.host.is_empty() {
return lic.host.clone(); return lic.host.clone();
} }
} }
if !custom.is_empty() {
return custom;
}
if !config::PROD_RENDEZVOUS_SERVER.read().unwrap().is_empty() { if !config::PROD_RENDEZVOUS_SERVER.read().unwrap().is_empty() {
return config::PROD_RENDEZVOUS_SERVER.read().unwrap().clone(); return config::PROD_RENDEZVOUS_SERVER.read().unwrap().clone();
} }
@ -849,15 +849,15 @@ pub fn get_custom_rendezvous_server(custom: String) -> String {
} }
pub fn get_api_server(api: String, custom: String) -> String { pub fn get_api_server(api: String, custom: String) -> String {
if !api.is_empty() {
return api.to_owned();
}
#[cfg(windows)] #[cfg(windows)]
if let Some(lic) = crate::platform::windows::get_license() { if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() {
if !lic.api.is_empty() { if !lic.api.is_empty() {
return lic.api.clone(); return lic.api.clone();
} }
} }
if !api.is_empty() {
return api.to_owned();
}
let api = option_env!("API_SERVER").unwrap_or_default(); let api = option_env!("API_SERVER").unwrap_or_default();
if !api.is_empty() { if !api.is_empty() {
return api.into(); return api.into();
@ -982,6 +982,10 @@ pub fn decode64<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, base64::DecodeError
} }
pub async fn get_key(sync: bool) -> String { pub async fn get_key(sync: bool) -> String {
#[cfg(windows)]
if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() {
return lic.key;
}
#[cfg(target_os = "ios")] #[cfg(target_os = "ios")]
let mut key = Config::get_option("key"); let mut key = Config::get_option("key");
#[cfg(not(target_os = "ios"))] #[cfg(not(target_os = "ios"))]
@ -991,12 +995,6 @@ pub async fn get_key(sync: bool) -> String {
let mut options = crate::ipc::get_options_async().await; let mut options = crate::ipc::get_options_async().await;
options.remove("key").unwrap_or_default() options.remove("key").unwrap_or_default()
}; };
if key.is_empty() {
#[cfg(windows)]
if let Some(lic) = crate::platform::windows::get_license() {
return lic.key;
}
}
if key.is_empty() && !option_env!("RENDEZVOUS_SERVER").unwrap_or("").is_empty() { if key.is_empty() && !option_env!("RENDEZVOUS_SERVER").unwrap_or("").is_empty() {
key = config::RS_PUB_KEY.to_owned(); key = config::RS_PUB_KEY.to_owned();
} }

View File

@ -25,6 +25,8 @@ macro_rules! my_println{
/// If it returns [`Some`], then the process will continue, and flutter gui will be started. /// If it returns [`Some`], then the process will continue, and flutter gui will be started.
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn core_main() -> Option<Vec<String>> { pub fn core_main() -> Option<Vec<String>> {
#[cfg(windows)]
crate::platform::windows::bootstrap();
let mut args = Vec::new(); let mut args = Vec::new();
let mut flutter_args = Vec::new(); let mut flutter_args = Vec::new();
let mut i = 0; let mut i = 0;

View File

@ -927,35 +927,6 @@ pub fn copy_exe_cmd(src_exe: &str, exe: &str, path: &str) -> String {
) )
} }
/* // update_me has bad compatibility, so disable it.
pub fn update_me() -> ResultType<()> {
let (_, path, _, exe) = get_install_info();
let src_exe = std::env::current_exe()?.to_str().unwrap_or("").to_owned();
let cmds = format!(
"
chcp 65001
sc stop {app_name}
taskkill /F /IM {broker_exe}
taskkill /F /IM {app_name}.exe /FI \"PID ne {cur_pid}\"
{copy_exe}
sc start {app_name}
{lic}
",
copy_exe = copy_exe_cmd(&src_exe, &exe, &path),
broker_exe = WIN_MAG_INJECTED_PROCESS_EXE,
app_name = crate::get_app_name(),
lic = register_licence(),
cur_pid = get_current_pid(),
);
run_cmds(cmds, false, "update")?;
run_after_run_cmds(false);
std::process::Command::new(&exe)
.args(&["--remove", &src_exe])
.spawn()?;
Ok(())
}
*/
fn get_after_install(exe: &str) -> String { fn get_after_install(exe: &str) -> String {
let app_name = crate::get_app_name(); let app_name = crate::get_app_name();
let ext = app_name.to_lowercase(); let ext = app_name.to_lowercase();
@ -1093,6 +1064,15 @@ if exist \"{tmp_path}\\{app_name} Tray.lnk\" del /f /q \"{tmp_path}\\{app_name}
"".to_owned() "".to_owned()
}; };
// potential bug here: if run_cmd cancelled, but config file is changed.
if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() {
if !lic.host.is_empty() {
Config::set_option("key".into(), lic.key);
Config::set_option("custom-rendezvous-server".into(), lic.host);
Config::set_option("api-server".into(), lic.api);
}
}
let cmds = format!( let cmds = format!(
" "
{uninstall_str} {uninstall_str}
@ -1113,7 +1093,6 @@ reg add {subkey} /f /v VersionBuild /t REG_DWORD /d {version_build}
reg add {subkey} /f /v UninstallString /t REG_SZ /d \"\\\"{exe}\\\" --uninstall\" reg add {subkey} /f /v UninstallString /t REG_SZ /d \"\\\"{exe}\\\" --uninstall\"
reg add {subkey} /f /v EstimatedSize /t REG_DWORD /d {size} reg add {subkey} /f /v EstimatedSize /t REG_DWORD /d {size}
reg add {subkey} /f /v WindowsInstaller /t REG_DWORD /d 0 reg add {subkey} /f /v WindowsInstaller /t REG_DWORD /d 0
{lic}
cscript \"{mk_shortcut}\" cscript \"{mk_shortcut}\"
cscript \"{uninstall_shortcut}\" cscript \"{uninstall_shortcut}\"
cscript \"{tray_shortcut}\" cscript \"{tray_shortcut}\"
@ -1128,7 +1107,6 @@ copy /Y \"{tmp_path}\\Uninstall {app_name}.lnk\" \"{path}\\\"
", ",
version=crate::VERSION, version=crate::VERSION,
build_date=crate::BUILD_DATE, build_date=crate::BUILD_DATE,
lic=register_licence(),
after_install=get_after_install(&exe), after_install=get_after_install(&exe),
sleep=if debug { sleep=if debug {
"timeout 300" "timeout 300"
@ -1347,7 +1325,7 @@ fn get_reg_of(subkey: &str, name: &str) -> String {
"".to_owned() "".to_owned()
} }
fn get_license_from_exe_name() -> ResultType<License> { pub fn get_license_from_exe_name() -> ResultType<License> {
let mut exe = std::env::current_exe()?.to_str().unwrap_or("").to_owned(); let mut exe = std::env::current_exe()?.to_str().unwrap_or("").to_owned();
// if defined portable appname entry, replace original executable name with it. // if defined portable appname entry, replace original executable name with it.
if let Ok(portable_exe) = std::env::var(PORTABLE_APPNAME_RUNTIME_ENV_KEY) { if let Ok(portable_exe) = std::env::var(PORTABLE_APPNAME_RUNTIME_ENV_KEY) {
@ -1362,42 +1340,9 @@ pub fn is_win_server() -> bool {
unsafe { is_windows_server() > 0 } unsafe { is_windows_server() > 0 }
} }
pub fn get_license() -> Option<License> {
let mut lic: License = Default::default();
if let Ok(tmp) = get_license_from_exe_name() {
lic = tmp;
} else {
lic.key = get_reg("Key");
lic.host = get_reg("Host");
lic.api = get_reg("Api");
}
if lic.key.is_empty() || lic.host.is_empty() {
return None;
}
Some(lic)
}
pub fn bootstrap() { pub fn bootstrap() {
if let Some(lic) = get_license() {
*config::PROD_RENDEZVOUS_SERVER.write().unwrap() = lic.host.clone();
}
}
fn register_licence() -> String {
let (subkey, _, _, _) = get_install_info();
if let Ok(lic) = get_license_from_exe_name() { if let Ok(lic) = get_license_from_exe_name() {
format!( *config::EXE_RENDEZVOUS_SERVER.write().unwrap() = lic.host.clone();
"
reg add {subkey} /f /v Key /t REG_SZ /d \"{key}\"
reg add {subkey} /f /v Host /t REG_SZ /d \"{host}\"
reg add {subkey} /f /v Api /t REG_SZ /d \"{api}\"
",
key = &lic.key,
host = &lic.host,
api = &lic.api,
)
} else {
"".to_owned()
} }
} }

View File

@ -379,8 +379,6 @@ pub async fn start_server(is_server: bool) {
std::process::exit(-1); std::process::exit(-1);
} }
}); });
#[cfg(windows)]
crate::platform::windows::bootstrap();
input_service::fix_key_down_timeout_loop(); input_service::fix_key_down_timeout_loop();
crate::hbbs_http::sync::start(); crate::hbbs_http::sync::start();
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]

View File

@ -118,7 +118,7 @@ pub fn show_run_without_install() -> bool {
#[inline] #[inline]
pub fn get_license() -> String { pub fn get_license() -> String {
#[cfg(windows)] #[cfg(windows)]
if let Some(lic) = crate::platform::windows::get_license() { if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() {
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
return format!("Key: {}\nHost: {}\nApi: {}", lic.key, lic.host, lic.api); return format!("Key: {}\nHost: {}\nApi: {}", lic.key, lic.host, lic.api);
// default license format is html formed (sciter) // default license format is html formed (sciter)