From 4f686fef2dece6077ce6f3aa014497a1287939c5 Mon Sep 17 00:00:00 2001 From: dev0 Date: Wed, 27 Dec 2023 01:12:59 +1100 Subject: [PATCH] wip --- Cargo.lock | 2 - clash_lib/Cargo.toml | 2 +- .../src/proxy/tun/{ => lwip}/datagram.rs | 0 clash_lib/src/proxy/tun/{ => lwip}/inbound.rs | 0 clash_lib/src/proxy/tun/lwip/mod.rs | 4 ++ clash_lib/src/proxy/tun/mod.rs | 8 ++-- clash_lib/src/proxy/tun/smoltcp/mod.rs | 42 +++++++++++++++++++ 7 files changed, 51 insertions(+), 7 deletions(-) rename clash_lib/src/proxy/tun/{ => lwip}/datagram.rs (100%) rename clash_lib/src/proxy/tun/{ => lwip}/inbound.rs (100%) create mode 100644 clash_lib/src/proxy/tun/lwip/mod.rs create mode 100644 clash_lib/src/proxy/tun/smoltcp/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 98e338125..7e3d561a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3703,8 +3703,6 @@ checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" [[package]] name = "smoltcp" version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d2e3a36ac8fea7b94e666dfa3871063d6e0a5c9d5d4fec9a1a6b7b6760f0229" dependencies = [ "bitflags 1.3.2", "byteorder", diff --git a/clash_lib/Cargo.toml b/clash_lib/Cargo.toml index b4845b929..597641856 100644 --- a/clash_lib/Cargo.toml +++ b/clash_lib/Cargo.toml @@ -64,7 +64,7 @@ tun = { git = "https://github.com/Watfaq/rust-tun.git", rev = "8f7568190f1200d3e netstack-lwip = { git = "https://github.com/Watfaq/netstack-lwip.git", rev = "2817bf82740e04bbee6b7bf1165f55657a6ed163" } boringtun = { version = "0.6.0" } -smoltcp = { version = "0.10", default-features = false, features = ["std", "log", "medium-ip", "proto-ipv4", "proto-ipv6", "socket-udp", "socket-tcp"] } +smoltcp = { path = "../../../gallery/smoltcp", version = "0.10", default-features = false, features = ["std", "log", "medium-ip", "proto-ipv4", "proto-ipv6", "socket-udp", "socket-tcp"] } serde = { version = "1.0", features=["derive"] } diff --git a/clash_lib/src/proxy/tun/datagram.rs b/clash_lib/src/proxy/tun/lwip/datagram.rs similarity index 100% rename from clash_lib/src/proxy/tun/datagram.rs rename to clash_lib/src/proxy/tun/lwip/datagram.rs diff --git a/clash_lib/src/proxy/tun/inbound.rs b/clash_lib/src/proxy/tun/lwip/inbound.rs similarity index 100% rename from clash_lib/src/proxy/tun/inbound.rs rename to clash_lib/src/proxy/tun/lwip/inbound.rs diff --git a/clash_lib/src/proxy/tun/lwip/mod.rs b/clash_lib/src/proxy/tun/lwip/mod.rs new file mode 100644 index 000000000..869a2426f --- /dev/null +++ b/clash_lib/src/proxy/tun/lwip/mod.rs @@ -0,0 +1,4 @@ +pub mod datagram; +pub mod inbound; + +pub use netstack_lwip as netstack; diff --git a/clash_lib/src/proxy/tun/mod.rs b/clash_lib/src/proxy/tun/mod.rs index cde10543e..90e24b30a 100644 --- a/clash_lib/src/proxy/tun/mod.rs +++ b/clash_lib/src/proxy/tun/mod.rs @@ -1,4 +1,4 @@ -pub mod inbound; -pub use netstack_lwip as netstack; -mod datagram; -pub use inbound::get_runner as get_tun_runner; +mod lwip; +mod smoltcp; + +pub use lwip::inbound::get_runner as get_tun_runner; diff --git a/clash_lib/src/proxy/tun/smoltcp/mod.rs b/clash_lib/src/proxy/tun/smoltcp/mod.rs new file mode 100644 index 000000000..b110b39b6 --- /dev/null +++ b/clash_lib/src/proxy/tun/smoltcp/mod.rs @@ -0,0 +1,42 @@ +use std::sync::Arc; + +use url::Url; + +use crate::{ + app::{dispatcher::Dispatcher, dns::ThreadSafeDNSResolver}, + config::internal::config::TunConfig, + Error, Runner, +}; + +pub fn get_runner( + cfg: TunConfig, + dispatcher: Arc, + resolver: ThreadSafeDNSResolver, +) -> Result, Error> { + let device_id = cfg.device_id; + + let u = + Url::parse(&device_id).map_err(|x| Error::InvalidConfig(format!("tun device {}", x)))?; + + let device = match u.scheme() { + "fd" => { + let fd = u + .host() + .expect("tun fd must be provided") + .to_string() + .parse() + .map_err(|x| Error::InvalidConfig(format!("tun fd {}", x)))?; + TunTapInterface::from_fd(fd)? + } + "dev" => { + let dev = u.host().expect("tun dev must be provided").to_string(); + tun_cfg.name(dev); + } + _ => { + return Err(Error::InvalidConfig(format!( + "invalid device id: {}", + device_id + ))); + } + }; +}