feat: add procfs
This commit is contained in:
@@ -3,6 +3,7 @@ use rust_util::{failure_and_exit, information, success, warning};
|
||||
use rust_util::util_clap::{Command, CommandError};
|
||||
|
||||
mod db;
|
||||
mod proc;
|
||||
mod jose;
|
||||
mod cli;
|
||||
mod serve;
|
||||
|
||||
48
src/proc.rs
Normal file
48
src/proc.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub fn get_process(_port: u16) -> Option<(i32, String)> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn get_process(port: u16) -> Option<(i32, String)> {
|
||||
let all_procs = match procfs::process::all_processes() {
|
||||
Error(e) => {
|
||||
rust_util::warning!("Get procfs all processes failed: {}", e);
|
||||
return None;
|
||||
}
|
||||
Ok(all_procs) => all_procs,
|
||||
};
|
||||
let mut map: std::collections::HashMap<u64, procfs::process::Stat> = std::collections::HashMap::new();
|
||||
for p in all_procs {
|
||||
if let Ok(process) = p {
|
||||
if let (Ok(stat), Ok(fds)) = (process.stat(), process.fd()) {
|
||||
for fd in fds {
|
||||
if let Ok(fd) = fd {
|
||||
if let procfs::process::FDTarget::Socket(inode) = fd.target {
|
||||
map.insert(inode, stat.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let tcp = match procfs::net::tcp() {
|
||||
Error(e) => {
|
||||
rust_util::warning!("Get procfs net tcp failed: {}", e);
|
||||
return None;
|
||||
}
|
||||
Ok(tcp) => tcp,
|
||||
};
|
||||
for entry in tcp.into_iter() {
|
||||
if port == entry.local_address.port() {
|
||||
if let Some(stat) = map.get(&entry.inode) {
|
||||
return Some((stat.pid, stat.comm));
|
||||
} else {
|
||||
rust_util::warning!("Cannot get process by port: {}, inode: {}", port, entry.inode);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
rust_util::warning!("Port not found: {}", port);
|
||||
None
|
||||
}
|
||||
@@ -16,7 +16,7 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Map, Value};
|
||||
use zeroize::Zeroize;
|
||||
|
||||
use crate::{db, jose};
|
||||
use crate::{db, jose, proc};
|
||||
use crate::db::Key;
|
||||
|
||||
type GenericError = Box<dyn std::error::Error + Send + Sync>;
|
||||
@@ -74,7 +74,8 @@ async fn response_requests(
|
||||
req: Request<Body>,
|
||||
_client: Client<HttpConnector>,
|
||||
) -> Result<Response<Body>> {
|
||||
information!("Receive request: {}, from: {}", req.uri(), remote_addr);
|
||||
let process = proc::get_process(remote_addr.port());
|
||||
information!("Receive request: {}, from: {}, process: {:?}", req.uri(), remote_addr, process);
|
||||
match (req.method(), req.uri().path()) {
|
||||
(&Method::POST, "/init") => init(req).await,
|
||||
(&Method::POST, "/update") => update().await,
|
||||
|
||||
Reference in New Issue
Block a user