docs: add project README and initialize repository
@@ -0,0 +1,7 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Generated by Tauri
|
||||
# will have schema files for capabilities auto-completion
|
||||
/gen/schemas
|
||||
@@ -0,0 +1,27 @@
|
||||
[package]
|
||||
name = "PortFlow"
|
||||
version = "0.1.0"
|
||||
description = "一个基于 Tauri 的端口管理工具"
|
||||
authors = ["寒寒"]
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
# The `_lib` suffix may seem redundant but it is necessary
|
||||
# to make the lib name unique and wouldn't conflict with the bin name.
|
||||
# This seems to be only an issue on Windows, see https://github.com/rust-lang/cargo/issues/8519
|
||||
name = "port_flow_lib"
|
||||
crate-type = ["staticlib", "cdylib", "rlib"]
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2", features = [] }
|
||||
winres = "0.1"
|
||||
|
||||
[dependencies]
|
||||
tauri = { version = "2", features = [] }
|
||||
tauri-plugin-opener = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
sysinfo = "0.30"
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
fn main() {
|
||||
// 💡 仅在 Windows 平台下注入管理员清单
|
||||
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
|
||||
let mut res = winres::WindowsResource::new();
|
||||
// 强行写入 UAC 清单,要求最高管理员权限
|
||||
res.set_manifest(r#"
|
||||
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<security>
|
||||
<requestedPrivileges>
|
||||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
|
||||
</requestedPrivileges>
|
||||
</security>
|
||||
</trustInfo>
|
||||
</assembly>
|
||||
"#);
|
||||
res.compile().unwrap();
|
||||
}
|
||||
tauri_build::build();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "default",
|
||||
"description": "Capability for the main window",
|
||||
"windows": ["main"],
|
||||
"permissions": [
|
||||
"core:default",
|
||||
"opener:default",
|
||||
"core:window:allow-start-dragging",
|
||||
"core:window:allow-minimize",
|
||||
"core:window:allow-maximize",
|
||||
"core:window:allow-unmaximize",
|
||||
"core:window:allow-close",
|
||||
"core:app:allow-version",
|
||||
"core:app:allow-name"
|
||||
]
|
||||
}
|
||||
|
After Width: | Height: | Size: 3.4 KiB |
|
After Width: | Height: | Size: 6.8 KiB |
|
After Width: | Height: | Size: 974 B |
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 3.8 KiB |
|
After Width: | Height: | Size: 3.9 KiB |
|
After Width: | Height: | Size: 7.6 KiB |
|
After Width: | Height: | Size: 903 B |
|
After Width: | Height: | Size: 8.4 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 2.4 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 85 KiB |
|
After Width: | Height: | Size: 14 KiB |
@@ -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");
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "PortFlow",
|
||||
"version": "0.1.0",
|
||||
"identifier": "com.administrator.portflow",
|
||||
"build": {
|
||||
"beforeDevCommand": "bun run dev",
|
||||
"devUrl": "http://localhost:1420",
|
||||
"beforeBuildCommand": "bun run build",
|
||||
"frontendDist": "../dist"
|
||||
},
|
||||
"app": {
|
||||
"withGlobalTauri": true,
|
||||
"windows": [
|
||||
{
|
||||
"label": "main",
|
||||
"title": "PortFlow - 端口管理控制台",
|
||||
"width": 1150,
|
||||
"height": 760,
|
||||
"minWidth": 980,
|
||||
"minHeight": 600,
|
||||
"resizable": true,
|
||||
"fullscreen": false,
|
||||
"decorations": false,
|
||||
"center": true
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": null
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"targets": ["nsis"],
|
||||
"icon": [
|
||||
"icons/32x32.png",
|
||||
"icons/128x128.png",
|
||||
"icons/128x128@2x.png",
|
||||
"icons/icon.icns",
|
||||
"icons/icon.ico"
|
||||
]
|
||||
}
|
||||
}
|
||||