fix: extern controler api secert with headers

This commit is contained in:
Tunglies 2025-03-05 07:59:16 +08:00
parent 4ed36f6223
commit 5760f16272
3 changed files with 12 additions and 11 deletions

View File

@ -2,22 +2,17 @@ use super::CmdResult;
use crate::core; use crate::core;
use mihomo_api; use mihomo_api;
#[tauri::command] #[tauri::command]
pub async fn get_proxies() -> CmdResult<serde_json::Value> { pub async fn get_proxies() -> CmdResult<serde_json::Value> {
let (mihomo_server, _) = core::clash_api::clash_client_info().unwrap(); let (mihomo_server, headers) = core::clash_api::clash_client_info().unwrap();
let mihomo = mihomo_api::MihomoManager::new(mihomo_server); let mihomo = mihomo_api::MihomoManager::new(mihomo_server, headers);
Ok(mihomo Ok(mihomo.refresh_proxies().await.unwrap().get_proxies())
.refresh_proxies()
.await
.unwrap()
.get_proxies())
} }
#[tauri::command] #[tauri::command]
pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> { pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> {
let (mihomo_server, _) = core::clash_api::clash_client_info().unwrap(); let (mihomo_server, headers) = core::clash_api::clash_client_info().unwrap();
let mihomo = mihomo_api::MihomoManager::new(mihomo_server); let mihomo = mihomo_api::MihomoManager::new(mihomo_server, headers);
Ok(mihomo Ok(mihomo
.refresh_providers_proxies() .refresh_providers_proxies()
.await .await

View File

@ -1,3 +1,4 @@
use reqwest::header::HeaderMap;
use std::{ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
time::Duration, time::Duration,
@ -6,13 +7,14 @@ pub mod model;
pub use model::{MihomoData, MihomoManager}; pub use model::{MihomoData, MihomoManager};
impl MihomoManager { impl MihomoManager {
pub fn new(mihomo_server: String) -> Self { pub fn new(mihomo_server: String, headers: HeaderMap) -> Self {
Self { Self {
mihomo_server, mihomo_server,
data: Arc::new(Mutex::new(MihomoData { data: Arc::new(Mutex::new(MihomoData {
proxies: serde_json::Value::Null, proxies: serde_json::Value::Null,
providers_proxies: serde_json::Value::Null, providers_proxies: serde_json::Value::Null,
})), })),
headers: headers,
} }
} }
@ -39,6 +41,7 @@ impl MihomoManager {
pub async fn refresh_proxies(&self) -> Result<&Self, String> { pub async fn refresh_proxies(&self) -> Result<&Self, String> {
let url = format!("{}/proxies", self.mihomo_server); let url = format!("{}/proxies", self.mihomo_server);
let response = reqwest::ClientBuilder::new() let response = reqwest::ClientBuilder::new()
.default_headers(self.headers.clone())
.no_proxy() .no_proxy()
.timeout(Duration::from_secs(3)) .timeout(Duration::from_secs(3))
.build() .build()
@ -58,6 +61,7 @@ impl MihomoManager {
pub async fn refresh_providers_proxies(&self) -> Result<&Self, String> { pub async fn refresh_providers_proxies(&self) -> Result<&Self, String> {
let url = format!("{}/providers/proxies", self.mihomo_server); let url = format!("{}/providers/proxies", self.mihomo_server);
let response = reqwest::ClientBuilder::new() let response = reqwest::ClientBuilder::new()
.default_headers(self.headers.clone())
.no_proxy() .no_proxy()
.timeout(Duration::from_secs(3)) .timeout(Duration::from_secs(3))
.build() .build()

View File

@ -1,4 +1,5 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use reqwest::header::HeaderMap;
pub struct MihomoData { pub struct MihomoData {
pub(crate) proxies: serde_json::Value, pub(crate) proxies: serde_json::Value,
@ -9,6 +10,7 @@ pub struct MihomoData {
pub struct MihomoManager { pub struct MihomoManager {
pub(crate) mihomo_server: String, pub(crate) mihomo_server: String,
pub(crate) data: Arc<Mutex<MihomoData>>, pub(crate) data: Arc<Mutex<MihomoData>>,
pub(crate) headers: HeaderMap,
} }
#[cfg(feature = "debug")] #[cfg(feature = "debug")]