Skip to content

Commit

Permalink
feat: expose the dns module as part of the library (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Nov 3, 2023
1 parent 69c78c0 commit 8164d19
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 19 deletions.
11 changes: 9 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use binding::{TuiBindings, TuiKeyBinding};
pub use cmd::Args;
pub use constants::MAX_HOPS;
pub use theme::{TuiColor, TuiTheme};
use trippy::dns::DnsResolveMethod;

/// The tool mode.
#[derive(Debug, Copy, Clone, ValueEnum, Deserialize)]
Expand Down Expand Up @@ -144,7 +145,7 @@ pub enum GeoIpMode {
/// How DNS queries will be resolved.
#[derive(Debug, Copy, Clone, ValueEnum, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DnsResolveMethod {
pub enum DnsResolveMethodConfig {
/// Resolve using the OS resolver.
System,
/// Resolve using the `/etc/resolv.conf` DNS configuration.
Expand Down Expand Up @@ -399,7 +400,7 @@ impl TryFrom<(Args, &Platform)> for TrippyConfig {
constants::DEFAULT_TUI_GEOIP_MODE,
);
let tui_max_addrs = cfg_layer_opt(args.tui_max_addrs, cfg_file_tui.tui_max_addrs);
let dns_resolve_method = cfg_layer(
let dns_resolve_method_config = cfg_layer(
args.dns_resolve_method,
cfg_file_dns.dns_resolve_method,
constants::DEFAULT_DNS_RESOLVE_METHOD,
Expand Down Expand Up @@ -478,6 +479,12 @@ impl TryFrom<(Args, &Platform)> for TrippyConfig {
}
};
let tui_refresh_rate = humantime::parse_duration(&tui_refresh_rate)?;
let dns_resolve_method = match dns_resolve_method_config {
DnsResolveMethodConfig::System => DnsResolveMethod::System,
DnsResolveMethodConfig::Resolv => DnsResolveMethod::Resolv,
DnsResolveMethodConfig::Google => DnsResolveMethod::Google,
DnsResolveMethodConfig::Cloudflare => DnsResolveMethod::Cloudflare,
};
let dns_timeout = humantime::parse_duration(&dns_timeout)?;
let max_rounds = match mode {
Mode::Stream | Mode::Tui => None,
Expand Down
4 changes: 2 additions & 2 deletions src/config/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::binding::TuiCommandItem;
use crate::config::theme::TuiThemeItem;
use crate::config::{
AddressMode, AsMode, DnsResolveMethod, GeoIpMode, LogFormat, LogSpanEvents, Mode,
AddressMode, AsMode, DnsResolveMethodConfig, GeoIpMode, LogFormat, LogSpanEvents, Mode,
MultipathStrategyConfig, Protocol, TuiColor, TuiKeyBinding,
};
use anyhow::anyhow;
Expand Down Expand Up @@ -116,7 +116,7 @@ pub struct Args {

/// How to perform DNS queries [default: system]
#[arg(value_enum, short = 'r', long)]
pub dns_resolve_method: Option<DnsResolveMethod>,
pub dns_resolve_method: Option<DnsResolveMethodConfig>,

/// The maximum time to wait to perform DNS queries [default: 5s]
#[arg(long)]
Expand Down
4 changes: 2 additions & 2 deletions src/config/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::{
AddressMode, AsMode, DnsResolveMethod, GeoIpMode, LogFormat, LogSpanEvents, Mode,
AddressMode, AsMode, DnsResolveMethodConfig, GeoIpMode, LogFormat, LogSpanEvents, Mode,
MultipathStrategyConfig, Protocol,
};
use std::time::Duration;
Expand Down Expand Up @@ -83,7 +83,7 @@ pub const DEFAULT_TUI_ADDRESS_MODE: AddressMode = AddressMode::Host;
pub const DEFAULT_TUI_REFRESH_RATE: &str = "100ms";

/// The default value for `dns-resolve-method`.
pub const DEFAULT_DNS_RESOLVE_METHOD: DnsResolveMethod = DnsResolveMethod::System;
pub const DEFAULT_DNS_RESOLVE_METHOD: DnsResolveMethodConfig = DnsResolveMethodConfig::System;

/// The default value for `dns-lookup-as-info`.
pub const DEFAULT_DNS_LOOKUP_AS_INFO: bool = false;
Expand Down
6 changes: 3 additions & 3 deletions src/config/file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::config::binding::TuiKeyBinding;
use crate::config::theme::TuiColor;
use crate::config::{
AddressFamily, AddressMode, AsMode, DnsResolveMethod, GeoIpMode, LogFormat, LogSpanEvents,
Mode, MultipathStrategyConfig, Protocol,
AddressFamily, AddressMode, AsMode, DnsResolveMethodConfig, GeoIpMode, LogFormat,
LogSpanEvents, Mode, MultipathStrategyConfig, Protocol,
};
use anyhow::Context;
use etcetera::BaseStrategy;
Expand Down Expand Up @@ -117,7 +117,7 @@ pub struct ConfigStrategy {
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ConfigDns {
pub dns_resolve_method: Option<DnsResolveMethod>,
pub dns_resolve_method: Option<DnsResolveMethodConfig>,
pub dns_lookup_as_info: Option<bool>,
pub dns_timeout: Option<String>,
}
Expand Down
21 changes: 20 additions & 1 deletion src/dns.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
use crate::config::DnsResolveMethod;
use crate::dns::inner::DnsResolverInner;
use std::fmt::{Display, Formatter};
use std::net::IpAddr;
use std::rc::Rc;
use std::time::Duration;

/// How DNS queries will be resolved.
#[derive(Debug, Copy, Clone)]
pub enum DnsResolveMethod {
/// Resolve using the OS resolver.
System,
/// Resolve using the `/etc/resolv.conf` DNS configuration.
Resolv,
/// Resolve using the Google `8.8.8.8` DNS service.
Google,
/// Resolve using the Cloudflare `1.1.1.1` DNS service.
Cloudflare,
}

/// The state of reverse DNS resolution.
#[derive(Debug, Clone)]
pub enum DnsEntry {
Expand Down Expand Up @@ -84,6 +96,7 @@ pub struct DnsResolverConfig {
}

impl DnsResolverConfig {
#[must_use]
pub fn new_ipv4(resolve_method: DnsResolveMethod, timeout: Duration) -> Self {
Self {
resolve_method,
Expand All @@ -92,6 +105,7 @@ impl DnsResolverConfig {
}
}

#[must_use]
pub fn new_ipv6(resolve_method: DnsResolveMethod, timeout: Duration) -> Self {
Self {
resolve_method,
Expand Down Expand Up @@ -122,6 +136,7 @@ impl DnsResolver {
/// Perform a blocking reverse DNS lookup of `IpAddr` and return a `DnsEntry`.
///
/// As this method is blocking it will never return a `DnsEntry::Pending`.
#[must_use]
pub fn reverse_lookup(&self, addr: IpAddr) -> DnsEntry {
self.inner.reverse_lookup(addr, false, false)
}
Expand All @@ -131,6 +146,7 @@ impl DnsResolver {
///
/// See [`DnsResolver::reverse_lookup`]
#[allow(dead_code)]
#[must_use]
pub fn reverse_lookup_with_asinfo(&self, addr: IpAddr) -> DnsEntry {
self.inner.reverse_lookup(addr, true, false)
}
Expand All @@ -146,18 +162,21 @@ impl DnsResolver {
/// and enqueued.
///
/// If enqueuing times out then the entry is changed to be `DnsEntry::Timeout` and returned.
#[must_use]
pub fn lazy_reverse_lookup(&self, addr: IpAddr) -> DnsEntry {
self.inner.reverse_lookup(addr, false, true)
}

/// Perform a lazy reverse DNS lookup of `IpAddr` and return a `DnsEntry` with `AS` information.
///
/// See [`DnsResolver::lazy_reverse_lookup`]
#[must_use]
pub fn lazy_reverse_lookup_with_asinfo(&self, addr: IpAddr) -> DnsEntry {
self.inner.reverse_lookup(addr, true, true)
}

/// Get the `DnsResolverConfig`.
#[must_use]
pub fn config(&self) -> &DnsResolverConfig {
self.inner.config()
}
Expand Down
5 changes: 2 additions & 3 deletions src/frontend/render/header.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::time::Duration;

use crate::config::DnsResolveMethod;
use crate::frontend::tui_app::TuiApp;
use chrono::SecondsFormat;
use humantime::format_duration;
Expand All @@ -9,6 +6,8 @@ use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Paragraph};
use ratatui::Frame;
use std::time::Duration;
use trippy::dns::DnsResolveMethod;
use trippy::tracing::{PortDirection, TracerProtocol};

/// Render the title, config, target, clock and keyboard controls.
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/render/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::{AddressMode, AsMode, DnsResolveMethod, GeoIpMode};
use crate::config::{AddressMode, AsMode, GeoIpMode};
use crate::frontend::render::util;
use crate::frontend::theme;
use crate::frontend::tui_app::TuiApp;
Expand All @@ -8,6 +8,7 @@ use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Cell, Clear, Paragraph, Row, Table, Tabs};
use ratatui::Frame;
use trippy::dns::DnsResolveMethod;
use trippy::tracing::PortDirection;

/// Render settings dialog.
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/render/table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::backend::Hop;
use crate::config::{AddressMode, AsMode, GeoIpMode};
use crate::dns::{AsInfo, DnsEntry, DnsResolver, Resolved, Unresolved};
use crate::frontend::config::TuiConfig;
use crate::frontend::theme::Theme;
use crate::frontend::tui_app::TuiApp;
Expand All @@ -12,6 +11,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Cell, Row, Table};
use ratatui::Frame;
use std::net::IpAddr;
use std::rc::Rc;
use trippy::dns::{AsInfo, DnsEntry, DnsResolver, Resolved, Unresolved};

/// Render the table of data about the hops.
///
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/tui_app.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::backend::{Hop, Trace};
use crate::config::DnsResolveMethod;
use crate::dns::DnsResolver;
use crate::frontend::config::TuiConfig;
use crate::frontend::render::settings::SETTINGS_TABS;
use crate::geoip::GeoIpLookup;
use crate::TraceInfo;
use ratatui::widgets::TableState;
use std::time::SystemTime;
use trippy::dns::{DnsResolveMethod, DnsResolver};

pub struct TuiApp {
pub selected_tracer_data: Trace,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
#![deny(unsafe_code)]

pub mod tracing;

pub mod dns;
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use crate::backend::Trace;
use crate::config::{LogFormat, LogSpanEvents, Mode, TrippyConfig};
use crate::dns::{DnsResolver, DnsResolverConfig};
use crate::geoip::GeoIpLookup;
use crate::platform::Platform;
use anyhow::{anyhow, Error};
Expand All @@ -31,6 +30,7 @@ use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use trippy::dns::{DnsResolver, DnsResolverConfig};
use trippy::tracing::{
MultipathStrategy, PortDirection, TracerAddrFamily, TracerChannelConfig, TracerConfig,
TracerProtocol,
Expand All @@ -39,7 +39,6 @@ use trippy::tracing::{PrivilegeMode, SourceAddr};

mod backend;
mod config;
mod dns;
mod frontend;
mod geoip;
mod platform;
Expand Down

0 comments on commit 8164d19

Please sign in to comment.