Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug committed Oct 21, 2024
1 parent d67f2de commit d4b9ec6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 58 deletions.
2 changes: 1 addition & 1 deletion clash_lib/src/app/dns/resolver/enhanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl ClashResolver for EnhancedResolver {
}

async fn exchange(&self, message: &op::Message) -> anyhow::Result<op::Message> {
let rv = self.exchange(&message).await?;
let rv = self.exchange(message).await?;
let hostname = message
.query()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/app/dns/resolver/system_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl ClashResolver for SystemResolver {

async fn exchange(
&self,
_: hickory_proto::op::Message,
_: &hickory_proto::op::Message,
) -> anyhow::Result<hickory_proto::op::Message> {
Err(anyhow::anyhow!("unsupported"))
}
Expand Down
108 changes: 52 additions & 56 deletions clash_lib/src/app/dns/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,70 +26,66 @@ impl watfaq_dns::DnsMessageExchanger for DnsMessageExchanger {
self.resolver.ipv6()
}

fn exchange(
async fn exchange(
&self,
message: &Message,
) -> impl Future<Output = Result<Message, watfaq_dns::DNSError>> + 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<Message, watfaq_dns::DNSError> {
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()))
}
}
}
}

Expand Down

0 comments on commit d4b9ec6

Please sign in to comment.