Files
clash-proxy/src-tauri/src/utils/server.rs

88 lines
2.8 KiB
Rust
Raw Normal View History

extern crate warp;
use super::resolve;
use crate::{
config::{Config, IVerge, DEFAULT_PAC},
logging_error,
process::AsyncHandler,
utils::logging::Type,
};
2022-11-18 08:24:27 +08:00
use anyhow::{bail, Result};
use port_scanner::local_port_available;
2024-09-16 06:37:39 +08:00
use std::convert::Infallible;
use warp::Filter;
2024-09-16 06:37:39 +08:00
#[derive(serde::Deserialize, Debug)]
struct QueryParam {
param: String,
}
/// check whether there is already exists
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) {
2024-09-16 06:37:39 +08:00
let argvs: Vec<String> = std::env::args().collect();
if argvs.len() > 1 {
#[cfg(not(target_os = "macos"))]
{
let param = argvs[1].as_str();
if param.starts_with("clash:") {
let _ = reqwest::get(format!(
"http://127.0.0.1:{port}/commands/scheme?param={param}"
))
.await;
}
}
} else {
let _ = reqwest::get(format!("http://127.0.0.1:{port}/commands/visible")).await;
}
log::error!("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
}
fix: clippy errors with new config (#4428) * refactor: improve code quality with clippy fixes and standardized logging - Replace dangerous unwrap()/expect() calls with proper error handling - Standardize logging from log:: to logging\! macro with Type:: classifications - Fix app handle panics with graceful fallback patterns - Improve error resilience across 35+ modules without breaking functionality - Reduce clippy warnings from 300+ to 0 in main library code * chore: update Cargo.toml configuration * refactor: resolve all clippy warnings - Fix Arc clone warnings using explicit Arc::clone syntax across 9 files - Add #[allow(clippy::expect_used)] to test functions for appropriate expect usage - Remove no-effect statements from debug code cleanup - Apply clippy auto-fixes for dbg\! macro removals and path statements - Achieve zero clippy warnings on all targets with -D warnings flag * chore: update Cargo.toml clippy configuration * refactor: simplify macOS job configuration and improve caching * refactor: remove unnecessary async/await from service and proxy functions * refactor: streamline pnpm installation in CI configuration * refactor: simplify error handling and remove unnecessary else statements * refactor: replace async/await with synchronous locks for core management * refactor: add workflow_dispatch trigger to clippy job * refactor: convert async functions to synchronous for service management * refactor: convert async functions to synchronous for UWP tool invocation * fix: change wrong logging * refactor: convert proxy restoration functions to async * Revert "refactor: convert proxy restoration functions to async" This reverts commit b82f5d250b2af7151e4dfd7dd411630b34ed2c18. * refactor: update proxy restoration functions to return Result types * fix: handle errors during proxy restoration and update async function signatures * fix: handle errors during proxy restoration and update async function signatures * refactor: update restore_pac_proxy and restore_sys_proxy functions to async * fix: convert restore_pac_proxy and restore_sys_proxy functions to async * fix: await restore_sys_proxy calls in proxy restoration logic * fix: suppress clippy warnings for unused async functions in proxy restoration * fix: suppress clippy warnings for unused async functions in proxy restoration
2025-08-18 02:02:25 +08:00
Ok(())
}
/// The embed server only be used to implement singleton process
/// maybe it can be used as pac server later
pub fn embed_server() {
2022-11-17 17:07:13 +08:00
let port = IVerge::get_singleton_port();
AsyncHandler::spawn(move || async move {
let visible = warp::path!("commands" / "visible").map(|| {
resolve::create_window(false);
warp::reply::with_status("ok".to_string(), warp::http::StatusCode::OK)
2022-11-12 11:37:23 +08:00
});
2021-12-29 18:49:38 +08:00
let pac = warp::path!("commands" / "pac").map(|| {
2024-05-26 17:59:39 +08:00
let content = Config::verge()
.latest_ref()
2024-05-26 17:59:39 +08:00
.pac_file_content
.clone()
.unwrap_or(DEFAULT_PAC.to_string());
let port = Config::verge()
.latest_ref()
2024-05-26 17:59:39 +08:00
.verge_mixed_port
.unwrap_or(Config::clash().latest_ref().get_mixed_port());
let content = content.replace("%mixed-port%", &format!("{port}"));
2024-05-26 19:26:57 +08:00
warp::http::Response::builder()
.header("Content-Type", "application/x-ns-proxy-autoconfig")
.body(content)
.unwrap_or_default()
2024-05-26 17:59:39 +08:00
});
async fn scheme_handler(query: QueryParam) -> Result<String, Infallible> {
logging_error!(
Type::Setup,
true,
resolve::resolve_scheme(query.param).await
);
Ok("ok".to_string())
2024-09-16 06:37:39 +08:00
}
2024-09-04 07:53:16 +08:00
2024-09-16 06:37:39 +08:00
let scheme = warp::path!("commands" / "scheme")
.and(warp::query::<QueryParam>())
.and_then(scheme_handler);
let commands = visible.or(scheme).or(pac);
warp::serve(commands).run(([127, 0, 0, 1], port)).await;
2022-11-12 11:37:23 +08:00
});
}