diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 16 | ||||
| -rw-r--r-- | src/geoparsers/geoip2.rs | 4 | ||||
| -rw-r--r-- | src/geoparsers/mod.rs | 2 | ||||
| -rw-r--r-- | src/geoparsers/v2ray/parsing.rs | 2 | ||||
| -rw-r--r-- | src/geoparsers/v2ray/types.rs | 12 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/main.rs | 7 | ||||
| -rw-r--r-- | src/sniffing/headers.rs | 142 | ||||
| -rw-r--r-- | src/sniffing/metadata.rs | 1 | ||||
| -rw-r--r-- | src/startup.rs | 4 |
10 files changed, 114 insertions, 78 deletions
diff --git a/src/config.rs b/src/config.rs index 846a1b5..a5739ec 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,14 +17,14 @@ pub struct Config { pub mode: RunTypes, } -// TODO: Think how to add other anonymisers -// Like VPN on localhost:10808 -// it can be like: -// ```toml +// TODO: Think how to add other anonymisers +// Like VPN on localhost:10808 +// it can be like: +// ```toml // [[proxy]] -// name = "VPN" -// addr = "127.0.0.1:10808" -// type = "SOCKS5" # ... +// name = "VPN" +// addr = "127.0.0.1:10808" +// type = "SOCKS5" # ... // ``` impl Default for Config { @@ -39,5 +39,3 @@ impl Default for Config { } } } - - diff --git a/src/geoparsers/geoip2.rs b/src/geoparsers/geoip2.rs index 9f2cc8b..1fd6ab1 100644 --- a/src/geoparsers/geoip2.rs +++ b/src/geoparsers/geoip2.rs @@ -3,8 +3,8 @@ use maxminddb::{Reader, geoip2}; use serde::Deserialize; use std::net::IpAddr; -// For now only MMDB because i cant found .proto schemes of -// V2Ray GeoSite.dat :(( +// For now only MMDB because i cant found .proto schemes of +// V2Ray GeoSite.dat :(( // TODO: V2Ray protobuf parsing && Test 4 ts /// Interface enum for `dst_addr` info diff --git a/src/geoparsers/mod.rs b/src/geoparsers/mod.rs index 7828b9b..c3cfece 100644 --- a/src/geoparsers/mod.rs +++ b/src/geoparsers/mod.rs @@ -1,3 +1,3 @@ pub mod geoip2; pub mod toml; -pub mod v2ray;
\ No newline at end of file +pub mod v2ray; diff --git a/src/geoparsers/v2ray/parsing.rs b/src/geoparsers/v2ray/parsing.rs index 0f897bf..4f0bbba 100644 --- a/src/geoparsers/v2ray/parsing.rs +++ b/src/geoparsers/v2ray/parsing.rs @@ -1,6 +1,6 @@ use crate::geoparsers::v2ray::types::{Domain, GeoSite, GeoSiteList}; -use prost::bytes::Buf; use prost::Message; +use prost::bytes::Buf; use std::fs; pub struct GeoSiteService { diff --git a/src/geoparsers/v2ray/types.rs b/src/geoparsers/v2ray/types.rs index d7c0436..b82fbbf 100644 --- a/src/geoparsers/v2ray/types.rs +++ b/src/geoparsers/v2ray/types.rs @@ -33,17 +33,7 @@ pub mod domain { } } /// Type of domain value. - #[derive( - Clone, - Copy, - Debug, - PartialEq, - Eq, - Hash, - PartialOrd, - Ord, - ::prost::Enumeration - )] + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] #[repr(i32)] pub enum Type { /// The value is used as is. @@ -1,4 +1,4 @@ -pub mod sniffing; pub mod config; pub mod geoparsers; +pub mod sniffing; pub mod startup; diff --git a/src/main.rs b/src/main.rs index df1a31c..c2b6707 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ //mod routing; -//mod config; -//mod geoparsers; -//pub mod sniffing; +mod config; +mod geoparsers; +pub mod sniffing; //mod startup; use nsc::startup::init; fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { - init() } diff --git a/src/sniffing/headers.rs b/src/sniffing/headers.rs index 40a311c..1db0da6 100644 --- a/src/sniffing/headers.rs +++ b/src/sniffing/headers.rs @@ -1,5 +1,5 @@ -use tun::Error; use std::fmt; +use tun::Error; // Here we will recieve bytes and try to get their destanation & apply Rules for them. use crate::config::Config; @@ -8,14 +8,14 @@ use crate::config::Config; pub enum Protocol { TCP, UDP, - Unsupported(u8) + Unsupported(u8), } type SourceV4Ip = Ipv4; type SourceV6Ip = Ipv6; #[derive(PartialEq, Debug)] pub enum IpVersion { V4, - V6 + V6, } type Ipv4 = [u8; 4]; type Ipv6 = [u16; 8]; @@ -29,7 +29,7 @@ pub enum PacketInfo { dst_ip: Ipv4, dst_port: Port, protocol: Protocol, - dns: bool + dns: bool, }, // <https://www.geeksforgeeks.org/computer-networks/internet-protocol-version-6-ipv6-header/> V6 { @@ -38,21 +38,59 @@ pub enum PacketInfo { dst_ip: Ipv6, dst_port: Port, protocol: Protocol, - dns: bool - } + dns: bool, + }, } impl fmt::Display for PacketInfo { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if self.version() == &IpVersion::V4 { - let src_ip = self.src_ipv4_ip().unwrap(); - let dst_ip = self.dst_ipv4_ip().unwrap(); - write!(f, "{}.{}.{}.{}:{} -> {}.{}.{}.{}:{} {:?} is dns? {:?}", src_ip[0], src_ip[1], src_ip[2], src_ip[3], self.src_port(), dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], self.dst_port(), self.protocol(), self.dns()) + let src_ip = self.src_ipv4_ip().unwrap(); + let dst_ip = self.dst_ipv4_ip().unwrap(); + write!( + f, + "{}.{}.{}.{}:{} -> {}.{}.{}.{}:{} {:?} is dns? {:?}", + src_ip[0], + src_ip[1], + src_ip[2], + src_ip[3], + self.src_port(), + dst_ip[0], + dst_ip[1], + dst_ip[2], + dst_ip[3], + self.dst_port(), + self.protocol(), + self.dns() + ) } else { - let src_ip = self.src_ipv6_ip().unwrap(); - let dst_ip = self.dst_ipv6_ip().unwrap(); + let src_ip = self.src_ipv6_ip().unwrap(); + let dst_ip = self.dst_ipv6_ip().unwrap(); // y:y:y:y:y:y:y:y = 8 hexademical; y = segment, pair of 2 u8 big endian - write!(f, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x} port:{} -> {:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x} port:{} {:?} is dns? {:?}", src_ip[0], src_ip[1], src_ip[2], src_ip[3], src_ip[4], src_ip[5], src_ip[6], src_ip[7], self.src_port(), dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], dst_ip[4], dst_ip[5], dst_ip[6], dst_ip[7], self.dst_port(), self.protocol(), self.dns()) + write!( + f, + "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x} port:{} -> {:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x} port:{} {:?} is dns? {:?}", + src_ip[0], + src_ip[1], + src_ip[2], + src_ip[3], + src_ip[4], + src_ip[5], + src_ip[6], + src_ip[7], + self.src_port(), + dst_ip[0], + dst_ip[1], + dst_ip[2], + dst_ip[3], + dst_ip[4], + dst_ip[5], + dst_ip[6], + dst_ip[7], + self.dst_port(), + self.protocol(), + self.dns() + ) } } } @@ -60,20 +98,20 @@ impl fmt::Display for PacketInfo { impl PacketInfo { pub fn dns(&self) -> &bool { match self { - PacketInfo::V4 { dns, ..} => dns, - PacketInfo::V6 { dns, ..} => dns, + PacketInfo::V4 { dns, .. } => dns, + PacketInfo::V6 { dns, .. } => dns, } } pub fn src_ipv6_ip(&self) -> Option<&SourceV6Ip> { match self { PacketInfo::V6 { src_ip, .. } => Some(src_ip), - _ => None + _ => None, } } pub fn dst_ipv6_ip(&self) -> Option<&SourceV6Ip> { match self { PacketInfo::V6 { dst_ip, .. } => Some(dst_ip), - _ => None + _ => None, } } pub fn src_ipv4_ip(&self) -> Option<&SourceV4Ip> { @@ -85,25 +123,25 @@ impl PacketInfo { pub fn dst_ipv4_ip(&self) -> Option<&SourceV4Ip> { match self { PacketInfo::V4 { dst_ip, .. } => Some(dst_ip), - _ => None + _ => None, } } pub fn src_port(&self) -> &Port { match self { PacketInfo::V4 { src_port, .. } => src_port, - PacketInfo::V6 { src_port, .. } => src_port + PacketInfo::V6 { src_port, .. } => src_port, } } pub fn dst_port(&self) -> &Port { match self { PacketInfo::V4 { dst_port, .. } => dst_port, - PacketInfo::V6 { dst_port, .. } => dst_port + PacketInfo::V6 { dst_port, .. } => dst_port, } } pub fn version(&self) -> &IpVersion { match self { - PacketInfo::V4 { .. }=> &IpVersion::V4, - PacketInfo::V6 { .. }=> &IpVersion::V6 + PacketInfo::V4 { .. } => &IpVersion::V4, + PacketInfo::V6 { .. } => &IpVersion::V6, } } pub fn protocol(&self) -> &Protocol { @@ -125,39 +163,51 @@ pub fn sniff_raw_packets(packet: &Packet) -> SniffedPacket { 4 => { // Internet Header Length (IHL). let ihl = (packet[0] & 0x0F) as usize * 4; - let dst_port = Port::from_be_bytes([packet[ihl+2], packet[ihl+3]]); + let dst_port = Port::from_be_bytes([packet[ihl + 2], packet[ihl + 3]]); let dns; - if dst_port == 53 { dns = true; } else { dns = false; }; - let v4 = PacketInfo::V4{ + if dst_port == 53 { + dns = true; + } else { + dns = false; + }; + let v4 = PacketInfo::V4 { src_ip: <Ipv4>::try_from(&packet[12..16])?, - src_port: Port::from_be_bytes([packet[ihl], packet[ihl+1]]), + src_port: Port::from_be_bytes([packet[ihl], packet[ihl + 1]]), dst_ip: <Ipv4>::try_from(&packet[16..20])?, dst_port, protocol: match packet[9] { 6 => Protocol::TCP, 17 => Protocol::UDP, - p => Protocol::Unsupported(p) + p => Protocol::Unsupported(p), }, - dns + dns, }; - if !matches!(v4.protocol(), Protocol::Unsupported(_)) { - println!("{v4}"); - } else { - // TODO: make --debug option which will include this diagnostic, for general use this + if !matches!(v4.protocol(), Protocol::Unsupported(_)) { + println!("{v4}"); + } else { + // TODO: make --debug option which will include this diagnostic, for general use this // should be off - // println!("oppsie unsupported protocol: {:?}", v4.protocol()); + // println!("oppsie unsupported protocol: {:?}", v4.protocol()); } Ok(v4) - }, + } 6 => { // y:y:y:y:y:y:y:y hexademical; y = segment, pair of 2 u8 in big endian - let src_ip = std::array::from_fn(|i| u16::from_be_bytes([packet[8 + i*2], packet[8 + i*2 + 1]])); - let dst_ip = std::array::from_fn(|i| u16::from_be_bytes([packet[24 + i*2], packet[24 + i*2 + 1]])); + let src_ip = std::array::from_fn(|i| { + u16::from_be_bytes([packet[8 + i * 2], packet[8 + i * 2 + 1]]) + }); + let dst_ip = std::array::from_fn(|i| { + u16::from_be_bytes([packet[24 + i * 2], packet[24 + i * 2 + 1]]) + }); let dst_port = Port::from_be_bytes([packet[42], packet[43]]); let dns; - if dst_port == 53 { dns = true; } else { dns = false; }; - let v6 = PacketInfo::V6{ + if dst_port == 53 { + dns = true; + } else { + dns = false; + }; + let v6 = PacketInfo::V6 { src_ip, src_port: Port::from_be_bytes([packet[40], packet[41]]), dst_ip, @@ -165,22 +215,20 @@ pub fn sniff_raw_packets(packet: &Packet) -> SniffedPacket { protocol: match packet[6] { 6 => Protocol::TCP, 17 => Protocol::UDP, - p => Protocol::Unsupported(p) + p => Protocol::Unsupported(p), }, - dns + dns, }; - if !matches!(v6.protocol(), Protocol::Unsupported(_)) { - println!("{v6}"); - } else { - // TODO: make --debug option which will include this diagnostic, for general use this + if !matches!(v6.protocol(), Protocol::Unsupported(_)) { + println!("{v6}"); + } else { + // TODO: make --debug option which will include this diagnostic, for general use this // should be off - // println!("oppsie unsupported protocol: {:?}", v6.protocol()); + // println!("oppsie unsupported protocol: {:?}", v6.protocol()); } Ok(v6) - }, - ver => { - Err(format!("unsuppiorted ver: {ver}").into()) } + ver => Err(format!("unsuppiorted ver: {ver}").into()), } } diff --git a/src/sniffing/metadata.rs b/src/sniffing/metadata.rs index e69de29..8b13789 100644 --- a/src/sniffing/metadata.rs +++ b/src/sniffing/metadata.rs @@ -0,0 +1 @@ + diff --git a/src/startup.rs b/src/startup.rs index 10a8441..3a1a09a 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -1,7 +1,7 @@ // Here we iniitialize systems crucial for nsc -use std::io::Read; -use crate::sniffing::headers::sniff_raw_packets; use crate::sniffing::headers::Protocol; +use crate::sniffing::headers::sniff_raw_packets; +use std::io::Read; pub fn init() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { let mut config = tun::Configuration::default(); config |
