diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index 2e3ca841db20183a446381a5f222047b0e66c7ea..4b42ebe4d07460327c1ce26230c97733f9107eb2 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -229,6 +229,17 @@ pub fn open_web_url(url: String) -> CmdResult<()> { wrap_err!(open::that(url)) } +#[tauri::command] +pub async fn clash_api_get_proxy_delay( + name: String, + url: Option<String>, +) -> CmdResult<clash_api::DelayRes> { + match clash_api::get_proxy_delay(name, url).await { + Ok(res) => Ok(res), + Err(err) => Err(format!("{}", err.to_string())), + } +} + #[cfg(windows)] pub mod service { use super::*; diff --git a/src-tauri/src/core/clash_api.rs b/src-tauri/src/core/clash_api.rs index 4714371bbcf1435e85718aa35fd933ef05593907..5dafcf6651883eec9869b3d58a4e1cd4db768a39 100644 --- a/src-tauri/src/core/clash_api.rs +++ b/src-tauri/src/core/clash_api.rs @@ -1,6 +1,7 @@ use crate::config::Config; use anyhow::{bail, Result}; use reqwest::header::HeaderMap; +use serde::{Deserialize, Serialize}; use serde_yaml::Mapping; use std::collections::HashMap; @@ -36,6 +37,28 @@ pub async fn patch_configs(config: &Mapping) -> Result<()> { Ok(()) } +#[derive(Default, Debug, Clone, Deserialize, Serialize)] +pub struct DelayRes { + delay: u64, +} + +/// GET /proxies/{name}/delay +/// 获å–代ç†å»¶è¿Ÿ +pub async fn get_proxy_delay(name: String, test_url: Option<String>) -> Result<DelayRes> { + let (url, headers) = clash_client_info()?; + let url = format!("{url}/proxies/{name}/delay"); + let test_url = test_url.unwrap_or("http://www.gstatic.com/generate_204".into()); + + let client = reqwest::ClientBuilder::new().no_proxy().build()?; + let builder = client + .get(&url) + .headers(headers) + .query(&[("timeout", "10000"), ("url", &test_url)]); + let response = builder.send().await?; + + Ok(response.json::<DelayRes>().await?) +} + /// æ ¹æ®clash info获å–clashæœåŠ¡åœ°å€å’Œè¯·æ±‚头 fn clash_client_info() -> Result<(String, HeaderMap)> { let client = { Config::clash().data().get_client_info() }; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 59903db9ce597b47d3c5c50ba8164a53dc658f1e..0c20becbd85ab8db82d30e568c5dfb597dd01e1c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -66,6 +66,8 @@ fn main() -> std::io::Result<()> { cmds::service::check_service, cmds::service::install_service, cmds::service::uninstall_service, + // clash api + cmds::clash_api_get_proxy_delay ]); #[cfg(target_os = "macos")] diff --git a/src/services/api.ts b/src/services/api.ts index 263f90edb2d5c7e1309edff88cf27d3fb9261c4c..c9ded0790ba2da71bdc64321bbd46de4b83c9a15 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -65,7 +65,7 @@ export const getRules = async () => { /// Get Proxy delay export const getProxyDelay = async (name: string, url?: string) => { const params = { - timeout: 5000, + timeout: 10000, url: url || "http://www.gstatic.com/generate_204", }; const instance = await getAxios(); diff --git a/src/services/cmds.ts b/src/services/cmds.ts index 2bd857e1643b38946da21e4c2c7310831d3fb792..02ee72a6dba8476e5f73f315317dd99aa9892719 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -153,6 +153,11 @@ export async function openWebUrl(url: string) { return invoke<void>("open_web_url", { url }); } +export async function cmdGetProxyDelay(name: string, url?: string) { + name = encodeURIComponent(name); + return invoke<{ delay: number }>("clash_api_get_proxy_delay", { name, url }); +} + /// service mode export async function checkService() { diff --git a/src/services/delay.ts b/src/services/delay.ts index 91e4ab8985f9c92d7a52e6efb3d122b444952af5..9b18776083570964f1b740384abb3c18e9fef3bb 100644 --- a/src/services/delay.ts +++ b/src/services/delay.ts @@ -1,4 +1,4 @@ -import { getProxyDelay } from "./api"; +import { cmdGetProxyDelay } from "./cmds"; const hashKey = (name: string, group: string) => `${group ?? ""}::${name}`; @@ -74,7 +74,7 @@ class DelayManager { try { const url = this.getUrl(group); - const result = await getProxyDelay(name, url); + const result = await cmdGetProxyDelay(name, url); delay = result.delay; } catch { delay = 1e6; // error @@ -84,7 +84,7 @@ class DelayManager { return delay; } - async checkListDelay(nameList: string[], group: string, concurrency = 6) { + async checkListDelay(nameList: string[], group: string, concurrency = 36) { const names = nameList.filter(Boolean); // 设置æ£åœ¨å»¶è¿Ÿæµ‹è¯•ä¸ names.forEach((name) => this.setDelay(name, group, -2));