This commit is contained in:
mux 2025-08-08 16:00:48 +02:00
parent f97d60aecb
commit 40cedfa7e1
2 changed files with 22 additions and 27 deletions

View File

@ -3,10 +3,10 @@ use std::collections::HashSet;
use std::fmt::{Display, Formatter, Write}; use std::fmt::{Display, Formatter, Write};
use std::{fmt, fs}; use std::{fmt, fs};
use std::fs::{read_to_string, ReadDir}; use std::fs::{read_to_string, ReadDir};
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf; use std::path::PathBuf;
use crate::socket::socket::{Id, Tcp_Socket}; use crate::socket::socket::{Id, Tcp_Socket};
type inode = u64; type inode = u64;
type pid = u64; type pid = u64;
@ -118,11 +118,9 @@ pub fn format_output(combination : socket::socket::Combination, sockets : &Vec<T
let mut max_rem_ip_len = header_rem_ip.len(); let mut max_rem_ip_len = header_rem_ip.len();
let mut max_rem_port_len = max_loc_port_len; let mut max_rem_port_len = max_loc_port_len;
let mut max_state_len = header_state.len(); let mut max_state_len = header_state.len();
let header_uid_real = "real-uid";
if combination == socket::socket::Combination::tnp{ let header_uid_eff = "effective-uid";
let header_uid_real = "real-uid"; let header_gid_real = "real-gid";
let header_uid_eff = "effective-uid";
let header_gid_real = "real-gid";
let header_gid_eff = "effective-gid"; let header_gid_eff = "effective-gid";
let header_pids = "PIDs"; let header_pids = "PIDs";
let mut max_real_uid_len = header_uid_real.len(); let mut max_real_uid_len = header_uid_real.len();
@ -130,10 +128,10 @@ pub fn format_output(combination : socket::socket::Combination, sockets : &Vec<T
let mut max_real_gid_len = header_gid_real.len(); let mut max_real_gid_len = header_gid_real.len();
let mut max_eff_gid_len = header_gid_eff.len(); let mut max_eff_gid_len = header_gid_eff.len();
let mut max_pids_len = header_pids.len(); let mut max_pids_len = header_pids.len();
}
sockets.clone().iter().for_each(|x| { sockets.clone().iter().for_each(|x| {
let curr_loc_ip = x.get_loc_ip_string(); let curr_loc_ip = x.get_loc_ip_string();
let curr_loc_ip_len = x.get_loc_ip_string().len(); let curr_loc_ip_len = x.get_loc_ip_string().len();
let curr_loc_port = x.get_loc_port_string(); let curr_loc_port = x.get_loc_port_string();
@ -144,33 +142,31 @@ pub fn format_output(combination : socket::socket::Combination, sockets : &Vec<T
let curr_rem_port_len = curr_rem_port.len(); let curr_rem_port_len = curr_rem_port.len();
let curr_state = x.get_state().to_string(); let curr_state = x.get_state().to_string();
let curr_state_len = curr_state.len(); let curr_state_len = curr_state.len();
// let curr_
max_loc_ip_len = if (curr_loc_ip_len > max_loc_ip_len) { curr_loc_ip_len } else { max_loc_ip_len }; max_loc_ip_len = if (curr_loc_ip_len > max_loc_ip_len) { curr_loc_ip_len } else { max_loc_ip_len };
max_loc_port_len = if (curr_loc_port_len > max_loc_port_len) { curr_loc_port_len } else { max_loc_port_len }; max_loc_port_len = if (curr_loc_port_len > max_loc_port_len) { curr_loc_port_len } else { max_loc_port_len };
max_rem_ip_len = if (curr_rem_ip_len > max_rem_ip_len) { curr_rem_ip_len } else { max_rem_ip_len }; max_rem_ip_len = if (curr_rem_ip_len > max_rem_ip_len) { curr_rem_ip_len } else { max_rem_ip_len };
max_rem_port_len = if (curr_rem_port_len > max_rem_port_len) { curr_rem_port_len } else { max_rem_port_len }; max_rem_port_len = if (curr_rem_port_len > max_rem_port_len) { curr_rem_port_len } else { max_rem_port_len };
max_state_len = if(curr_state_len > max_state_len) { curr_state_len } else { max_state_len }; max_state_len = if(curr_state_len > max_state_len) { curr_state_len } else { max_state_len };
if combination == socket::socket::Combination::tnap{
// max_real_uid_len = if (curr_uid_)
if combination == socket::socket::Combination::tnp{
} }
}); });
max_loc_ip_len = max_loc_ip_len + 3; max_loc_ip_len = max_loc_ip_len + 3;
max_loc_port_len = max_loc_port_len + 3; max_loc_port_len = max_loc_port_len + 3;
max_rem_ip_len = max_rem_ip_len + 3; max_rem_ip_len = max_rem_ip_len + 3;
match combination { match combination {
socket::socket::Combination::tn => { socket::socket::Combination::tna => {
ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:<max_loc_port_len$} {:>max_rem_ip_len$}:{:<max_rem_port_len$}\n", header_state , header_loc_ip , header_loc_port, header_rem_ip,header_rem_port)); ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:<max_loc_port_len$} {:>max_rem_ip_len$}:{:<max_rem_port_len$}", header_state , header_loc_ip , header_loc_port, header_rem_ip,header_rem_port));
sockets.clone().iter().for_each(|x| { sockets.clone().iter().for_each(|x| {
ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:<max_loc_port_len$} {:>max_rem_ip_len$}:{:<max_rem_port_len$}", x.get_state().to_string(), x.get_loc_ip_string(), x.get_loc_port_string(), x.get_rem_ip_string(), x.get_rem_port_string())); ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:<max_loc_port_len$} {:>max_rem_ip_len$}:{:<max_rem_port_len$}", x.get_state().to_string(), x.get_loc_ip_string(), x.get_loc_port_string(), x.get_rem_ip_string(), x.get_rem_port_string()));
ret_str.push('\n'); ret_str.push('\n');
}); });
} }
socket::socket::Combination::tnp => { socket::socket::Combination::tnap => {
} }
} }
ret_str ret_str
} }
@ -190,7 +186,6 @@ fn get_all_tcp_sockets(pid_pattern : &Regex, socket_patt : &Regex) -> Vec<socket
} }
Tcp_Socket::add_uid_gid(sockets.get_mut(i).unwrap(),uid, gid); Tcp_Socket::add_uid_gid(sockets.get_mut(i).unwrap(),uid, gid);
} }
}); });
sockets sockets
} }
@ -217,5 +212,5 @@ fn main() {
let socket_patt : Regex = Regex::new(r"socket:\[([0-9]+)\]").expect("kannst kein regex?? xd"); let socket_patt : Regex = Regex::new(r"socket:\[([0-9]+)\]").expect("kannst kein regex?? xd");
let pid_pattern = Regex::new(r"^[1-4]?[0-9]{1,6}$").expect("kannst kein regex??"); let pid_pattern = Regex::new(r"^[1-4]?[0-9]{1,6}$").expect("kannst kein regex??");
let sockets = get_all_tcp_sockets(&pid_pattern, &socket_patt); let sockets = get_all_tcp_sockets(&pid_pattern, &socket_patt);
println!("{}", format_output(socket::socket::Combination::tn, &sockets)); println!("{}", format_output(socket::socket::Combination::tna, &sockets));
} }

