From b61c140c24f0216d790be56aed4939045d28e91e Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Thu, 9 May 2024 20:10:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4linux=E4=B8=8Atap=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt-cli/README.md | 4 ++- vnt-cli/src/config/mod.rs | 6 ++-- vnt-cli/src/main.rs | 5 +-- vnt-jni/src/config.rs | 4 +-- vnt/src/tun_tap_device/create_device.rs | 12 +++---- vnt/tun/src/linux/device.rs | 45 +++++++------------------ 6 files changed, 29 insertions(+), 47 deletions(-) diff --git a/vnt-cli/README.md b/vnt-cli/README.md index 52da0e0..97c5ea2 100644 --- a/vnt-cli/README.md +++ b/vnt-cli/README.md @@ -15,6 +15,8 @@ 使用stun服务探测客户端NAT类型,不同类型有不同的打洞策略 ### -a 加了此参数表示使用tap网卡,默认使用tun网卡,tun网卡效率更高 + +注意:仅在windows上支持使用tap,用于兼容低版本windows系统(低版本windows不支持wintun) ### --nic `` 指定虚拟网卡名称,默认tun模式使用vnt-tun,tap模式使用vnt-tap ### -i ``、-o `` @@ -98,7 +100,7 @@ 配置文件采用yaml格式,可参考: ```yaml # 全部参数 -tap: false #是否使用tap +tap: false #是否使用tap 仅在windows上支持使用tap token: xxx #组网token device_id: xxx #当前设备id name: windows 11 #当前设备名称 diff --git a/vnt-cli/src/config/mod.rs b/vnt-cli/src/config/mod.rs index 3d9dfff..0cbe330 100644 --- a/vnt-cli/src/config/mod.rs +++ b/vnt-cli/src/config/mod.rs @@ -12,7 +12,7 @@ use vnt::core::Config; #[derive(Serialize, Deserialize, Debug)] #[serde(default)] pub struct FileConfig { - #[cfg(any(target_os = "windows", target_os = "linux"))] + #[cfg(target_os = "windows")] pub tap: bool, pub token: String, pub device_id: String, @@ -47,7 +47,7 @@ pub struct FileConfig { impl Default for FileConfig { fn default() -> Self { Self { - #[cfg(any(target_os = "windows", target_os = "linux"))] + #[cfg(target_os = "windows")] tap: false, token: "".to_string(), device_id: get_device_id(), @@ -134,7 +134,7 @@ pub fn read_config(file_path: &str) -> io::Result<(Config, bool)> { let use_channel_type = UseChannelType::from_str(&file_conf.use_channel) .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; let config = Config::new( - #[cfg(any(target_os = "windows", target_os = "linux"))] + #[cfg(target_os = "windows")] file_conf.tap, file_conf.token, file_conf.device_id, diff --git a/vnt-cli/src/main.rs b/vnt-cli/src/main.rs index 3f1c0c4..e3611c6 100644 --- a/vnt-cli/src/main.rs +++ b/vnt-cli/src/main.rs @@ -131,7 +131,7 @@ fn main() { println!("parameter -k not found ."); return; } - #[cfg(any(target_os = "windows", target_os = "linux"))] + #[cfg(target_os = "windows")] let tap = matches.opt_present("a"); let device_name = matches.opt_str("nic"); let token: String = matches.opt_get("k").unwrap().unwrap(); @@ -291,7 +291,7 @@ fn main() { #[cfg(feature = "port_mapping")] let port_mapping_list = matches.opt_strs("mapping"); let config = match Config::new( - #[cfg(any(target_os = "windows", target_os = "linux"))] + #[cfg(target_os = "windows")] tap, token, device_id, @@ -426,6 +426,7 @@ fn print_usage(program: &str, _opts: Options) { println!(" -d 设备唯一标识符,不使用--ip参数时,服务端凭此参数分配虚拟ip,注意不能重复"); println!(" -s 注册和中继服务器地址,以'TXT:'开头表示解析TXT记录"); println!(" -e stun服务器,用于探测NAT类型,可使用多个地址,如-e stun1.l.google.com -e stun2.l.google.com"); + #[cfg(target_os = "windows")] println!(" -a 使用tap模式,默认使用tun模式"); println!(" -i 配置点对网(IP代理)时使用,-i 192.168.0.0/24,10.26.0.3表示允许接收网段192.168.0.0/24的数据"); println!(" 并转发到10.26.0.3,可指定多个网段"); diff --git a/vnt-jni/src/config.rs b/vnt-jni/src/config.rs index eb1db2d..2a94cab 100644 --- a/vnt-jni/src/config.rs +++ b/vnt-jni/src/config.rs @@ -12,7 +12,7 @@ use vnt::core::Config; use crate::utils::*; pub fn new_config(env: &mut JNIEnv, config: JObject) -> Result { - #[cfg(any(target_os = "windows", target_os = "linux"))] + #[cfg(target_os = "windows")] let tap = env.get_field(&config, "tap", "Z")?.z()?; let token = to_string_not_null(env, &config, "token")?; let name = to_string_not_null(env, &config, "name")?; @@ -90,7 +90,7 @@ pub fn new_config(env: &mut JNIEnv, config: JObject) -> Result { #[cfg(not(target_os = "android"))] let device_name = to_string(env, &config, "deviceName")?; let config = match Config::new( - #[cfg(any(target_os = "windows", target_os = "linux"))] + #[cfg(target_os = "windows")] tap, token, device_id, diff --git a/vnt/src/tun_tap_device/create_device.rs b/vnt/src/tun_tap_device/create_device.rs index 30a904f..5b6fd4e 100644 --- a/vnt/src/tun_tap_device/create_device.rs +++ b/vnt/src/tun_tap_device/create_device.rs @@ -5,12 +5,12 @@ use tun::Device; #[cfg(any(target_os = "windows", target_os = "linux"))] const DEFAULT_TUN_NAME: &str = "vnt-tun"; -#[cfg(any(target_os = "windows", target_os = "linux"))] +#[cfg(target_os = "windows")] const DEFAULT_TAP_NAME: &str = "vnt-tap"; #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] pub fn create_device(config: &crate::core::Config) -> io::Result> { - #[cfg(any(target_os = "windows", target_os = "linux"))] + #[cfg(target_os = "windows")] let default_name: &str = if config.tap { DEFAULT_TAP_NAME } else { @@ -21,11 +21,11 @@ pub fn create_device(config: &crate::core::Config) -> io::Result> { let device_name = config .device_name .clone() - .unwrap_or(default_name.to_string()); - if &device_name == default_name { - delete_device(default_name); + .unwrap_or(DEFAULT_TUN_NAME.to_string()); + if &device_name == DEFAULT_TUN_NAME { + delete_device(DEFAULT_TUN_NAME); } - Arc::new(Device::new(Some(device_name), config.tap)?) + Arc::new(Device::new(Some(device_name))?) }; #[cfg(target_os = "macos")] let device = Arc::new(Device::new(config.device_name.clone())?); diff --git a/vnt/tun/src/linux/device.rs b/vnt/tun/src/linux/device.rs index f84ccf2..a6d55b6 100644 --- a/vnt/tun/src/linux/device.rs +++ b/vnt/tun/src/linux/device.rs @@ -19,11 +19,10 @@ pub struct Device { name: String, ctl: Fd, tun: Fd, - mac: Option<[u8; 6]>, } impl Device { - pub fn new(name: Option, tap: bool) -> io::Result { + pub fn new(name: Option) -> io::Result { let device = unsafe { let dev = match name { Some(name) => { @@ -50,7 +49,7 @@ impl Device { ); } - let device_type: c_short = if tap { IFF_TAP } else { IFF_TUN } as c_short; + let device_type: c_short = IFF_TUN as c_short;//if tap { IFF_TAP } else { IFF_TUN } as c_short; let queues_num = 1; @@ -73,28 +72,14 @@ impl Device { let name = CStr::from_ptr(req.ifr_name.as_ptr()) .to_string_lossy() .to_string(); - let mac = if tap { - let get_mac_cmd = format!("cat /sys/class/net/{}/address", name); - let mac_out = exe_cmd(&get_mac_cmd)?; - let mac_str = String::from_utf8(mac_out.stdout).unwrap(); - let mut mac = [0; 6]; - let mut split = mac_str.split(":"); - for i in 0..6 { - mac[i] = u8::from_str_radix(&split.next().unwrap()[..2], 16).unwrap(); - } - Some(mac) - } else { - None - }; - let set_txqueuelen = format!("ifconfig {} txqueuelen 1000", name); - if let Err(e) = exe_cmd(&set_txqueuelen){ + let set_txqueuelen = format!("ifconfig {} txqueuelen 1000", name); + if let Err(e) = exe_cmd(&set_txqueuelen) { log::warn!("{:?}",e); } Device { name, tun, ctl, - mac, } }; device.enabled(true)?; @@ -234,6 +219,12 @@ impl Device { } } +impl Device { + pub fn as_tun_fd(&self) -> &Fd { + &self.tun + } +} + impl IFace for Device { fn version(&self) -> io::Result { Ok(String::new()) @@ -286,22 +277,10 @@ impl IFace for Device { } fn read(&self, buf: &mut [u8]) -> io::Result { - if self.mac.is_some() { - packet::read_tap( - buf, - |eth_buf| self.tun.read(eth_buf), - |eth_buf| self.tun.write(eth_buf), - ) - } else { - self.tun.read(buf) - } + self.tun.read(buf) } fn write(&self, buf: &[u8]) -> io::Result { - if let Some(mac) = &self.mac { - packet::write_tap(buf, |eth_buf| self.tun.write(eth_buf), mac) - } else { - self.tun.write(buf) - } + self.tun.write(buf) } }