debug: dns proxy

This commit is contained in:
Ishan Jain 2023-12-12 04:58:21 +05:30
parent 0c411468d1
commit cc03bf0996
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
4 changed files with 63 additions and 45 deletions

View File

@ -1,4 +1,4 @@
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Header { pub struct Header {
pub ident: u16, pub ident: u16,
pub query: bool, pub query: bool,

View File

@ -10,7 +10,7 @@ use std::net::{SocketAddr, UdpSocket};
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
let resolver: SocketAddr = args[2].parse().unwrap(); let resolver: SocketAddr = args[2].parse().expect("resolver address not provided");
println!("resolver = {:?}", resolver); println!("resolver = {:?}", resolver);
@ -19,42 +19,68 @@ fn main() {
let udp_socket = UdpSocket::bind("127.0.0.1:2053").expect("Failed to bind to address"); let udp_socket = UdpSocket::bind("127.0.0.1:2053").expect("Failed to bind to address");
let mut buf = [0; 512]; let mut buf = [0; 512];
'outer: loop { loop {
match udp_socket.recv_from(&mut buf) { match udp_socket.recv_from(&mut buf) {
Ok((size, source)) => { Ok((size, source)) => {
println!("Received {} bytes from {}", size, source); println!("Received {} bytes from {}", size, source);
let received_data = &buf[0..size]; let received_data = &buf[0..size];
let mut recv_packet = Packet::parse(received_data).unwrap();
println!("{:0x?}", received_data); for question in recv_packet.questions.iter() {
let mut packet = recv_packet.clone();
packet.header.qd_count = 1;
packet.questions = vec![question.clone()];
let mut data = vec![];
packet.write_to(&mut data);
upstream_socket upstream_socket
.send_to(received_data, resolver) .send_to(&data, resolver)
.expect("error in sending data to upstream"); .expect("error in sending data to upstream");
}
recv_packet.header.query = true;
recv_packet.header.authoritative = false;
recv_packet.header.truncated = false;
recv_packet.header.recursion_avail = false;
recv_packet.header.reserved = 0;
recv_packet.header.rcode = if recv_packet.header.opcode == 0 { 0 } else { 4 };
recv_packet.header.an_count = recv_packet.header.qd_count;
recv_packet.header.authority_records = 0;
recv_packet.header.additional_records = 0;
let mut lbuf = [0; 512]; let mut responses = vec![[0; 512]; recv_packet.header.qd_count as usize];
let mut upstream_packets = vec![None; recv_packet.header.qd_count as usize];
match upstream_socket.recv_from(&mut lbuf) { for (lbuf, packet) in responses.iter_mut().zip(upstream_packets.iter_mut()) {
match upstream_socket.recv_from(lbuf) {
Ok((size, upstream)) => { Ok((size, upstream)) => {
println!( println!(
"Received {}bytes from {} on upstream socket", "Received {}bytes from {} on upstream socket",
size, upstream size, upstream
); );
let mut recv_packet = Packet::parse(received_data).unwrap(); *packet = Packet::parse(lbuf).ok();
let upstream_packet = Packet::parse(&lbuf).unwrap(); }
Err(e) => {
eprintln!("error in receiving data from upstream: {}", e);
}
}
}
recv_packet.header.query = true; recv_packet.answers = upstream_packets
recv_packet.header.authoritative = false; .into_iter()
recv_packet.header.truncated = false; .filter(|x| x.is_some())
recv_packet.header.recursion_avail = false; .flat_map(|packet| {
recv_packet.header.reserved = 0; if let Some(packet) = packet {
recv_packet.header.rcode = if packet.answers.is_empty() {
if recv_packet.header.opcode == 0 { 0 } else { 4 }; return None;
recv_packet.header.an_count = upstream_packet.header.an_count; }
recv_packet.header.authority_records = 0; Some(packet.answers[0].clone())
recv_packet.header.additional_records = 0; } else {
None
recv_packet.answers = upstream_packet.answers; }
})
.collect();
let mut response = vec![]; let mut response = vec![];
recv_packet.write_to(&mut response); recv_packet.write_to(&mut response);
@ -63,12 +89,6 @@ fn main() {
.send_to(&response, source) .send_to(&response, source)
.expect("Failed to send response"); .expect("Failed to send response");
} }
Err(e) => {
eprintln!("error in receving data from upstream: {}", e);
continue 'outer;
}
}
}
Err(e) => { Err(e) => {
eprintln!("Error receiving data: {}", e); eprintln!("Error receiving data: {}", e);
break; break;
@ -77,7 +97,7 @@ fn main() {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
struct Packet<'a> { struct Packet<'a> {
header: Header, header: Header,
questions: Vec<Question<'a>>, questions: Vec<Question<'a>>,

View File

@ -1,6 +1,6 @@
use crate::qname::Qname; use crate::qname::Qname;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Question<'a> { pub struct Question<'a> {
pub name: Qname<'a>, pub name: Qname<'a>,
pub q_type: u16, pub q_type: u16,

View File

@ -1,6 +1,6 @@
use crate::qname::Qname; use crate::qname::Qname;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct RRecord<'a> { pub struct RRecord<'a> {
pub name: Qname<'a>, pub name: Qname<'a>,
pub r_type: u16, pub r_type: u16,
@ -60,17 +60,15 @@ impl<'a> RRecord<'a> {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum RData { pub enum RData {
A([u8; 4]), A([u8; 4]),
Aaaa([u8; 16]),
} }
impl RData { impl RData {
pub fn write_to(self, buf: &mut Vec<u8>) { pub fn write_to(self, buf: &mut Vec<u8>) {
match self { match self {
RData::A(addr) => buf.extend(addr), RData::A(addr) => buf.extend(addr),
RData::Aaaa(addr) => buf.extend(addr),
} }
} }
} }