From 1338bcbbc54b8d557a7d0cc36eaee99b7a2e7e1a Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Thu, 9 May 2024 21:25:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E9=99=A4=E4=BB=A3=E7=A0=81=E8=AD=A6?= =?UTF-8?q?=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/tun/src/android/mod.rs | 10 +++++----- vnt/tun/src/lib.rs | 4 +++- vnt/tun/src/linux/device.rs | 15 +++++---------- vnt/tun/src/macos/device.rs | 3 ++- vnt/tun/src/packet/arp/packet.rs | 1 + vnt/tun/src/packet/ethernet/packet.rs | 5 ++++- vnt/tun/src/packet/mod.rs | 2 +- vnt/tun/src/unix/fd.rs | 2 +- vnt/tun/src/windows/netsh.rs | 3 ++- vnt/tun/src/windows/tap/mod.rs | 7 ++----- vnt/tun/src/windows/tun/mod.rs | 7 ++++--- vnt/tun/src/windows/tun/wintun_log.rs | 1 + vnt/tun/src/windows/tun/wintun_raw.rs | 2 ++ 13 files changed, 33 insertions(+), 29 deletions(-) diff --git a/vnt/tun/src/android/mod.rs b/vnt/tun/src/android/mod.rs index d9c7957..79f78b9 100644 --- a/vnt/tun/src/android/mod.rs +++ b/vnt/tun/src/android/mod.rs @@ -14,7 +14,7 @@ impl Device { } } impl Device { - pub fn as_tun_fd(&self) ->&Fd{ + pub fn as_tun_fd(&self) -> &Fd { &self.fd } } @@ -31,7 +31,7 @@ impl IFace for Device { Err(io::Error::from(io::ErrorKind::Unsupported)) } - fn set_ip(&self, address: Ipv4Addr, mask: Ipv4Addr) -> io::Result<()> { + fn set_ip(&self, _address: Ipv4Addr, _mask: Ipv4Addr) -> io::Result<()> { Err(io::Error::from(io::ErrorKind::Unsupported)) } @@ -39,15 +39,15 @@ impl IFace for Device { Err(io::Error::from(io::ErrorKind::Unsupported)) } - fn set_mtu(&self, value: u32) -> io::Result<()> { + fn set_mtu(&self, _value: u32) -> io::Result<()> { Err(io::Error::from(io::ErrorKind::Unsupported)) } - fn add_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr, metric: u16) -> io::Result<()> { + fn add_route(&self, _dest: Ipv4Addr, _netmask: Ipv4Addr, _metric: u16) -> io::Result<()> { Err(io::Error::from(io::ErrorKind::Unsupported)) } - fn delete_route(&self, dest: Ipv4Addr, netmask: Ipv4Addr) -> io::Result<()> { + fn delete_route(&self, _dest: Ipv4Addr, _netmask: Ipv4Addr) -> io::Result<()> { Err(io::Error::from(io::ErrorKind::Unsupported)) } diff --git a/vnt/tun/src/lib.rs b/vnt/tun/src/lib.rs index d287dd9..b597a48 100644 --- a/vnt/tun/src/lib.rs +++ b/vnt/tun/src/lib.rs @@ -3,7 +3,6 @@ /// https://github.com/Tazdevil971/tap-windows /// https://github.com/nulldotblack/wintun pub mod device; -mod packet; #[cfg(target_os = "linux")] mod linux; @@ -29,3 +28,6 @@ mod windows; #[cfg(windows)] pub use windows::Device; + +#[cfg(windows)] +mod packet; diff --git a/vnt/tun/src/linux/device.rs b/vnt/tun/src/linux/device.rs index a6d55b6..77450a3 100644 --- a/vnt/tun/src/linux/device.rs +++ b/vnt/tun/src/linux/device.rs @@ -1,18 +1,17 @@ +#![allow(dead_code)] use std::ffi::{CStr, CString}; use std::net::Ipv4Addr; use std::os::fd::AsRawFd; -use std::process::Command; use std::{io, mem, ptr}; use libc::{ - c_char, c_short, ifreq, AF_INET, IFF_MULTI_QUEUE, IFF_NO_PI, IFF_RUNNING, IFF_TAP, IFF_TUN, + c_char, c_short, ifreq, AF_INET, IFF_MULTI_QUEUE, IFF_NO_PI, IFF_RUNNING, IFF_TUN, IFF_UP, IFNAMSIZ, O_RDWR, SOCK_DGRAM, }; use crate::device::IFace; use crate::linux::route; use crate::linux::sys::*; -use crate::packet; use crate::unix::{exe_cmd, Fd, SockAddr}; pub struct Device { @@ -49,7 +48,7 @@ impl Device { ); } - let device_type: c_short = IFF_TUN as 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; @@ -74,13 +73,9 @@ impl Device { .to_string(); let set_txqueuelen = format!("ifconfig {} txqueuelen 1000", name); if let Err(e) = exe_cmd(&set_txqueuelen) { - log::warn!("{:?}",e); - } - Device { - name, - tun, - ctl, + log::warn!("{:?}", e); } + Device { name, tun, ctl } }; device.enabled(true)?; Ok(device) diff --git a/vnt/tun/src/macos/device.rs b/vnt/tun/src/macos/device.rs index b423632..b7d70eb 100644 --- a/vnt/tun/src/macos/device.rs +++ b/vnt/tun/src/macos/device.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use std::ffi::{c_void, CStr}; use std::net::Ipv4Addr; use std::os::fd::AsRawFd; @@ -225,7 +226,7 @@ impl Device { } } impl Device { - pub fn as_tun_fd(&self) ->&Fd{ + pub fn as_tun_fd(&self) -> &Fd { &self.tun } } diff --git a/vnt/tun/src/packet/arp/packet.rs b/vnt/tun/src/packet/arp/packet.rs index ea027a3..a2c96cd 100644 --- a/vnt/tun/src/packet/arp/packet.rs +++ b/vnt/tun/src/packet/arp/packet.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use std::{fmt, io}; /// 地址解析协议,由IP地址找到MAC地址 diff --git a/vnt/tun/src/packet/ethernet/packet.rs b/vnt/tun/src/packet/ethernet/packet.rs index 440d61a..a3f7460 100644 --- a/vnt/tun/src/packet/ethernet/packet.rs +++ b/vnt/tun/src/packet/ethernet/packet.rs @@ -21,7 +21,10 @@ impl> EthernetPacket { let packet = EthernetPacket::unchecked(buffer); //头部固定14位 if packet.buffer.as_ref().len() < 14 { - Err(io::Error::new(io::ErrorKind::InvalidData,format!("len={}", packet.buffer.as_ref().len())))?; + Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("len={}", packet.buffer.as_ref().len()), + ))?; } Ok(packet) diff --git a/vnt/tun/src/packet/mod.rs b/vnt/tun/src/packet/mod.rs index 07905b7..aee2cf1 100644 --- a/vnt/tun/src/packet/mod.rs +++ b/vnt/tun/src/packet/mod.rs @@ -13,7 +13,7 @@ where let mut eth_buf = [0; 65536]; loop { let len = read_fn(&mut eth_buf)?; - if len == 0{ + if len == 0 { return Ok(len); } //处理arp包 diff --git a/vnt/tun/src/unix/fd.rs b/vnt/tun/src/unix/fd.rs index bdb0098..4c3f973 100644 --- a/vnt/tun/src/unix/fd.rs +++ b/vnt/tun/src/unix/fd.rs @@ -1,6 +1,6 @@ +use libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK}; use std::io; use std::os::fd::{AsRawFd, IntoRawFd, RawFd}; -use libc::{F_GETFL, F_SETFL, fcntl, O_NONBLOCK}; pub struct Fd(pub RawFd); diff --git a/vnt/tun/src/windows/netsh.rs b/vnt/tun/src/windows/netsh.rs index 2fed74a..d2e3962 100644 --- a/vnt/tun/src/windows/netsh.rs +++ b/vnt/tun/src/windows/netsh.rs @@ -1,6 +1,7 @@ +#![allow(dead_code)] use crate::windows::exe_cmd; +use std::io; use std::net::Ipv4Addr; -use std::{io, process}; /// 设置网卡名称 pub fn set_interface_name(old_name: &str, new_name: &str) -> io::Result<()> { diff --git a/vnt/tun/src/windows/tap/mod.rs b/vnt/tun/src/windows/tap/mod.rs index bb3024e..74c5c0a 100644 --- a/vnt/tun/src/windows/tap/mod.rs +++ b/vnt/tun/src/windows/tap/mod.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use std::io; use std::net::Ipv4Addr; use winapi::shared::ifdef::NET_LUID; @@ -11,8 +12,6 @@ use winapi::um::winnt::{ use crate::device::IFace; use crate::packet; -use crate::packet::ethernet::protocol::Protocol; -use crate::packet::{arp, ethernet}; use crate::windows::{ctl_code, decode_utf16, encode_utf16, ffi, netsh, route}; /* Present in 8.1 */ @@ -97,7 +96,7 @@ impl Device { let index = ffi::luid_to_index(&luid).map(|index| index as u32)?; // 设置网卡跃点 if let Err(e) = netsh::set_interface_metric(index, 0) { - log::warn!("{:?}",e); + log::warn!("{:?}", e); } let device = Self { handle, @@ -122,8 +121,6 @@ impl Device { } } -const MAC: [u8; 6] = [0xf, 0xf, 0xf, 0xf, 0xe, 0x9]; - impl IFace for Device { fn version(&self) -> io::Result { let mut version = [0u32; 3]; diff --git a/vnt/tun/src/windows/tun/mod.rs b/vnt/tun/src/windows/tun/mod.rs index 0d027c9..62c0326 100644 --- a/vnt/tun/src/windows/tun/mod.rs +++ b/vnt/tun/src/windows/tun/mod.rs @@ -1,4 +1,5 @@ -use libloading::{Error, Library}; +#![allow(dead_code)] +use libloading::Library; use std::io; use std::net::Ipv4Addr; @@ -84,7 +85,7 @@ impl Device { //SAFETY: guid is a unique integer so transmuting either all zeroes or the user's preferred //guid to the winapi guid type is safe and will allow the windows kernel to see our GUID - let guid_struct: wintun_raw::GUID = unsafe { std::mem::transmute(guid) }; + let guid_struct: wintun_raw::GUID = std::mem::transmute(guid); let guid_ptr = &guid_struct as *const wintun_raw::GUID; //SAFETY: the function is loaded from the wintun dll properly, we are providing valid @@ -118,7 +119,7 @@ impl Device { let index = ffi::luid_to_index(&std::mem::transmute(luid)).map(|index| index as u32)?; // 设置网卡跃点 if let Err(e) = netsh::set_interface_metric(index, 0) { - log::warn!("{:?}",e); + log::warn!("{:?}", e); } Ok(Self { luid: std::mem::transmute(luid), diff --git a/vnt/tun/src/windows/tun/wintun_log.rs b/vnt/tun/src/windows/tun/wintun_log.rs index aa50546..4472cf2 100644 --- a/vnt/tun/src/windows/tun/wintun_log.rs +++ b/vnt/tun/src/windows/tun/wintun_log.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use log::*; use crate::windows::tun::wintun_raw; diff --git a/vnt/tun/src/windows/tun/wintun_raw.rs b/vnt/tun/src/windows/tun/wintun_raw.rs index 9e11d33..e754bd5 100644 --- a/vnt/tun/src/windows/tun/wintun_raw.rs +++ b/vnt/tun/src/windows/tun/wintun_raw.rs @@ -1,3 +1,5 @@ +#![allow(non_snake_case)] +#![allow(non_camel_case_types)] /* automatically generated by rust-bindgen 0.59.1 */ #[repr(C)] #[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]