Skip to content

Commit

Permalink
ignore mode in local-dns local resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Dec 28, 2020
1 parent 303f96c commit 91d25d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

42 changes: 17 additions & 25 deletions crates/shadowsocks-service/src/local/dns/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,36 +678,28 @@ impl DnsClient {
message.set_recursion_desired(true);
message.add_query(query.clone());

let mut last_err = io::Error::new(ErrorKind::InvalidData, "resolve empty");

// Query UDP then TCP

if self.mode.enable_udp() {
match self
.client_cache
.lookup_udp_local(local_addr, message.clone(), self.context.connect_opts_ref())
.await
{
Ok(msg) => return Ok(msg),
Err(err) => {
last_err = err.into();
}
}
}
let udp_query =
self.client_cache
.lookup_udp_local(local_addr, message.clone(), self.context.connect_opts_ref());
let tcp_query = async move {
// Send TCP query after 500ms, because UDP will always return faster than TCP, there is no need to send queries simutaneously
time::sleep(Duration::from_millis(500)).await;

if self.mode.enable_tcp() {
match self
.client_cache
self.client_cache
.lookup_tcp_local(local_addr, message, self.context.connect_opts_ref())
.await
{
Ok(msg) => return Ok(msg),
Err(err) => {
last_err = err.into();
}
}
}
};

Err(last_err)
tokio::pin!(udp_query);
tokio::pin!(tcp_query);

match future::select(udp_query, tcp_query).await {
Either::Left((Ok(m), ..)) => Ok(m),
Either::Left((Err(..), next)) => next.await.map_err(From::from),
Either::Right((Ok(m), ..)) => Ok(m),
Either::Right((Err(..), next)) => next.await.map_err(From::from),
}
}
}

0 comments on commit 91d25d3

Please sign in to comment.