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 ident: u16,
pub query: bool,

View File

@ -10,7 +10,7 @@ use std::net::{SocketAddr, UdpSocket};
fn main() {
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);
@ -19,42 +19,68 @@ fn main() {
let udp_socket = UdpSocket::bind("127.0.0.1:2053").expect("Failed to bind to address");
let mut buf = [0; 512];
'outer: loop {
loop {
match udp_socket.recv_from(&mut buf) {
Ok((size, source)) => {
println!("Received {} bytes from {}", size, source);
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
.send_to(received_data, resolver)
.send_to(&data, resolver)
.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)) => {
println!(
"Received {}bytes from {} on upstream socket",
size, upstream
);
let mut recv_packet = Packet::parse(received_data).unwrap();
let upstream_packet = Packet::parse(&lbuf).unwrap();
*packet = Packet::parse(lbuf).ok();
}
Err(e) => {
eprintln!("error in receiving data from upstream: {}", e);
}
}
}
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 = upstream_packet.header.an_count;
recv_packet.header.authority_records = 0;
recv_packet.header.additional_records = 0;
recv_packet.answers = upstream_packet.answers;
recv_packet.answers = upstream_packets
.into_iter()
.filter(|x| x.is_some())
.flat_map(|packet| {
if let Some(packet) = packet {
if packet.answers.is_empty() {
return None;
}
Some(packet.answers[0].clone())
} else {
None
}
})
.collect();
let mut response = vec![];
recv_packet.write_to(&mut response);
@ -63,12 +89,6 @@ fn main() {
.send_to(&response, source)
.expect("Failed to send response");
}
Err(e) => {
eprintln!("error in receving data from upstream: {}", e);
continue 'outer;
}
}
}
Err(e) => {
eprintln!("Error receiving data: {}", e);
break;
@ -77,7 +97,7 @@ fn main() {
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
struct Packet<'a> {
header: Header,
questions: Vec<Question<'a>>,

View File

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

View File

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