diff --git a/Cargo.toml b/Cargo.toml index 76e744a..2e85def 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ log = "0.4" mio = { version = "0.8", features = ["os-poll", "net", "os-ext"] } nix = { version = "0.26", features = ["process", "signal"] } prctl = "1.0" -smoltcp = { version = "0.9", git = "https://github.com/smoltcp-rs/smoltcp.git", features = ["std"] } +smoltcp = { version = "0.9", git = "https://github.com/blechschmidt/smoltcp", branch = "android", features = ["std"] } thiserror = "1.0" url = "2.3" diff --git a/src/lib.rs b/src/lib.rs index 5c2ad60..1f167a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,11 @@ pub struct Proxy { pub credentials: Option, } +pub enum NetworkInterface { + Named(String), + Fd(std::os::fd::RawFd), +} + impl Proxy { pub fn from_url(s: &str) -> Result { let e = format!("`{s}` is not a valid proxy URL"); @@ -83,6 +88,7 @@ impl std::fmt::Display for ProxyType { #[derive(Default)] pub struct Options { virtdns: Option, + mtu: Option, } impl Options { @@ -94,6 +100,11 @@ impl Options { self.virtdns = Some(virtdns::VirtualDns::new()); self } + + pub fn with_mtu(mut self, mtu: usize) -> Self { + self.mtu = Some(mtu); + self + } } #[derive(Default, Clone, Debug)] @@ -111,8 +122,12 @@ impl Credentials { } } -pub fn main_entry(tun: &str, proxy: &Proxy, options: Options) -> Result<(), Error> { - let mut ttp = TunToProxy::new(tun, options)?; +pub fn main_entry( + interface: &NetworkInterface, + proxy: &Proxy, + options: Options, +) -> Result<(), Error> { + let mut ttp = TunToProxy::new(interface, options)?; match proxy.proxy_type { ProxyType::Socks4 => { ttp.add_connection_manager(SocksManager::new( diff --git a/src/main.rs b/src/main.rs index d62913f..166012c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,8 @@ use std::net::IpAddr; use std::process::ExitCode; use tun2proxy::error::Error; -use tun2proxy::Options; use tun2proxy::{main_entry, Proxy}; +use tun2proxy::{NetworkInterface, Options}; #[cfg(target_os = "linux")] use tun2proxy::setup::{get_default_cidrs, Setup}; @@ -89,7 +89,8 @@ fn main() -> ExitCode { } } - main_entry(&args.tun, &args.proxy, options)?; + let interface = NetworkInterface::Named(args.tun); + main_entry(&interface, &args.proxy, options)?; Ok(()) })() { diff --git a/src/tun2proxy.rs b/src/tun2proxy.rs index b98f03a..3940e3a 100644 --- a/src/tun2proxy.rs +++ b/src/tun2proxy.rs @@ -1,6 +1,6 @@ use crate::error::Error; use crate::virtdevice::VirtualTunDevice; -use crate::{Credentials, Options}; +use crate::{Credentials, NetworkInterface, Options}; use log::{error, info}; use mio::event::Event; use mio::net::TcpStream; @@ -260,8 +260,13 @@ pub(crate) struct TunToProxy<'a> { } impl<'a> TunToProxy<'a> { - pub(crate) fn new(interface: &str, options: Options) -> Result { - let tun = TunTapInterface::new(interface, Medium::Ip)?; + pub(crate) fn new(interface: &NetworkInterface, options: Options) -> Result { + let tun = match interface { + NetworkInterface::Named(name) => TunTapInterface::new(name.as_str(), Medium::Ip)?, + NetworkInterface::Fd(fd) => { + TunTapInterface::from_fd(*fd, Medium::Ip, options.mtu.unwrap_or(1500))? + } + }; let poll = Poll::new()?; poll.registry().register( &mut SourceFd(&tun.as_raw_fd()), diff --git a/tests/proxy.rs b/tests/proxy.rs index 82589d7..8e1f71c 100644 --- a/tests/proxy.rs +++ b/tests/proxy.rs @@ -12,7 +12,7 @@ mod tests { use serial_test::serial; use tun2proxy::setup::{get_default_cidrs, Setup}; - use tun2proxy::{main_entry, Options, Proxy, ProxyType}; + use tun2proxy::{main_entry, NetworkInterface, Options, Proxy, ProxyType}; #[derive(Clone, Debug)] struct Test { @@ -85,7 +85,7 @@ mod tests { Ok(Fork::Child) => { prctl::set_death_signal(signal::SIGINT as isize).unwrap(); let _ = main_entry( - TUN_TEST_DEVICE, + &NetworkInterface::Named(TUN_TEST_DEVICE.into()), &test.proxy, Options::new().with_virtual_dns(), );