feat: add procfs

This commit is contained in:
2022-07-29 00:35:05 +08:00
parent f598db686b
commit ffb630bb64
5 changed files with 176 additions and 5 deletions

122
Cargo.lock generated
View File

@@ -81,6 +81,12 @@ dependencies = [
"generic-array",
]
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "bytes"
version = "1.2.0"
@@ -99,6 +105,19 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"time 0.1.44",
"winapi",
]
[[package]]
name = "clap"
version = "2.34.0"
@@ -173,6 +192,27 @@ dependencies = [
"winapi",
]
[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
@@ -407,6 +447,12 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "io-lifetimes"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24c3f4eff5495aee4c0399d7b6a0dc2b6e81be84242ffbfcf253ebacccc1d0cb"
[[package]]
name = "itoa"
version = "1.0.2"
@@ -428,7 +474,7 @@ dependencies = [
"serde",
"serde_json",
"thiserror",
"time",
"time 0.3.11",
]
[[package]]
@@ -453,9 +499,15 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "linux-raw-sys"
version = "0.0.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4d2456c373231a208ad294c33dc5bff30051eafd954cd4caae83a712b12854d"
[[package]]
name = "local-mini-kms"
version = "0.2.1"
version = "0.2.2"
dependencies = [
"base64",
"clap",
@@ -463,6 +515,7 @@ dependencies = [
"hyper",
"josekit",
"lazy_static",
"procfs",
"rusqlite",
"rust_util",
"seckey",
@@ -532,6 +585,25 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
@@ -646,6 +718,21 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "procfs"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "979e5cb47caafb8e14653bb083358e19917ca8c9c4c2648932eccd935f5c4d80"
dependencies = [
"bitflags",
"byteorder",
"chrono",
"flate2",
"hex",
"lazy_static",
"rustix",
]
[[package]]
name = "quote"
version = "1.0.20"
@@ -719,6 +806,20 @@ dependencies = [
"term_size",
]
[[package]]
name = "rustix"
version = "0.35.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51cc38aa10f6bbb377ed28197aa052aa4e2b762c22be9d3153d01822587e787"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "rustversion"
version = "1.0.8"
@@ -903,6 +1004,17 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]]
name = "time"
version = "0.3.11"
@@ -1043,6 +1155,12 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View File

@@ -1,6 +1,6 @@
[package]
name = "local-mini-kms"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -22,3 +22,6 @@ rust_util = { version = "0.6", features = ["use_clap"] }
tokio = { version = "1.19", features = ["full"] }
hyper = { version = "0.14.20", features = ["client", "server", "tcp", "http1", "http2"] }
rusqlite = "0.28.0"
[target.'cfg(target_os = "linux")'.dependencies]
procfs = "0.13.2"

View File

@@ -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
View 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
}

View File

@@ -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,