Skip to content

Commit

Permalink
tun biopolate
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug committed Sep 2, 2023
1 parent 2e108c7 commit 2da7390
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ filetime = "0.2"
axum = { version = "0.6.20", features = ["ws"] }
tower-http = { version = "0.4.0", features = ["fs", "trace", "cors"] }
chrono = { version = "0.4.26", features = ["serde"] }
tun = "0.5"


serde = { version = "1.0", features=["derive"] }
Expand Down
10 changes: 10 additions & 0 deletions clash_lib/src/config/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub struct Config {
#[serde(rename = "proxy-providers")]
pub proxy_provider: Option<HashMap<String, HashMap<String, Value>>>,
pub experimental: Option<Experimental>,

pub tun: Option<HashMap<String, Value>>,
}

impl FromStr for Config {
Expand Down Expand Up @@ -161,6 +163,7 @@ impl Default for Config {
"https://github.com/Loyalsoldier/geoip/releases/download/202307271745/Country.mmdb"
.to_owned(),
),
tun: Default::default(),
}
}
}
Expand Down Expand Up @@ -297,6 +300,13 @@ socks-port: 7891
# other LAN IP addresses
allow-lan: false
tun:
enable: true
stack: system
device-url: dev://clash0
dns-hijack:
- 10.0.0.5
# This is only applicable when `allow-lan` is `true`
# '*': bind all IP addresses
# 192.168.122.11: bind a single IPv4 address
Expand Down
26 changes: 23 additions & 3 deletions clash_lib/src/config/internal/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use std::fmt::Display;
use std::net::IpAddr;
use std::str::FromStr;

use serde::de::value::MapDeserializer;
use serde::{Deserialize, Serialize};

use crate::common::auth;
use crate::config::def;
use crate::config::def::{self};
use crate::config::internal::proxy::{OutboundProxy, PROXY_DIRECT, PROXY_REJECT};
use crate::config::internal::rule::RuleType;
use crate::proxy::utils::Interface;
use crate::{
app::dns,
config::def::{Experimental, LogLevel, RunMode},
config::def::{LogLevel, RunMode},
Error,
};

Expand All @@ -22,7 +23,8 @@ use super::proxy::{OutboundProxyProtocol, OutboundProxyProvider};
pub struct Config {
pub general: General,
pub dns: dns::Config,
pub experimental: Option<Experimental>,
pub tun: TunConfig,
pub experimental: Option<def::Experimental>,
pub profile: Profile,
pub rules: Vec<RuleType>,
pub users: Vec<auth::User>,
Expand Down Expand Up @@ -70,6 +72,15 @@ impl TryFrom<def::Config> for Config {
},
dns: (&c).try_into()?,
experimental: c.experimental,
tun: match c.tun {
Some(mapping) => TunConfig::deserialize(MapDeserializer::new(mapping.into_iter()))
.map_err(|e| Error::InvalidConfig(format!("invalid tun config: {}", e)))?,
None => TunConfig {
enable: false,
device_url: String::new(),
dns_hijack: Vec::new(),
},
},
profile: Profile {
store_selected: c.profile.store_selected,
store_fakeip: c.profile.store_fake_ip,
Expand Down Expand Up @@ -195,6 +206,15 @@ pub struct Profile {
store_selected: bool,
store_fakeip: bool,
}

#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TunConfig {
pub enable: bool,
pub device_url: String,
pub dns_hijack: Vec<String>,
}

#[derive(Clone, Default)]
pub enum BindAddress {
#[default]
Expand Down
5 changes: 4 additions & 1 deletion clash_lib/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ use tokio::io::AsyncWrite;
pub mod direct;
pub mod reject;

pub(crate) mod datagram;
pub mod http;
pub mod mixed;

pub(crate) mod datagram;

#[cfg(feature = "shadowsocks")]
pub mod shadowsocks;
pub mod socks;
pub mod tun;
//pub mod trojan;
pub mod utils;
pub mod vmess;
Expand Down
7 changes: 7 additions & 0 deletions clash_lib/src/proxy/tun/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::sync::Arc;

use crate::{app::dispatcher::Dispatcher, config::internal::config::TunConfig, Runner};

pub fn get_runner(cfg: TunConfig, dispatcher: Arc<Dispatcher>) -> anyhow::Result<Runner> {
todo!()
}

0 comments on commit 2da7390

Please sign in to comment.