Skip to content

Commit

Permalink
Random remote lookup port (#32)
Browse files Browse the repository at this point in the history
* issue 15: implemented default config locations

* finished issue 15

* check config file exists before attempting read

* moved config loaded msg, removed config_file from DnsSettings

* removed commented code

* moved file read out of loop

* refactored into separate load functions so config param now optional

* second attempt

* will randomly generate remote lookup port

* removed unwanted debug line

---------

Co-authored-by: ghost <[email protected]>
  • Loading branch information
witchofthewires and ghost authored Dec 11, 2024
1 parent 9d5dd64 commit aa0ed2a
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 14 deletions.
1 change: 0 additions & 1 deletion dns.config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
listening-port: 53
remote-lookup-port: 42069
thread-count: 1
use-udp: true
use-tcp: false
Expand Down
6 changes: 2 additions & 4 deletions src/dns_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ use std::net::UdpSocket;

pub struct DnsResolver {
database: SimpleDatabase,
remote_lookup_port: u16,
}

impl DnsResolver {
pub fn new(database_file: String, remote_lookup_port: u16) -> DnsResolver {
pub fn new(database_file: String) -> DnsResolver {
Self {
database: SimpleDatabase::new(database_file),
remote_lookup_port,
}
}

Expand Down Expand Up @@ -66,7 +64,7 @@ impl DnsResolver {
fn do_remote_lookup(&self, question: &DnsQuestion, packet: &mut DnsPacket) -> Result<(), Box<dyn Error>> {
let server = (self.database.get_random_remote_lookup_server().unwrap(), 53);

let socket = UdpSocket::bind(("0.0.0.0", self.remote_lookup_port))?;
let socket = UdpSocket::bind(("0.0.0.0:0"))?;

let mut remote_packet = DnsPacket::new();
remote_packet.header.recurse_desired = true;
Expand Down
4 changes: 2 additions & 2 deletions src/dns_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl DnsServer for DnsUdpServer {
};

// process request
let resolver = DnsResolver::new(settings.database_file.clone(), settings.remote_lookup_port);
let resolver = DnsResolver::new(settings.database_file.clone());

match resolver.answer_question(request_packet) {
Ok(result) => {
Expand Down Expand Up @@ -155,7 +155,7 @@ impl DnsServer for DnsTcpServer {
ignore_result_or_log_error_continue!(stream.read_to_end(&mut packet_buffer), "Failed to read the packet into a buffer");

let request = return_result_or_log_error_continue!(DnsPacket::from_bytes(packet_buffer.as_slice()), "Failed to parse packet from buffer");
let resolver = DnsResolver::new(settings.database_file.clone(), settings.remote_lookup_port);
let resolver = DnsResolver::new(settings.database_file.clone());

match resolver.answer_question(request) {
Ok(result) => {
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ fn main() -> Result<(), Box<dyn Error>> {
};
let settings = settings.expect("Error reading settings!");
log_debug!("Settings: {:?}", settings);

let server_udp = DnsUdpServer::new(settings.clone());
let server_tcp = DnsTcpServer::new(settings.clone());

Expand Down
9 changes: 3 additions & 6 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::error::Error;
use std::fs;
use std::io::ErrorKind;
use yaml_rust::YamlLoader;
use std::path::Path;

use crate::log_info;

use crate::log_debug;

Expand All @@ -10,7 +13,6 @@ extern crate shellexpand;
#[derive(Clone, Debug)]
pub struct DnsSettings {
pub listening_port: u16,
pub remote_lookup_port: u16,
pub database_file: String,
pub thread_count: u32,
pub use_udp: bool,
Expand All @@ -34,10 +36,6 @@ impl DnsSettings {
Some(x) => x as u16,
None => 53,
};
let remote_lookup_port = match config_settings["remote-lookup-port"].as_i64() {
Some(x) => x as u16,
None => 42069,
};
let thread_count = match config_settings["thread-count"].as_i64() {
Some(x) => x as u32,
None => 1, // TODO is this the best default?
Expand All @@ -61,7 +59,6 @@ impl DnsSettings {

Ok(DnsSettings {
listening_port,
remote_lookup_port,
database_file,
thread_count,
use_udp,
Expand Down

0 comments on commit aa0ed2a

Please sign in to comment.