去除linux上tap的支持

This commit is contained in:
lbl8603
2024-05-09 20:10:17 +08:00
parent 035ae46ee5
commit b61c140c24
6 changed files with 29 additions and 47 deletions
+3 -1
View File
@@ -15,6 +15,8 @@
使用stun服务探测客户端NAT类型,不同类型有不同的打洞策略
### -a
加了此参数表示使用tap网卡,默认使用tun网卡,tun网卡效率更高
注意:仅在windows上支持使用tap,用于兼容低版本windows系统(低版本windows不支持wintun
### --nic `<tun0>`
指定虚拟网卡名称,默认tun模式使用vnt-tuntap模式使用vnt-tap
### -i `<in-ip>`、-o `<out-ip>`
@@ -98,7 +100,7 @@
配置文件采用yaml格式,可参考:
```yaml
# 全部参数
tap: false #是否使用tap
tap: false #是否使用tap 仅在windows上支持使用tap
token: xxx #组网token
device_id: xxx #当前设备id
name: windows 11 #当前设备名称
+3 -3
View File
@@ -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,
+3 -2
View File
@@ -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 <id> 设备唯一标识符,不使用--ip参数时,服务端凭此参数分配虚拟ip,注意不能重复");
println!(" -s <server> 注册和中继服务器地址,以'TXT:'开头表示解析TXT记录");
println!(" -e <stun-server> stun服务器,用于探测NAT类型,可使用多个地址,如-e stun1.l.google.com -e stun2.l.google.com");
#[cfg(target_os = "windows")]
println!(" -a 使用tap模式,默认使用tun模式");
println!(" -i <in-ip> 配置点对网(IP代理)时使用,-i 192.168.0.0/24,10.26.0.3表示允许接收网段192.168.0.0/24的数据");
println!(" 并转发到10.26.0.3,可指定多个网段");
+2 -2
View File
@@ -12,7 +12,7 @@ use vnt::core::Config;
use crate::utils::*;
pub fn new_config(env: &mut JNIEnv, config: JObject) -> Result<Config, Error> {
#[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<Config, Error> {
#[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,
+6 -6
View File
@@ -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<Arc<Device>> {
#[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<Arc<Device>> {
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())?);
+12 -33
View File
@@ -19,11 +19,10 @@ pub struct Device {
name: String,
ctl: Fd,
tun: Fd,
mac: Option<[u8; 6]>,
}
impl Device {
pub fn new(name: Option<String>, tap: bool) -> io::Result<Self> {
pub fn new(name: Option<String>) -> io::Result<Self> {
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<String> {
Ok(String::new())
@@ -286,22 +277,10 @@ impl IFace for Device {
}
fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
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<usize> {
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)
}
}