feat: DNS-over-HTTPS (DoH) upstream forwarding #14
27
Cargo.lock
generated
27
Cargo.lock
generated
@@ -393,6 +393,12 @@ dependencies = [
|
|||||||
"miniz_oxide",
|
"miniz_oxide",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fnv"
|
||||||
|
version = "1.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "form_urlencoded"
|
name = "form_urlencoded"
|
||||||
version = "1.2.2"
|
version = "1.2.2"
|
||||||
@@ -523,6 +529,25 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "h2"
|
||||||
|
version = "0.4.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2f44da3a8150a6703ed5d34e164b875fd14c2cdab9af1252a9a1020bde2bdc54"
|
||||||
|
dependencies = [
|
||||||
|
"atomic-waker",
|
||||||
|
"bytes",
|
||||||
|
"fnv",
|
||||||
|
"futures-core",
|
||||||
|
"futures-sink",
|
||||||
|
"http",
|
||||||
|
"indexmap",
|
||||||
|
"slab",
|
||||||
|
"tokio",
|
||||||
|
"tokio-util",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.16.1"
|
version = "0.16.1"
|
||||||
@@ -584,6 +609,7 @@ dependencies = [
|
|||||||
"bytes",
|
"bytes",
|
||||||
"futures-channel",
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"h2",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
"httparse",
|
"httparse",
|
||||||
@@ -1211,6 +1237,7 @@ dependencies = [
|
|||||||
"base64",
|
"base64",
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"h2",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
"http-body-util",
|
"http-body-util",
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ serde_json = "1"
|
|||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
env_logger = "0.11"
|
env_logger = "0.11"
|
||||||
reqwest = { version = "0.12", features = ["rustls-tls", "gzip"], default-features = false }
|
reqwest = { version = "0.12", features = ["rustls-tls", "gzip", "http2"], default-features = false }
|
||||||
hyper = { version = "1", features = ["client", "http1", "server"] }
|
hyper = { version = "1", features = ["client", "http1", "server"] }
|
||||||
hyper-util = { version = "0.1", features = ["client-legacy", "http1", "tokio"] }
|
hyper-util = { version = "0.1", features = ["client-legacy", "http1", "tokio"] }
|
||||||
http-body-util = "0.1"
|
http-body-util = "0.1"
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ sudo ./target/release/numa
|
|||||||
- **Path-based routing** — `app.numa/api → :5001`, `app.numa/auth → :5002`. Route URL paths to different backends with optional prefix stripping. Like nginx location blocks, zero config files.
|
- **Path-based routing** — `app.numa/api → :5001`, `app.numa/auth → :5002`. Route URL paths to different backends with optional prefix stripping. Like nginx location blocks, zero config files.
|
||||||
- **LAN service discovery** — Numa instances on the same network find each other automatically via mDNS. Access a teammate's `api.numa` from your machine. Opt-in via `[lan] enabled = true`.
|
- **LAN service discovery** — Numa instances on the same network find each other automatically via mDNS. Access a teammate's `api.numa` from your machine. Opt-in via `[lan] enabled = true`.
|
||||||
- **Developer overrides** — point any hostname to any IP, auto-reverts after N minutes. REST API with 25+ endpoints. Built-in diagnostics: `curl localhost:5380/diagnose/example.com` tells you exactly how any domain resolves.
|
- **Developer overrides** — point any hostname to any IP, auto-reverts after N minutes. REST API with 25+ endpoints. Built-in diagnostics: `curl localhost:5380/diagnose/example.com` tells you exactly how any domain resolves.
|
||||||
|
- **DNS-over-HTTPS** — upstream queries encrypted via DoH. Your ISP sees HTTPS traffic, not DNS queries. Set `address = "https://9.9.9.9/dns-query"` in `[upstream]` or any DoH provider.
|
||||||
- **Ad blocking that travels with you** — 385K+ domains blocked via [Hagezi Pro](https://github.com/hagezi/dns-blocklists). Works on any network: coffee shops, hotels, airports.
|
- **Ad blocking that travels with you** — 385K+ domains blocked via [Hagezi Pro](https://github.com/hagezi/dns-blocklists). Works on any network: coffee shops, hotels, airports.
|
||||||
- **Sub-millisecond caching** — cached lookups in 0ms. Faster than any public resolver.
|
- **Sub-millisecond caching** — cached lookups in 0ms. Faster than any public resolver.
|
||||||
- **Live dashboard** — real-time stats, query log, blocking controls, service management. LAN accessibility badges show which services are reachable from other devices.
|
- **Live dashboard** — real-time stats, query log, blocking controls, service management. LAN accessibility badges show which services are reachable from other devices.
|
||||||
@@ -134,6 +135,7 @@ bind_addr = "0.0.0.0:53"
|
|||||||
| Path-based routing | No | No | No | No | Prefix match + strip |
|
| Path-based routing | No | No | No | No | Prefix match + strip |
|
||||||
| LAN service discovery | No | No | No | No | mDNS, opt-in |
|
| LAN service discovery | No | No | No | No | mDNS, opt-in |
|
||||||
| Developer overrides | No | No | No | No | REST API + auto-expiry |
|
| Developer overrides | No | No | No | No | REST API + auto-expiry |
|
||||||
|
| Encrypted upstream (DoH) | No (needs cloudflared) | Yes | Cloud only | Cloud only | Native, single binary |
|
||||||
| Portable (travels with laptop) | No (appliance) | No (appliance) | Cloud only | Cloud only | Single binary |
|
| Portable (travels with laptop) | No (appliance) | No (appliance) | Cloud only | Cloud only | Single binary |
|
||||||
| Zero config | Complex | Docker/setup | Yes | Yes | Works out of the box |
|
| Zero config | Complex | Docker/setup | Yes | Yes | Works out of the box |
|
||||||
| Ad blocking | Yes | Yes | Yes | Limited | 385K+ domains |
|
| Ad blocking | Yes | Yes | Yes | Limited | 385K+ domains |
|
||||||
@@ -158,6 +160,7 @@ No DNS libraries — no `hickory-dns`, no `trust-dns`. The wire protocol — hea
|
|||||||
- [x] Local service proxy — `.numa` domains, HTTP/HTTPS proxy, auto TLS, WebSocket
|
- [x] Local service proxy — `.numa` domains, HTTP/HTTPS proxy, auto TLS, WebSocket
|
||||||
- [x] Path-based routing — URL prefix routing with optional strip, REST API
|
- [x] Path-based routing — URL prefix routing with optional strip, REST API
|
||||||
- [x] LAN service discovery — mDNS auto-discovery (opt-in), cross-machine DNS + proxy
|
- [x] LAN service discovery — mDNS auto-discovery (opt-in), cross-machine DNS + proxy
|
||||||
|
- [x] DNS-over-HTTPS — encrypted upstream via DoH (Quad9, Cloudflare, any provider)
|
||||||
- [ ] pkarr integration — self-sovereign DNS via Mainline DHT (15M nodes)
|
- [ ] pkarr integration — self-sovereign DNS via Mainline DHT (15M nodes)
|
||||||
- [ ] Global `.numa` names — self-publish, DHT-backed, first-come-first-served
|
- [ ] Global `.numa` names — self-publish, DHT-backed, first-come-first-served
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ api_port = 5380
|
|||||||
# api_bind_addr = "127.0.0.1" # default; set to "0.0.0.0" for LAN dashboard access
|
# api_bind_addr = "127.0.0.1" # default; set to "0.0.0.0" for LAN dashboard access
|
||||||
|
|
||||||
# [upstream]
|
# [upstream]
|
||||||
# address = "" # auto-detect from system resolver (default)
|
# address = "" # auto-detect from system resolver (default)
|
||||||
# address = "9.9.9.9" # or set explicitly
|
# address = "https://dns.quad9.net/dns-query" # DNS-over-HTTPS (encrypted)
|
||||||
# port = 53
|
# address = "https://cloudflare-dns.com/dns-query" # Cloudflare DoH
|
||||||
|
# address = "9.9.9.9" # plain UDP
|
||||||
|
# port = 53 # only used for plain UDP
|
||||||
# timeout_ms = 3000
|
# timeout_ms = 3000
|
||||||
|
|
||||||
# [blocking]
|
# [blocking]
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use axum::{Json, Router};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::ctx::ServerCtx;
|
use crate::ctx::ServerCtx;
|
||||||
use crate::forward::forward_query;
|
use crate::forward::{forward_query, Upstream};
|
||||||
use crate::query_log::QueryLogFilter;
|
use crate::query_log::QueryLogFilter;
|
||||||
use crate::question::QueryType;
|
use crate::question::QueryType;
|
||||||
use crate::stats::QueryPath;
|
use crate::stats::QueryPath;
|
||||||
@@ -355,9 +355,9 @@ async fn diagnose(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check upstream (async, no locks held)
|
// Check upstream (async, no locks held)
|
||||||
let upstream = *ctx.upstream.lock().unwrap();
|
let upstream = ctx.upstream.lock().unwrap().clone();
|
||||||
let (upstream_matched, upstream_detail) =
|
let (upstream_matched, upstream_detail) =
|
||||||
forward_query_for_diagnose(&domain_lower, upstream, ctx.timeout).await;
|
forward_query_for_diagnose(&domain_lower, &upstream, ctx.timeout).await;
|
||||||
steps.push(DiagnoseStep {
|
steps.push(DiagnoseStep {
|
||||||
source: "upstream".to_string(),
|
source: "upstream".to_string(),
|
||||||
matched: upstream_matched,
|
matched: upstream_matched,
|
||||||
@@ -373,7 +373,7 @@ async fn diagnose(
|
|||||||
|
|
||||||
async fn forward_query_for_diagnose(
|
async fn forward_query_for_diagnose(
|
||||||
domain: &str,
|
domain: &str,
|
||||||
upstream: std::net::SocketAddr,
|
upstream: &Upstream,
|
||||||
timeout: std::time::Duration,
|
timeout: std::time::Duration,
|
||||||
) -> (bool, String) {
|
) -> (bool, String) {
|
||||||
use crate::packet::DnsPacket;
|
use crate::packet::DnsPacket;
|
||||||
|
|||||||
@@ -21,6 +21,13 @@ impl BytePacketBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_bytes(data: &[u8]) -> Self {
|
||||||
|
let mut buf = Self::new();
|
||||||
|
let len = data.len().min(BUF_SIZE);
|
||||||
|
buf.buf[..len].copy_from_slice(&data[..len]);
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
|
||||||
pub fn pos(&self) -> usize {
|
pub fn pos(&self) -> usize {
|
||||||
self.pos
|
self.pos
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/ctx.rs
12
src/ctx.rs
@@ -12,7 +12,7 @@ use crate::blocklist::BlocklistStore;
|
|||||||
use crate::buffer::BytePacketBuffer;
|
use crate::buffer::BytePacketBuffer;
|
||||||
use crate::cache::DnsCache;
|
use crate::cache::DnsCache;
|
||||||
use crate::config::ZoneMap;
|
use crate::config::ZoneMap;
|
||||||
use crate::forward::forward_query;
|
use crate::forward::{forward_query, Upstream};
|
||||||
use crate::header::ResultCode;
|
use crate::header::ResultCode;
|
||||||
use crate::lan::PeerStore;
|
use crate::lan::PeerStore;
|
||||||
use crate::override_store::OverrideStore;
|
use crate::override_store::OverrideStore;
|
||||||
@@ -35,7 +35,7 @@ pub struct ServerCtx {
|
|||||||
pub services: Mutex<ServiceStore>,
|
pub services: Mutex<ServiceStore>,
|
||||||
pub lan_peers: Mutex<PeerStore>,
|
pub lan_peers: Mutex<PeerStore>,
|
||||||
pub forwarding_rules: Vec<ForwardingRule>,
|
pub forwarding_rules: Vec<ForwardingRule>,
|
||||||
pub upstream: Mutex<SocketAddr>,
|
pub upstream: Mutex<Upstream>,
|
||||||
pub upstream_auto: bool,
|
pub upstream_auto: bool,
|
||||||
pub upstream_port: u16,
|
pub upstream_port: u16,
|
||||||
pub lan_ip: Mutex<std::net::Ipv4Addr>,
|
pub lan_ip: Mutex<std::net::Ipv4Addr>,
|
||||||
@@ -143,9 +143,11 @@ pub async fn handle_query(
|
|||||||
(resp, QueryPath::Cached)
|
(resp, QueryPath::Cached)
|
||||||
} else {
|
} else {
|
||||||
let upstream =
|
let upstream =
|
||||||
crate::system_dns::match_forwarding_rule(&qname, &ctx.forwarding_rules)
|
match crate::system_dns::match_forwarding_rule(&qname, &ctx.forwarding_rules) {
|
||||||
.unwrap_or_else(|| *ctx.upstream.lock().unwrap());
|
Some(addr) => Upstream::Udp(addr),
|
||||||
match forward_query(&query, upstream, ctx.timeout).await {
|
None => ctx.upstream.lock().unwrap().clone(),
|
||||||
|
};
|
||||||
|
match forward_query(&query, &upstream, ctx.timeout).await {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
ctx.cache.lock().unwrap().insert(&qname, qtype, &resp);
|
ctx.cache.lock().unwrap().insert(&qname, qtype, &resp);
|
||||||
(resp, QueryPath::Forwarded)
|
(resp, QueryPath::Forwarded)
|
||||||
|
|||||||
211
src/forward.rs
211
src/forward.rs
@@ -1,3 +1,4 @@
|
|||||||
|
use std::fmt;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@@ -8,7 +9,46 @@ use crate::buffer::BytePacketBuffer;
|
|||||||
use crate::packet::DnsPacket;
|
use crate::packet::DnsPacket;
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum Upstream {
|
||||||
|
Udp(SocketAddr),
|
||||||
|
Doh {
|
||||||
|
url: String,
|
||||||
|
client: reqwest::Client,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Upstream {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
match (self, other) {
|
||||||
|
(Self::Udp(a), Self::Udp(b)) => a == b,
|
||||||
|
(Self::Doh { url: a, .. }, Self::Doh { url: b, .. }) => a == b,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Upstream {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Upstream::Udp(addr) => write!(f, "{}", addr),
|
||||||
|
Upstream::Doh { url, .. } => f.write_str(url),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn forward_query(
|
pub async fn forward_query(
|
||||||
|
query: &DnsPacket,
|
||||||
|
upstream: &Upstream,
|
||||||
|
timeout_duration: Duration,
|
||||||
|
) -> Result<DnsPacket> {
|
||||||
|
match upstream {
|
||||||
|
Upstream::Udp(addr) => forward_udp(query, *addr, timeout_duration).await,
|
||||||
|
Upstream::Doh { url, client } => forward_doh(query, url, client, timeout_duration).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn forward_udp(
|
||||||
query: &DnsPacket,
|
query: &DnsPacket,
|
||||||
upstream: SocketAddr,
|
upstream: SocketAddr,
|
||||||
timeout_duration: Duration,
|
timeout_duration: Duration,
|
||||||
@@ -33,3 +73,174 @@ pub async fn forward_query(
|
|||||||
|
|
||||||
DnsPacket::from_buffer(&mut recv_buffer)
|
DnsPacket::from_buffer(&mut recv_buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn forward_doh(
|
||||||
|
query: &DnsPacket,
|
||||||
|
url: &str,
|
||||||
|
client: &reqwest::Client,
|
||||||
|
timeout_duration: Duration,
|
||||||
|
) -> Result<DnsPacket> {
|
||||||
|
let mut send_buffer = BytePacketBuffer::new();
|
||||||
|
query.write(&mut send_buffer)?;
|
||||||
|
|
||||||
|
let resp = timeout(
|
||||||
|
timeout_duration,
|
||||||
|
client
|
||||||
|
.post(url)
|
||||||
|
.header("content-type", "application/dns-message")
|
||||||
|
.header("accept", "application/dns-message")
|
||||||
|
.body(send_buffer.filled().to_vec())
|
||||||
|
.send(),
|
||||||
|
)
|
||||||
|
.await??
|
||||||
|
.error_for_status()?;
|
||||||
|
|
||||||
|
let bytes = resp.bytes().await?;
|
||||||
|
log::debug!("DoH response: {} bytes", bytes.len());
|
||||||
|
|
||||||
|
let mut recv_buffer = BytePacketBuffer::from_bytes(&bytes);
|
||||||
|
DnsPacket::from_buffer(&mut recv_buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::future::IntoFuture;
|
||||||
|
|
||||||
|
use crate::header::ResultCode;
|
||||||
|
use crate::question::{DnsQuestion, QueryType};
|
||||||
|
use crate::record::DnsRecord;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn upstream_display_udp() {
|
||||||
|
let u = Upstream::Udp("9.9.9.9:53".parse().unwrap());
|
||||||
|
assert_eq!(u.to_string(), "9.9.9.9:53");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn upstream_display_doh() {
|
||||||
|
let u = Upstream::Doh {
|
||||||
|
url: "https://dns.quad9.net/dns-query".to_string(),
|
||||||
|
client: reqwest::Client::new(),
|
||||||
|
};
|
||||||
|
assert_eq!(u.to_string(), "https://dns.quad9.net/dns-query");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_query() -> DnsPacket {
|
||||||
|
let mut q = DnsPacket::new();
|
||||||
|
q.header.id = 0xABCD;
|
||||||
|
q.header.recursion_desired = true;
|
||||||
|
q.questions
|
||||||
|
.push(DnsQuestion::new("example.com".to_string(), QueryType::A));
|
||||||
|
q
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_response(query: &DnsPacket) -> DnsPacket {
|
||||||
|
let mut resp = DnsPacket::response_from(query, ResultCode::NOERROR);
|
||||||
|
resp.answers.push(DnsRecord::A {
|
||||||
|
domain: "example.com".to_string(),
|
||||||
|
addr: "93.184.216.34".parse().unwrap(),
|
||||||
|
ttl: 300,
|
||||||
|
});
|
||||||
|
resp
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_wire(pkt: &DnsPacket) -> Vec<u8> {
|
||||||
|
let mut buf = BytePacketBuffer::new();
|
||||||
|
pkt.write(&mut buf).unwrap();
|
||||||
|
buf.filled().to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn doh_mock_server_resolves() {
|
||||||
|
let query = make_query();
|
||||||
|
let response_bytes = to_wire(&make_response(&query));
|
||||||
|
|
||||||
|
let app = axum::Router::new().route(
|
||||||
|
"/dns-query",
|
||||||
|
axum::routing::post(move || {
|
||||||
|
let body = response_bytes.clone();
|
||||||
|
async move {
|
||||||
|
(
|
||||||
|
[(axum::http::header::CONTENT_TYPE, "application/dns-message")],
|
||||||
|
body,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let addr = listener.local_addr().unwrap();
|
||||||
|
tokio::spawn(axum::serve(listener, app).into_future());
|
||||||
|
|
||||||
|
let upstream = Upstream::Doh {
|
||||||
|
url: format!("http://{}/dns-query", addr),
|
||||||
|
client: reqwest::Client::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = forward_query(&query, &upstream, Duration::from_secs(2))
|
||||||
|
.await
|
||||||
|
.expect("DoH forward should succeed");
|
||||||
|
|
||||||
|
assert_eq!(result.header.id, 0xABCD);
|
||||||
|
assert!(result.header.response);
|
||||||
|
assert_eq!(result.header.rescode, ResultCode::NOERROR);
|
||||||
|
assert_eq!(result.answers.len(), 1);
|
||||||
|
match &result.answers[0] {
|
||||||
|
DnsRecord::A { domain, addr, ttl } => {
|
||||||
|
assert_eq!(domain, "example.com");
|
||||||
|
assert_eq!(
|
||||||
|
*addr,
|
||||||
|
"93.184.216.34".parse::<std::net::Ipv4Addr>().unwrap()
|
||||||
|
);
|
||||||
|
assert_eq!(*ttl, 300);
|
||||||
|
}
|
||||||
|
other => panic!("expected A record, got {:?}", other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn doh_http_error_propagates() {
|
||||||
|
let app = axum::Router::new().route(
|
||||||
|
"/dns-query",
|
||||||
|
axum::routing::post(|| async {
|
||||||
|
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, "bad")
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let addr = listener.local_addr().unwrap();
|
||||||
|
tokio::spawn(axum::serve(listener, app).into_future());
|
||||||
|
|
||||||
|
let upstream = Upstream::Doh {
|
||||||
|
url: format!("http://{}/dns-query", addr),
|
||||||
|
client: reqwest::Client::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = forward_query(&make_query(), &upstream, Duration::from_secs(2)).await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn doh_timeout() {
|
||||||
|
let app = axum::Router::new().route(
|
||||||
|
"/dns-query",
|
||||||
|
axum::routing::post(|| async {
|
||||||
|
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||||
|
"never"
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
|
let addr = listener.local_addr().unwrap();
|
||||||
|
tokio::spawn(axum::serve(listener, app).into_future());
|
||||||
|
|
||||||
|
let upstream = Upstream::Doh {
|
||||||
|
url: format!("http://{}/dns-query", addr),
|
||||||
|
client: reqwest::Client::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = forward_query(&make_query(), &upstream, Duration::from_millis(100)).await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
43
src/main.rs
43
src/main.rs
@@ -11,6 +11,7 @@ use numa::buffer::BytePacketBuffer;
|
|||||||
use numa::cache::DnsCache;
|
use numa::cache::DnsCache;
|
||||||
use numa::config::{build_zone_map, load_config, ConfigLoad};
|
use numa::config::{build_zone_map, load_config, ConfigLoad};
|
||||||
use numa::ctx::{handle_query, ServerCtx};
|
use numa::ctx::{handle_query, ServerCtx};
|
||||||
|
use numa::forward::Upstream;
|
||||||
use numa::override_store::OverrideStore;
|
use numa::override_store::OverrideStore;
|
||||||
use numa::query_log::QueryLog;
|
use numa::query_log::QueryLog;
|
||||||
use numa::service_store::ServiceStore;
|
use numa::service_store::ServiceStore;
|
||||||
@@ -111,13 +112,27 @@ async fn main() -> numa::Result<()> {
|
|||||||
.default_upstream
|
.default_upstream
|
||||||
.or_else(numa::system_dns::detect_dhcp_dns)
|
.or_else(numa::system_dns::detect_dhcp_dns)
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
info!("could not detect system DNS, falling back to 9.9.9.9 (Quad9)");
|
info!("could not detect system DNS, falling back to Quad9 DoH");
|
||||||
"9.9.9.9".to_string()
|
"https://dns.quad9.net/dns-query".to_string()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
config.upstream.address.clone()
|
config.upstream.address.clone()
|
||||||
};
|
};
|
||||||
let upstream: SocketAddr = format!("{}:{}", upstream_addr, config.upstream.port).parse()?;
|
|
||||||
|
let upstream: Upstream = if upstream_addr.starts_with("https://") {
|
||||||
|
let client = reqwest::Client::builder()
|
||||||
|
.use_rustls_tls()
|
||||||
|
.build()
|
||||||
|
.unwrap_or_default();
|
||||||
|
Upstream::Doh {
|
||||||
|
url: upstream_addr,
|
||||||
|
client,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let addr: SocketAddr = format!("{}:{}", upstream_addr, config.upstream.port).parse()?;
|
||||||
|
Upstream::Udp(addr)
|
||||||
|
};
|
||||||
|
let upstream_label = upstream.to_string();
|
||||||
let api_port = config.server.api_port;
|
let api_port = config.server.api_port;
|
||||||
|
|
||||||
let mut blocklist = BlocklistStore::new();
|
let mut blocklist = BlocklistStore::new();
|
||||||
@@ -217,7 +232,7 @@ async fn main() -> numa::Result<()> {
|
|||||||
let val_w = [
|
let val_w = [
|
||||||
config.server.bind_addr.len(),
|
config.server.bind_addr.len(),
|
||||||
api_url.len(),
|
api_url.len(),
|
||||||
upstream.to_string().len(),
|
upstream_label.len(),
|
||||||
config_label.len(),
|
config_label.len(),
|
||||||
data_label.len(),
|
data_label.len(),
|
||||||
services_label.len(),
|
services_label.len(),
|
||||||
@@ -261,7 +276,7 @@ async fn main() -> numa::Result<()> {
|
|||||||
row("DNS", g, &config.server.bind_addr);
|
row("DNS", g, &config.server.bind_addr);
|
||||||
row("API", g, &api_url);
|
row("API", g, &api_url);
|
||||||
row("Dashboard", g, &api_url);
|
row("Dashboard", g, &api_url);
|
||||||
row("Upstream", g, &upstream.to_string());
|
row("Upstream", g, &upstream_label);
|
||||||
row("Zones", g, &format!("{} records", zone_count));
|
row("Zones", g, &format!("{} records", zone_count));
|
||||||
row(
|
row(
|
||||||
"Cache",
|
"Cache",
|
||||||
@@ -298,7 +313,7 @@ async fn main() -> numa::Result<()> {
|
|||||||
|
|
||||||
info!(
|
info!(
|
||||||
"numa listening on {}, upstream {}, {} zone records, cache max {}, API on port {}",
|
"numa listening on {}, upstream {}, {} zone records, cache max {}, API on port {}",
|
||||||
config.server.bind_addr, upstream, zone_count, config.cache.max_entries, api_port,
|
config.server.bind_addr, upstream_label, zone_count, config.cache.max_entries, api_port,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Download blocklists on startup
|
// Download blocklists on startup
|
||||||
@@ -413,20 +428,24 @@ async fn network_watch_loop(ctx: Arc<numa::ctx::ServerCtx>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check upstream change every 30s or immediately on LAN IP change
|
// Re-detect upstream every 30s or on LAN IP change (UDP only —
|
||||||
// (heavier — spawns scutil/ipconfig, only when auto-detected)
|
// DoH upstreams are explicitly configured via URL, not auto-detected)
|
||||||
if ctx.upstream_auto && (changed || tick.is_multiple_of(6)) {
|
if ctx.upstream_auto
|
||||||
|
&& matches!(*ctx.upstream.lock().unwrap(), Upstream::Udp(_))
|
||||||
|
&& (changed || tick.is_multiple_of(6))
|
||||||
|
{
|
||||||
let dns_info = numa::system_dns::discover_system_dns();
|
let dns_info = numa::system_dns::discover_system_dns();
|
||||||
let new_addr = dns_info
|
let new_addr = dns_info
|
||||||
.default_upstream
|
.default_upstream
|
||||||
.or_else(numa::system_dns::detect_dhcp_dns)
|
.or_else(numa::system_dns::detect_dhcp_dns)
|
||||||
.unwrap_or_else(|| "9.9.9.9".to_string());
|
.unwrap_or_else(|| "9.9.9.9".to_string());
|
||||||
if let Ok(new_upstream) =
|
if let Ok(new_sock) =
|
||||||
format!("{}:{}", new_addr, ctx.upstream_port).parse::<SocketAddr>()
|
format!("{}:{}", new_addr, ctx.upstream_port).parse::<SocketAddr>()
|
||||||
{
|
{
|
||||||
|
let new_upstream = Upstream::Udp(new_sock);
|
||||||
let mut upstream = ctx.upstream.lock().unwrap();
|
let mut upstream = ctx.upstream.lock().unwrap();
|
||||||
if new_upstream != *upstream {
|
if *upstream != new_upstream {
|
||||||
info!("upstream changed: {} → {}", *upstream, new_upstream);
|
info!("upstream changed: {} → {}", upstream, new_upstream);
|
||||||
*upstream = new_upstream;
|
*upstream = new_upstream;
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user