Skip to content

Commit

Permalink
Merge pull request #2 from ragibkl/built-in-bind-recursion
Browse files Browse the repository at this point in the history
Built in bind recursion
  • Loading branch information
ragibkl authored Mar 8, 2024
2 parents c17cf82 + e4547d1 commit c197e85
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ RUN cargo build --release
FROM debian:12 AS runtime

RUN apt-get update
RUN apt-get install -y openssl libc6 libstdc++6
RUN apt-get install -y openssl libc6 libstdc++6 bind9

# set default logging, can be overridden
ENV RUST_LOG=info

# copy bind config
COPY named.conf.options /etc/bind/named.conf.options

# copy binary
COPY --from=builder /code/bancuh-dns/target/release/bancuh-dns /usr/local/bin/bancuh-dns

Expand Down
12 changes: 3 additions & 9 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
version: "3"

services:
bind:
image: ragibkl/bancuh-bind
restart: always
ports:
- 1154:53/tcp
- 1154:53/udp

dns:
image: ragibkl/bancuh-dns
build:
context: .
restart: always
environment:
CONFIG_URL: /data/configuration.yaml
FORWARDERS: "127.0.0.1"
FORWARDERS_PORT: "1154"
PORT: 53
# FORWARDERS: "1.1.1.1,1.0.0.1"
# FORWARDERS_PORT: "53"
volumes:
- ./data:/data
ports:
Expand Down
13 changes: 13 additions & 0 deletions named.conf.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
options {
listen-on port 5353 { any; };
listen-on-v6 port 5353 { any; };

allow-query { any; };
allow-recursion { any; };
allow-transfer { none; };

prefetch 10;
max-cache-size 256m;
max-cache-ttl 600;
max-ncache-ttl 600;
};
7 changes: 7 additions & 0 deletions src/bind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use tokio::process::{Child, Command};

pub fn spawn_bind() -> anyhow::Result<Child> {
let child = Command::new("named").arg("-f").kill_on_drop(true).spawn()?;

Ok(child)
}
49 changes: 38 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bind;
mod compiler;
mod config;
mod db;
Expand All @@ -7,7 +8,7 @@ mod handler;
mod resolver;

use std::{
net::{IpAddr, SocketAddr},
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::Arc,
time::Duration,
};
Expand All @@ -22,6 +23,7 @@ use tokio::{
use tokio_util::{sync::CancellationToken, task::TaskTracker};

use crate::{
bind::spawn_bind,
config::{Config, FileOrUrl},
engine::AdblockEngine,
handler::Handler,
Expand All @@ -30,6 +32,8 @@ use crate::{

const TCP_TIMEOUT: Duration = Duration::from_secs(10);
const UPDATE_INTERVAL: Duration = Duration::from_secs(86400); // 1 day
const BIND_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
const BIND_PORT: u16 = 5353;

#[derive(Parser, Debug)]
#[command(name = "Bancuh DNS")]
Expand All @@ -50,15 +54,8 @@ struct Args {
#[arg(short, long, env, value_name = "PORT", default_value = "53")]
port: u16,

/// Sets a custom forward resolvers
#[arg(
short,
long,
env,
value_name = "FORWARDERS",
value_delimiter = ',',
default_value = "8.8.8.8,8.8.4.4"
)]
/// Sets custom forward resolvers
#[arg(short, long, env, value_name = "FORWARDERS", value_delimiter = ',')]
forwarders: Vec<IpAddr>,

/// Sets a custom forward resolvers port, useful for local custom port
Expand Down Expand Up @@ -128,9 +125,39 @@ async fn main() -> anyhow::Result<()> {
});
tracing::info!("Starting engine-update task. DONE");

let resolver = if forwarders.is_empty() {
tracing::info!("Starting bind");
let cloned_token = token.clone();
tracker.spawn(async move {
let mut child = match spawn_bind() {
Ok(child) => child,
Err(err) => {
tracing::error!("Starting bind. ERROR: {err}");
cloned_token.cancel();
return;
}
};
tracing::info!("Starting bind. DONE");

tokio::select! {
_ = cloned_token.cancelled() => {
tracing::info!("bind received cancel signal");
let _ = child.kill().await;
},
_ = child.wait() => {
tracing::info!("bind ended prematurely");
cloned_token.cancel();
},
}
});

Resolver::new(&[BIND_IP], &BIND_PORT)
} else {
Resolver::new(&forwarders, &forwarders_port)
};

tracker.close();

let resolver = Resolver::new(&forwarders, &forwarders_port);
let handler = Handler::new(engine, resolver);

tracing::info!("Starting dns server");
Expand Down
2 changes: 1 addition & 1 deletion src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use itertools::Itertools;

pub fn create_resolver(forwarders: &[IpAddr], port: &u16) -> TokioAsyncResolver {
tracing::info!(
"Setting up forwarders: {} on port: {port}",
"Setting up forwarders: [{}] on port: {port}",
forwarders.iter().join(", ")
);

Expand Down

0 comments on commit c197e85

Please sign in to comment.