Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add support for querying PTR from hosts file #335

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ tracing-subscriber = { version = "0.3", features = [
# tracing-appender = "0.2"

# hickory dns
hickory-proto = { git = "https://github.com/mokeyish/hickory-dns.git", rev = "bbf1262", version = "0.24", features = ["serde-config"]}
hickory-resolver = { git = "https://github.com/mokeyish/hickory-dns.git", rev = "bbf1262", version = "0.24", features = [
hickory-proto = { git = "https://github.com/mokeyish/hickory-dns.git", rev = "0.25.0-smartdns.2", version = "0.25.0-alpha.1", features = ["serde-config"]}
hickory-resolver = { git = "https://github.com/mokeyish/hickory-dns.git", rev = "0.25.0-smartdns.2", version = "0.25.0-alpha.1", features = [
"serde-config",
"system-config",
] }
hickory-server = { git = "https://github.com/mokeyish/hickory-dns.git", rev = "bbf1262", version = "0.24", features = ["resolver"], optional = true }
hickory-server = { git = "https://github.com/mokeyish/hickory-dns.git", rev = "0.25.0-smartdns.2", version = "0.25.0-alpha.1", features = ["resolver"], optional = true }

# ssl
webpki-roots = "0.25.2"
Expand All @@ -155,6 +155,7 @@ hostname = "0.3"
byte-unit = { version = "5.0.3", features = ["serde"]}
ipnet = "2.7"
which = { version = "6.0.1", optional = true }
glob = "0.3.1"

# process
sysinfo = "0.29"
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn main() -> anyhow::Result<()> {

println!(
"cargo:rustc-env=CARGO_BUILD_TARGET={}",
std::env::var("TARGET").unwrap()
env::var("TARGET").unwrap()
);
Ok(())
}
4 changes: 3 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ pub struct Config {
/// whether resolv local hostname to ip address
pub resolv_hostname: Option<bool>,

pub hosts_file: Option<PathBuf>,
pub hosts_file: Option<glob::Pattern>,

pub expand_ptr_from_address: Option<bool>,

/// dns server run user
///
Expand Down
58 changes: 58 additions & 0 deletions src/config/parser/glob_pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use glob::Pattern;

use super::*;

impl NomParser for Pattern {
fn parse(input: &str) -> IResult<&str, Self> {
let delimited_path = delimited(char('"'), is_not("\""), char('"'));
let unix_path = recognize(tuple((
opt(char('/')),
separated_list1(char('/'), escaped(is_not("\n \t\\"), '\\', one_of(r#" \"#))),
opt(char('/')),
)));
let windows_path = recognize(tuple((
opt(pair(alpha1, tag(":\\"))),
separated_list1(char('\\'), is_not("\\")),
opt(char('\\')),
)));
map_res(
alt((delimited_path, unix_path, windows_path)),
FromStr::from_str,
)(input)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse() {
assert_eq!(Pattern::parse("a*"), Ok(("", "a*".parse().unwrap())));
assert_eq!(Pattern::parse("/"), Ok(("", "/".parse().unwrap())));
assert_eq!(
Pattern::parse("a/b😁/c"),
Ok(("", "a/b😁/c".parse().unwrap()))
);
assert_eq!(
Pattern::parse("a/ b/c"),
Ok((" b/c", "a/".parse().unwrap()))
);
assert_eq!(
Pattern::parse("/a/b/c"),
Ok(("", "/a/b/c".parse().unwrap()))
);
assert_eq!(
Pattern::parse("/a/b/c/"),
Ok(("", "/a/b/c/".parse().unwrap()))
);
assert_eq!(
Pattern::parse("a/b/c*/"),
Ok(("", "a/b/c*/".parse().unwrap()))
);
assert_eq!(
Pattern::parse("**/*.rs"),
Ok(("", "**/*.rs".parse().unwrap()))
);
}
}
13 changes: 10 additions & 3 deletions src/config/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod domain_rule;
mod domain_set;
mod file_mode;
mod forward_rule;
mod glob_pattern;
mod ipnet;
mod listener;
mod log_level;
Expand Down Expand Up @@ -96,10 +97,11 @@ pub enum OneConfig {
DualstackIpSelection(bool),
DualstackIpSelectionThreshold(u16),
EdnsClientSubnet(IpNet),
ExpandPtrFromAddress(bool),
ForceAAAASOA(bool),
ForceQtypeSoa(RecordType),
ForwardRule(ForwardRule),
HostsFile(PathBuf),
HostsFile(glob::Pattern),
IgnoreIp(IpNet),
Listener(ListenerConfig),
LocalTtl(u64),
Expand Down Expand Up @@ -182,6 +184,10 @@ pub fn parse_config(input: &str) -> IResult<&str, OneConfig> {
parse_item("edns-client-subnet"),
OneConfig::EdnsClientSubnet,
),
map(
parse_item("expand-ptr-from-address"),
OneConfig::ExpandPtrFromAddress,
),
map(parse_item("force-AAAA-SOA"), OneConfig::ForceAAAASOA),
map(parse_item("force-qtype-soa"), OneConfig::ForceQtypeSoa),
map(parse_item("response"), OneConfig::ResponseMode),
Expand All @@ -199,17 +205,18 @@ pub fn parse_config(input: &str) -> IResult<&str, OneConfig> {
map(parse_item("log-num"), OneConfig::LogNum),
map(parse_item("log-size"), OneConfig::LogSize),
map(parse_item("max-reply-ip-num"), OneConfig::MaxReplyIpNum),
map(parse_item("mdns-lookup"), OneConfig::MdnsLookup),
map(parse_item("nameserver"), OneConfig::ForwardRule),
));

let group3 = alt((
map(parse_item("mdns-lookup"), OneConfig::MdnsLookup),
map(parse_item("nameserver"), OneConfig::ForwardRule),
map(parse_item("proxy-server"), OneConfig::ProxyConfig),
map(parse_item("rr-ttl-reply-max"), OneConfig::RrTtlReplyMax),
map(parse_item("rr-ttl-min"), OneConfig::RrTtlMin),
map(parse_item("rr-ttl-max"), OneConfig::RrTtlMax),
map(parse_item("rr-ttl"), OneConfig::RrTtl),
map(parse_item("resolv-file"), OneConfig::ResolvFile),
map(parse_item("resolv-hostanme"), OneConfig::ResolvHostname),
map(parse_item("response-mode"), OneConfig::ResponseMode),
map(parse_item("server-name"), OneConfig::ServerName),
map(parse_item("speed-check-mode"), OneConfig::SpeedMode),
Expand Down
17 changes: 12 additions & 5 deletions src/dns_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,14 @@ impl RuntimeConfig {

/// hosts file path
#[inline]
pub fn hosts_file(&self) -> Option<&Path> {
self.hosts_file.as_deref()
pub fn hosts_file(&self) -> Option<&glob::Pattern> {
self.hosts_file.as_ref()
}

/// Whether to expand the address record corresponding to PTR record
#[inline]
pub fn expand_ptr_from_address(&self) -> bool {
self.expand_ptr_from_address.unwrap_or_default()
}

/// whether resolv mdns
Expand Down Expand Up @@ -775,6 +781,7 @@ impl RuntimeConfigBuilder {
CacheFile(v) => self.cache.file = Some(v),
CachePersist(v) => self.cache.persist = Some(v),
CName(v) => self.cnames.push(v),
ExpandPtrFromAddress(v) => self.expand_ptr_from_address = Some(v),
NftSet(config) => self.nftsets.push(config),
Server(server) => self.nameservers.push(server),
ResponseMode(mode) => self.response_mode = Some(mode),
Expand Down Expand Up @@ -1116,7 +1123,7 @@ mod tests {
#[test]
fn test_config_domain_rules_without_args() {
let mut cfg = RuntimeConfig::builder();
cfg.config("domain-set -name domain-forwarding-list -file tests/test_confs/block-list.txt");
cfg.config("domain-set -name domain-forwarding-list -file tests/test_data/block-list.txt");
cfg.config("domain-rules /domain-set:domain-forwarding-list/");
assert!(cfg.address_rules.last().is_none());
}
Expand Down Expand Up @@ -1435,7 +1442,7 @@ mod tests {

#[test]
fn test_parse_load_config_file_b() {
let cfg = RuntimeConfig::load_from_file("tests/test_confs/b_main.conf");
let cfg = RuntimeConfig::load_from_file("tests/test_data/b_main.conf");

assert_eq!(cfg.server_name, "SmartDNS123".parse().ok());
assert_eq!(
Expand All @@ -1459,7 +1466,7 @@ mod tests {
#[test]
#[cfg(failed_tests)]
fn test_domain_set() {
let cfg = RuntimeConfig::load_from_file("tests/test_confs/b_main.conf");
let cfg = RuntimeConfig::load_from_file("tests/test_data/b_main.conf");

assert!(!cfg.domain_sets.is_empty());

Expand Down
Loading