mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-04-20 22:09:09 +00:00
Add file descriptor support
This commit is contained in:
parent
9437308283
commit
500f6ef21f
5 changed files with 31 additions and 10 deletions
|
@ -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"
|
||||
|
||||
|
|
19
src/lib.rs
19
src/lib.rs
|
@ -18,6 +18,11 @@ pub struct Proxy {
|
|||
pub credentials: Option<Credentials>,
|
||||
}
|
||||
|
||||
pub enum NetworkInterface {
|
||||
Named(String),
|
||||
Fd(std::os::fd::RawFd),
|
||||
}
|
||||
|
||||
impl Proxy {
|
||||
pub fn from_url(s: &str) -> Result<Proxy, Error> {
|
||||
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<virtdns::VirtualDns>,
|
||||
mtu: Option<usize>,
|
||||
}
|
||||
|
||||
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(
|
||||
|
|
|
@ -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(())
|
||||
})() {
|
||||
|
|
|
@ -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<Self, Error> {
|
||||
let tun = TunTapInterface::new(interface, Medium::Ip)?;
|
||||
pub(crate) fn new(interface: &NetworkInterface, options: Options) -> Result<Self, Error> {
|
||||
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()),
|
||||
|
|
|
@ -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(),
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue