docs: add project README and initialize repository
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
// Prevents additional console window on Windows in release
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use serde::Serialize;
|
||||
use std::process::Command;
|
||||
use sysinfo::{Pid, System};
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
struct PortInfo {
|
||||
protocol: String,
|
||||
port: u16,
|
||||
name: String,
|
||||
pid: u32,
|
||||
state: String,
|
||||
}
|
||||
|
||||
// 💡 异步处理命令,使用 spawn_blocking 将耗时任务抛给专用后台线程池
|
||||
#[tauri::command]
|
||||
async fn get_active_ports() -> Result<Vec<PortInfo>, String> {
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
let mut ports = Vec::new();
|
||||
|
||||
// 在后台线程中初始化系统进程快照
|
||||
let mut sys = System::new_all();
|
||||
sys.refresh_processes();
|
||||
|
||||
if cfg!(target_os = "windows") {
|
||||
// Windows 环境下执行 netstat -ano
|
||||
let output = Command::new("cmd")
|
||||
.args(&["/C", "netstat -ano"])
|
||||
.output()
|
||||
.map_err(|e| e.to_string())?;
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
for line in stdout.lines() {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 5 && (parts[0] == "TCP" || parts[0] == "UDP") {
|
||||
let local_addr = parts[1];
|
||||
let state = parts[3].to_string();
|
||||
let pid_str = parts[4];
|
||||
|
||||
if let Some(port_str) = local_addr.split(':').last() {
|
||||
if let (Ok(port), Ok(pid_val)) = (port_str.parse::<u16>(), pid_str.parse::<u32>()) {
|
||||
let process_name = if let Some(proc) = sys.process(Pid::from(pid_val as usize)) {
|
||||
proc.name().to_string()
|
||||
} else {
|
||||
"未知进程 (已结束)".to_string()
|
||||
};
|
||||
|
||||
ports.push(PortInfo {
|
||||
protocol: parts[0].to_string(),
|
||||
port,
|
||||
name: process_name,
|
||||
pid: pid_val,
|
||||
state,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// macOS / Linux 降级兼容处理
|
||||
let output = Command::new("sh")
|
||||
.args(&["-c", "lsof -i -P -n | grep LISTEN"])
|
||||
.output()
|
||||
.map_err(|e| e.to_string())?;
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
for line in stdout.lines() {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 9 {
|
||||
let pid_str = parts[1];
|
||||
let protocol = if parts[7].contains("UDP") { "UDP" } else { "TCP" };
|
||||
let local_addr = parts[8];
|
||||
|
||||
if let Some(port_str) = local_addr.split(':').last() {
|
||||
if let (Ok(port), Ok(pid_val)) = (port_str.parse::<u16>(), pid_str.parse::<u32>()) {
|
||||
let process_name = if let Some(proc) = sys.process(Pid::from(pid_val as usize)) {
|
||||
proc.name().to_string()
|
||||
} else {
|
||||
parts[0].to_string()
|
||||
};
|
||||
|
||||
ports.push(PortInfo {
|
||||
protocol: protocol.to_string(),
|
||||
port,
|
||||
name: process_name,
|
||||
pid: pid_val,
|
||||
state: "LISTENING".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(ports)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("进程同步线程崩溃: {}", e))?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn kill_process_by_pid(pid: u32) -> Result<String, String> {
|
||||
let output = if cfg!(target_os = "windows") {
|
||||
Command::new("taskkill")
|
||||
.args(&["/F", "/PID", &pid.to_string()])
|
||||
.output()
|
||||
} else {
|
||||
Command::new("kill")
|
||||
.args(&["-9", &pid.to_string()])
|
||||
.output()
|
||||
};
|
||||
|
||||
match output {
|
||||
Ok(out) => {
|
||||
if out.status.success() {
|
||||
Ok(format!("成功结束 PID: {} 的进程", pid))
|
||||
} else {
|
||||
Err(String::from_utf8_lossy(&out.stderr).to_string())
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![get_active_ports, kill_process_by_pid])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
Reference in New Issue
Block a user