From ca76c35f6acb730a0c2a274ba0c5d64e7442c312 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Mon, 4 Sep 2023 20:35:06 +0800 Subject: [PATCH] =?UTF-8?q?linux=E5=9B=BA=E5=AE=9A=E7=BD=91=E5=8D=A1?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=EF=BC=8C=E5=90=AF=E5=8A=A8=E6=97=B6=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E7=BD=91=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/tun_tap_device/linux.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/vnt/src/tun_tap_device/linux.rs b/vnt/src/tun_tap_device/linux.rs index 7282a07..753137f 100644 --- a/vnt/src/tun_tap_device/linux.rs +++ b/vnt/src/tun_tap_device/linux.rs @@ -7,6 +7,9 @@ use std::process::Command; use std::sync::Arc; use tun::Device; +pub const TUN_INTERFACE_NAME: &str = "vnt-tun"; +pub const TAP_INTERFACE_NAME: &str = "vnt-tap"; + impl DeviceWriter { pub fn change_ip( &self, @@ -88,8 +91,11 @@ pub fn create_device( // .queues(2) 用多个队列有兼容性问题 .up(); match device_type { - DeviceType::Tun => {} + DeviceType::Tun => { + config.name(TUN_INTERFACE_NAME); + } DeviceType::Tap => { + config.name(TAP_INTERFACE_NAME); config.layer(tun::Layer::L2); } } @@ -154,4 +160,16 @@ pub fn create_device( )) } -pub fn delete_device(_device_type: DeviceType) {} +pub fn delete_device(_device_type: DeviceType) { + for name in [TUN_INTERFACE_NAME, TAP_INTERFACE_NAME] { + let cmd = format!("sudo ip link delete {}", 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); + } + } +}