mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
chore: simplify service logging
This commit is contained in:
@@ -13,7 +13,7 @@ use std::{
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
const REQUIRED_SERVICE_VERSION: &str = "1.0.9"; // 定义所需的服务版本号
|
||||
const REQUIRED_SERVICE_VERSION: &str = "1.1.0"; // 定义所需的服务版本号
|
||||
|
||||
// 限制重装时间和次数的常量
|
||||
const REINSTALL_COOLDOWN_SECS: u64 = 300; // 5分钟冷却期
|
||||
@@ -375,7 +375,7 @@ pub async fn uninstall_service() -> Result<()> {
|
||||
r#"do shell script "sudo '{uninstall_shell}'" with administrator privileges with prompt "{prompt}""#
|
||||
);
|
||||
|
||||
logging!(debug, Type::Service, true, "uninstall command: {}", command);
|
||||
// logging!(debug, Type::Service, true, "uninstall command: {}", command);
|
||||
|
||||
let status = StdCommand::new("osascript")
|
||||
.args(vec!["-e", &command])
|
||||
@@ -411,7 +411,7 @@ pub async fn install_service() -> Result<()> {
|
||||
r#"do shell script "sudo '{install_shell}'" with administrator privileges with prompt "{prompt}""#
|
||||
);
|
||||
|
||||
logging!(debug, Type::Service, true, "install command: {}", command);
|
||||
// logging!(debug, Type::Service, true, "install command: {}", command);
|
||||
|
||||
let status = StdCommand::new("osascript")
|
||||
.args(vec!["-e", &command])
|
||||
@@ -481,18 +481,18 @@ pub async fn check_ipc_service_status() -> Result<JsonResponse> {
|
||||
|
||||
// 使用IPC通信
|
||||
let payload = serde_json::json!({});
|
||||
logging!(debug, Type::Service, true, "发送GetClash请求");
|
||||
// logging!(debug, Type::Service, true, "发送GetClash请求");
|
||||
|
||||
match send_ipc_request(IpcCommand::GetClash, payload).await {
|
||||
Ok(response) => {
|
||||
logging!(
|
||||
/* logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"收到GetClash响应: success={}, error={:?}",
|
||||
response.success,
|
||||
response.error
|
||||
);
|
||||
); */
|
||||
|
||||
if !response.success {
|
||||
let err_msg = response.error.unwrap_or_else(|| "未知服务错误".to_string());
|
||||
@@ -588,18 +588,18 @@ pub async fn check_service_version() -> Result<String> {
|
||||
logging!(info, Type::Service, true, "开始检查服务版本 (IPC)");
|
||||
|
||||
let payload = serde_json::json!({});
|
||||
logging!(debug, Type::Service, true, "发送GetVersion请求");
|
||||
// logging!(debug, Type::Service, true, "发送GetVersion请求");
|
||||
|
||||
match send_ipc_request(IpcCommand::GetVersion, payload).await {
|
||||
Ok(response) => {
|
||||
logging!(
|
||||
/* logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"收到GetVersion响应: success={}, error={:?}",
|
||||
response.success,
|
||||
response.error
|
||||
);
|
||||
); */
|
||||
|
||||
if !response.success {
|
||||
let err_msg = response
|
||||
@@ -687,14 +687,14 @@ pub async fn check_service_needs_reinstall() -> bool {
|
||||
match check_service_version().await {
|
||||
Ok(version) => {
|
||||
log::info!(target: "app", "服务版本检测:当前={}, 要求={}", version, REQUIRED_SERVICE_VERSION);
|
||||
logging!(
|
||||
/* logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"服务版本检测:当前={}, 要求={}",
|
||||
version,
|
||||
REQUIRED_SERVICE_VERSION
|
||||
);
|
||||
); */
|
||||
|
||||
let needs_reinstall = version != REQUIRED_SERVICE_VERSION;
|
||||
if needs_reinstall {
|
||||
@@ -702,11 +702,11 @@ pub async fn check_service_needs_reinstall() -> bool {
|
||||
version, REQUIRED_SERVICE_VERSION);
|
||||
logging!(warn, Type::Service, true, "服务版本不匹配,需要重装");
|
||||
|
||||
log::debug!(target: "app", "当前版本字节: {:?}", version.as_bytes());
|
||||
log::debug!(target: "app", "要求版本字节: {:?}", REQUIRED_SERVICE_VERSION.as_bytes());
|
||||
// log::debug!(target: "app", "当前版本字节: {:?}", version.as_bytes());
|
||||
// log::debug!(target: "app", "要求版本字节: {:?}", REQUIRED_SERVICE_VERSION.as_bytes());
|
||||
} else {
|
||||
log::info!(target: "app", "服务版本匹配,无需重装");
|
||||
logging!(info, Type::Service, true, "服务版本匹配,无需重装");
|
||||
// logging!(info, Type::Service, true, "服务版本匹配,无需重装");
|
||||
}
|
||||
|
||||
needs_reinstall
|
||||
@@ -718,18 +718,18 @@ pub async fn check_service_needs_reinstall() -> bool {
|
||||
match is_service_running().await {
|
||||
Ok(true) => {
|
||||
log::info!(target: "app", "服务正在运行但版本检查失败: {}", err);
|
||||
logging!(
|
||||
/* logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"服务正在运行但版本检查失败: {}",
|
||||
err
|
||||
);
|
||||
); */
|
||||
false
|
||||
}
|
||||
_ => {
|
||||
log::info!(target: "app", "服务不可用或未运行,需要重装");
|
||||
logging!(info, Type::Service, true, "服务不可用或未运行,需要重装");
|
||||
// logging!(info, Type::Service, true, "服务不可用或未运行,需要重装");
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -740,7 +740,7 @@ pub async fn check_service_needs_reinstall() -> bool {
|
||||
/// 尝试使用服务启动core
|
||||
pub(super) async fn start_with_existing_service(config_file: &PathBuf) -> Result<()> {
|
||||
log::info!(target:"app", "尝试使用现有服务启动核心 (IPC)");
|
||||
logging!(info, Type::Service, true, "尝试使用现有服务启动核心");
|
||||
// logging!(info, Type::Service, true, "尝试使用现有服务启动核心");
|
||||
|
||||
let clash_core = { Config::verge().latest().clash_core.clone() };
|
||||
let clash_core = clash_core.unwrap_or("verge-mihomo".into());
|
||||
@@ -767,20 +767,20 @@ pub(super) async fn start_with_existing_service(config_file: &PathBuf) -> Result
|
||||
"log_file": log_path,
|
||||
});
|
||||
|
||||
log::info!(target:"app", "启动服务参数: {:?}", payload);
|
||||
logging!(info, Type::Service, true, "发送StartClash请求");
|
||||
// log::info!(target:"app", "启动服务参数: {:?}", payload);
|
||||
// logging!(info, Type::Service, true, "发送StartClash请求");
|
||||
|
||||
// 使用IPC通信
|
||||
match send_ipc_request(IpcCommand::StartClash, payload).await {
|
||||
Ok(response) => {
|
||||
logging!(
|
||||
/* logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"收到StartClash响应: success={}, error={:?}",
|
||||
response.success,
|
||||
response.error
|
||||
);
|
||||
); */
|
||||
|
||||
if !response.success {
|
||||
let err_msg = response.error.unwrap_or_else(|| "启动核心失败".to_string());
|
||||
@@ -910,7 +910,7 @@ pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> {
|
||||
|
||||
/// 通过服务停止core
|
||||
pub(super) async fn stop_core_by_service() -> Result<()> {
|
||||
logging!(debug, Type::Service, "通过服务停止核心 (IPC)");
|
||||
logging!(info, Type::Service, true, "通过服务停止核心 (IPC)");
|
||||
|
||||
let payload = serde_json::json!({});
|
||||
let response = send_ipc_request(IpcCommand::StopClash, payload)
|
||||
@@ -969,42 +969,42 @@ pub async fn is_service_running() -> Result<bool> {
|
||||
}
|
||||
Err(err) => {
|
||||
logging!(error, Type::Service, true, "检查服务运行状态失败: {}", err);
|
||||
/*
|
||||
let error_type = err.root_cause().to_string();
|
||||
logging!(
|
||||
error,
|
||||
Type::Service,
|
||||
true,
|
||||
"连接失败的根本原因: {}",
|
||||
error_type
|
||||
);
|
||||
|
||||
let error_type = err.root_cause().to_string();
|
||||
logging!(
|
||||
error,
|
||||
Type::Service,
|
||||
true,
|
||||
"连接失败的根本原因: {}",
|
||||
error_type
|
||||
);
|
||||
|
||||
let socket_path = if cfg!(windows) {
|
||||
r"\\.\pipe\clash-verge-service"
|
||||
} else {
|
||||
"/tmp/clash-verge-service.sock"
|
||||
};
|
||||
|
||||
if cfg!(windows) {
|
||||
logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"检查Windows命名管道: {}",
|
||||
socket_path
|
||||
);
|
||||
} else {
|
||||
let socket_exists = std::path::Path::new(socket_path).exists();
|
||||
logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"检查Unix套接字文件: {} 是否存在: {}",
|
||||
socket_path,
|
||||
socket_exists
|
||||
);
|
||||
}
|
||||
let socket_path = if cfg!(windows) {
|
||||
r"\\.\pipe\clash-verge-service"
|
||||
} else {
|
||||
"/tmp/clash-verge-service.sock"
|
||||
};
|
||||
|
||||
if cfg!(windows) {
|
||||
logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"检查Windows命名管道: {}",
|
||||
socket_path
|
||||
);
|
||||
} else {
|
||||
let socket_exists = std::path::Path::new(socket_path).exists();
|
||||
logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"检查Unix套接字文件: {} 是否存在: {}",
|
||||
socket_path,
|
||||
socket_exists
|
||||
);
|
||||
}
|
||||
*/
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,35 +129,12 @@ pub async fn send_ipc_request(
|
||||
winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE},
|
||||
};
|
||||
|
||||
logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"准备发送IPC请求到Windows命名管道: {}",
|
||||
IPC_SOCKET_NAME
|
||||
);
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"IPC请求: 命令={:?}, 数据大小={}字节",
|
||||
command,
|
||||
serde_json::to_string(&payload)?.len()
|
||||
);
|
||||
logging!(info, Type::Service, true, "正在连接服务 (Windows)...");
|
||||
|
||||
let command_type = format!("{:?}", command);
|
||||
|
||||
let request = match create_signed_request(command, payload) {
|
||||
Ok(req) => {
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"创建签名请求成功: ID={}",
|
||||
req.id
|
||||
);
|
||||
req
|
||||
}
|
||||
Ok(req) => req,
|
||||
Err(e) => {
|
||||
logging!(error, Type::Service, true, "创建签名请求失败: {}", e);
|
||||
return Err(e);
|
||||
@@ -165,14 +142,6 @@ pub async fn send_ipc_request(
|
||||
};
|
||||
|
||||
let request_json = serde_json::to_string(&request)?;
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"请求JSON大小: {}字节",
|
||||
request_json.len()
|
||||
);
|
||||
logging!(debug, Type::Service, true, "尝试连接Windows命名管道...");
|
||||
|
||||
let result = tokio::task::spawn_blocking(move || -> Result<IpcResponse> {
|
||||
let c_pipe_name = match CString::new(IPC_SOCKET_NAME) {
|
||||
@@ -197,18 +166,22 @@ pub async fn send_ipc_request(
|
||||
|
||||
if handle == INVALID_HANDLE_VALUE {
|
||||
let error = std::io::Error::last_os_error();
|
||||
logging!(error, Type::Service, true, "连接到命名管道失败: {}", error);
|
||||
logging!(
|
||||
error,
|
||||
Type::Service,
|
||||
true,
|
||||
"连接到服务命名管道失败: {}",
|
||||
error
|
||||
);
|
||||
return Err(anyhow::anyhow!("无法连接到服务命名管道: {}", error));
|
||||
}
|
||||
|
||||
let mut pipe = unsafe { File::from_raw_handle(handle as RawHandle) };
|
||||
logging!(info, Type::Service, true, "成功连接到Windows命名管道");
|
||||
logging!(info, Type::Service, true, "服务连接成功 (Windows)");
|
||||
|
||||
let request_bytes = request_json.as_bytes();
|
||||
let len_bytes = (request_bytes.len() as u32).to_be_bytes();
|
||||
|
||||
logging!(debug, Type::Service, true, "发送IPC请求...");
|
||||
|
||||
if let Err(e) = pipe.write_all(&len_bytes) {
|
||||
logging!(error, Type::Service, true, "写入请求长度失败: {}", e);
|
||||
return Err(anyhow::anyhow!("写入请求长度失败: {}", e));
|
||||
@@ -219,8 +192,6 @@ pub async fn send_ipc_request(
|
||||
return Err(anyhow::anyhow!("写入请求内容失败: {}", e));
|
||||
}
|
||||
|
||||
logging!(debug, Type::Service, true, "请求已发送,等待响应...");
|
||||
|
||||
if let Err(e) = pipe.flush() {
|
||||
logging!(error, Type::Service, true, "刷新管道失败: {}", e);
|
||||
return Err(anyhow::anyhow!("刷新管道失败: {}", e));
|
||||
@@ -233,13 +204,6 @@ pub async fn send_ipc_request(
|
||||
}
|
||||
|
||||
let response_len = u32::from_be_bytes(response_len_bytes) as usize;
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"收到响应长度: {}字节",
|
||||
response_len
|
||||
);
|
||||
|
||||
let mut response_bytes = vec![0u8; response_len];
|
||||
if let Err(e) = pipe.read_exact(&mut response_bytes) {
|
||||
@@ -247,36 +211,10 @@ pub async fn send_ipc_request(
|
||||
return Err(anyhow::anyhow!("读取响应内容失败: {}", e));
|
||||
}
|
||||
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"成功接收响应: {}字节",
|
||||
response_bytes.len()
|
||||
);
|
||||
|
||||
let response: IpcResponse = match serde_json::from_slice::<IpcResponse>(&response_bytes) {
|
||||
Ok(r) => {
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"响应解析成功: success={}",
|
||||
r.success
|
||||
);
|
||||
r
|
||||
}
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
logging!(
|
||||
error,
|
||||
Type::Service,
|
||||
true,
|
||||
"解析响应失败: {}, 原始数据: {:?}",
|
||||
e,
|
||||
String::from_utf8_lossy(
|
||||
&response_bytes[..std::cmp::min(100, response_bytes.len())]
|
||||
)
|
||||
);
|
||||
logging!(error, Type::Service, true, "服务响应解析失败: {}", e);
|
||||
return Err(anyhow::anyhow!("解析响应失败: {}", e));
|
||||
}
|
||||
};
|
||||
@@ -287,7 +225,6 @@ pub async fn send_ipc_request(
|
||||
logging!(error, Type::Service, true, "服务响应签名验证失败");
|
||||
bail!("服务响应签名验证失败");
|
||||
}
|
||||
logging!(debug, Type::Service, true, "响应签名验证成功");
|
||||
}
|
||||
Err(e) => {
|
||||
logging!(error, Type::Service, true, "验证响应签名时出错: {}", e);
|
||||
@@ -318,35 +255,12 @@ pub async fn send_ipc_request(
|
||||
) -> Result<IpcResponse> {
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
||||
logging!(
|
||||
info,
|
||||
Type::Service,
|
||||
true,
|
||||
"准备发送IPC请求到Unix套接字: {}",
|
||||
IPC_SOCKET_NAME
|
||||
);
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"IPC请求: 命令={:?}, 数据大小={}字节",
|
||||
command,
|
||||
serde_json::to_string(&payload)?.len()
|
||||
);
|
||||
logging!(info, Type::Service, true, "正在连接服务 (Unix)...");
|
||||
|
||||
let command_type = format!("{:?}", command);
|
||||
|
||||
let request = match create_signed_request(command, payload) {
|
||||
Ok(req) => {
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"创建签名请求成功: ID={}",
|
||||
req.id
|
||||
);
|
||||
req
|
||||
}
|
||||
Ok(req) => req,
|
||||
Err(e) => {
|
||||
logging!(error, Type::Service, true, "创建签名请求失败: {}", e);
|
||||
return Err(e);
|
||||
@@ -354,29 +268,10 @@ pub async fn send_ipc_request(
|
||||
};
|
||||
|
||||
let request_json = serde_json::to_string(&request)?;
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"请求JSON大小: {}字节",
|
||||
request_json.len()
|
||||
);
|
||||
|
||||
logging!(debug, Type::Service, true, "尝试连接Unix套接字...");
|
||||
|
||||
let socket_exists = std::path::Path::new(IPC_SOCKET_NAME).exists();
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"Unix套接字文件 {} 是否存在: {}",
|
||||
IPC_SOCKET_NAME,
|
||||
socket_exists
|
||||
);
|
||||
|
||||
let mut stream = match UnixStream::connect(IPC_SOCKET_NAME) {
|
||||
Ok(s) => {
|
||||
logging!(info, Type::Service, true, "成功连接到Unix套接字");
|
||||
logging!(info, Type::Service, true, "服务连接成功 (Unix)");
|
||||
s
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -388,8 +283,6 @@ pub async fn send_ipc_request(
|
||||
let request_bytes = request_json.as_bytes();
|
||||
let len_bytes = (request_bytes.len() as u32).to_be_bytes();
|
||||
|
||||
logging!(debug, Type::Service, true, "发送IPC请求...");
|
||||
|
||||
if let Err(e) = std::io::Write::write_all(&mut stream, &len_bytes) {
|
||||
logging!(error, Type::Service, true, "写入请求长度失败: {}", e);
|
||||
return Err(anyhow::anyhow!("写入请求长度失败: {}", e));
|
||||
@@ -400,8 +293,6 @@ pub async fn send_ipc_request(
|
||||
return Err(anyhow::anyhow!("写入请求内容失败: {}", e));
|
||||
}
|
||||
|
||||
logging!(debug, Type::Service, true, "请求已发送,等待响应...");
|
||||
|
||||
let mut response_len_bytes = [0u8; 4];
|
||||
if let Err(e) = std::io::Read::read_exact(&mut stream, &mut response_len_bytes) {
|
||||
logging!(error, Type::Service, true, "读取响应长度失败: {}", e);
|
||||
@@ -409,13 +300,6 @@ pub async fn send_ipc_request(
|
||||
}
|
||||
|
||||
let response_len = u32::from_be_bytes(response_len_bytes) as usize;
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"收到响应长度: {}字节",
|
||||
response_len
|
||||
);
|
||||
|
||||
let mut response_bytes = vec![0u8; response_len];
|
||||
if let Err(e) = std::io::Read::read_exact(&mut stream, &mut response_bytes) {
|
||||
@@ -423,36 +307,10 @@ pub async fn send_ipc_request(
|
||||
return Err(anyhow::anyhow!("读取响应内容失败: {}", e));
|
||||
}
|
||||
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"成功接收响应: {}字节",
|
||||
response_bytes.len()
|
||||
);
|
||||
|
||||
let response: IpcResponse = match serde_json::from_slice::<IpcResponse>(&response_bytes) {
|
||||
Ok(r) => {
|
||||
logging!(
|
||||
debug,
|
||||
Type::Service,
|
||||
true,
|
||||
"响应解析成功: success={}",
|
||||
r.success
|
||||
);
|
||||
r
|
||||
}
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
logging!(
|
||||
error,
|
||||
Type::Service,
|
||||
true,
|
||||
"解析响应失败: {}, 原始数据: {:?}",
|
||||
e,
|
||||
String::from_utf8_lossy(
|
||||
&response_bytes[..std::cmp::min(100, response_bytes.len())]
|
||||
)
|
||||
);
|
||||
logging!(error, Type::Service, true, "服务响应解析失败: {}", e,);
|
||||
return Err(anyhow::anyhow!("解析响应失败: {}", e));
|
||||
}
|
||||
};
|
||||
@@ -463,7 +321,6 @@ pub async fn send_ipc_request(
|
||||
logging!(error, Type::Service, true, "服务响应签名验证失败");
|
||||
bail!("服务响应签名验证失败");
|
||||
}
|
||||
logging!(debug, Type::Service, true, "响应签名验证成功");
|
||||
}
|
||||
Err(e) => {
|
||||
logging!(error, Type::Service, true, "验证响应签名时出错: {}", e);
|
||||
|
||||
Reference in New Issue
Block a user