diff --git a/dap-lib/src/doh_client/dns_message.rs b/dap-lib/src/doh_client/dns_message.rs index 211b616..30e6ed8 100644 --- a/dap-lib/src/doh_client/dns_message.rs +++ b/dap-lib/src/doh_client/dns_message.rs @@ -12,14 +12,16 @@ use hickory_proto::{ }; use std::{net::IpAddr, str::FromStr}; -// https://github.com/aaronriekenberg/rust-doh-proxy/blob/master/src/doh/request_key.rs #[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)] +/// QueryKey is a tuple of query name, query type and query class +/// https://github.com/aaronriekenberg/rust-doh-proxy/blob/master/src/doh/request_key.rs pub struct QueryKey { pub query_name: String, pub query_type: RecordType, pub query_class: DNSClass, } #[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)] +/// Request is a sorted list of QueryKey pub struct Request(pub Vec); impl TryFrom<&Message> for Request { type Error = anyhow::Error; @@ -46,14 +48,17 @@ impl TryFrom<&Message> for Request { } } +/// Check if the message is a DNS query pub fn is_query(packet_buf: &[u8]) -> anyhow::Result { is(packet_buf, MessageType::Query) } +/// Check if the message is a DNS response pub fn is_response(packet_buf: &[u8]) -> anyhow::Result { is(packet_buf, MessageType::Response) } +/// Check if the message is a DNS query or response fn is(packet_buf: &[u8], mtype: MessageType) -> anyhow::Result { let msg = decode(packet_buf)?; if msg.message_type() == mtype { @@ -70,16 +75,19 @@ fn is(packet_buf: &[u8], mtype: MessageType) -> anyhow::Result { } } +/// Decode a DNS message pub fn decode(packet_buf: &[u8]) -> anyhow::Result { Message::from_bytes(packet_buf).map_err(|e| anyhow!("Undecodable packet buffer as DNS message: {}", e)) } +/// Encode a DNS message pub fn encode(msg: &Message) -> anyhow::Result> { msg .to_bytes() .map_err(|e| anyhow!("Failed to encode DNS message: {}", e)) } +/// Build a DNS query message for A record pub fn build_query_a(fqdn: &str) -> anyhow::Result { let qname: Name = Name::from_ascii(fqdn).unwrap(); let mut query = Query::query(qname, RecordType::A); @@ -105,6 +113,7 @@ pub fn build_query_a(fqdn: &str) -> anyhow::Result { Ok(msg) } +/// Build a DNS response message with NXDOMAIN pub fn build_response_nx(msg: &Message) -> Message { let mut res = msg.clone(); res.set_message_type(hickory_proto::op::MessageType::Response); @@ -113,6 +122,7 @@ pub fn build_response_nx(msg: &Message) -> Message { res } +/// Build a DNS response message for given QueryKey and IP address pub fn build_response_given_ipaddr( msg: &Message, q_key: &QueryKey, diff --git a/dap-lib/src/doh_client/doh_client_main.rs b/dap-lib/src/doh_client/doh_client_main.rs index 633fb2d..6e933cd 100644 --- a/dap-lib/src/doh_client/doh_client_main.rs +++ b/dap-lib/src/doh_client/doh_client_main.rs @@ -301,7 +301,7 @@ impl DoHClient { } } -// TODO: implement ResolveIps for DoHClient +// ResolveIps for DoHClient #[async_trait] impl ResolveIps for Arc { /// Resolve ip addresses of the given domain name diff --git a/dap-lib/src/globals.rs b/dap-lib/src/globals.rs index ad51b2a..ba5b103 100644 --- a/dap-lib/src/globals.rs +++ b/dap-lib/src/globals.rs @@ -1,4 +1,4 @@ -use crate::{constants::*, doh_client::DoHMethod, http_client::HttpClient}; +use crate::{constants::*, doh_client::DoHMethod}; use auth_client::AuthenticationConfig; use std::{ net::{IpAddr, SocketAddr}, @@ -10,8 +10,6 @@ use url::Url; #[derive(Debug)] /// Global objects containing shared resources pub struct Globals { - // /// HTTP client shared by DoH client and authentication client, etc. - // pub http_client: Arc, /// proxy configuration pub proxy_config: ProxyConfig, @@ -42,20 +40,19 @@ pub struct ProxyConfig { /// timeout for HTTP requests (DoH, ODoH, and authentication requests) pub http_timeout_sec: Duration, - // doh, odoh, modoh target settings + /// doh, odoh, modoh target settings pub target_config: TargetConfig, - // odoh and modoh nexthop + /// odoh and modoh nexthop settings pub nexthop_relay_config: Option, - // modoh + /// modoh relay settings pub subseq_relay_config: Option, - // authentication + /// authentication settings pub authentication_config: Option, // pub query_plugins: Option, // pub min_ttl: u32, // TTL of overridden response - // pub credential: Arc>>, } #[derive(PartialEq, Eq, Debug, Clone)]