Implement Wireguard

Implements Wireguard
This commit is contained in:
Jett Chen 2023-12-17 01:20:56 +08:00 committed by Conrad Kramer
parent 60257b256a
commit b008762a5b
59 changed files with 3824 additions and 529 deletions

View file

@ -1,17 +1,27 @@
use fehler::throws;
use std::io::Error;
use super::TunInterface;
use fehler::throws;
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
#[cfg(feature = "tokio")]
use super::tokio::TunInterface;
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)
)]
pub struct TunOptions {
/// (Windows + Linux) Name the tun interface.
pub(crate) name: Option<String>,
pub name: Option<String>,
/// (Linux) Don't include packet information.
pub(crate) no_pi: Option<()>,
pub no_pi: bool,
/// (Linux) Avoid opening an existing persistant device.
pub(crate) tun_excl: Option<()>,
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>,
}
impl TunOptions {
@ -24,16 +34,26 @@ impl TunOptions {
self
}
pub fn no_pi(mut self, enable: bool) {
self.no_pi = enable.then_some(());
pub fn no_pi(mut self, enable: bool) -> Self {
self.no_pi = enable;
self
}
pub fn tun_excl(mut self, enable: bool) {
self.tun_excl = enable.then_some(());
pub fn tun_excl(mut self, enable: bool) -> Self {
self.tun_excl = enable;
self
}
pub fn address(mut self, address: impl ToString) -> Self {
self.address = Some(address.to_string());
self
}
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
#[cfg(feature = "tokio")]
#[throws]
pub fn open(self) -> TunInterface {
TunInterface::new_with_options(self)?
let ti = super::TunInterface::new_with_options(self)?;
TunInterface::new(ti)?
}
}