mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 06:53:44 +08:00
refactor: migrate clash client info retrieval to MihomoManager
This commit is contained in:
parent
e6686e0b82
commit
18f86874ee
@ -1,6 +1,4 @@
|
|||||||
use crate::config::Config;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use reqwest::header::HeaderMap;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, PartialEq)]
|
#[derive(Debug, Clone, Default, PartialEq)]
|
||||||
pub struct Rate {
|
pub struct Rate {
|
||||||
@ -8,26 +6,11 @@ pub struct Rate {
|
|||||||
pub down: u64,
|
pub down: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 根据clash info获取clash服务地址和请求头
|
|
||||||
pub fn clash_client_info() -> Result<(String, HeaderMap)> {
|
|
||||||
let client = { Config::clash().data().get_client_info() };
|
|
||||||
|
|
||||||
let server = format!("http://{}", client.server);
|
|
||||||
|
|
||||||
let mut headers = HeaderMap::new();
|
|
||||||
headers.insert("Content-Type", "application/json".parse()?);
|
|
||||||
|
|
||||||
if let Some(secret) = client.secret {
|
|
||||||
let secret = format!("Bearer {}", secret).parse()?;
|
|
||||||
headers.insert("Authorization", secret);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((server, headers))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
pub fn get_traffic_ws_url() -> Result<String> {
|
pub fn get_traffic_ws_url() -> Result<String> {
|
||||||
let (url, _) = clash_client_info()?;
|
use crate::module::mihomo::MihomoManager;
|
||||||
|
|
||||||
|
let (url, _) = MihomoManager::get_clash_client_info().unwrap();
|
||||||
let ws_url = url.replace("http://", "ws://") + "/traffic";
|
let ws_url = url.replace("http://", "ws://") + "/traffic";
|
||||||
Ok(ws_url)
|
Ok(ws_url)
|
||||||
}
|
}
|
@ -100,16 +100,8 @@ impl MihomoManager {
|
|||||||
let payload = serde_json::json!({
|
let payload = serde_json::json!({
|
||||||
"path": clash_config_path,
|
"path": clash_config_path,
|
||||||
});
|
});
|
||||||
let response = self.send_request("PUT", url, Some(payload)).await?;
|
let _response = self.send_request("PUT", url, Some(payload)).await?;
|
||||||
println!("put_configs_force::{:?}", response);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
// if response["code"] == 204 {
|
|
||||||
// Ok(())
|
|
||||||
// } else {
|
|
||||||
// Err(response["message"]
|
|
||||||
// .as_str()
|
|
||||||
// .unwrap_or("unknown error")
|
|
||||||
// .to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn patch_configs(&self, config: serde_json::Value) -> Result<(), String> {
|
pub async fn patch_configs(&self, config: serde_json::Value) -> Result<(), String> {
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use crate::core::clash_api;
|
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;
|
||||||
|
|
||||||
pub struct MihomoManager {
|
pub struct MihomoManager {
|
||||||
mihomo: Mutex<OnceCell<mihomo_api::MihomoManager>>,
|
mihomo: Mutex<OnceCell<mihomo_api::MihomoManager>>,
|
||||||
@ -17,7 +18,7 @@ impl MihomoManager {
|
|||||||
|
|
||||||
pub fn global() -> mihomo_api::MihomoManager {
|
pub fn global() -> mihomo_api::MihomoManager {
|
||||||
let instance = MihomoManager::__global();
|
let instance = MihomoManager::__global();
|
||||||
let (current_server, headers) = clash_api::clash_client_info().unwrap();
|
let (current_server, headers) = MihomoManager::get_clash_client_info().unwrap();
|
||||||
|
|
||||||
let lock = instance.mihomo.lock().unwrap();
|
let lock = instance.mihomo.lock().unwrap();
|
||||||
if let Some(mihomo) = lock.get() {
|
if let Some(mihomo) = lock.get() {
|
||||||
@ -31,3 +32,18 @@ impl MihomoManager {
|
|||||||
lock.get().unwrap().clone()
|
lock.get().unwrap().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl MihomoManager {
|
||||||
|
pub fn get_clash_client_info() -> Option<(String, HeaderMap)> {
|
||||||
|
let client = { Config::clash().data().get_client_info() };
|
||||||
|
let server = format!("http://{}", client.server);
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert("Content-Type", "application/json".parse().unwrap());
|
||||||
|
if let Some(secret) = client.secret {
|
||||||
|
let secret = format!("Bearer {}", secret).parse().unwrap();
|
||||||
|
headers.insert("Authorization", secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
Some((server, headers))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user