Add file descriptor support

This commit is contained in:
B. Blechschmidt 2023-04-10 23:24:53 +02:00
parent 9437308283
commit 500f6ef21f
5 changed files with 31 additions and 10 deletions

View file

@ -20,7 +20,7 @@ log = "0.4"
mio = { version = "0.8", features = ["os-poll", "net", "os-ext"] } mio = { version = "0.8", features = ["os-poll", "net", "os-ext"] }
nix = { version = "0.26", features = ["process", "signal"] } nix = { version = "0.26", features = ["process", "signal"] }
prctl = "1.0" 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" thiserror = "1.0"
url = "2.3" url = "2.3"

View file

@ -18,6 +18,11 @@ pub struct Proxy {
pub credentials: Option<Credentials>, pub credentials: Option<Credentials>,
} }
pub enum NetworkInterface {
Named(String),
Fd(std::os::fd::RawFd),
}
impl Proxy { impl Proxy {
pub fn from_url(s: &str) -> Result<Proxy, Error> { pub fn from_url(s: &str) -> Result<Proxy, Error> {
let e = format!("`{s}` is not a valid proxy URL"); let e = format!("`{s}` is not a valid proxy URL");
@ -83,6 +88,7 @@ impl std::fmt::Display for ProxyType {
#[derive(Default)] #[derive(Default)]
pub struct Options { pub struct Options {
virtdns: Option<virtdns::VirtualDns>, virtdns: Option<virtdns::VirtualDns>,
mtu: Option<usize>,
} }
impl Options { impl Options {
@ -94,6 +100,11 @@ impl Options {
self.virtdns = Some(virtdns::VirtualDns::new()); self.virtdns = Some(virtdns::VirtualDns::new());
self self
} }
pub fn with_mtu(mut self, mtu: usize) -> Self {
self.mtu = Some(mtu);
self
}
} }
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]
@ -111,8 +122,12 @@ impl Credentials {
} }
} }
pub fn main_entry(tun: &str, proxy: &Proxy, options: Options) -> Result<(), Error> { pub fn main_entry(
let mut ttp = TunToProxy::new(tun, options)?; interface: &NetworkInterface,
proxy: &Proxy,
options: Options,
) -> Result<(), Error> {
let mut ttp = TunToProxy::new(interface, options)?;
match proxy.proxy_type { match proxy.proxy_type {
ProxyType::Socks4 => { ProxyType::Socks4 => {
ttp.add_connection_manager(SocksManager::new( ttp.add_connection_manager(SocksManager::new(

View file

@ -5,8 +5,8 @@ use std::net::IpAddr;
use std::process::ExitCode; use std::process::ExitCode;
use tun2proxy::error::Error; use tun2proxy::error::Error;
use tun2proxy::Options;
use tun2proxy::{main_entry, Proxy}; use tun2proxy::{main_entry, Proxy};
use tun2proxy::{NetworkInterface, Options};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
use tun2proxy::setup::{get_default_cidrs, Setup}; 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(()) Ok(())
})() { })() {

View file

@ -1,6 +1,6 @@
use crate::error::Error; use crate::error::Error;
use crate::virtdevice::VirtualTunDevice; use crate::virtdevice::VirtualTunDevice;
use crate::{Credentials, Options}; use crate::{Credentials, NetworkInterface, Options};
use log::{error, info}; use log::{error, info};
use mio::event::Event; use mio::event::Event;
use mio::net::TcpStream; use mio::net::TcpStream;
@ -260,8 +260,13 @@ pub(crate) struct TunToProxy<'a> {
} }
impl<'a> TunToProxy<'a> { impl<'a> TunToProxy<'a> {
pub(crate) fn new(interface: &str, options: Options) -> Result<Self, Error> { pub(crate) fn new(interface: &NetworkInterface, options: Options) -> Result<Self, Error> {
let tun = TunTapInterface::new(interface, Medium::Ip)?; 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()?; let poll = Poll::new()?;
poll.registry().register( poll.registry().register(
&mut SourceFd(&tun.as_raw_fd()), &mut SourceFd(&tun.as_raw_fd()),

View file

@ -12,7 +12,7 @@ mod tests {
use serial_test::serial; use serial_test::serial;
use tun2proxy::setup::{get_default_cidrs, Setup}; 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)] #[derive(Clone, Debug)]
struct Test { struct Test {
@ -85,7 +85,7 @@ mod tests {
Ok(Fork::Child) => { Ok(Fork::Child) => {
prctl::set_death_signal(signal::SIGINT as isize).unwrap(); prctl::set_death_signal(signal::SIGINT as isize).unwrap();
let _ = main_entry( let _ = main_entry(
TUN_TEST_DEVICE, &NetworkInterface::Named(TUN_TEST_DEVICE.into()),
&test.proxy, &test.proxy,
Options::new().with_virtual_dns(), Options::new().with_virtual_dns(),
); );