完善配置

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
+33 -20
View File
@@ -1,7 +1,7 @@
use std::io;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, ToSocketAddrs, UdpSocket};
use std::sync::Arc;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
use crossbeam::atomic::AtomicCell;
@@ -11,11 +11,11 @@ use tokio::sync::watch;
use error::*;
use crate::handle::{
ApplicationStatus, ConnectStatus, CurrentDeviceInfo, DEVICE_LIST, DIRECT_ROUTE_TABLE, PeerDeviceInfo,
Route, RouteType, SERVER_RT,
};
use crate::handle::registration_handler::CONNECTION_STATUS;
use crate::handle::{
ApplicationStatus, ConnectStatus, CurrentDeviceInfo, NatInfo, PeerDeviceInfo, Route, RouteType,
DEVICE_LIST, DIRECT_ROUTE_TABLE, NAT_INFO, SERVER_RT,
};
pub mod error;
pub mod handle;
@@ -29,6 +29,8 @@ pub struct Config<F> {
pub token: String,
pub mac_address: String,
pub name: String,
pub server_address: SocketAddr,
pub nat_test_server: Vec<SocketAddr>,
pub abnormal_call: F,
}
@@ -37,10 +39,12 @@ impl<F> Config<F> {
token: String,
mac_address: String,
name: Option<String>,
server_address: SocketAddr,
nat_test_server: Vec<SocketAddr>,
abnormal_call: F,
) -> Result<Self>
where
F: FnOnce() + Send + 'static,
where
F: FnOnce() + Send + 'static,
{
if token.is_empty() || token.len() > 64 {
return Err(Error::Stop("token invalid".to_string()));
@@ -56,6 +60,8 @@ impl<F> Config<F> {
token,
mac_address,
name,
server_address,
nat_test_server,
abnormal_call,
})
} else {
@@ -69,6 +75,8 @@ impl<F> Config<F> {
token,
mac_address,
name,
server_address,
nat_test_server,
abnormal_call,
})
}
@@ -84,8 +92,8 @@ pub struct Switch {
impl Switch {
pub fn start<F>(config: Config<F>) -> Result<Self>
where
F: FnOnce() + Send + 'static,
where
F: FnOnce() + Send + 'static,
{
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
@@ -109,6 +117,9 @@ impl Switch {
pub fn current_device(&self) -> &CurrentDeviceInfo {
&self.current_device
}
pub fn nat_info(&self) -> Option<NatInfo> {
NAT_INFO.lock().clone()
}
pub fn server_rt(&self) -> i64 {
SERVER_RT.load(Ordering::Relaxed)
}
@@ -141,11 +152,12 @@ impl Switch {
return status == ApplicationStatus::Starting;
}
pub async fn start_<F>(config: Config<F>) -> Result<Self>
where
F: FnOnce() + Send + 'static,
where
F: FnOnce() + Send + 'static,
{
// let server_address = "nat1.wherewego.top:29876"
let server_address = "nat1.wherewego.top:29875".to_socket_addrs().unwrap().next().unwrap();
// let server_address = "nat1.wherewego.top:29875".to_socket_addrs().unwrap().next().unwrap();
let server_address = config.server_address;
let mut port = 101 as u16;
let udp = loop {
match UdpSocket::bind(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::from(0), port))) {
@@ -215,9 +227,10 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
}
//初始化nat数据
handle::init_nat_test_addr(config.nat_test_server);
handle::init_nat_info(response.public_ip, response.public_port as u16);
// tun服务
let (tun_writer, tun_reader) =
@@ -249,7 +262,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
let udp1 = udp.try_clone()?;
let wait_group1 = wait_group.clone();
let status_sender1 = status_sender.clone();
@@ -269,7 +282,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
}
//打洞处理
{
@@ -291,7 +304,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
let udp1 = udp.try_clone()?;
let wait_group1 = wait_group.clone();
let status_sender1 = status_sender.clone();
@@ -310,7 +323,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
let udp1 = udp.try_clone()?;
let wait_group1 = wait_group.clone();
let status_sender1 = status_sender.clone();
@@ -329,7 +342,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
}
//tun数据处理
{
@@ -350,7 +363,7 @@ impl Switch {
drop(wait_group1);
},
)
.await;
.await;
}
Ok(Switch {
current_device,