2022-12-03 21:47:09 -08:00
|
|
|
use netstat2::{AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo};
|
|
|
|
use sysinfo::{System, SystemExt, Pid, ProcessExt};
|
|
|
|
|
|
|
|
use crate::errors::*;
|
|
|
|
|
|
|
|
|
|
|
|
fn get_associated_pids(local_port: u16) -> Result<Vec<u32>, netstat2::error::Error> {
|
|
|
|
let mut it = netstat2::iterate_sockets_info(
|
|
|
|
AddressFamilyFlags::IPV4,
|
|
|
|
ProtocolFlags::TCP
|
|
|
|
)?;
|
|
|
|
|
|
|
|
for (i, item) in it.enumerate() {
|
|
|
|
let sock_info = item?;
|
|
|
|
let proto_info = match sock_info.protocol_socket_info {
|
|
|
|
ProtocolSocketInfo::Tcp(tcp_info) => tcp_info,
|
|
|
|
ProtocolSocketInfo::Udp(_) => {continue;}
|
|
|
|
};
|
|
|
|
|
|
|
|
if proto_info.local_port == local_port
|
|
|
|
&& proto_info.remote_port == 12345
|
|
|
|
&& proto_info.local_addr == std::net::Ipv4Addr::LOCALHOST
|
|
|
|
&& proto_info.remote_addr == std::net::Ipv4Addr::LOCALHOST
|
|
|
|
{
|
|
|
|
println!("PIDs associated with socket: {:?}", &sock_info.associated_pids);
|
|
|
|
println!("Scanned {i} sockets");
|
|
|
|
return Ok(sock_info.associated_pids)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_client_info(local_port: u16) -> Result<(), ClientInfoError> {
|
|
|
|
let mut sys = System::new();
|
|
|
|
for p in get_associated_pids(local_port)? {
|
2022-12-13 10:58:52 -08:00
|
|
|
let pid = Pid::from(p as i32);
|
2022-12-03 21:47:09 -08:00
|
|
|
sys.refresh_process(pid);
|
|
|
|
let proc = sys.process(pid)
|
|
|
|
.ok_or(ClientInfoError::PidNotFound)?;
|
|
|
|
let path = proc.exe().to_string_lossy();
|
|
|
|
println!("exe for requesting process: {path}");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|