From 9b383bfecbba5dfedbcc3a5e2ad74c3e5ddd8b00 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Sat, 14 Mar 2020 22:26:55 +0800 Subject: [PATCH] Resolve hosts to IPv6 address first ref #205 --- src/bin/local.rs | 11 +++++++++++ src/bin/manager.rs | 11 +++++++++++ src/bin/redir.rs | 11 +++++++++++ src/bin/server.rs | 11 +++++++++++ src/bin/tunnel.rs | 11 +++++++++++ src/config.rs | 15 +++++++++++++++ src/context.rs | 2 +- src/relay/dns_resolver/trust_dns_resolver.rs | 7 ++++++- 8 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/bin/local.rs b/src/bin/local.rs index d9577fda..26fdea53 100644 --- a/src/bin/local.rs +++ b/src/bin/local.rs @@ -144,6 +144,11 @@ fn main() { .long("acl") .takes_value(true) .help("Path to ACL (Access Control List)"), + ) + .arg( + Arg::with_name("IPV6_FIRST") + .short("6") + .help("Resovle hostname to IPv6 address first"), ); if cfg!(target_os = "android") { @@ -319,6 +324,12 @@ fn main() { config.acl = Some(acl); } + if matches.is_present("IPV6_FIRST") { + config.ipv6_first = true; + } + + // DONE READING options + if config.local.is_none() { eprintln!( "missing `local_address`, consider specifying it by --local-addr command line option, \ diff --git a/src/bin/manager.rs b/src/bin/manager.rs index 35be3198..3b205043 100644 --- a/src/bin/manager.rs +++ b/src/bin/manager.rs @@ -103,6 +103,11 @@ fn main() { .takes_value(true) .help("Path to ACL (Access Control List)"), ) + .arg( + Arg::with_name("IPV6_FIRST") + .short("6") + .help("Resovle hostname to IPv6 address first"), + ) .get_matches(); let debug_level = matches.occurrences_of("VERBOSE"); @@ -169,6 +174,12 @@ fn main() { config.acl = Some(acl); } + if matches.is_present("IPV6_FIRST") { + config.ipv6_first = true; + } + + // DONE reading options + if config.manager_address.is_none() { eprintln!( "missing `manager_address`, consider specifying it by --manager-address command line option, \ diff --git a/src/bin/redir.rs b/src/bin/redir.rs index d60d6367..95d13f39 100644 --- a/src/bin/redir.rs +++ b/src/bin/redir.rs @@ -138,6 +138,11 @@ fn main() { .long("acl") .takes_value(true) .help("Path to ACL (Access Control List)"), + ) + .arg( + Arg::with_name("IPV6_FIRST") + .short("6") + .help("Resovle hostname to IPv6 address first"), ); let available_types = RedirType::available_types(); @@ -270,6 +275,12 @@ fn main() { config.udp_redir = udp_redir.parse::().expect("UDP redir type"); } + if matches.is_present("IPV6_FIRST") { + config.ipv6_first = true; + } + + // DONE READING options + if config.local.is_none() { eprintln!( "missing `local_address`, consider specifying it by --local-addr command line option, \ diff --git a/src/bin/server.rs b/src/bin/server.rs index 1fe76ee2..93802000 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -137,6 +137,11 @@ fn main() { .takes_value(true) .help("Path to ACL (Access Control List)"), ) + .arg( + Arg::with_name("IPV6_FIRST") + .short("6") + .help("Resovle hostname to IPv6 address first"), + ) .get_matches(); drop(available_ciphers); @@ -232,6 +237,12 @@ fn main() { config.acl = Some(acl); } + if matches.is_present("IPV6_FIRST") { + config.ipv6_first = true; + } + + // DONE READING options + if config.server.is_empty() { eprintln!( "missing proxy servers, consider specifying it by \ diff --git a/src/bin/tunnel.rs b/src/bin/tunnel.rs index 92d2384e..4dfa371f 100644 --- a/src/bin/tunnel.rs +++ b/src/bin/tunnel.rs @@ -139,6 +139,11 @@ fn main() { .takes_value(true) .help("Set RLIMIT_NOFILE with both soft and hard limit (only for *nix systems)"), ) + .arg( + Arg::with_name("IPV6_FIRST") + .short("6") + .help("Resovle hostname to IPv6 address first"), + ) .get_matches(); drop(available_ciphers); @@ -232,6 +237,12 @@ fn main() { config.nofile = Some(nofile.parse::().expect("an unsigned integer for `nofile`")); } + if matches.is_present("IPV6_FIRST") { + config.ipv6_first = true; + } + + // DONE READING options + if config.local.is_none() { eprintln!( "missing `local_address`, consider specifying it by --local-addr command line option, \ diff --git a/src/config.rs b/src/config.rs index 24d55064..538f4927 100644 --- a/src/config.rs +++ b/src/config.rs @@ -114,6 +114,8 @@ struct SSConfig { no_delay: Option, #[serde(skip_serializing_if = "Option::is_none")] nofile: Option, + #[serde(skip_serializing_if = "Option::is_none")] + ipv6_first: Option, } #[derive(Serialize, Deserialize, Debug)] @@ -912,6 +914,9 @@ pub struct Config { /// Remote DNS's address #[cfg(target_os = "android")] pub remote_dns_addr: Option
, + /// Uses IPv6 addresses first + /// This would affect DNS resolution logic + pub ipv6_first: bool, } /// Configuration parsing error kind @@ -1001,6 +1006,7 @@ impl Config { local_dns_addr: None, #[cfg(target_os = "android")] remote_dns_addr: None, + ipv6_first: false, } } @@ -1180,6 +1186,11 @@ impl Config { // This is mostly used for manager for creating new servers nconfig.timeout = config.timeout.map(Duration::from_secs); + // Uses IPv6 first + if let Some(f) = config.ipv6_first { + nconfig.ipv6_first = f; + } + Ok(nconfig) } @@ -1384,6 +1395,10 @@ impl fmt::Display for Config { jconf.nofile = self.nofile; + if self.ipv6_first { + jconf.ipv6_first = Some(self.ipv6_first); + } + write!(f, "{}", json5::to_string(&jconf).unwrap()) } } diff --git a/src/context.rs b/src/context.rs index b0ce68b6..987f48c8 100644 --- a/src/context.rs +++ b/src/context.rs @@ -123,7 +123,7 @@ impl ServerState { #[cfg(feature = "trust-dns")] pub async fn new_shared(config: &Config, rt: Handle) -> SharedServerState { let state = ServerState { - dns_resolver: match create_resolver(config.get_dns_config(), config.timeout, rt).await { + dns_resolver: match create_resolver(config.get_dns_config(), config.timeout, config.ipv6_first, rt).await { Ok(resolver) => Some(resolver), Err(..) => None, }, diff --git a/src/relay/dns_resolver/trust_dns_resolver.rs b/src/relay/dns_resolver/trust_dns_resolver.rs index af039572..7cc7c38f 100644 --- a/src/relay/dns_resolver/trust_dns_resolver.rs +++ b/src/relay/dns_resolver/trust_dns_resolver.rs @@ -9,7 +9,7 @@ use std::{ use log::{error, trace}; use tokio::{self, runtime::Handle}; use trust_dns_resolver::{ - config::{ResolverConfig, ResolverOpts}, + config::{LookupIpStrategy, ResolverConfig, ResolverOpts}, TokioAsyncResolver, }; @@ -20,6 +20,7 @@ use crate::context::Context; pub async fn create_resolver( dns: Option, timeout: Option, + ipv6_first: bool, rt: Handle, ) -> io::Result { let mut resolver_opts = ResolverOpts::default(); @@ -27,6 +28,10 @@ pub async fn create_resolver( resolver_opts.timeout = d; } + if ipv6_first { + resolver_opts.ip_strategy = LookupIpStrategy::Ipv6thenIpv4; + } + { // To make this independent, if targeting macOS, BSD, Linux, or Windows, we can use the system's configuration: #[cfg(any(unix, windows))]