Initialize Tun Interface based on platform

This commit is contained in:
Jett Chen 2023-12-12 15:48:38 +08:00
parent c346ec5b39
commit 54ec260fe3
6 changed files with 24 additions and 14 deletions

View file

@ -2,7 +2,7 @@ use std::io::Error;
use fehler::throws;
use super::TunInterface;
use super::tokio::TunInterface;
#[derive(Debug, Clone, Default)]
#[cfg_attr(
@ -16,6 +16,8 @@ pub struct TunOptions {
pub no_pi: bool,
/// (Linux) Avoid opening an existing persistant device.
pub tun_excl: bool,
/// (Apple) Retrieve the tun interface
pub tun_retrieve: bool,
/// (Linux) The IP address of the tun interface.
pub address: Option<String>,
}
@ -47,6 +49,7 @@ impl TunOptions {
#[throws]
pub fn open(self) -> TunInterface {
TunInterface::new_with_options(self)?
let ti = super::TunInterface::new_with_options(self)?;
TunInterface::new(ti)?
}
}

View file

@ -35,7 +35,14 @@ impl TunInterface {
#[throws]
#[instrument]
pub fn new_with_options(options: TunOptions) -> TunInterface {
let ti = TunInterface::connect(0)?;
let ti = if options.tun_retrieve{
TunInterface::retrieve().ok_or(Error::new(
std::io::ErrorKind::NotFound,
"No tun interface found",
))?
} else {
TunInterface::connect(0)?
};
ti.configure(options)?;
ti
}