refactor: streamline clash delay test and improve API interactions

This commit is contained in:
Tunglies
2025-03-09 00:04:48 +08:00
parent 15e54df67c
commit 0f60d84f6c
3 changed files with 56 additions and 73 deletions

View File

@@ -1,7 +1,6 @@
use crate::config::Config;
use anyhow::Result;
use reqwest::header::HeaderMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Rate {
@@ -9,36 +8,6 @@ pub struct Rate {
pub down: u64,
}
#[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>,
timeout: i32,
) -> Result<DelayRes> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/proxies/{name}/delay");
let default_url = "http://cp.cloudflare.com/generate_204";
let test_url = test_url
.map(|s| if s.is_empty() { default_url.into() } else { s })
.unwrap_or(default_url.into());
let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client
.get(&url)
.headers(headers)
.query(&[("timeout", &format!("{timeout}")), ("url", &test_url)]);
let response = builder.send().await?;
Ok(response.json::<DelayRes>().await?)
}
/// 根据clash info获取clash服务地址和请求头
pub fn clash_client_info() -> Result<(String, HeaderMap)> {
let client = { Config::clash().data().get_client_info() };