somewhat wip sniffing

This commit is contained in:
zedddie 2026-03-15 17:17:24 +01:00 committed by tuturuu
parent 59a17c5475
commit 891a76eaf5
No known key found for this signature in database
GPG key ID: B352C3C2894405A7
3 changed files with 22 additions and 18 deletions

View file

@ -1,6 +1,6 @@
mod config; mod config;
mod geoparsers; mod geoparsers;
mod sniffing; pub mod sniffing;
mod startup; mod startup;
use startup::init; use startup::init;

View file

@ -3,6 +3,7 @@ use tun::Error;
// Here we will recieve bytes and try to get their destanation & apply Rules for them. // Here we will recieve bytes and try to get their destanation & apply Rules for them.
use crate::config::Config; use crate::config::Config;
#[derive(Debug)]
enum Protocol { enum Protocol {
TCP, TCP,
UDP UDP
@ -10,7 +11,8 @@ enum Protocol {
type Ipv4 = [u8; 4]; type Ipv4 = [u8; 4];
type Ipv6 = [u8; 16]; type Ipv6 = [u8; 16];
type Port = u16; type Port = u16;
enum PacketInfo { #[derive(Debug)]
pub enum PacketInfo {
// <https://www.geeksforgeeks.org/computer-networks/what-is-ipv4/> // <https://www.geeksforgeeks.org/computer-networks/what-is-ipv4/>
V4 { V4 {
src_ip: Ipv4, src_ip: Ipv4,
@ -29,37 +31,38 @@ enum PacketInfo {
} }
} }
pub fn sniff_raw_packets(packet: &[u8]) -> Result<PacketInfo, Box<dyn std::error::Error + Send + Sync + 'static>> { pub fn sniff_raw_packets(packet: &[u8]) -> Result<PacketInfo, Box<dyn std::error::Error + Send + Sync + 'static>> {
println!("something");
let ver = packet[0] >> 4; let ver = packet[0] >> 4;
dbg!(ver);
match ver { match ver {
4 => { 4 => {
PacketInfo::V4{ Ok(PacketInfo::V4{
src_ip: packet[12..16], src_ip: packet[12..16].try_into()?,
src_port: u16::from_be_bytes([packet[20], packet[21]]), src_port: u16::from_be_bytes([packet[20], packet[21]]),
dst_ip: packet[16..20], dst_ip: packet[16..20].try_into()?,
dst_port: u16::from_be_bytes([packet[22], packet[23]]), dst_port: u16::from_be_bytes([packet[22], packet[23]]),
protocol: match packet[9] { protocol: match packet[9] {
6 => Protocol::TCP, 6 => Protocol::TCP,
4 => Protocol::UDP, 4 => Protocol::UDP,
_ => return Err(format!("unsuppiorted protocol: {p}").into()) p => return Err(format!("unsuppiorted protocol: {p}").into())
} }
} })
}, },
6 => { 6 => {
PacketInfo::V6{ Ok(PacketInfo::V6{
src_ip: packet[8..24], src_ip: packet[8..24].try_into()?,
src_port: u16::from_be_bytes([packet[40], packet[41]]), src_port: u16::from_be_bytes([packet[40], packet[41]]),
dst_ip: packet[24..40], dst_ip: packet[24..40].try_into()?,
dst_port: u16::from_be_bytes([packet[42], packet[43]]), dst_port: u16::from_be_bytes([packet[42], packet[43]]),
protocol: match packet[6] { protocol: match packet[6] {
6 => Protocol::TCP, 6 => Protocol::TCP,
4 => Protocol::UDP, 4 => Protocol::UDP,
_ => return Err(format!("unsuppiorted protocol: {p}").into()) p => return Err(format!("unsuppiorted protocol: {p}").into())
} }
} })
}, },
ver => { ver => {
panic!("unexpected packet ver: {ver}"); Err(format!("unsuppiorted ver: {ver}").into())
Error
} }
} }
} }

View file

@ -1,6 +1,6 @@
// Here we iniitialize systems crucial for nsc // Here we iniitialize systems crucial for nsc
use std::io::Read; use std::io::Read;
use sniffing::headers::sniff_raw_packets; use crate::sniffing::headers::sniff_raw_packets;
pub fn init() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { pub fn init() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut config = tun::Configuration::default(); let mut config = tun::Configuration::default();
@ -21,8 +21,9 @@ pub fn init() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
loop { loop {
let amount = dev.read(&mut buf)?; let amount = dev.read(&mut buf)?;
sniff_raw_packets(&buf[0..amount]); // dbg!(sniff_raw_packets(&buf[0..amount])?);
dbg!("{:?}", &buf[0..amount]); let govno = sniff_raw_packets(&buf[0..amount])?;
println!("{govno:?}")
// dbg!("{:?}", &buf[0..amount]);
} }
} }