Skip to content

Commit

Permalink
Merge pull request #60 from lbl8603/1.2.x
Browse files Browse the repository at this point in the history
1.2.x
  • Loading branch information
vnt-dev authored Jul 13, 2024
2 parents dc45602 + 14ee9c4 commit aa332b4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 62 deletions.
5 changes: 4 additions & 1 deletion common/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ pub fn parse_args_config() -> anyhow::Result<Option<(Config, Vec<String>, bool)>
} else if matches.opt_present("all") {
command::command(command::CommandEnum::All);
return Ok(None);
} else if matches.opt_present("chart_a") {
}
#[cfg(feature = "command")]
if matches.opt_present("chart_a") {
command::command(command::CommandEnum::ChartA);
return Ok(None);
}
#[cfg(feature = "command")]
if let Some(v) = matches.opt_str("chart_b") {
command::command(command::CommandEnum::ChartB(v));
return Ok(None);
Expand Down
1 change: 0 additions & 1 deletion vnt/src/core/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ pub fn start<Call: VntCallback>(
&scheduler,
context.clone(),
current_device.clone(),
server_cipher.clone(),
nat_test.clone(),
config_info.clone(),
);
Expand Down
55 changes: 6 additions & 49 deletions vnt/src/handle/maintain/addr_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,30 @@ use crossbeam_utils::atomic::AtomicCell;

use crate::channel::context::ChannelContext;
use crate::channel::punch::NatType;
use crate::cipher::Cipher;
use crate::handle::{BaseConfigInfo, CurrentDeviceInfo};
use crate::nat::NatTest;
use crate::protocol::body::ENCRYPTION_RESERVED;
use crate::protocol::{control_packet, NetPacket, Protocol, MAX_TTL};
use crate::util::Scheduler;

pub fn addr_request(
scheduler: &Scheduler,
context: ChannelContext,
current_device_info: Arc<AtomicCell<CurrentDeviceInfo>>,
server_cipher: Cipher,
nat_test: NatTest,
_config: BaseConfigInfo,
) {
pub_address_request(
scheduler,
context,
current_device_info.clone(),
server_cipher,
nat_test,
0,
);
pub_address_request(scheduler, context, current_device_info.clone(), nat_test, 0);
}

fn pub_address_request(
scheduler: &Scheduler,
context: ChannelContext,
current_device_info: Arc<AtomicCell<CurrentDeviceInfo>>,
server_cipher: Cipher,
nat_test: NatTest,
count: usize,
) {
let channel_num = context.channel_num();
let index = count % channel_num;
if let Err(e) = addr_request0(
&context,
&current_device_info,
&server_cipher,
&nat_test,
index,
) {
if let Err(e) = addr_request0(&context, &current_device_info, &nat_test, index) {
log::warn!("{:?}", e);
}
let nat_info = nat_test.nat_info();
Expand All @@ -58,22 +40,15 @@ fn pub_address_request(
if index == channel_num - 1 {
19
} else {
7
9
}
}
} else {
3
};

let rs = scheduler.timeout(Duration::from_secs(time), move |s| {
pub_address_request(
s,
context,
current_device_info,
server_cipher,
nat_test,
index + 1,
)
pub_address_request(s, context, current_device_info, nat_test, index + 1)
});
if !rs {
log::info!("定时任务停止");
Expand All @@ -83,32 +58,14 @@ fn pub_address_request(
fn addr_request0(
context: &ChannelContext,
current_device: &AtomicCell<CurrentDeviceInfo>,
server_cipher: &Cipher,
nat_test: &NatTest,
index: usize,
) -> anyhow::Result<()> {
let current_dev = current_device.load();
if current_dev.status.offline() {
return Ok(());
}

if current_dev.connect_server.is_ipv4() && !context.main_protocol().is_base_tcp() {
// 如果连接的是ipv4服务,则探测公网端口
let gateway_ip = current_dev.virtual_gateway;
let src_ip = current_dev.virtual_ip;
let mut packet = NetPacket::new_encrypt([0; 12 + ENCRYPTION_RESERVED]).unwrap();
packet.set_default_version();
packet.set_gateway_flag(true);
packet.set_protocol(Protocol::Control);
packet.set_transport_protocol(control_packet::Protocol::AddrRequest.into());
packet.first_set_ttl(MAX_TTL);
packet.set_source(src_ip);
packet.set_destination(gateway_ip);
server_cipher.encrypt_ipv4(&mut packet)?;
context.send_main_udp(index, packet.buffer(), current_dev.connect_server)?;
} else {
let (data, addr) = nat_test.send_data()?;
context.send_main_udp(index, &data, addr)?;
}
let (data, addr) = nat_test.send_data()?;
context.send_main_udp(index, &data, addr)?;
Ok(())
}
2 changes: 1 addition & 1 deletion vnt/src/handle/maintain/punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn punch0(
|| nat_info.public_ports.iter().filter(|&&v| v == 0).count()
> nat_info.public_ports.len() / 2)
{
log::info!("公网地址为空,暂时放弃打洞,第{}轮", total_count);
log::info!("未获取到公网地址,暂时放弃打洞,第{}轮", total_count);
return Ok(());
}
let current_ip = current_device.virtual_ip;
Expand Down
2 changes: 1 addition & 1 deletion vnt/src/handle/maintain/re_nat_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn retrieve_nat_type0(
};
#[cfg(feature = "upnp")]
nat_test.reset_upnp();
log::info!("刷新nat成功")
log::info!("刷新nat结束")
}
})
.expect("natTest");
Expand Down
22 changes: 13 additions & 9 deletions vnt/src/nat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Context;
use anyhow::{anyhow, Context};
use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, ToSocketAddrs};
use std::net::{SocketAddr, UdpSocket};
Expand Down Expand Up @@ -139,17 +139,12 @@ impl Into<NatType> for PunchNatType {
impl NatTest {
pub fn new(
_channel_num: usize,
mut stun_server: Vec<String>,
stun_server: Vec<String>,
local_ipv4: Option<Ipv4Addr>,
ipv6: Option<Ipv6Addr>,
udp_ports: Vec<u16>,
tcp_port: u16,
) -> NatTest {
if stun_server.len() > 5 {
stun_server.shuffle(&mut rand::thread_rng());
stun_server.truncate(5);
log::info!("stun_server truncate {:?}", stun_server);
}
let ports = vec![0; udp_ports.len()];
let nat_info = NatInfo::new(
Vec::new(),
Expand Down Expand Up @@ -262,8 +257,17 @@ impl NatTest {
&self,
local_ipv4: Option<Ipv4Addr>,
ipv6: Option<Ipv6Addr>,
) -> io::Result<NatInfo> {
let (nat_type, public_ips, port_range) = stun::stun_test_nat(self.stun_server.clone())?;
) -> anyhow::Result<NatInfo> {
let mut stun_server = self.stun_server.clone();
if stun_server.len() > 5 {
stun_server.shuffle(&mut rand::thread_rng());
stun_server.truncate(5);
log::info!("stun_server truncate {:?}", stun_server);
}
let (nat_type, public_ips, port_range) = stun::stun_test_nat(stun_server)?;
if public_ips.is_empty() {
Err(anyhow!("public_ips.is_empty"))?
}
let mut guard = self.info.lock();
guard.nat_type = nat_type;
guard.public_ips = public_ips;
Expand Down

0 comments on commit aa332b4

Please sign in to comment.