diff --git a/src/main.rs b/src/main.rs index 85498c1..8ad1e87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,34 @@ use regex::Regex; use std::collections::HashSet; -use std::fmt::{Display, Formatter, Write}; -use std::{fmt, fs}; -use std::fs::{read_to_string, ReadDir}; +use std::fs; +use std::fs::ReadDir; use std::path::PathBuf; -use crate::socket::socket::{pids_to_string, Id, Tcp_Socket}; +use crate::socket::socket::*; -type inode = u64; -type pid = u64; - -type pids = HashSet; +type Inode = u64; +type Pid = u64; mod socket; -fn get_socket_inodes_by_pid(pid : u64, socket_patt : &Regex) -> HashSet { - let mut inodes = HashSet::::new(); +fn get_socket_inodes_by_pid(pid : u64, socket_patt : &Regex) -> HashSet { + let mut inodes = HashSet::::new(); let mut path = String::from("/proc/"); path.push_str(&pid.to_string()); path.push_str("/fd/"); - let mut contents: ReadDir; + let contents: ReadDir; match fs::read_dir(path) { Ok(dir) => { contents = dir; }, - Err(_) => return HashSet::::new() + Err(_) => return HashSet::::new() }; for fd in contents { match fd { Ok(fd) => { let path_name = fs::read_link(fd.path()); - let mut name : PathBuf; + let name : PathBuf; match path_name { Ok(link) => name = link, Err(_) => continue, @@ -47,23 +44,19 @@ fn get_socket_inodes_by_pid(pid : u64, socket_patt : &Regex) -> HashSet { inodes } -fn get_socket_by_inode(sockets : &HashSet, inode : u64) -> Option { - sockets.clone().iter().map(|x| x.to_owned()).find(|x|x.inode == inode) -} - -fn get_tcp_file_by_pid(pid_pattern : &Regex, pid: pid) -> String { +fn get_tcp_file_by_pid(pid: Pid) -> String { let mut path = String::from("/proc/"); path.push_str(&pid.to_string()); path.push_str("/net/tcp"); fs::read_to_string(path).unwrap().lines().skip(1).map(|x| x.to_owned()).collect::>().join("\n") } -fn parse_tcp_file_line(line : String) -> socket::socket::Tcp_Socket { - socket::socket::Tcp_Socket::new(socket::socket::Peer::build_from_hex(&get_index(&line.clone(),1)), socket::socket::Peer::build_from_hex(&get_index(&line.clone(),2)), HashSet::::new(), socket::socket::Tcp_SocketState::new(socket::socket::Peer::convert_port_hex_dec(&get_index(&line.clone(), 3))), socket::socket::Id::create_def(), socket::socket::Id::create_def(), get_index(&line, 9).parse().unwrap()) +fn parse_tcp_file_line(line : String) -> Tcp_Socket { + Tcp_Socket::new(Peer::build_from_hex(&get_index(&line.clone(),1)), Peer::build_from_hex(&get_index(&line.clone(),2)), HashSet::::new(), Tcp_SocketState::new(Peer::convert_port_hex_dec(&get_index(&line.clone(), 3))), Id::create_def(), Id::create_def(), get_index(&line, 9).parse().unwrap()) } -fn get_readable_proc_pids(pid_pattern : &Regex) -> Vec { - let mut pids = Vec::::new(); +fn get_readable_proc_pids(pid_pattern : &Regex) -> Vec { + let mut pids = Vec::::new(); let dirs = fs::read_dir("/proc/").expect("proc kann nicht gelesen werden, Berechtigung?"); for dir in dirs { match dir { @@ -80,9 +73,8 @@ fn get_readable_proc_pids(pid_pattern : &Regex) -> Vec { pids } -fn get_uid_guid_by_pid(pid : pid) -> (socket::socket::Id,socket::socket::Id) { - let mut uid : Id; - let mut gid : Id; +//noinspection ALL +fn get_uid_guid_by_pid(pid : Pid) -> (Id, Id) { let mut status_path = String::from("/proc/"); status_path.push_str(&pid.to_string()); status_path.push_str("/status"); @@ -90,14 +82,12 @@ fn get_uid_guid_by_pid(pid : pid) -> (socket::socket::Id,socket::socket::Id) { let mut iter = status_file.lines(); let uid_str = iter.find(|x| x.starts_with("Uid:")).unwrap().to_owned(); let gid_str = iter.find(|x| x.starts_with("Gid:")).unwrap().to_owned(); - (Id::new(get_index(&uid_str.clone(), 1).parse().unwrap(), get_index(&uid_str.clone(), 2).parse().unwrap(), - get_index(&uid_str.clone(), 3).parse().unwrap(), get_index(&uid_str, 4).parse().unwrap()), - Id::new(get_index(&gid_str.clone(), 1).parse().unwrap(), get_index(&gid_str.clone(), 2).parse().unwrap(), - get_index(&gid_str.clone(), 3).parse().unwrap(), get_index(&gid_str, 4).parse().unwrap())) + (Id::new(get_index(&uid_str.clone(), 1).parse().unwrap(), get_index(&uid_str.clone(), 2).parse().unwrap(),get_index(&uid_str.clone(), 3).parse().unwrap(), get_index(&uid_str, 4).parse().unwrap()), + Id::new(get_index(&gid_str.clone(), 1).parse().unwrap(), get_index(&gid_str.clone(), 2).parse().unwrap(),get_index(&gid_str.clone(), 3).parse().unwrap(), get_index(&gid_str, 4).parse().unwrap())) } -fn add_pid_to_tcp_sockets(sockets : &mut Vec, inodes : HashSet, pid : pid) -> (){ +fn add_pid_to_tcp_sockets(sockets : &mut Vec, inodes : HashSet, pid : Pid) -> (){ let s2 = sockets.clone(); for (i, v) in s2.iter().enumerate(){ if inodes.contains(&v.inode) { @@ -106,7 +96,7 @@ fn add_pid_to_tcp_sockets(sockets : &mut Vec, inodes : HashSet) -> String{ +pub fn format_output(combination : Combination, sockets : &Vec) -> String{ let header_loc_ip = "Local Address"; let header_loc_port = "Port"; let header_rem_ip = "Remote Address"; @@ -143,18 +133,18 @@ pub fn format_output(combination : socket::socket::Combination, sockets : &Vec 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_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_state_len = if(curr_state_len > max_state_len) { curr_state_len } else { max_state_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_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_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_real_uid_len > max_real_uid_len) { curr_real_uid_len } else { max_real_uid_len }; - max_eff_uid_len = if (curr_eff_uid_len > max_eff_uid_len) { curr_eff_uid_len } else { max_eff_uid_len }; - max_real_gid_len = if (curr_real_gid_len > max_real_gid_len) { curr_real_gid_len } else { max_real_gid_len }; - max_eff_gid_len = if (curr_eff_gid_len > max_eff_gid_len) { curr_eff_gid_len } else { max_eff_gid_len }; - max_pids_len = if (curr_pids_len > max_pids_len) { curr_pids_len } else { max_pids_len }; + if combination == Combination::tnap{ + max_real_uid_len = if curr_real_uid_len > max_real_uid_len { curr_real_uid_len } else { max_real_uid_len }; + max_eff_uid_len = if curr_eff_uid_len > max_eff_uid_len { curr_eff_uid_len } else { max_eff_uid_len }; + max_real_gid_len = if curr_real_gid_len > max_real_gid_len { curr_real_gid_len } else { max_real_gid_len }; + max_eff_gid_len = if curr_eff_gid_len > max_eff_gid_len { curr_eff_gid_len } else { max_eff_gid_len }; + max_pids_len = if curr_pids_len > max_pids_len { curr_pids_len } else { max_pids_len }; } }); @@ -165,14 +155,14 @@ pub fn format_output(combination : socket::socket::Combination, sockets : &Vec { + Combination::tna => { ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:max_rem_ip_len$}:{:max_state_len$} {:>max_loc_ip_len$}:{:max_rem_ip_len$}:{: { + Combination::tnap => { ret_str.push_str(&format!("{:>max_state_len$} {:>max_loc_ip_len$}:{:max_rem_ip_len$}:{:max_real_uid_len$}|{:max_real_gid_len$}|{:max_pids_len$}\n", header_state , header_loc_ip , header_loc_port, header_rem_ip,header_rem_port, header_uid_real, header_uid_eff, header_gid_real, header_gid_eff, header_pids )); sockets.clone().iter().for_each(|x| { let (real_uid, eff_uid) = x.get_real_eff_uid_string(); @@ -184,12 +174,12 @@ pub fn format_output(combination : socket::socket::Combination, sockets : &Vec Vec { - let mut sockets = Vec::::new(); +fn get_all_tcp_sockets(pid_pattern : &Regex, socket_patt : &Regex) -> Vec { + let mut sockets = Vec::::new(); let pids = get_readable_proc_pids(pid_pattern); - let mut inodes = HashSet::::new(); + let mut inodes = HashSet::::new(); pids.iter().for_each(|x| { - get_tcp_file_by_pid(&pid_pattern.clone(), *x).lines().for_each(|y| { + get_tcp_file_by_pid(*x).lines().for_each(|y| { add_to_tcp_sockets(&mut sockets, parse_tcp_file_line(y.to_owned())); }); inodes = get_socket_inodes_by_pid(*x, socket_patt); @@ -204,27 +194,19 @@ fn get_all_tcp_sockets(pid_pattern : &Regex, socket_patt : &Regex) -> Vec, socket: socket::socket::Tcp_Socket) -> (){ +fn add_to_tcp_sockets(sockets : &mut Vec, socket: Tcp_Socket) -> (){ if !sockets.contains(&socket) { sockets.push(socket); } } -fn get_socket_info() -> Vec { - let mut sockets = Vec::::new(); - sockets -} - fn get_index(line: &String, index: usize) -> String { line.clone().split_whitespace().nth(index).expect("upsi").to_owned() } -fn get_remote_address() {} -fn get_remote_port() {} - 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 sockets = get_all_tcp_sockets(&pid_pattern, &socket_patt); - println!("{}", format_output(socket::socket::Combination::tnap, &sockets)); + println!("{}", format_output(Combination::tnap, &sockets)); } diff --git a/src/socket.rs b/src/socket.rs index e456766..e801563 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -1,9 +1,6 @@ pub mod socket { - use crate::pids; - use regex::Regex; use std::cmp::PartialEq; use std::collections::HashSet; - use std::fmt::Display; use std::fs; use std::hash::{Hash, Hasher}; use std::os::unix::fs::MetadataExt; @@ -265,16 +262,15 @@ pub mod socket { self.peer_rem.get_port_string() } - pub fn get_real_eff_uid_string(&self) -> (String,String) { - (self.user.real.to_string(),self.user.effective.to_string()) + pub fn get_real_eff_uid_string(&self) -> (String, String) { + (self.user.real.to_string(), self.user.effective.to_string()) } - pub fn get_real_eff_gid_String(&self) -> (String,String) { - (self.group.real.to_string(),self.group.effective.to_string()) + pub fn get_real_eff_gid_String(&self) -> (String, String) { + (self.group.real.to_string(), self.group.effective.to_string()) } - pub fn to_string(&self, combination: Combination) -> String { let mut ret_str = String::from(""); match combination { @@ -322,48 +318,6 @@ pub mod socket { pub fn get_state(&self) -> Tcp_SocketState { self.state.clone() } - - fn get_all_unique_socket_infos() -> String { - let pattern = Regex::new(r"^[1-4]?[0-9]{1,6}$").expect("kannst kein regex??"); - let contents = fs::read_dir("/proc/") - .expect("upsi 4 (proc kann nicht gelesen werden, Berechtigung?)"); - let mut seen_inodes = HashSet::::new(); - let mut complete_file: String = String::new(); - for dir in contents { - match dir { - Ok(dir) => { - if !pattern.is_match( - &dir.file_name() - .into_string() - .expect("Dateiname ist kein gueltiger Unicode-String")[..], - ) { - continue; - } - let mut path = dir.path().into_os_string().into_string().expect("gehtnd"); - path.push_str("/net/tcp"); - let pid = &path.split("/").nth(2).expect("gesplittert"); - if !Tcp_Socket::check_inode_seen(&seen_inodes, pid.parse::().unwrap()) - { - Tcp_Socket::add_inode(&mut seen_inodes, pid.parse::().unwrap()); - let test = &fs::read_to_string(path) - .expect("Could not read proc/pid/net/tcp") - .lines() - .skip(1) - // .map(|x| socket::convert_line(x.to_string())) - .map(|x| x.to_owned()) - .collect::>() - .join("\n"); - complete_file.push_str(test); - } else { - continue; - } - } - Err(_) => continue, - }; - } - // complete_file - complete_file - } } }