Skip to content

Commit

Permalink
feat(tun): dns hijack config compatible (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug authored Nov 21, 2024
1 parent bc7c20b commit 92b5bfc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
9 changes: 5 additions & 4 deletions clash/tests/data/config/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ tproxy-port: 8900
tun:
enable: true
device-id: "dev://utun1989"
route-all: true
route-all: false
gateway: "198.19.0.1/32"
so-mark: 3389
dns-hijack: true
# routes:
# - 0.0.0.0/1
# - 128.0.0.0/1
# dns-hijack:
# - 1.1.1.1:53
routes:
- 1.1.1.1/32

ipv6: true

Expand Down
16 changes: 15 additions & 1 deletion clash_lib/src/config/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ fn default_tun_address() -> String {
"198.18.0.1/32".to_string()
}

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum DnsHijack {
Switch(bool),
List(Vec<String>),
}

impl Default for DnsHijack {
fn default() -> Self {
DnsHijack::Switch(false)
}
}

#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct TunConfig {
Expand All @@ -26,8 +39,9 @@ pub struct TunConfig {
/// policy routing table on Linux only
pub route_table: Option<u32>,
/// Will hijack UDP:53 DNS queries to the Clash DNS server if set to true
/// setting to a list has the same effect as setting to true
#[serde(default)]
pub dns_hijack: bool,
pub dns_hijack: DnsHijack,
}

#[derive(Serialize, Deserialize, Default, Copy, Clone)]
Expand Down
5 changes: 4 additions & 1 deletion clash_lib/src/config/internal/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ impl TryFrom<def::Config> for Config {
mtu: t.mtu,
so_mark: t.so_mark,
route_table: t.route_table,
dns_hijack: t.dns_hijack,
dns_hijack: match t.dns_hijack {
def::DnsHijack::Switch(b) => b,
def::DnsHijack::List(_) => true,
},
},
None => TunConfig::default(),
},
Expand Down

0 comments on commit 92b5bfc

Please sign in to comment.