refactor: update MihomoManager to handle traffic WebSocket URL and authorization

This commit is contained in:
Tunglies 2025-03-09 14:43:58 +08:00
parent e1905aced4
commit 48f1da963a
7 changed files with 35 additions and 23 deletions

1
src-tauri/Cargo.lock generated
View File

@ -1184,6 +1184,7 @@ dependencies = [
"tempfile", "tempfile",
"tokio", "tokio",
"tokio-tungstenite 0.26.2", "tokio-tungstenite 0.26.2",
"tungstenite 0.26.2",
"url", "url",
"users", "users",
"warp", "warp",

View File

@ -70,6 +70,7 @@ sys-locale = "0.3.1"
async-trait = "0.1.87" async-trait = "0.1.87"
mihomo_api = { path = "./src/crate_mihomo_api" } mihomo_api = { path = "./src/crate_mihomo_api" }
ab_glyph = "0.2.29" ab_glyph = "0.2.29"
tungstenite = "0.26.2"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
runas = "=1.2.0" runas = "=1.2.0"

View File

@ -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<String> {
use crate::module::mihomo::MihomoManager;
let (url, _) = MihomoManager::get_clash_client_info().unwrap();
let ws_url = url.replace("http://", "ws://") + "/traffic";
Ok(ws_url)
}

View File

@ -1,5 +1,4 @@
pub mod backup; pub mod backup;
pub mod clash_api;
#[allow(clippy::module_inception)] #[allow(clippy::module_inception)]
mod core; mod core;
pub mod handle; pub mod handle;

View File

@ -1,7 +1,7 @@
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub mod speed_rate; pub mod speed_rate;
use crate::core::clash_api::Rate; use crate::module::mihomo::Rate;
use crate::{ use crate::{
cmd, cmd,
config::Config, config::Config,

View File

@ -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 crate::utils::help::format_bytes_speed;
use ab_glyph::FontArc; use ab_glyph::FontArc;
use anyhow::Result; use anyhow::Result;
@ -8,8 +9,9 @@ use imageproc::drawing::draw_text_mut;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::io::Cursor; use std::io::Cursor;
use std::sync::Arc; use std::sync::Arc;
use tokio_tungstenite::tungstenite::http;
use tokio_tungstenite::tungstenite::Message; use tokio_tungstenite::tungstenite::Message;
use tungstenite::client::IntoClientRequest;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SpeedRate { pub struct SpeedRate {
rate: Arc<Mutex<(Rate, Rate)>>, rate: Arc<Mutex<(Rate, Rate)>>,
@ -198,9 +200,13 @@ impl Traffic {
let stream = Box::pin( let stream = Box::pin(
stream::unfold((), |_| async { stream::unfold((), |_| async {
loop { 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, _)) => { Ok((ws_stream, _)) => {
log::info!(target: "app", "traffic ws connection established"); log::info!(target: "app", "traffic ws connection established");
return Some(( return Some((

View File

@ -2,7 +2,15 @@ use crate::config::Config;
use mihomo_api; use mihomo_api;
use once_cell::sync::{Lazy, OnceCell}; use once_cell::sync::{Lazy, OnceCell};
use std::sync::Mutex; 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 { pub struct MihomoManager {
mihomo: Mutex<OnceCell<mihomo_api::MihomoManager>>, mihomo: Mutex<OnceCell<mihomo_api::MihomoManager>>,
@ -46,4 +54,17 @@ impl MihomoManager {
Some((server, headers)) 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);
}
} }