From de05a7f8483be0a32a3760851eadd15723d34ba4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 1 Jan 2025 20:32:55 +0100 Subject: [PATCH] feat: logging --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 1 + example.json | 6 +++--- src/main.rs | 14 ++++++++------ 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a883dfc..8236eea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -472,6 +472,7 @@ dependencies = [ "serde_regex", "simple-dns", "socket2", + "tracing", "tracing-subscriber", ] @@ -603,9 +604,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.33" diff --git a/Cargo.toml b/Cargo.toml index 739dea6..bca1236 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ serde_json = "1.0.134" serde_regex = "1.1.0" simple-dns = "0.9.1" socket2 = { version = "0.5.8", features = ["all"] } +tracing = "0.1.41" tracing-subscriber = "0.3.19" diff --git a/example.json b/example.json index ac0f789..df0dd4c 100644 --- a/example.json +++ b/example.json @@ -1,9 +1,9 @@ { - "interfaces": "lan*", + "interfaces": "lan.*", "rules": [ { - "from": "wan", - "to": "lan", + "from": "lan", + "to": "wan", "allow_questions": "printer", "allow_answers": ".*" } diff --git a/src/main.rs b/src/main.rs index 0dff47e..19176eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use std::{ collections::HashSet, net::{Ipv4Addr, SocketAddr, SocketAddrV4}, }; +use tracing::{debug, error, info}; fn get_iface(from: &SocketAddr, ifaces: &Vec) -> Result { for i in ifaces { @@ -49,6 +50,7 @@ struct Rule { allow_answers: Regex, } +#[derive(Debug)] struct Iface { iface: NetworkInterface, socket: socket2::Socket, @@ -70,6 +72,7 @@ struct Config { } fn main() -> Result<()> { + tracing_subscriber::fmt::init(); let cli = Cli::parse(); let file = File::open(cli.config)?; let reader = BufReader::new(file); @@ -81,7 +84,6 @@ fn main() -> Result<()> { let addr = SocketAddrV4::new(ADDR, 5353).into(); socket.bind(&addr)?; socket.join_multicast_v4(&ADDR, &Ipv4Addr::UNSPECIFIED)?; - //socket.set_nonblocking(true)?; let interfaces = interfaces(); let interfaces = interfaces @@ -108,6 +110,7 @@ fn main() -> Result<()> { }) }) .collect::>>()?; + debug!("{:?}", interfaces); let mut buf = [0u8; 4096]; let socket: UdpSocket = socket.into(); loop { @@ -123,14 +126,14 @@ fn main() -> Result<()> { let packet = Packet::parse(buf)?; let iface = match get_iface(&from, &interfaces) { Err(_) => { - eprintln!("Invalid packet received from {}", from); + error!("Invalid packet received from {}", from); continue; } Ok(name) => name, }; let questions = get_questions(&packet); let answers = get_answers(&packet); - println!( + debug!( "received packet on interface {} from {} questioning {:?} and answering {:?}", iface, from, questions, answers ); @@ -144,15 +147,14 @@ fn main() -> Result<()> { } } out.remove(&iface); - println!("relaying packet to {:?}", out); + debug!("relaying packet to {:?}", out); for i in &interfaces { if out.contains(&i.iface.name) { - println!("sending packet on {}", i.iface.name); + debug!("sending packet on {}", i.iface.name); let sock_addr = SocketAddrV4::new(ADDR, 5353).into(); i.socket.send_to(buf, &sock_addr)?; } } - // socket.send_to(&buf, addr); } Err(_) => todo!(), }