Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tun): windows auto-route #561

Merged
merged 8 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions clash/tests/data/config/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ socks-port: 8889
mixed-port: 8899

tun:
enable: false
enable: true
device-id: "dev://utun1989"
route-all: false
routes:
- 0.0.0.0/1
- 128.0.0.0/1

ipv6: true

Expand Down Expand Up @@ -42,7 +46,7 @@ dns:
nameserver:
# - 114.114.114.114 # default value
# - 1.1.1.1#auto # default value
- tls://1.1.1.1:853#en0 # DNS over TLS
- tls://1.1.1.1:853#auto # DNS over TLS
# - dhcp://en0 # dns from dhcp

allow-lan: true
Expand Down
1 change: 1 addition & 0 deletions clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,5 @@ security-framework = "2.11.1"
windows = { version = "0.58", features = [
"Win32_Networking_WinSock",
"Win32_Foundation",
"Win32_NetworkManagement_Rras",
]}
3 changes: 2 additions & 1 deletion clash_lib/src/app/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl From<LogLevel> for filter::LevelFilter {
LogLevel::Warning => filter::LevelFilter::WARN,
LogLevel::Info => filter::LevelFilter::INFO,
LogLevel::Debug => filter::LevelFilter::DEBUG,
LogLevel::Trace => filter::LevelFilter::TRACE,
LogLevel::Silent => filter::LevelFilter::OFF,
}
}
Expand Down Expand Up @@ -65,7 +66,7 @@ where
tracing::Level::WARN => LogLevel::Warning,
tracing::Level::INFO => LogLevel::Info,
tracing::Level::DEBUG => LogLevel::Debug,
tracing::Level::TRACE => LogLevel::Debug,
tracing::Level::TRACE => LogLevel::Trace,
},
msg: strs.join(" "),
};
Expand Down
25 changes: 25 additions & 0 deletions clash_lib/src/common/defer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// https://stackoverflow.com/a/29963675/1109167
pub struct ScopeCall<F: FnOnce()> {
pub c: Option<F>,
}
impl<F: FnOnce()> Drop for ScopeCall<F> {
fn drop(&mut self) {
self.c.take().unwrap()()
}
}

#[macro_export]
macro_rules! expr {
($e:expr) => {
$e
};
} // tt hack

#[macro_export]
macro_rules! defer {
($($data: tt)*) => (
let _scope_call = $crate::common::defer::ScopeCall {
c: Some(|| -> () { $crate::expr!({ $($data)* }) })
};
)
}
1 change: 1 addition & 0 deletions clash_lib/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod auth;
pub mod crypto;
pub mod defer;
pub mod errors;
pub mod geodata;
pub mod http;
Expand Down
20 changes: 19 additions & 1 deletion clash_lib/src/config/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ use std::{collections::HashMap, fmt::Display, path::PathBuf, str::FromStr};
use serde::{Deserialize, Serialize};
use serde_yaml::Value;

fn default_tun_address() -> String {
"198.18.0.1/32".to_string()
}

#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct TunConfig {
pub enable: bool,
pub device_id: String,
/// tun interface address
#[serde(default = "default_tun_address")]
pub gateway: String,
pub routes: Option<Vec<String>>,
pub route_all: bool,
}

