2022-09-06 00:45:01 +08:00
|
|
|
use super::resolve;
|
2025-03-13 12:51:20 +08:00
|
|
|
use crate::{
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
config::{Config, DEFAULT_PAC, IVerge},
|
2025-10-12 22:55:40 +08:00
|
|
|
logging, logging_error,
|
|
|
|
|
module::lightweight,
|
2025-04-11 17:27:56 +08:00
|
|
|
process::AsyncHandler,
|
2025-10-12 22:55:40 +08:00
|
|
|
utils::{logging::Type, window_manager::WindowManager},
|
2025-03-13 12:51:20 +08:00
|
|
|
};
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
use anyhow::{Result, bail};
|
2025-10-09 20:08:25 +08:00
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
|
use parking_lot::Mutex;
|
2021-12-23 01:35:17 +08:00
|
|
|
use port_scanner::local_port_available;
|
2025-10-12 22:55:40 +08:00
|
|
|
use reqwest::ClientBuilder;
|
2025-10-22 16:25:44 +08:00
|
|
|
use smartstring::alias::String;
|
|
|
|
|
use std::time::Duration;
|
2025-10-09 20:08:25 +08:00
|
|
|
use tokio::sync::oneshot;
|
2021-12-23 01:35:17 +08:00
|
|
|
use warp::Filter;
|
|
|
|
|
|
2024-09-16 06:37:39 +08:00
|
|
|
#[derive(serde::Deserialize, Debug)]
|
|
|
|
|
struct QueryParam {
|
|
|
|
|
param: String,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-09 20:08:25 +08:00
|
|
|
// 关闭 embedded server 的信号发送端
|
|
|
|
|
static SHUTDOWN_SENDER: OnceCell<Mutex<Option<oneshot::Sender<()>>>> = OnceCell::new();
|
|
|
|
|
|
2021-12-23 01:35:17 +08:00
|
|
|
/// check whether there is already exists
|
2024-06-29 19:02:37 +08:00
|
|
|
pub async fn check_singleton() -> Result<()> {
|
2022-11-17 17:07:13 +08:00
|
|
|
let port = IVerge::get_singleton_port();
|
2022-11-12 11:37:23 +08:00
|
|
|
if !local_port_available(port) {
|
2025-10-12 22:55:40 +08:00
|
|
|
let client = ClientBuilder::new()
|
2025-10-20 13:26:24 +08:00
|
|
|
.timeout(Duration::from_millis(500))
|
2025-10-12 22:55:40 +08:00
|
|
|
.build()?;
|
2025-10-22 16:25:44 +08:00
|
|
|
let argvs: Vec<std::string::String> = std::env::args().collect();
|
2024-09-16 06:37:39 +08:00
|
|
|
if argvs.len() > 1 {
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
{
|
|
|
|
|
let param = argvs[1].as_str();
|
|
|
|
|
if param.starts_with("clash:") {
|
2025-10-12 22:55:40 +08:00
|
|
|
client
|
|
|
|
|
.get(format!(
|
|
|
|
|
"http://127.0.0.1:{port}/commands/scheme?param={param}"
|
|
|
|
|
))
|
|
|
|
|
.send()
|
|
|
|
|
.await?;
|
2024-09-16 06:37:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-10-12 22:55:40 +08:00
|
|
|
client
|
|
|
|
|
.get(format!("http://127.0.0.1:{port}/commands/visible"))
|
|
|
|
|
.send()
|
|
|
|
|
.await?;
|
2024-09-16 06:37:39 +08:00
|
|
|
}
|
Refactor logging to use a centralized logging utility across the application (#5277)
- Replaced direct log calls with a new logging macro that includes a logging type for better categorization.
- Updated logging in various modules including `merge.rs`, `mod.rs`, `tun.rs`, `clash.rs`, `profile.rs`, `proxy.rs`, `window.rs`, `lightweight.rs`, `guard.rs`, `autostart.rs`, `dirs.rs`, `dns.rs`, `scheme.rs`, `server.rs`, and `window_manager.rs`.
- Introduced logging types such as `Core`, `Network`, `ProxyMode`, `Window`, `Lightweight`, `Service`, and `File` to enhance log clarity and filtering.
2025-11-01 20:47:01 +08:00
|
|
|
logging!(
|
|
|
|
|
error,
|
|
|
|
|
Type::Window,
|
|
|
|
|
"failed to setup singleton listen server"
|
|
|
|
|
);
|
2024-09-12 19:01:08 +08:00
|
|
|
bail!("app exists");
|
2022-11-12 11:37:23 +08:00
|
|
|
}
|
2025-08-18 02:02:25 +08:00
|
|
|
Ok(())
|
2021-12-23 01:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The embed server only be used to implement singleton process
|
|
|
|
|
/// maybe it can be used as pac server later
|
2024-09-23 16:31:58 +08:00
|
|
|
pub fn embed_server() {
|
2025-10-09 20:08:25 +08:00
|
|
|
let (shutdown_tx, shutdown_rx) = oneshot::channel();
|
|
|
|
|
#[allow(clippy::expect_used)]
|
|
|
|
|
SHUTDOWN_SENDER
|
|
|
|
|
.set(Mutex::new(Some(shutdown_tx)))
|
|
|
|
|
.expect("failed to set shutdown signal for embedded server");
|
2022-11-17 17:07:13 +08:00
|
|
|
let port = IVerge::get_singleton_port();
|
2021-12-23 01:35:17 +08:00
|
|
|
|
2025-10-21 22:39:32 +08:00
|
|
|
AsyncHandler::spawn(move || async move {
|
2025-08-26 01:49:51 +08:00
|
|
|
let visible = warp::path!("commands" / "visible").and_then(|| async {
|
2025-10-12 22:55:40 +08:00
|
|
|
logging!(info, Type::Window, "检测到从单例模式恢复应用窗口");
|
|
|
|
|
if !lightweight::exit_lightweight_mode().await {
|
|
|
|
|
WindowManager::show_main_window().await;
|
|
|
|
|
} else {
|
|
|
|
|
logging!(error, Type::Window, "轻量模式退出失败,无法恢复应用窗口");
|
|
|
|
|
};
|
2025-10-22 16:25:44 +08:00
|
|
|
Ok::<_, warp::Rejection>(warp::reply::with_status::<std::string::String>(
|
|
|
|
|
"ok".to_string(),
|
2025-08-26 01:49:51 +08:00
|
|
|
warp::http::StatusCode::OK,
|
|
|
|
|
))
|
2022-11-12 11:37:23 +08:00
|
|
|
});
|
2021-12-29 18:49:38 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
let verge_config = Config::verge().await;
|
|
|
|
|
let clash_config = Config::clash().await;
|
|
|
|
|
|
2025-10-20 16:34:38 +08:00
|
|
|
let pac_content = verge_config
|
Refactor configuration access to use latest_arc() instead of latest_ref()
- Updated multiple instances in the codebase to replace calls to latest_ref() with latest_arc() for improved performance and memory management.
- This change affects various modules including validate, enhance, feat (backup, clash, config, profile, proxy, window), utils (draft, i18n, init, network, resolve, server).
- Ensured that all references to configuration data are now using the new arc-based approach to enhance concurrency and reduce cloning overhead.
refactor: update imports to explicitly include ClashInfo and Config in command files
2025-11-03 05:41:53 +08:00
|
|
|
.latest_arc()
|
2025-08-26 01:49:51 +08:00
|
|
|
.pac_file_content
|
|
|
|
|
.clone()
|
2025-10-22 17:33:55 +08:00
|
|
|
.unwrap_or_else(|| DEFAULT_PAC.into());
|
2025-08-26 01:49:51 +08:00
|
|
|
|
2025-10-20 16:34:38 +08:00
|
|
|
let pac_port = verge_config
|
Refactor configuration access to use latest_arc() instead of latest_ref()
- Updated multiple instances in the codebase to replace calls to latest_ref() with latest_arc() for improved performance and memory management.
- This change affects various modules including validate, enhance, feat (backup, clash, config, profile, proxy, window), utils (draft, i18n, init, network, resolve, server).
- Ensured that all references to configuration data are now using the new arc-based approach to enhance concurrency and reduce cloning overhead.
refactor: update imports to explicitly include ClashInfo and Config in command files
2025-11-03 05:41:53 +08:00
|
|
|
.latest_arc()
|
2025-08-26 01:49:51 +08:00
|
|
|
.verge_mixed_port
|
Refactor configuration access to use latest_arc() instead of latest_ref()
- Updated multiple instances in the codebase to replace calls to latest_ref() with latest_arc() for improved performance and memory management.
- This change affects various modules including validate, enhance, feat (backup, clash, config, profile, proxy, window), utils (draft, i18n, init, network, resolve, server).
- Ensured that all references to configuration data are now using the new arc-based approach to enhance concurrency and reduce cloning overhead.
refactor: update imports to explicitly include ClashInfo and Config in command files
2025-11-03 05:41:53 +08:00
|
|
|
.unwrap_or_else(|| clash_config.latest_arc().get_mixed_port());
|
2025-08-26 01:49:51 +08:00
|
|
|
|
|
|
|
|
let pac = warp::path!("commands" / "pac").map(move || {
|
|
|
|
|
let processed_content = pac_content.replace("%mixed-port%", &format!("{pac_port}"));
|
2024-05-26 19:26:57 +08:00
|
|
|
warp::http::Response::builder()
|
|
|
|
|
.header("Content-Type", "application/x-ns-proxy-autoconfig")
|
2025-08-26 01:49:51 +08:00
|
|
|
.body(processed_content)
|
2024-05-26 19:26:57 +08:00
|
|
|
.unwrap_or_default()
|
2024-05-26 17:59:39 +08:00
|
|
|
});
|
2024-09-04 07:53:16 +08:00
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
// Use map instead of and_then to avoid Send issues
|
2024-09-16 06:37:39 +08:00
|
|
|
let scheme = warp::path!("commands" / "scheme")
|
|
|
|
|
.and(warp::query::<QueryParam>())
|
2025-08-26 01:49:51 +08:00
|
|
|
.map(|query: QueryParam| {
|
2025-10-21 22:39:32 +08:00
|
|
|
tokio::task::spawn_local(async move {
|
2025-11-01 20:03:56 +08:00
|
|
|
logging_error!(Type::Setup, resolve::resolve_scheme(&query.param).await);
|
2025-08-26 01:49:51 +08:00
|
|
|
});
|
2025-10-22 16:25:44 +08:00
|
|
|
warp::reply::with_status::<std::string::String>(
|
|
|
|
|
"ok".to_string(),
|
|
|
|
|
warp::http::StatusCode::OK,
|
|
|
|
|
)
|
2025-08-26 01:49:51 +08:00
|
|
|
});
|
|
|
|
|
|
2024-09-16 06:37:39 +08:00
|
|
|
let commands = visible.or(scheme).or(pac);
|
2025-10-09 20:08:25 +08:00
|
|
|
warp::serve(commands)
|
|
|
|
|
.bind(([127, 0, 0, 1], port))
|
|
|
|
|
.await
|
|
|
|
|
.graceful(async {
|
|
|
|
|
shutdown_rx.await.ok();
|
|
|
|
|
})
|
|
|
|
|
.run()
|
|
|
|
|
.await;
|
2022-11-12 11:37:23 +08:00
|
|
|
});
|
2021-12-23 01:35:17 +08:00
|
|
|
}
|
2025-10-09 20:08:25 +08:00
|
|
|
|
|
|
|
|
pub fn shutdown_embedded_server() {
|
Refactor logging to use a centralized logging utility across the application (#5277)
- Replaced direct log calls with a new logging macro that includes a logging type for better categorization.
- Updated logging in various modules including `merge.rs`, `mod.rs`, `tun.rs`, `clash.rs`, `profile.rs`, `proxy.rs`, `window.rs`, `lightweight.rs`, `guard.rs`, `autostart.rs`, `dirs.rs`, `dns.rs`, `scheme.rs`, `server.rs`, and `window_manager.rs`.
- Introduced logging types such as `Core`, `Network`, `ProxyMode`, `Window`, `Lightweight`, `Service`, and `File` to enhance log clarity and filtering.
2025-11-01 20:47:01 +08:00
|
|
|
logging!(info, Type::Window, "shutting down embedded server");
|
2025-10-09 20:08:25 +08:00
|
|
|
if let Some(sender) = SHUTDOWN_SENDER.get()
|
|
|
|
|
&& let Some(sender) = sender.lock().take()
|
|
|
|
|
{
|
|
|
|
|
sender.send(()).ok();
|
|
|
|
|
}
|
|
|
|
|
}
|