Skip to content

Commit

Permalink
bring back tun fake ip (#250)
Browse files Browse the repository at this point in the history
* bring back tun fake ip

* clipy
  • Loading branch information
ibigbug authored Jan 1, 2024
1 parent 672aa59 commit 9cf013e
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions clash_lib/src/app/dns/server/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -19,6 +22,8 @@ use crate::Runner;

use super::{Config, ThreadSafeDNSResolver};

static DEFAULT_DNS_SERVER_TTL: u32 = 60;

struct DnsListener {
server: ServerFuture<DnsHandler>,
}
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 9cf013e

Please sign in to comment.