Skip to content

Commit

Permalink
refactor(single-instance): refactor single instance check (#499)
Browse files Browse the repository at this point in the history
* fix: deps conflict

* refactor(single-instance): remove related old single instance check code

* fix: lint

* refactor: improve ux

---------

Co-authored-by: Jonson Petard <i@a632079.me>
  • Loading branch information
4o3F and greenhat616 authored Feb 28, 2024
1 parent a48aa09 commit 11d7241
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 147 deletions.
213 changes: 148 additions & 65 deletions backend/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/sysproxy-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ iptools = "0.2.5"
winreg = { version = "0.52", features = ["transactions"] }

[target.'cfg(target_os = "windows")'.dependencies.windows]
version = "0.52"
version = "0.54"
features = [
"Win32_Networking_WinInet",
# "Win32_Networking_WinHttp",
Expand Down
6 changes: 3 additions & 3 deletions backend/sysproxy-rs/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn unset_proxy() -> Result<()> {
dwSize: size_of::<INTERNET_PER_CONN_OPTION_LISTW>() as u32,
dwOptionCount: 1,
dwOptionError: 0,
pOptions: p_opts.as_mut_ptr() as *mut INTERNET_PER_CONN_OPTIONW,
pOptions: p_opts.as_mut_ptr(),
pszConnection: PWSTR::null(),
};
let res = apply(&opts);
Expand Down Expand Up @@ -83,7 +83,7 @@ fn set_auto_proxy(url: &str) -> Result<()> {
dwSize: size_of::<INTERNET_PER_CONN_OPTION_LISTW>() as u32,
dwOptionCount: 2,
dwOptionError: 0,
pOptions: p_opts.as_mut_ptr() as *mut INTERNET_PER_CONN_OPTIONW,
pOptions: p_opts.as_mut_ptr(),
pszConnection: PWSTR::null(),
};

Expand Down Expand Up @@ -133,7 +133,7 @@ fn set_global_proxy(server: String, bypass: String) -> Result<()> {
dwSize: size_of::<INTERNET_PER_CONN_OPTION_LISTW>() as u32,
dwOptionCount: 3,
dwOptionError: 0,
pOptions: p_opts.as_mut_ptr() as *mut INTERNET_PER_CONN_OPTIONW,
pOptions: p_opts.as_mut_ptr(),
pszConnection: PWSTR::null(),
};

Expand Down
1 change: 1 addition & 0 deletions backend/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ tracing-error = "0.2"
tracing-log = { version = "0.2" }
tracing-appender = { version = "0.2", features = ["parking_lot"] }
base64 = "0.21"
single-instance = "0.3.3"

[target.'cfg(windows)'.dependencies]
deelevate = "0.2.0"
Expand Down
13 changes: 0 additions & 13 deletions backend/tauri/src/config/nyanpasu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,4 @@ impl IVerge {
patch!(max_log_files);
patch!(window_size_state);
}

/// 在初始化前尝试拿到单例端口的值
pub fn get_singleton_port() -> u16 {
#[cfg(not(feature = "verge-dev"))]
const SERVER_PORT: u16 = 33331;
#[cfg(feature = "verge-dev")]
const SERVER_PORT: u16 = 11233;

match dirs::verge_path().and_then(|path| help::read_yaml::<IVerge>(&path)) {
Ok(config) => config.app_singleton_port.unwrap_or(SERVER_PORT),
Err(_) => SERVER_PORT, // 这里就不log错误了
}
}
}
1 change: 1 addition & 0 deletions backend/tauri/src/core/tasks/jobs/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn clear_logs() -> Result<()> {
let minutes = {
let verge = Config::verge();
let verge = verge.data();
#[allow(deprecated)]
verge.auto_log_clean.unwrap_or(0)
};
if minutes == 0 {
Expand Down
22 changes: 17 additions & 5 deletions backend/tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ mod utils;

use crate::{
config::Config,
utils::{init, resolve, server},
utils::{init, resolve},
};
use anyhow::Context;
use tauri::{api, SystemTray};

rust_i18n::i18n!("../../locales");
Expand Down Expand Up @@ -44,11 +45,17 @@ fn deadlock_detection() {
fn main() -> std::io::Result<()> {
#[cfg(feature = "deadlock-detection")]
deadlock_detection();

// 单例检测
if server::check_singleton().is_err() {
println!("app exists");
return Ok(());
}
let single_instance_result: anyhow::Result<()> =
single_instance::SingleInstance::new(utils::dirs::APP_NAME)
.context("failed to create single instance")
.map(|instance| {
if !instance.is_single() {
println!("app exists");
std::process::exit(0);
}
});

// Use system locale as default
let locale = {
Expand All @@ -70,6 +77,11 @@ fn main() -> std::io::Result<()> {
let verge = { Config::verge().latest().language.clone().unwrap() };
rust_i18n::set_locale(verge.as_str());

// show a dialog to print the single instance error
if let Err(e) = single_instance_result {
utils::dialog::panic_dialog(&format!("{:?}", e));
}

#[allow(unused_mut)]
let mut builder = tauri::Builder::default()
.system_tray(SystemTray::new())
Expand Down
22 changes: 12 additions & 10 deletions backend/tauri/src/utils/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use tauri::{
};

#[cfg(not(feature = "verge-dev"))]
static OLD_APP_DIR: &str = "clash-verge";
const PREVIOUS_APP_NAME: &str = "clash-verge";
#[cfg(feature = "verge-dev")]
static OLD_APP_DIR: &str = "clash-verge-dev";
const PREVIOUS_APP_NAME: &str = "clash-verge-dev";
#[cfg(not(feature = "verge-dev"))]
static APP_DIR: &str = "clash-nyanpasu";
pub const APP_NAME: &str = "clash-nyanpasu";
#[cfg(feature = "verge-dev")]
static APP_DIR: &str = "clash-nyanpasu-dev";
pub const APP_NAME: &str = "clash-nyanpasu-dev";

static CLASH_CONFIG: &str = "config.yaml";
static VERGE_CONFIG: &str = "verge.yaml";
Expand Down Expand Up @@ -64,22 +64,24 @@ pub fn old_app_home_dir() -> Result<PathBuf> {
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to check old app home dir"))?
.join(".config")
.join(OLD_APP_DIR))
.join(PREVIOUS_APP_NAME))
} else {
let app_exe = current_exe()?;
let app_exe = dunce::canonicalize(app_exe)?;
let app_dir = app_exe
.parent()
.ok_or(anyhow::anyhow!("failed to check the old portable app dir"))?;
Ok(PathBuf::from(app_dir).join(".config").join(OLD_APP_DIR))
Ok(PathBuf::from(app_dir)
.join(".config")
.join(PREVIOUS_APP_NAME))
}
}

#[cfg(not(target_os = "windows"))]
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to get the app home dir"))?
.join(".config")
.join(OLD_APP_DIR))
.join(PREVIOUS_APP_NAME))
}

/// get the verge app home dir
Expand All @@ -92,22 +94,22 @@ pub fn app_home_dir() -> Result<PathBuf> {
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to get app home dir"))?
.join(".config")
.join(APP_DIR))
.join(APP_NAME))
} else {
let app_exe = current_exe()?;
let app_exe = dunce::canonicalize(app_exe)?;
let app_dir = app_exe
.parent()
.ok_or(anyhow::anyhow!("failed to get the portable app dir"))?;
Ok(PathBuf::from(app_dir).join(".config").join(APP_DIR))
Ok(PathBuf::from(app_dir).join(".config").join(APP_NAME))
}
}

#[cfg(not(target_os = "windows"))]
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to get the app home dir"))?
.join(".config")
.join(APP_DIR))
.join(APP_NAME))
}

/// get the resources dir
Expand Down
1 change: 0 additions & 1 deletion backend/tauri/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ pub mod dirs;
pub mod help;
pub mod init;
pub mod resolve;
pub mod server;
pub mod tmpl;
// mod winhelp;
6 changes: 1 addition & 5 deletions backend/tauri/src/utils/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
*,
},
log_err, trace_err,
utils::{init, server},
utils::init,
};
use anyhow::Result;
use semver::Version;
Expand Down Expand Up @@ -76,10 +76,6 @@ pub fn resolve_setup(app: &mut App) {
log::trace!("launch core");
log_err!(CoreManager::global().init());

// setup a simple http server for singleton
log::trace!("launch embed server");
server::embed_server(app.app_handle());

log::trace!("init system tray");
log_err!(tray::Tray::update_systray(&app.app_handle()));

Expand Down
44 changes: 0 additions & 44 deletions backend/tauri/src/utils/server.rs

This file was deleted.

0 comments on commit 11d7241

Please sign in to comment.