diff --git a/clash_lib/src/app/dns/resolver/enhanced.rs b/clash_lib/src/app/dns/resolver/enhanced.rs index 2ef90799a..3a7612fae 100644 --- a/clash_lib/src/app/dns/resolver/enhanced.rs +++ b/clash_lib/src/app/dns/resolver/enhanced.rs @@ -582,7 +582,7 @@ impl ClashResolver for EnhancedResolver { } async fn exchange(&self, message: &op::Message) -> anyhow::Result { - let rv = self.exchange(&message).await?; + let rv = self.exchange(message).await?; let hostname = message .query() .unwrap() diff --git a/clash_lib/src/app/dns/resolver/system_linux.rs b/clash_lib/src/app/dns/resolver/system_linux.rs index 44567ccd8..90a512c86 100644 --- a/clash_lib/src/app/dns/resolver/system_linux.rs +++ b/clash_lib/src/app/dns/resolver/system_linux.rs @@ -62,7 +62,7 @@ impl ClashResolver for SystemResolver { async fn exchange( &self, - _: hickory_proto::op::Message, + _: &hickory_proto::op::Message, ) -> anyhow::Result { Err(anyhow::anyhow!("unsupported")) } diff --git a/clash_lib/src/app/dns/server/mod.rs b/clash_lib/src/app/dns/server/mod.rs index 39e157d33..aa35e67ee 100644 --- a/clash_lib/src/app/dns/server/mod.rs +++ b/clash_lib/src/app/dns/server/mod.rs @@ -26,70 +26,66 @@ impl watfaq_dns::DnsMessageExchanger for DnsMessageExchanger { self.resolver.ipv6() } - fn exchange( + async fn exchange( &self, message: &Message, - ) -> impl Future> + Send { - async { - if self.resolver.fake_ip_enabled() { - let name = message - .query() - .ok_or(watfaq_dns::DNSError::InvalidOpQuery( - "malformed query message".to_string(), - ))? - .name(); - - let host = message - .query() - .map(|x| x.name().to_ascii().trim_end_matches('.').to_owned()) - .unwrap(); - - let mut message = Message::new(); - message.set_recursion_available(false); - message.set_authoritative(true); - - match self.resolver.resolve(&host, true).await { - Ok(resp) => match resp { - Some(ip) => { - let rdata = match ip { - IpAddr::V4(a) => RData::A(A(a)), - IpAddr::V6(aaaa) => RData::AAAA(AAAA(aaaa)), - }; - - let records = vec![Record::from_rdata( - name.clone(), - DEFAULT_DNS_SERVER_TTL, - rdata, - )]; - - message.set_response_code(ResponseCode::NoError); - message.set_answer_count(records.len() as u16); - - message.add_answers(records); - - return Ok(message); - } - None => { - message.set_response_code(ResponseCode::NXDomain); - return Ok(message); - } - }, - Err(e) => { - debug!("dns resolve error: {}", e); - return Err(watfaq_dns::DNSError::QueryFailed( - e.to_string(), - )); + ) -> Result { + if self.resolver.fake_ip_enabled() { + let name = message + .query() + .ok_or(watfaq_dns::DNSError::InvalidOpQuery( + "malformed query message".to_string(), + ))? + .name(); + + let host = message + .query() + .map(|x| x.name().to_ascii().trim_end_matches('.').to_owned()) + .unwrap(); + + let mut message = Message::new(); + message.set_recursion_available(false); + message.set_authoritative(true); + + match self.resolver.resolve(&host, true).await { + Ok(resp) => match resp { + Some(ip) => { + let rdata = match ip { + IpAddr::V4(a) => RData::A(A(a)), + IpAddr::V6(aaaa) => RData::AAAA(AAAA(aaaa)), + }; + + let records = vec![Record::from_rdata( + name.clone(), + DEFAULT_DNS_SERVER_TTL, + rdata, + )]; + + message.set_response_code(ResponseCode::NoError); + message.set_answer_count(records.len() as u16); + + message.add_answers(records); + + return Ok(message); } - } - } - match self.resolver.exchange(message).await { - Ok(m) => Ok(m), + None => { + message.set_response_code(ResponseCode::NXDomain); + return Ok(message); + } + }, Err(e) => { debug!("dns resolve error: {}", e); - Err(watfaq_dns::DNSError::QueryFailed(e.to_string())) + return Err(watfaq_dns::DNSError::QueryFailed(e.to_string())); } } } + match self.resolver.exchange(message).await { + Ok(m) => Ok(m), + Err(e) => { + debug!("dns resolve error: {}", e); + Err(watfaq_dns::DNSError::QueryFailed(e.to_string())) + } + } } }