完善配置

This commit is contained in:
lubeilin
2023-02-07 21:31:42 +08:00
parent efb7053931
commit 6872ec3618
11 changed files with 242 additions and 72 deletions
+11 -5
View File
@@ -31,6 +31,7 @@ lazy_static! {
.time_to_idle(Duration::from_secs(60*5)).build();
/// 当前设备的nat信息
pub static ref NAT_INFO:Mutex<Option<NatInfo>> = const_mutex(None);
static ref NAT_TEST_ADDRESS:Mutex<Vec<SocketAddr>> = const_mutex(Vec::new());
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PeerDeviceInfo {
@@ -96,10 +97,10 @@ impl Into<u8> for ConnectStatus {
#[derive(Clone, Debug)]
pub struct NatInfo {
public_ips: Vec<u32>,
public_port: u16,
public_port_range: u16,
nat_type: NatType,
pub public_ips: Vec<u32>,
pub public_port: u16,
pub public_port_range: u16,
pub nat_type: NatType,
}
impl NatInfo {
@@ -118,9 +119,14 @@ impl NatInfo {
}
}
pub fn init_nat_test_addr(addrs: Vec<SocketAddr>) {
NAT_TEST_ADDRESS.lock().extend_from_slice(&addrs);
}
/// 初始化nat信息
pub fn init_nat_info(public_ip: u32, public_port: u16) {
match crate::nat::check::public_ip_list() {
let addrs = NAT_TEST_ADDRESS.lock().clone();
match crate::nat::check::public_ip_list(&addrs) {
Ok((nat_type, ips, port_range)) => {
let mut public_ips = Vec::new();
public_ips.push(public_ip);
+3 -3
View File
@@ -132,10 +132,10 @@ fn handle(
} else {
punch.public_port + punch.public_port_range
};
let k = if max_port - min_port +1 > 60 {
let k = if max_port - min_port + 1 > 60 {
60
} else {
max_port - min_port +1
max_port - min_port + 1
};
send_f(min_port as u16, max_port as u16, k as usize)?;
}
@@ -371,7 +371,7 @@ fn send_punch(udp: &UdpSocket, cur_info: &CurrentDeviceInfo, nat_info: NatInfo)
let ip = peer_info.virtual_ip;
//只向ip比自己大的发起打洞,避免双方同时发起打洞浪费流量
if ip > cur_info.virtual_ip && !DIRECT_ROUTE_TABLE.contains_key(&ip) {
log::info!("发起打洞 {:?}, peer_info:{:?}",nat_info,peer_info);
log::info!("发起打洞 {:?}, peer_info:{:?}", nat_info, peer_info);
let bytes = punch_packet(cur_info.virtual_ip, nat_info.clone(), ip)?;
udp.send_to(&bytes, cur_info.connect_server)?;
}
+2 -2
View File
@@ -148,10 +148,10 @@ pub async fn handler_start<F>(
) where
F: FnOnce() + Send + 'static,
{
#[cfg(target_os = "linux")]
use std::os::unix::io::AsRawFd;
#[cfg(target_os = "macos")]
use std::os::fd::AsRawFd;
#[cfg(target_os = "linux")]
use std::os::unix::io::AsRawFd;
let raw_fd = tun_reader.0.as_raw_fd();
tokio::spawn(async move {
let _ = status_watch.changed().await;