View File

@ -197,8 +197,8 @@ pub mod socket {
#[derive(Clone,Copy, PartialEq)] #[derive(Clone,Copy, PartialEq)]
pub enum Combination{ pub enum Combination{
tnp, tnap,
tn, tna,
} }
impl Tcp_Socket { impl Tcp_Socket {
@ -263,9 +263,9 @@ pub mod socket {
pub fn to_string (&self, combination: Combination) -> String { pub fn to_string (&self, combination: Combination) -> String {
let mut ret_str = String::from(""); let mut ret_str = String::from("");
match combination { match combination {
Combination::tnp => { Combination::tnap => {
//state rem_ip:rem_port loc_ip:loc_port real|effective-uid real|effective-gid pids(.,.,.,.) //state rem_ip:rem_port loc_ip:loc_port real|effective-uid real|effective-gid pids(.,.,.,.)
ret_str.push_str(&self.clone().to_string(Combination::tn)); ret_str.push_str(&self.clone().to_string(Combination::tna));
ret_str.push(' '); ret_str.push(' ');
ret_str.push_str(&self.user.real.to_string()); ret_str.push_str(&self.user.real.to_string());
ret_str.push('|'); ret_str.push('|');
@ -278,7 +278,7 @@ pub mod socket {
ret_str ret_str
} }
Combination::tn => { Combination::tna => {
//state rem_ip:rem_port loc_ip:loc_port //state rem_ip:rem_port loc_ip:loc_port
ret_str.push_str(&self.state.to_string()); ret_str.push_str(&self.state.to_string());
ret_str.push(' '); ret_str.push(' ');
@ -375,8 +375,8 @@ mod tests {
#[test] #[test]
fn test_to_string() -> () { fn test_to_string() -> () {
let sockets = init_sockets(); let sockets = init_sockets();
assert_eq!("93.83.160.12:21345 0.0.0.0:*\n", sockets[0].clone().to_string(Combination::tn)); assert_eq!("93.83.160.12:21345 0.0.0.0:*\n", sockets[0].clone().to_string(Combination::tna));
assert_eq!("195.201.90.97:8843 0.0.0.0:443\n", sockets[1].clone().to_string(Combination::tn)); assert_eq!("195.201.90.97:8843 0.0.0.0:443\n", sockets[1].clone().to_string(Combination::tna));
assert_eq!("255.255.255.255:193193 0.0.0.0:8080\n", sockets[2].clone().to_string(Combination::tn)); assert_eq!("255.255.255.255:193193 0.0.0.0:8080\n", sockets[2].clone().to_string(Combination::tna));
} }
} }