From 9cf013ed6afccbe9e10d9140273a550335526731 Mon Sep 17 00:00:00 2001 From: Yuwei Ba Date: Mon, 1 Jan 2024 23:21:08 +1100 Subject: [PATCH] bring back tun fake ip (#250) * bring back tun fake ip * clipy --- clash_lib/src/app/dns/server/mod.rs | 50 +++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/clash_lib/src/app/dns/server/mod.rs b/clash_lib/src/app/dns/server/mod.rs index a0d5f0d2c..ae00e4986 100644 --- a/clash_lib/src/app/dns/server/mod.rs +++ b/clash_lib/src/app/dns/server/mod.rs @@ -1,10 +1,13 @@ -use std::time::Duration; +use std::{net::IpAddr, time::Duration}; use async_trait::async_trait; use hickory_proto::{ op::{Header, Message, MessageType, OpCode, ResponseCode}, - rr::RecordType, + rr::{ + rdata::{A, AAAA}, + RData, Record, RecordType, + }, }; use hickory_server::{ authority::MessageResponseBuilder, @@ -19,6 +22,8 @@ use crate::Runner; use super::{Config, ThreadSafeDNSResolver}; +static DEFAULT_DNS_SERVER_TTL: u32 = 60; + struct DnsListener { server: ServerFuture, } @@ -67,6 +72,47 @@ impl DnsHandler { return Ok(response_handle.send_response(resp).await?); } + if self.resolver.fake_ip_enabled() { + let name = request.query().name(); + let host = if name.is_fqdn() { + name.to_string().strip_suffix('.').unwrap().to_string() + } else { + name.to_string() + }; + + let builder = MessageResponseBuilder::from_message_request(request); + let mut header = Header::response_from_request(request.header()); + header.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.into(), + DEFAULT_DNS_SERVER_TTL, + rdata, + )]; + + let resp = builder.build(header, records.iter(), &[], &[], &[]); + return Ok(response_handle.send_response(resp).await?); + } + None => { + let resp = builder.build_no_records(header); + return Ok(response_handle.send_response(resp).await?); + } + }, + Err(e) => { + debug!("dns resolve error: {}", e); + return Err(DNSError::QueryFailed(e.to_string())); + } + } + } + let mut m = Message::new(); m.set_op_code(request.op_code()); m.set_message_type(request.message_type());