feat: logging

This commit is contained in:
Patrick 2025-01-01 20:32:55 +01:00
parent f10c7a59c9
commit de05a7f848
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F
4 changed files with 25 additions and 9 deletions

13
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -1,9 +1,9 @@
{
"interfaces": "lan*",
"interfaces": "lan.*",
"rules": [
{
"from": "wan",
"to": "lan",
"from": "lan",
"to": "wan",
"allow_questions": "printer",
"allow_answers": ".*"
}

View file

@ -15,6 +15,7 @@ use std::{
collections::HashSet,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
};
use tracing::{debug, error, info};
fn get_iface(from: &SocketAddr, ifaces: &Vec<Iface>) -> Result<String> {
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::<Result<Vec<_>>>()?;
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!(),
}