[mio] 创建tun/tap设备
This commit is contained in:
@@ -1,52 +1,62 @@
|
|||||||
#[cfg(target_os = "android")]
|
use std::io;
|
||||||
mod android;
|
use std::sync::Arc;
|
||||||
#[cfg(any(target_os = "linux"))]
|
|
||||||
mod linux;
|
|
||||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
||||||
mod linux_mac;
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
mod mac;
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
mod windows;
|
|
||||||
|
|
||||||
#[cfg(target_os = "android")]
|
use tun::device::IFace;
|
||||||
pub use android::create;
|
use tun::Device;
|
||||||
#[cfg(target_os = "android")]
|
|
||||||
pub use android::{DeviceReader, DeviceWriter};
|
|
||||||
#[cfg(any(target_os = "linux"))]
|
|
||||||
pub use linux::create_device;
|
|
||||||
#[cfg(any(target_os = "linux"))]
|
|
||||||
pub use linux::delete_device;
|
|
||||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
||||||
pub use linux_mac::{DeviceReader, DeviceWriter};
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
pub use mac::create_device;
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
pub use mac::delete_device;
|
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
use crate::core::Config;
|
||||||
pub use windows::create_device;
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
pub use windows::delete_device;
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
pub use windows::{DeviceReader, DeviceWriter};
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
const DEFAULT_NAME: &str = "vnt0";
|
||||||
pub enum DeviceType {
|
|
||||||
Tun,
|
pub fn create_device(config: &Config) -> io::Result<Arc<Device>> {
|
||||||
Tap,
|
#[cfg(target_os = "linux")]
|
||||||
|
let device = {
|
||||||
|
let device_name = config
|
||||||
|
.device_name
|
||||||
|
.clone()
|
||||||
|
.unwrap_or(DEFAULT_NAME.to_string());
|
||||||
|
if &device_name == DEFAULT_NAME {
|
||||||
|
delete_device();
|
||||||
|
}
|
||||||
|
Arc::new(Device::new(Some(&device_name), config.tap)?)
|
||||||
|
};
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
let device = Arc::new(Device::new(Some(&config.device_name.clone()))?);
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
let device = Arc::new(Device::new(
|
||||||
|
&config
|
||||||
|
.device_name
|
||||||
|
.as_ref()
|
||||||
|
.unwrap_or(&DEFAULT_NAME.to_string()),
|
||||||
|
config.tap,
|
||||||
|
)?);
|
||||||
|
#[cfg(target_os = "android")]
|
||||||
|
let device = Arc::new(Device::new(config.device_fd as _)?);
|
||||||
|
#[cfg(not(target_os = "android"))]
|
||||||
|
{
|
||||||
|
let mtu = config.mtu.unwrap_or_else(|| {
|
||||||
|
if config.password.is_none() {
|
||||||
|
1450
|
||||||
|
} else {
|
||||||
|
1410
|
||||||
|
}
|
||||||
|
});
|
||||||
|
device.set_mtu(mtu)?;
|
||||||
|
}
|
||||||
|
Ok(device)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeviceType {
|
#[cfg(target_os = "linux")]
|
||||||
pub fn is_tun(&self) -> bool {
|
fn delete_device() {
|
||||||
*self == DeviceType::Tun
|
// 删除默认网卡,此操作有风险,后续可能去除
|
||||||
|
use std::process::Command;
|
||||||
|
let cmd = format!("ip link delete {}", DEFAULT_NAME);
|
||||||
|
let delete_tun = Command::new("sh")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(&cmd)
|
||||||
|
.output()
|
||||||
|
.expect("sh exec error!");
|
||||||
|
if !delete_tun.status.success() {
|
||||||
|
log::warn!("删除网卡失败:{:?}", delete_tun);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DriverInfo {
|
|
||||||
pub device_type: DeviceType,
|
|
||||||
pub name: String,
|
|
||||||
pub version: String,
|
|
||||||
pub mac: Option<String>,
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user