pre-urlaub commit
This commit is contained in:
parent
b12c83014d
commit
f556f5714a
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -20,9 +20,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
version = "2.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
|
|
|
|||
175
src/Socket.rs
175
src/Socket.rs
|
|
@ -1,175 +0,0 @@
|
|||
|
||||
mod Socket {
|
||||
|
||||
use regex::Regex;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::fs::read_to_string;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct Peer {
|
||||
ip_address: String,
|
||||
port: usize,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ use std::os::unix::fs::MetadataExt;
|
|||
// port: usize,
|
||||
//}
|
||||
|
||||
mod Socket;
|
||||
mod socket;
|
||||
|
||||
//impl Peer {
|
||||
// fn new() -> Peer {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user