Skip to content

Commit 501a9e8

Browse files
authored
Merge pull request #99 from vnt-dev/1.2.x
1.2.x
2 parents 2979f2e + 8aa4736 commit 501a9e8

File tree

14 files changed

+80
-96
lines changed

14 files changed

+80
-96
lines changed

.github/workflows/rust.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
OS: ${{ matrix.OS }}
8787
FEATURES: ${{ matrix.FEATURES }}
8888
steps:
89-
- uses: actions/checkout@v2
89+
- uses: actions/checkout@v3
9090
- name: Init submodules
9191
uses: snickerbockers/submodules-init@v4
9292
- name: Cargo cache
@@ -221,7 +221,7 @@ jobs:
221221
cd ./artifacts
222222
tar -czf vnt-$TARGET-$TAG.tar.gz *
223223
- name: Archive artifact
224-
uses: actions/upload-artifact@v2
224+
uses: actions/upload-artifact@v3
225225
with:
226226
name: vnt-cli
227227
path: |
@@ -233,7 +233,7 @@ jobs:
233233
runs-on: ubuntu-latest
234234
steps:
235235
- name: Download artifacts
236-
uses: actions/download-artifact@v2
236+
uses: actions/download-artifact@v3
237237
with:
238238
name: vnt-cli
239239
path: ./artifacts

Cargo.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "common"
3-
version = "1.2.13"
3+
version = "1.2.14"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

