diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 43ce006c..7327c9f8 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1184,6 +1184,7 @@ dependencies = [ "tempfile", "tokio", "tokio-tungstenite 0.26.2", + "tungstenite 0.26.2", "url", "users", "warp", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 94788498..7063ee0d 100755 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -70,6 +70,7 @@ sys-locale = "0.3.1" async-trait = "0.1.87" mihomo_api = { path = "./src/crate_mihomo_api" } ab_glyph = "0.2.29" +tungstenite = "0.26.2" [target.'cfg(windows)'.dependencies] runas = "=1.2.0" diff --git a/src-tauri/src/core/clash_api.rs b/src-tauri/src/core/clash_api.rs deleted file mode 100644 index b594ba62..00000000 --- a/src-tauri/src/core/clash_api.rs +++ /dev/null @@ -1,16 +0,0 @@ -use anyhow::Result; - -#[derive(Debug, Clone, Default, PartialEq)] -pub struct Rate { - pub up: u64, - pub down: u64, -} - -#[cfg(target_os = "macos")] -pub fn get_traffic_ws_url() -> Result { - use crate::module::mihomo::MihomoManager; - - let (url, _) = MihomoManager::get_clash_client_info().unwrap(); - let ws_url = url.replace("http://", "ws://") + "/traffic"; - Ok(ws_url) -} \ No newline at end of file diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index 098052b9..7fade5ce 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -1,5 +1,4 @@ pub mod backup; -pub mod clash_api; #[allow(clippy::module_inception)] mod core; pub mod handle; diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 68f69fa1..50f266ea 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -1,7 +1,7 @@ use once_cell::sync::OnceCell; #[cfg(target_os = "macos")] pub mod speed_rate; -use crate::core::clash_api::Rate; +use crate::module::mihomo::Rate; use crate::{ cmd, config::Config, diff --git a/src-tauri/src/core/tray/speed_rate.rs b/src-tauri/src/core/tray/speed_rate.rs index 59a00ea5..d0dc4755 100644 --- a/src-tauri/src/core/tray/speed_rate.rs +++ b/src-tauri/src/core/tray/speed_rate.rs @@ -1,4 +1,5 @@ -use crate::core::clash_api::{get_traffic_ws_url, Rate}; +use crate::module::mihomo::Rate; +use crate::module::mihomo::MihomoManager; use crate::utils::help::format_bytes_speed; use ab_glyph::FontArc; use anyhow::Result; @@ -8,8 +9,9 @@ use imageproc::drawing::draw_text_mut; use parking_lot::Mutex; use std::io::Cursor; use std::sync::Arc; +use tokio_tungstenite::tungstenite::http; use tokio_tungstenite::tungstenite::Message; - +use tungstenite::client::IntoClientRequest; #[derive(Debug, Clone)] pub struct SpeedRate { rate: Arc>, @@ -198,9 +200,13 @@ impl Traffic { let stream = Box::pin( stream::unfold((), |_| async { loop { - let ws_url = get_traffic_ws_url().unwrap(); + let (url, token) = MihomoManager::get_traffic_ws_url(); + let mut request = url.into_client_request().unwrap(); + request + .headers_mut() + .insert(http::header::AUTHORIZATION, token); - match tokio_tungstenite::connect_async(&ws_url).await { + match tokio_tungstenite::connect_async(request).await { Ok((ws_stream, _)) => { log::info!(target: "app", "traffic ws connection established"); return Some(( diff --git a/src-tauri/src/module/mihomo.rs b/src-tauri/src/module/mihomo.rs index 41c8ac10..67dd0d00 100644 --- a/src-tauri/src/module/mihomo.rs +++ b/src-tauri/src/module/mihomo.rs @@ -2,7 +2,15 @@ use crate::config::Config; use mihomo_api; use once_cell::sync::{Lazy, OnceCell}; use std::sync::Mutex; -use tauri::http::HeaderMap; +use tauri::http::{HeaderMap, HeaderValue}; +#[cfg(target_os = "macos")] +use tokio_tungstenite::tungstenite::http; + +#[derive(Debug, Clone, Default, PartialEq)] +pub struct Rate { + pub up: u64, + pub down: u64, +} pub struct MihomoManager { mihomo: Mutex>, @@ -46,4 +54,17 @@ impl MihomoManager { Some((server, headers)) } + #[cfg(target_os = "macos")] + pub fn get_traffic_ws_url() -> (String, HeaderValue) { + let (url, headers) = MihomoManager::get_clash_client_info().unwrap(); + let ws_url = url.replace("http://", "ws://") + "/traffic"; + let auth = headers + .get("Authorization") + .unwrap() + .to_str() + .unwrap() + .to_string(); + let token = http::header::HeaderValue::from_str(&auth).unwrap(); + return (ws_url, token); + } }