#[derive(Serialize, Deserialize, Default, Copy, Clone)]
#[serde(rename_all = "lowercase")]
pub enum RunMode {
Expand All @@ -29,6 +45,7 @@ impl Display for RunMode {
#[derive(PartialEq, Serialize, Deserialize, Default, Copy, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
Trace,
Debug,
#[default]
Info,
Expand All @@ -41,6 +58,7 @@ pub enum LogLevel {
impl Display for LogLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LogLevel::Trace => write!(f, "trace"),
LogLevel::Debug => write!(f, "debug"),
LogLevel::Info => write!(f, "info"),
LogLevel::Warning => write!(f, "warn"),
Expand Down Expand Up @@ -313,7 +331,7 @@ pub struct Config {
/// enable: true
/// device-id: "dev://utun1989"
/// ```
pub tun: Option<HashMap<String, Value>>,
pub tun: Option<TunConfig>,
}

impl TryFrom<PathBuf> for Config {
Expand Down
42 changes: 24 additions & 18 deletions clash_lib/src/config/internal/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;

use std::{fmt::Display, net::IpAddr, str::FromStr};

use ipnet::IpNet;
use serde::{de::value::MapDeserializer, Deserialize, Serialize};
use serde_yaml::Value;

Expand Down Expand Up @@ -97,15 +98,26 @@ 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
))
Some(t) => TunConfig {
enable: t.enable,
device_id: t.device_id,
route_all: t.route_all,
routes: t
.routes
.map(|r| {
r.into_iter()
.map(|x| x.parse())
.collect::<Result<Vec<_>, _>>()
})
.transpose()
.map_err(|x| {
Error::InvalidConfig(format!("parse tun routes: {}", x))
})?
}
.unwrap_or_default(),
gateway: t.gateway.parse().map_err(|x| {
Error::InvalidConfig(format!("parse tun gateway: {}", x))
})?,
},
None => TunConfig::default(),
},
profile: Profile {
Expand Down Expand Up @@ -279,19 +291,13 @@ pub struct Profile {
// store_fake_ip: bool,
}

#[derive(Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
#[derive(Default)]
pub struct TunConfig {
pub enable: bool,
/// tun device id, could be
/// dev://utun886 # Linux
/// fd://3 # file descriptor
#[serde(alias = "device-url")]
pub device_id: String,
/// tun device address
/// default: 198.18.0.0/16
pub network: Option<String>,
pub gateway: Option<IpAddr>,
pub route_all: bool,
pub routes: Vec<IpNet>,
pub gateway: IpNet,
}

#[derive(Clone, Default)]
Expand Down
57 changes: 54 additions & 3 deletions clash_lib/src/proxy/tun/inbound.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use super::{datagram::TunDatagram, netstack};
use std::{net::SocketAddr, sync::Arc};
use std::{
net::{Ipv4Addr, SocketAddr},

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin-static-crt

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu-static-crt

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabihf

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu-static-crt

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-musl

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-musleabihf

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-musl

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi-static-crt

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin-static-crt

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin

unused import: `Ipv4Addr`

Check failure on line 3 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu-static-crt

unused import: `Ipv4Addr`
sync::Arc,
};

use futures::{SinkExt, StreamExt};
use ipnet::IpNet;

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin-static-crt

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu-static-crt

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabihf

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu-static-crt

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-musl

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-musleabihf

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-musl

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi-static-crt

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin-static-crt

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin

unused import: `ipnet::IpNet`

Check failure on line 8 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu-static-crt

unused import: `ipnet::IpNet`
use network_interface::NetworkInterfaceConfig;

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin-static-crt

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu-static-crt

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabihf

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu-static-crt

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-musl

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-musleabihf

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-musl

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi-static-crt

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin-static-crt

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin

unused import: `network_interface::NetworkInterfaceConfig`

Check failure on line 9 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu-static-crt

unused import: `network_interface::NetworkInterfaceConfig`
use tracing::{debug, error, info, trace, warn};
use tun::{Device, TunPacket};
use url::Url;
Expand All @@ -10,7 +15,11 @@
app::{dispatcher::Dispatcher, dns::ThreadSafeDNSResolver},
common::errors::{map_io_error, new_io_error},
config::internal::config::TunConfig,
proxy::{datagram::UdpPacket, utils::get_outbound_interface},
proxy::{
datagram::UdpPacket,
tun::routes::add_route,

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin-static-crt

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu-static-crt

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabihf

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu-static-crt

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-musl

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-musleabihf

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-musl

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi-static-crt

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin-static-crt

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin

unresolved import `crate::proxy::tun::routes::add_route`

Check failure on line 20 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu-static-crt

unresolved import `crate::proxy::tun::routes::add_route`
utils::{get_outbound_interface, OutboundInterface},

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-apple-darwin-static-crt

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu-static-crt

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabihf

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu-static-crt

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-musl

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-musleabihf

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-gnu

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-unknown-linux-gnu

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / aarch64-unknown-linux-musl

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / armv7-unknown-linux-gnueabi-static-crt

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin-static-crt

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / x86_64-apple-darwin

unused import: `OutboundInterface`

Check failure on line 21 in clash_lib/src/proxy/tun/inbound.rs

View workflow job for this annotation

GitHub Actions / i686-unknown-linux-gnu-static-crt

unused import: `OutboundInterface`
},
session::{Network, Session, SocksAddr, Type},
Error, Runner,
};
Expand Down Expand Up @@ -177,14 +186,56 @@
}
}

tun_cfg.up();
let gw = cfg.gateway;
tun_cfg.address(gw.addr()).netmask(gw.netmask()).up();

let tun = tun::create_as_async(&tun_cfg)
.map_err(|x| new_io_error(format!("failed to create tun device: {}", x)))?;

let tun_name = tun.get_ref().name().map_err(map_io_error)?;
info!("tun started at {}", tun_name);

#[cfg(target_os = "windows")]
if cfg.route_all || !cfg.routes.is_empty() {
let tun_iface = network_interface::NetworkInterface::show()
.map_err(map_io_error)?
.into_iter()
.find(|iface| iface.name == tun_name)
.map(|x| OutboundInterface {
name: x.name,
addr_v4: x.addr.iter().find_map(|addr| match addr {
network_interface::Addr::V4(addr) => Some(addr.ip),
_ => None,
}),
addr_v6: x.addr.iter().find_map(|addr| match addr {
network_interface::Addr::V6(addr) => Some(addr.ip),
_ => None,
}),
index: x.index,
})
.expect("tun interface not found");

if cfg.route_all {
warn!(
"route_all is enabled, all traffic will be routed through the tun \
interface"
);
let default_routes = vec![
IpNet::new(std::net::IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 1)
.unwrap(),
IpNet::new(std::net::IpAddr::V4(Ipv4Addr::new(128, 0, 0, 0)), 1)
.unwrap(),
];
for r in default_routes {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we make this a two step operation of build_routes & add_routes, thus is can be separated into the routes module

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm hoping the 0/1 and 128/1 can work on all platforms so the build_route will just be the same 2 entries

add_route(&tun_iface, &r).map_err(map_io_error)?;
}
} else {
for r in cfg.routes {
add_route(&tun_iface, &r).map_err(map_io_error)?;
}
}
}

let (stack, mut tcp_listener, udp_socket) =
netstack::NetStack::with_buffer_size(512, 256).map_err(map_io_error)?;

Expand Down
1 change: 1 addition & 0 deletions clash_lib/src/proxy/tun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod inbound;
pub use netstack_lwip as netstack;
mod datagram;
pub use inbound::get_runner as get_tun_runner;
mod routes;
4 changes: 4 additions & 0 deletions clash_lib/src/proxy/tun/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub(crate) use windows::add_route;
Loading
Loading