common/src/cli.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn parse_args_config() -> anyhow::Result<Option<(Config, Vec<String>, bool)>
7676
opts.optmulti("", "vnt-mapping", "vnt-mapping", "<mapping>");
7777
opts.optopt("f", "", "配置文件", "<conf>");
7878
opts.optopt("", "compressor", "压缩算法", "<lz4>");
79-
opts.optopt("", "local-ipv4", "指定本地ipv4网卡IP", "<IP>");
79+
opts.optopt("", "local-dev", "指定本地ipv4网卡名称", "<NAME>");
8080
opts.optflag("", "disable-stats", "关闭流量统计");
8181
opts.optflag("", "allow-wg", "允许接入WireGuard");
8282
//"后台运行时,查看其他设备列表"
@@ -284,15 +284,8 @@ pub fn parse_args_config() -> anyhow::Result<Option<(Config, Vec<String>, bool)>
284284
#[cfg(feature = "port_mapping")]
285285
let port_mapping_list = matches.opt_strs("mapping");
286286
let vnt_mapping_list = matches.opt_strs("vnt-mapping");
287-
let local_ipv4: Option<String> = matches.opt_get("local-ipv4").unwrap();
288-
let local_ipv4 = local_ipv4
289-
.map(|v| Ipv4Addr::from_str(&v).expect(&format!("'--local-ipv4 {}' error", v)));
290-
if let Some(local_ipv4) = local_ipv4 {
291-
if local_ipv4.is_unspecified() || local_ipv4.is_broadcast() || local_ipv4.is_multicast()
292-
{
293-
return Err(anyhow::anyhow!("'--local-ipv4 {}' invalid", local_ipv4));
294-
}
295-
}
287+
let local_dev: Option<String> = matches.opt_get("local-dev").unwrap();
288+
296289
let disable_stats = matches.opt_present("disable-stats");
297290
let allow_wire_guard = matches.opt_present("allow-wg");
298291
let compressor = if let Some(compressor) = matches.opt_str("compressor").as_ref() {
@@ -336,7 +329,7 @@ pub fn parse_args_config() -> anyhow::Result<Option<(Config, Vec<String>, bool)>
336329
compressor,
337330
!disable_stats,
338331
allow_wire_guard,
339-
local_ipv4,
332+
local_dev,
340333
)?;
341334
(config, vnt_mapping_list, cmd)
342335
};
@@ -383,7 +376,7 @@ fn get_description(key: &str, language: &str) -> String {
383376
("--compressor-lz4 <lz4>", ("启用压缩,可选值lz4,例如 --compressor lz4", "Enable compression, option lz4, e.g., --compressor lz4")),
384377
("--compressor-zstd <zstd>", ("启用压缩,可选值zstd<,level>,level为压缩级别,例如 --compressor zstd,10", "Enable compression, options zstd<,level>, level is compression level, e.g., --compressor zstd,10")),
385378
("--vnt-mapping <x>", ("vnt地址映射,例如 --vnt-mapping tcp:80-10.26.0.10:80 映射目标是vnt网络或其子网中的设备", "VNT address mapping, e.g., --vnt-mapping tcp:80-10.26.0.10:80 maps to a device in VNT network or its subnet")),
386-
("--local-ipv4", ("本地出口网卡的ipv4地址", "IPv4 address of local export network card")),
379+
("--local-dev", ("本地出口网卡的名称", "name of local export network card")),
387380
("--disable-stats", ("关闭流量统计", "Disable traffic statistics")),
388381
("--allow-wg", ("允许接入WireGuard客户端", "Allow access to WireGuard client")),
389382
("--list", ("后台运行时,查看其他设备列表", "View list of other devices when running in background")),
@@ -572,8 +565,8 @@ fn print_usage(program: &str, _opts: Options) {
572565
green(get_description("--vnt-mapping <x>", &language).to_string())
573566
);
574567
println!(
575-
" --local-ipv4 <IP> {}",
576-
get_description("--local-ipv4", &language)
568+
" --local-dev <NAME> {}",
569+
get_description("--local-dev", &language)
577570
);
578571
println!(
579572
" --disable-stats {}",

common/src/config/file_config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct FileConfig {
4848
pub disable_stats: bool,
4949
// 允许传递wg流量
5050
pub allow_wire_guard: bool,
51-
pub local_ipv4: Option<Ipv4Addr>,
51+
pub local_dev: Option<String>,
5252
}
5353

5454
impl Default for FileConfig {
@@ -94,7 +94,7 @@ impl Default for FileConfig {
9494
vnt_mapping: vec![],
9595
disable_stats: false,
9696
allow_wire_guard: false,
97-
local_ipv4: None,
97+
local_dev: None,
9898
}
9999
}
100100
}
@@ -183,7 +183,7 @@ pub fn read_config(file_path: &str) -> anyhow::Result<(Config, Vec<String>, bool
183183
compressor,
184184
!file_conf.disable_stats,
185185
file_conf.allow_wire_guard,
186-
file_conf.local_ipv4,
186+
file_conf.local_dev,
187187
)?;
188188

189189
Ok((config, file_conf.vnt_mapping, file_conf.cmd))

vn-link-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vn-link-cli"
3-
version = "1.2.13"
3+
version = "1.2.14"
44
edition = "2021"
55

66
[dependencies]

vn-link/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vn-link"
3-
version = "1.2.13"
3+
version = "1.2.14"
44
edition = "2021"
55

66
[dependencies]

vnt-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vnt-cli"
3-
version = "1.2.13"
3+
version = "1.2.14"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

vnt/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vnt"
3-
version = "1.2.13"
3+
version = "1.2.14"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

vnt/packet/src/lib.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ unsigned short getChecksum(unsigned short * iphead, int count)
7171
pub fn cal_checksum(buffer: &[u8]) -> u16 {
7272
use std::io::Cursor;
7373
let mut sum = 0;
74-
let length = buffer.len();
7574
let mut buffer = Cursor::new(buffer);
7675
while let Ok(value) = buffer.read_u16::<BigEndian>() {
7776
sum += u32::from(value);
7877
}
79-
if length & 1 == 1 {
80-
//奇数,说明还有一位,不足的补0
81-
sum += u32c(buffer.read_u8().unwrap(), 0);
78+
if let Ok(l) = buffer.read_u8() {
79+
sum += u32c(l, 0);
8280
}
8381
while sum >> 16 != 0 {
8482
sum = (sum & 0xffff) + (sum >> 16);
@@ -119,9 +117,8 @@ pub fn ipv4_cal_checksum(
119117
while let Ok(value) = buffer.read_u16::<BigEndian>() {
120118
sum += u32::from(value);
121119
}
122-
if length & 1 == 1 {
123-
//奇数,说明还有一位
124-
sum += u32c(buffer.read_u8().unwrap(), 0);
120+
if let Ok(l) = buffer.read_u8() {
121+
sum += u32c(l, 0);
125122
}
126123
while sum >> 16 != 0 {
127124
sum = (sum & 0xffff) + (sum >> 16);

vnt/src/channel/socket/mod.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ use anyhow::{anyhow, Context};
22
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
33
use socket2::Protocol;
44
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
5-
#[cfg(unix)]
6-
pub use unix::*;
7-
#[cfg(windows)]
8-
pub use windows::*;
95

106
#[cfg(unix)]
117
mod unix;
@@ -120,20 +116,23 @@ pub fn bind_udp(
120116
bind_udp_ops(addr, true, default_interface).with_context(|| format!("{}", addr))
121117
}
122118

123-
pub fn get_interface(dest_ip: Ipv4Addr) -> anyhow::Result<LocalInterface> {
119+
pub fn get_interface(dest_name: String) -> anyhow::Result<(LocalInterface, Ipv4Addr)> {
124120
let network_interfaces = NetworkInterface::show()?;
125121
for iface in network_interfaces {
126-
for addr in iface.addr {
127-
if let IpAddr::V4(ip) = addr.ip() {
128-
if ip == dest_ip {
129-
return Ok(LocalInterface {
130-
index: iface.index,
131-
#[cfg(unix)]
132-
name: Some(iface.name),
133-
});
122+
if iface.name == dest_name {
123+
for addr in iface.addr {
124+
if let IpAddr::V4(ip) = addr.ip() {
125+
return Ok((
126+
LocalInterface {
127+
index: iface.index,
128+
#[cfg(unix)]
129+
name: Some(iface.name),
130+
},
131+
ip,
132+
));
134133
}
135134
}
136135
}
137136
}
138-
Err(anyhow!("No network card with IP {} found", dest_ip))
137+
Err(anyhow!("No network card with name {} found", dest_name))
139138
}

vnt/src/channel/socket/unix.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
#[cfg(any(target_os = "linux", target_os = "macos"))]
2-
use crate::channel::socket::get_interface;
31
use crate::channel::socket::{LocalInterface, VntSocketTrait};
42
#[cfg(any(target_os = "linux", target_os = "macos"))]
53
use anyhow::Context;
6-
use std::net::Ipv4Addr;
74

85
#[cfg(target_os = "linux")]
96
impl VntSocketTrait for socket2::Socket {
@@ -32,18 +29,18 @@ impl VntSocketTrait for socket2::Socket {
3229
}
3330
}
3431

35-
#[cfg(any(target_os = "linux", target_os = "macos"))]
36-
pub fn get_best_interface(dest_ip: Ipv4Addr) -> anyhow::Result<LocalInterface> {
37-
match get_interface(dest_ip) {
38-
Ok(iface) => return Ok(iface),
39-
Err(e) => {
40-
log::warn!("not find interface e={:?},ip={}", e, dest_ip);
41-
}
42-
}
43-
// 应该再查路由表找到默认路由的
44-
Ok(LocalInterface::default())
45-
}
46-
#[cfg(target_os = "android")]
47-
pub fn get_best_interface(_dest_ip: Ipv4Addr) -> anyhow::Result<LocalInterface> {
48-
Ok(LocalInterface::default())
49-
}
32+
// #[cfg(any(target_os = "linux", target_os = "macos"))]
33+
// pub fn get_best_interface(dest_ip: Ipv4Addr) -> anyhow::Result<LocalInterface> {
34+
// match get_interface(dest_ip) {
35+
// Ok(iface) => return Ok(iface),
36+
// Err(e) => {
37+
// log::warn!("not find interface e={:?},ip={}", e, dest_ip);
38+
// }
39+
// }
40+
// // 应该再查路由表找到默认路由的
41+
// Ok(LocalInterface::default())
42+
// }
43+
// #[cfg(target_os = "android")]
44+
// pub fn get_best_interface(_dest_ip: Ipv4Addr) -> anyhow::Result<LocalInterface> {
45+
// Ok(LocalInterface::default())
46+
// }

vnt/src/channel/socket/windows.rs

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use std::mem;
2-
use std::net::Ipv4Addr;
32
use std::os::windows::io::AsRawSocket;
43

54
use windows_sys::core::PCSTR;
6-
use windows_sys::Win32::NetworkManagement::IpHelper::GetBestInterfaceEx;
75
use windows_sys::Win32::Networking::WinSock::{
8-
htonl, setsockopt, AF_INET, IPPROTO_IP, IP_UNICAST_IF, SOCKADDR, SOCKADDR_IN, SOCKET_ERROR,
6+
htonl, setsockopt, IPPROTO_IP, IP_UNICAST_IF, SOCKET_ERROR,
97
};
108

119
use crate::channel::socket::{LocalInterface, VntSocketTrait};
@@ -38,21 +36,21 @@ impl VntSocketTrait for socket2::Socket {
3836
}
3937
}
4038

41-
pub fn get_best_interface(dest_ip: Ipv4Addr) -> anyhow::Result<LocalInterface> {
42-
// 获取最佳接口
43-
let index = unsafe {
44-
let mut dest: SOCKADDR_IN = mem::zeroed();
45-
dest.sin_family = AF_INET as u16;
46-
dest.sin_addr.S_un.S_addr = u32::from_ne_bytes(dest_ip.octets());
47-
48-
let mut index: u32 = 0;
49-
if GetBestInterfaceEx(&dest as *const _ as *mut SOCKADDR, &mut index) != 0 {
50-
Err(anyhow::anyhow!(
51-
"Failed to GetBestInterfaceEx: {:?}",
52-
std::io::Error::last_os_error()
53-
))?;
54-
}
55-
index
56-
};
57-
Ok(LocalInterface { index })
58-
}
39+
// pub fn get_best_interface(dest_ip: Ipv4Addr) -> anyhow::Result<LocalInterface> {
40+
// // 获取最佳接口
41+
// let index = unsafe {
42+
// let mut dest: SOCKADDR_IN = mem::zeroed();
43+
// dest.sin_family = AF_INET as u16;
44+
// dest.sin_addr.S_un.S_addr = u32::from_ne_bytes(dest_ip.octets());
45+
//
46+
// let mut index: u32 = 0;
47+
// if GetBestInterfaceEx(&dest as *const _ as *mut SOCKADDR, &mut index) != 0 {
48+
// Err(anyhow::anyhow!(
49+
// "Failed to GetBestInterfaceEx: {:?}",
50+
// std::io::Error::last_os_error()
51+
// ))?;
52+
// }
53+
// index
54+
// };
55+
// Ok(LocalInterface { index })
56+
// }

vnt/src/core/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Config {
9494
enable_traffic: bool,
9595
// 允许传递wg流量
9696
allow_wire_guard: bool,
97-
local_ipv4: Option<Ipv4Addr>,
97+
local_dev: Option<String>,
9898
) -> anyhow::Result<Self> {
9999
#[cfg(windows)]
100100
#[cfg(feature = "integrated_tun")]
@@ -163,12 +163,12 @@ impl Config {
163163
*dest = *mask & *dest;
164164
}
165165
in_ips.sort_by(|(dest1, _, _), (dest2, _, _)| dest2.cmp(dest1));
166-
let local_interface = if let Some(local_ip) = local_ipv4 {
167-
let default_interface = crate::channel::socket::get_interface(local_ip)?;
168-
log::info!("default_interface = {:?}", default_interface);
169-
default_interface
166+
let (local_interface, local_ipv4) = if let Some(local_dev) = local_dev {
167+
let (default_interface, ip) = crate::channel::socket::get_interface(local_dev)?;
168+
log::info!("default_interface = {:?} local_ip= {ip}", default_interface);
169+
(default_interface, Some(ip))
170170
} else {
171-
LocalInterface::default()
171+
(LocalInterface::default(), None)
172172
};
173173
Ok(Self {
174174
#[cfg(feature = "integrated_tun")]

0 commit comments

Comments
 (0)