Skip to content

Commit

Permalink
refactor(dns): extrat dns server (#632)
Browse files Browse the repository at this point in the history
* extrat dns server

* f

* f
  • Loading branch information
ibigbug authored Oct 22, 2024
1 parent d22ff7c commit 1fba387
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 626 deletions.
23 changes: 22 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ serde_json = "1"
erased-serde = "0.4"

# DNS
watfaq-dns = { version = "0.1" }
hickory-client = "0.25.0-alpha.2"
hickory-resolver = "0.25.0-alpha.2"
hickory-server = { version = "0.25.0-alpha.2", features = ["dns-over-rustls", "dns-over-https-rustls", "dns-over-h3"] }
hickory-proto = { version = "0.25.0-alpha.2", features = ["dns-over-rustls", "dns-over-https-rustls", "dns-over-h3"]}

dhcproto = "0.12"
Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/app/api/handlers/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn query_dns(

m.add_query(hickory_proto::op::Query::query(name.unwrap(), typ));

match state.resolver.exchange(m).await {
match state.resolver.exchange(&m).await {
Ok(response) => {
let mut resp = Map::new();
resp.insert("Status".to_owned(), response.response_code().low().into());
Expand Down
39 changes: 1 addition & 38 deletions clash_lib/src/app/dns/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use regex::Regex;

use serde::Deserialize;
use url::Url;
use watfaq_dns::{DNSListenAddr, DoH3Config, DoHConfig, DoTConfig};

use crate::{
common::trie,
Expand Down Expand Up @@ -45,44 +46,6 @@ pub struct FallbackFilter {
pub domain: Vec<String>,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct DoHConfig {
pub addr: SocketAddr,
pub ca_cert: DnsServerCert,
pub ca_key: DnsServerKey,
pub hostname: Option<String>,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct DoH3Config {
pub addr: SocketAddr,
pub ca_cert: DnsServerCert,
pub ca_key: DnsServerKey,
pub hostname: Option<String>,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct DoTConfig {
pub addr: SocketAddr,
pub ca_cert: DnsServerCert,
pub ca_key: DnsServerKey,
}

pub type DnsServerKey = Option<String>;
pub type DnsServerCert = Option<String>;

#[derive(Debug, Default, Clone)]
pub struct DNSListenAddr {
pub udp: Option<SocketAddr>,
pub tcp: Option<SocketAddr>,
pub doh: Option<DoHConfig>,
pub dot: Option<DoTConfig>,
pub doh3: Option<DoH3Config>,
}

#[derive(Default)]
pub struct Config {
pub enable: bool,
Expand Down
5 changes: 2 additions & 3 deletions clash_lib/src/app/dns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,15 @@ pub trait ClashResolver: Sync + Send {
async fn cached_for(&self, ip: std::net::IpAddr) -> Option<String>;

/// Used for DNS Server
async fn exchange(&self, message: op::Message) -> anyhow::Result<op::Message>;
async fn exchange(&self, message: &op::Message) -> anyhow::Result<op::Message>;

/// Only used for look up fake IP
async fn reverse_lookup(&self, ip: std::net::IpAddr) -> Option<String>;
async fn is_fake_ip(&self, ip: std::net::IpAddr) -> bool;
fn fake_ip_enabled(&self) -> bool;

fn ipv6(&self) -> bool;
fn set_ipv6(&self, enable: bool);

fn kind(&self) -> ResolverKind;

fn fake_ip_enabled(&self) -> bool;
}
6 changes: 3 additions & 3 deletions clash_lib/src/app/dns/resolver/enhanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl EnhancedResolver {

fn domain_name_of_message(m: &op::Message) -> Option<String> {
m.query()
.map(|x| x.name().to_ascii().trim_matches('.').to_owned())
.map(|x| x.name().to_ascii().trim_end_matches('.').to_owned())
}

pub(crate) fn ip_list_of_message(m: &op::Message) -> Vec<net::IpAddr> {
Expand Down Expand Up @@ -581,8 +581,8 @@ impl ClashResolver for EnhancedResolver {
None
}

async fn exchange(&self, message: op::Message) -> anyhow::Result<op::Message> {
let rv = self.exchange(&message).await?;
async fn exchange(&self, message: &op::Message) -> anyhow::Result<op::Message> {
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
2 changes: 1 addition & 1 deletion clash_lib/src/app/dns/resolver/system_non_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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
6 changes: 0 additions & 6 deletions clash_lib/src/app/dns/server/dummy_keys.rs

This file was deleted.

Loading

0 comments on commit 1fba387

Please sign in to comment.