allerlei...
This commit is contained in:
parent
6a9567c5de
commit
78276e8243
279
src/main.rs
279
src/main.rs
|
|
@ -4,244 +4,67 @@ use std::fs;
|
|||
use std::fs::read_to_string;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
|
||||
//#[derive(PartialEq)]
|
||||
//struct Peer {
|
||||
// ip_address: String,
|
||||
// port: usize,
|
||||
//}
|
||||
type inode = u64;
|
||||
type pid = u64;
|
||||
|
||||
mod socket;
|
||||
|
||||
//impl Peer {
|
||||
// fn new() -> Peer {
|
||||
// Peer {
|
||||
// ip_address: "0.0.0.0".to_string(),
|
||||
// port: 0,
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fn build_from_hex(hex_ip: &str, hex_port: &str) -> Peer {
|
||||
// Peer {
|
||||
// ip_address: Self::convert_ip_hex_dec(hex_ip),
|
||||
// port: Self::convert_port_hex_dec(hex_port),
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fn convert_ip_hex_dec(ip_address: &str) -> String {
|
||||
// let mut buffer: String = String::new();
|
||||
// let mut ip_quads: Vec<String> = vec![];
|
||||
// for (i, v) in ip_address.chars().enumerate() {
|
||||
// if i % 2 == 1 {
|
||||
// buffer.push(v);
|
||||
// ip_quads.push(
|
||||
// u64::from_str_radix(&buffer, 16)
|
||||
// .expect("upsi 2")
|
||||
// .to_string(),
|
||||
// );
|
||||
// buffer = String::new();
|
||||
// } else {
|
||||
// buffer.push(v);
|
||||
// }
|
||||
// }
|
||||
// ip_quads
|
||||
// .iter()
|
||||
// .rev()
|
||||
// .map(|x| x.to_owned())
|
||||
// .collect::<Vec<String>>()
|
||||
// .join(".")
|
||||
// }
|
||||
//
|
||||
// fn convert_port_hex_dec(port: &str) -> usize {
|
||||
// usize::from_str_radix(port, 16).expect("upsi 3")
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//#[derive(PartialEq)]
|
||||
//enum SocketState{
|
||||
// Listening,
|
||||
// Established,
|
||||
// Closed
|
||||
//}
|
||||
//
|
||||
//#[derive(PartialEq)]
|
||||
//struct Socket {
|
||||
// peer_loc: Peer,
|
||||
// peer_rem: Peer,
|
||||
// pids: Vec<u64>,
|
||||
// state : SocketState,
|
||||
// user : String,
|
||||
// group : String,
|
||||
// inode : u64
|
||||
//}
|
||||
//
|
||||
//impl Socket {
|
||||
// pub fn new(ip_loc: String, port_loc: usize, ip_rem: String, port_rem: usize, pids : Vec<u64>, state : SocketState, user : String, group : String, inode : u64) -> Socket {
|
||||
// Socket {
|
||||
// peer_loc: Peer {
|
||||
// ip_address: ip_loc,
|
||||
// port: port_loc,
|
||||
// },
|
||||
// peer_rem: Peer {
|
||||
// ip_address: ip_rem,
|
||||
// port: port_rem,
|
||||
// },
|
||||
// pids : pids,
|
||||
// state : state,
|
||||
// user : user,
|
||||
// group : group,
|
||||
// inode : inode,
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fn get_tcp_file_inode(pid: u64) -> u64 {
|
||||
// let mut path = String::from("/proc/");
|
||||
// path.push_str(&pid.to_string());
|
||||
// path.push_str("/net/tcp");
|
||||
//
|
||||
// let inode = fs::metadata(&path)
|
||||
// .expect("Cannot read metadata")
|
||||
// .ino();
|
||||
// return inode;
|
||||
// }
|
||||
//
|
||||
// fn check_inode_seen(seen_inodes : &HashSet<u64>, pid : u64) -> bool {
|
||||
// let inode = Socket::get_tcp_file_inode(pid);
|
||||
// return seen_inodes.contains(&inode);
|
||||
// }
|
||||
//
|
||||
// fn add_inode(seen_inodes : &mut HashSet<u64>, pid: u64){
|
||||
// let inode = Socket::get_tcp_file_inode(pid);
|
||||
// seen_inodes.insert(inode);
|
||||
// }
|
||||
//
|
||||
//// fn get_unique_inodes() -> HashSet<u64> {
|
||||
//// let mut unique_inodes = HashSet::new();
|
||||
//// let pattern = Regex::new(r"^[1-4]?[0-9]{1,6}$").expect("kannst kein regex??");
|
||||
//// let dirs = fs::read_dir("/proc").expect("Cannot access /proc");
|
||||
//// for dir in dirs{
|
||||
//// match dir {
|
||||
//// Ok(dir) => {
|
||||
//// if !pattern.is_match(
|
||||
//// &dir.file_name()
|
||||
//// .into_string()
|
||||
//// .expect("Dateiname ist kein gueltiger Unicode-String")[..],
|
||||
//// ) {
|
||||
//// continue;
|
||||
//// }
|
||||
////
|
||||
//// if !unique_inodes.contains(&inode){
|
||||
//// println!("{}", &inode);
|
||||
//// unique_inodes.insert(inode);
|
||||
//// }
|
||||
//// else{
|
||||
//// continue;
|
||||
//// }
|
||||
//// },
|
||||
//// Err(_) => continue,
|
||||
//// };
|
||||
//// }
|
||||
//// unique_inodes
|
||||
//// }
|
||||
//
|
||||
// pub fn to_string (socket : Socket) -> String {
|
||||
// let mut ret_str = String::from("");
|
||||
// ret_str.push_str(&socket.peer_loc.ip_address);
|
||||
// ret_str.push(':');
|
||||
// let loc_port_str = socket.peer_loc.port.clone().to_string();
|
||||
// ret_str.push_str(if socket.peer_loc.port == 0 { "*" } else { &loc_port_str });
|
||||
// ret_str.push(' ');
|
||||
// ret_str.push_str(&socket.peer_rem.ip_address);
|
||||
// ret_str.push(':');
|
||||
// let rem_port_str = socket.peer_rem.port.clone().to_string();
|
||||
// ret_str.push_str(if socket.peer_rem.port == 0 { "*" } else { &rem_port_str });
|
||||
// ret_str.push('\n');
|
||||
// return ret_str;
|
||||
// }
|
||||
//
|
||||
//// fn convert_line(line : String) -> HashSet<Socket>{
|
||||
//// let mut sockets : HashSet<Socket>;
|
||||
//// let line_vec = line.split_whitespace().collect::<Vec<&str>>();
|
||||
//// conv_str.push_str(&Peer::convert_ip_hex_dec(line_vec[1].split(":").nth(0).unwrap()));
|
||||
//// conv_str.push_str(":");
|
||||
//// let loc_port = Peer::convert_port_hex_dec(line_vec[1].split(":").nth(1).expect("gehtnd3"));
|
||||
//// let loc_port_str = loc_port.clone().to_string();
|
||||
//// conv_str.push_str(if loc_port == 0 { "*" } else { &loc_port_str } );
|
||||
//// conv_str.push_str(" ");
|
||||
//// conv_str.push_str(&Peer::convert_ip_hex_dec(line_vec[2].split(":").nth(0).expect("gehtnd2")));
|
||||
//// conv_str.push_str(":");
|
||||
//// let loc_port = Peer::convert_port_hex_dec(line_vec[2].split(":").nth(1).expect("gehtnd3"));
|
||||
//// let loc_port_str = loc_port.clone().to_string();
|
||||
//// conv_str.push_str(if loc_port == 0 { "*" } else { &loc_port_str } );
|
||||
//// conv_str.push('\n');
|
||||
//// return conv_str;
|
||||
//// }
|
||||
//
|
||||
// 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::<u64>::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 !Socket::check_inode_seen(&seen_inodes, pid.parse::<u64>().unwrap()) {
|
||||
// Socket::add_inode(& mut seen_inodes, pid.parse::<u64>().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::<Vec<String>>()
|
||||
// .join("\n");
|
||||
// complete_file.push_str(test);
|
||||
// }
|
||||
// else{
|
||||
// continue;
|
||||
// }
|
||||
// },
|
||||
// Err(_) => continue,
|
||||
// };
|
||||
// }
|
||||
// // complete_file
|
||||
// complete_file
|
||||
// }
|
||||
//
|
||||
// fn get_pids (complete_file : String) -> Vec<u64>{
|
||||
// return Vec::<u64>::new();
|
||||
// }
|
||||
//}
|
||||
fn get_socket_inodes_by_pid(pid : u64, socket_patt : Regex) -> Vec<inode> {
|
||||
let mut inodes : Vec<inode> = Vec::<inode>::new();
|
||||
let mut path = String::from("/proc/");
|
||||
path.push_str(&pid.to_string());
|
||||
path.push_str("/fd/");
|
||||
let contents = fs::read_dir(path).expect("upsi 4 (proc kann nicht gelesen werden, Berechtigung?)");
|
||||
for fd in contents {
|
||||
match fd {
|
||||
Ok(fd) => {
|
||||
let name = fs::read_link(fd.path()).unwrap();
|
||||
let name2 = name.file_name().unwrap().to_str().unwrap();
|
||||
if (socket_patt.is_match(name2)){
|
||||
inodes.push(socket_patt.captures(name2).unwrap().get(1).unwrap().as_str().parse::<u64>().unwrap())
|
||||
}
|
||||
},
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
inodes
|
||||
}
|
||||
|
||||
fn get_readable_proc_pids(pid_pattern : Regex) -> Vec<pid> {
|
||||
let mut pids = Vec::<pid>::new();
|
||||
let dirs = fs::read_dir("/proc/").expect("proc kann nicht gelesen werden, Berechtigung?");
|
||||
for dir in dirs {
|
||||
match dir {
|
||||
|
||||
Ok(dir) => {
|
||||
let name = dir.file_name().into_string().unwrap();
|
||||
if !pid_pattern.is_match(&name) {
|
||||
continue;
|
||||
}
|
||||
pids.push(name.parse().unwrap())
|
||||
},
|
||||
Err(_) => continue
|
||||
};
|
||||
}
|
||||
pids
|
||||
}
|
||||
|
||||
fn get_socket_info() -> Vec<socket::socket::Socket> {
|
||||
let mut sockets = Vec::<socket::socket::Socket>::new();
|
||||
sockets
|
||||
}
|
||||
|
||||
fn get_index(line: String, index: usize) -> String {
|
||||
line.split_whitespace().nth(index).expect("upsi").to_owned()
|
||||
}
|
||||
|
||||
//fn get_local_addresses_and_ports(proc_string : &mut String){
|
||||
// let local_addresses : Vec<String> = Vec::new();
|
||||
// let tcp_file : String = get_socket_info();
|
||||
// tcp.lines().
|
||||
// println!("{:?}", local_addresses);
|
||||
//}
|
||||
|
||||
//fn get_local_port(){
|
||||
// proc_string.lines().for_each( |x| {
|
||||
// local_addresses.push(i64::from_str_radix(&get_index_and_convert_to_dec(x.to_owned(), 2), 16))
|
||||
// });
|
||||
//}
|
||||
fn get_remote_address() {}
|
||||
fn get_remote_port() {}
|
||||
|
||||
fn main() {
|
||||
// let complete_file = Socket::get_all_unique_socket_infos();
|
||||
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 mut socket_infos : Vec<socket::socket::Socket>;
|
||||
println!("{:?}", get_socket_inodes_by_pid(55238, socket_patt));
|
||||
println!("{:?}", get_readable_proc_pids(pid_pattern));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
mod Socket {
|
||||
pub mod socket {
|
||||
|
||||
use regex::Regex;
|
||||
use std::collections::HashSet;
|
||||
|
|
@ -8,16 +8,11 @@ mod Socket {
|
|||
use std::os::unix::fs::MetadataExt;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct Peer {
|
||||
ip_address: String,
|
||||
port: usize,
|
||||
}
|
||||
|
||||
impl Clone for Peer {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
}
|
||||
}
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug)]
|
||||
pub struct Peer {
|
||||
pub ip_address: String,
|
||||
pub port: usize,
|
||||
}
|
||||
|
||||
impl Peer {
|
||||
|
|
@ -28,7 +23,7 @@ mod Socket {
|
|||
}
|
||||
}
|
||||
|
||||
fn build_from_hex(hex_ip: &str, hex_port: &str) -> Peer {
|
||||
pub fn build_from_hex(hex_ip: &str, hex_port: &str) -> Peer {
|
||||
Peer {
|
||||
ip_address: Self::convert_ip_hex_dec(hex_ip),
|
||||
port: Self::convert_port_hex_dec(hex_port),
|
||||
|
|
@ -65,6 +60,7 @@ mod Socket {
|
|||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
pub enum SocketState{
|
||||
Listening,
|
||||
Established,
|
||||
|
|
@ -72,6 +68,7 @@ mod Socket {
|
|||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
pub struct Socket {
|
||||
peer_loc: Peer,
|
||||
peer_rem: Peer,
|
||||
|
|
@ -82,20 +79,6 @@ mod Socket {
|
|||
inode : u64
|
||||
}
|
||||
|
||||
impl Clone for Socket {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
peer_loc : self.peer_loc.clone(),
|
||||
peer_rem : self.peer_rem .clone(),
|
||||
pids : self.pids.clone(),
|
||||
state : self.state.clone(),
|
||||
user : self.user.clone(),
|
||||
group : self.user.clone(),
|
||||
inode : self.inode.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Socket {
|
||||
pub fn new(ip_loc: &str, port_loc: usize, ip_rem: &str, port_rem: usize, pids : Vec<u64>, state : SocketState, user : &str, group : &str, inode : u64) -> Socket {
|
||||
Socket {
|
||||
|
|
@ -107,11 +90,11 @@ mod Socket {
|
|||
ip_address: ip_rem.to_string(),
|
||||
port: port_rem,
|
||||
},
|
||||
pids : pids,
|
||||
state : state,
|
||||
pids,
|
||||
state,
|
||||
user : user.to_string(),
|
||||
group : group.to_string(),
|
||||
inode : inode,
|
||||
inode,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -123,15 +106,15 @@ mod Socket {
|
|||
let inode = fs::metadata(&path)
|
||||
.expect("Cannot read metadata")
|
||||
.ino();
|
||||
return inode;
|
||||
inode
|
||||
}
|
||||
|
||||
fn check_inode_seen(seen_inodes : &HashSet<u64>, pid : u64) -> bool {
|
||||
pub fn check_inode_seen(seen_inodes : &HashSet<u64>, pid : u64) -> bool {
|
||||
let inode = Socket::get_tcp_file_inode(pid);
|
||||
return seen_inodes.contains(&inode);
|
||||
seen_inodes.contains(&inode)
|
||||
}
|
||||
|
||||
fn add_inode(seen_inodes : &mut HashSet<u64>, pid: u64){
|
||||
pub fn add_inode(seen_inodes : &mut HashSet<u64>, pid: u64){
|
||||
let inode = Socket::get_tcp_file_inode(pid);
|
||||
seen_inodes.insert(inode);
|
||||
}
|
||||
|
|
@ -148,7 +131,7 @@ mod Socket {
|
|||
let rem_port_str = self.peer_rem.port.clone().to_string();
|
||||
ret_str.push_str(if self.peer_rem.port == 0 { "*" } else { &rem_port_str });
|
||||
ret_str.push('\n');
|
||||
return ret_str;
|
||||
ret_str
|
||||
}
|
||||
|
||||
fn get_all_unique_socket_infos() -> String {
|
||||
|
|
@ -176,7 +159,7 @@ mod Socket {
|
|||
.expect("Could not read proc/pid/net/tcp")
|
||||
.lines()
|
||||
.skip(1)
|
||||
// .map(|x| Socket::convert_line(x.to_string()))
|
||||
// .map(|x| socket::convert_line(x.to_string()))
|
||||
.map(|x| x.to_owned())
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
|
|
@ -199,21 +182,27 @@ mod Socket {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use crate::socket::Socket::*;
|
||||
use super::*;
|
||||
|
||||
fn init_sockets() -> Vec<Socket> {
|
||||
let sockets = Vec::<Socket>::new();
|
||||
sockets.push(Socket::new("93.83.160.12", 21345, "0.0.0.0", 0, Vec::<u64>::new(), Socket::SocketState::Listening, "root", "root", 123123));
|
||||
sockets.push(Socket::new("195.201.90.97", 8843, "0.0.0.0", 443, Vec::<u64>::new(), Socket::SocketState::Established, "apache", "apache", 218389));
|
||||
sockets.push(Socket::new("255.255.255.255", 193193, "0.0.0.0", 8080, Vec::<u64>::new(), Socket::SocketState::Listening, "node", "node", 1301011));
|
||||
return sockets;
|
||||
fn init_sockets() -> Vec<socket::Socket> {
|
||||
let mut sockets = Vec::<socket::Socket>::new();
|
||||
sockets.push(socket::Socket::new("93.83.160.12", 21345, "0.0.0.0", 0, Vec::<u64>::new(), socket::SocketState::Listening, "root", "root", 123123));
|
||||
sockets.push(socket::Socket::new("195.201.90.97", 8843, "0.0.0.0", 443, Vec::<u64>::new(), socket::SocketState::Established, "apache", "apache", 218389));
|
||||
sockets.push(socket::Socket::new("255.255.255.255", 193193, "0.0.0.0", 8080, Vec::<u64>::new(), socket::SocketState::Listening, "node", "node", 1301011));
|
||||
sockets
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn test_to_string() -> (){
|
||||
#[test]
|
||||
fn test_build_from_hex() -> () {
|
||||
let hex_val = "0100007F";
|
||||
assert_eq!(socket::Peer::build_from_hex("0100007F", "F168"), socket::Peer { ip_address: "127.0.0.1".to_owned(), port: 61800 });
|
||||
assert_eq!(socket::Peer::build_from_hex("980FB23E", "ACCE"), socket::Peer { ip_address: "62.178.15.152".to_owned(), port: 44238 });
|
||||
assert_eq!(socket::Peer::build_from_hex("00000000", "30C8"), socket::Peer { ip_address: "0.0.0.0".to_owned(), port: 12488 });
|
||||
}
|
||||
#[test]
|
||||
fn test_to_string() -> () {
|
||||
let sockets = init_sockets();
|
||||
assert_eq!("93.83.160.12:21345 0.0.0.0:*", sockets[0].clone().to_string());
|
||||
assert_eq!("195.201.90.97:8843 0.0.0.0:443", sockets[1].clone().to_string());
|
||||
assert_eq!("255.255.255.255:193193 0.0.0.0:8080", sockets[2].clone().to_string());
|
||||
assert_eq!("93.83.160.12:21345 0.0.0.0:*\n", sockets[0].clone().to_string());
|
||||
assert_eq!("195.201.90.97:8843 0.0.0.0:443\n", sockets[1].clone().to_string());
|
||||
assert_eq!("255.255.255.255:193193 0.0.0.0:8080\n", sockets[2].clone().to_string());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user