Skip to content

Commit

Permalink
chore: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Oct 27, 2023
1 parent c297fbc commit 135a0e3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
12 changes: 11 additions & 1 deletion dap-lib/src/doh_client/dns_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryKey>);
impl TryFrom<&Message> for Request {
type Error = anyhow::Error;
Expand All @@ -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<Message> {
is(packet_buf, MessageType::Query)
}

/// Check if the message is a DNS response
pub fn is_response(packet_buf: &[u8]) -> anyhow::Result<Message> {
is(packet_buf, MessageType::Response)
}

/// Check if the message is a DNS query or response
fn is(packet_buf: &[u8], mtype: MessageType) -> anyhow::Result<Message> {
let msg = decode(packet_buf)?;
if msg.message_type() == mtype {
Expand All @@ -70,16 +75,19 @@ fn is(packet_buf: &[u8], mtype: MessageType) -> anyhow::Result<Message> {
}
}

/// Decode a DNS message
pub fn decode(packet_buf: &[u8]) -> anyhow::Result<Message> {
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<Vec<u8>> {
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<Message> {
let qname: Name = Name::from_ascii(fqdn).unwrap();
let mut query = Query::query(qname, RecordType::A);
Expand All @@ -105,6 +113,7 @@ pub fn build_query_a(fqdn: &str) -> anyhow::Result<Message> {
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);
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion dap-lib/src/doh_client/doh_client_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl DoHClient {
}
}

// TODO: implement ResolveIps for DoHClient
// ResolveIps for DoHClient
#[async_trait]
impl ResolveIps for Arc<DoHClient> {
/// Resolve ip addresses of the given domain name
Expand Down
13 changes: 5 additions & 8 deletions dap-lib/src/globals.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<HttpClient>,
/// proxy configuration
pub proxy_config: ProxyConfig,

Expand Down Expand Up @@ -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<NextHopRelayConfig>,

// modoh
/// modoh relay settings
pub subseq_relay_config: Option<SubseqRelayConfig>,

// authentication
/// authentication settings
pub authentication_config: Option<AuthenticationConfig>,
// pub query_plugins: Option<QueryPluginsApplied>,
// pub min_ttl: u32, // TTL of overridden response
// pub credential: Arc<RwLock<Option<Credential>>>,
}

#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down

0 comments on commit 135a0e3

Please sign in to comment.