2025-03-04 01:01:24 +08:00
|
|
|
use super::CmdResult;
|
2025-06-30 20:14:04 +08:00
|
|
|
use crate::module::mihomo::MihomoManager;
|
|
|
|
|
use std::time::Duration;
|
2025-05-25 21:34:48 +08:00
|
|
|
|
2025-06-30 20:14:04 +08:00
|
|
|
use crate::state::proxy::ProxyRequestCache;
|
|
|
|
|
|
|
|
|
|
const PROXIES_REFRESH_INTERVAL: Duration = Duration::from_secs(60);
|
|
|
|
|
const PROVIDERS_REFRESH_INTERVAL: Duration = Duration::from_secs(60);
|
2025-03-05 00:45:08 +08:00
|
|
|
|
2025-03-04 01:01:24 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn get_proxies() -> CmdResult<serde_json::Value> {
|
2025-05-25 21:34:48 +08:00
|
|
|
let manager = MihomoManager::global();
|
2025-06-30 20:14:04 +08:00
|
|
|
let cache = ProxyRequestCache::global();
|
|
|
|
|
let key = ProxyRequestCache::make_key("proxies", "default");
|
|
|
|
|
let value = cache
|
|
|
|
|
.get_or_fetch(key, PROXIES_REFRESH_INTERVAL, || async {
|
|
|
|
|
manager.get_refresh_proxies().await.expect("fetch failed")
|
|
|
|
|
})
|
|
|
|
|
.await;
|
|
|
|
|
Ok((*value).clone())
|
2025-03-04 01:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-17 11:38:53 +08:00
|
|
|
/// 强制刷新代理缓存用于profile切换
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn force_refresh_proxies() -> CmdResult<serde_json::Value> {
|
2025-06-30 20:14:04 +08:00
|
|
|
let cache = ProxyRequestCache::global();
|
|
|
|
|
let key = ProxyRequestCache::make_key("proxies", "default");
|
|
|
|
|
cache.map.remove(&key);
|
|
|
|
|
get_proxies().await
|
2025-06-17 11:38:53 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-04 01:01:24 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> {
|
2025-06-30 20:14:04 +08:00
|
|
|
let manager = MihomoManager::global();
|
|
|
|
|
let cache = ProxyRequestCache::global();
|
|
|
|
|
let key = ProxyRequestCache::make_key("providers", "default");
|
|
|
|
|
let value = cache
|
|
|
|
|
.get_or_fetch(key, PROVIDERS_REFRESH_INTERVAL, || async {
|
|
|
|
|
manager.get_providers_proxies().await.expect("fetch failed")
|
|
|
|
|
})
|
|
|
|
|
.await;
|
|
|
|
|
Ok((*value).clone())
|
2025-03-05 00:45:08 +08:00
|
|
|
}
|