[mio] 修复指定通道类型无效的问题

This commit is contained in:
lubeilin
2024-03-02 14:03:19 +08:00
parent b6aba36510
commit e3f328cc9b
5 changed files with 25 additions and 26 deletions
+3
View File
@@ -73,6 +73,9 @@ pub struct ContextInner {
} }
impl ContextInner { impl ContextInner {
pub fn use_channel_type(&self) -> UseChannelType {
self.route_table.use_channel_type
}
pub fn is_stop(&self) -> bool { pub fn is_stop(&self) -> bool {
!self.state.load(Ordering::Acquire) !self.state.load(Ordering::Acquire)
} }
+14 -15
View File
@@ -14,7 +14,7 @@ use tun::device::IFace;
use crate::channel::context::Context; use crate::channel::context::Context;
use crate::channel::idle::Idle; use crate::channel::idle::Idle;
use crate::channel::punch::{NatInfo, Punch}; use crate::channel::punch::{NatInfo, Punch};
use crate::channel::{init_channel, init_context, Route, RouteKey, UseChannelType}; use crate::channel::{init_channel, init_context, Route, RouteKey};
use crate::cipher::Cipher; use crate::cipher::Cipher;
#[cfg(feature = "server_encrypt")] #[cfg(feature = "server_encrypt")]
use crate::cipher::RsaCipher; use crate::cipher::RsaCipher;
@@ -136,7 +136,8 @@ impl Vnt {
let (punch_sender, punch_receiver) = sync_channel(3); let (punch_sender, punch_receiver) = sync_channel(3);
let peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>> = let peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>> =
Arc::new(RwLock::new(HashMap::with_capacity(16))); Arc::new(RwLock::new(HashMap::with_capacity(16)));
let down_counter = U64Adder::with_capacity(config.ports.as_ref().map(|v| v.len()).unwrap_or_default() + 8); let down_counter =
U64Adder::with_capacity(config.ports.as_ref().map(|v| v.len()).unwrap_or_default() + 8);
let down_count_watcher = down_counter.watch(); let down_count_watcher = down_counter.watch();
let handler = RecvDataHandler::new( let handler = RecvDataHandler::new(
#[cfg(feature = "server_encrypt")] #[cfg(feature = "server_encrypt")]
@@ -149,7 +150,6 @@ impl Vnt {
config_info.clone(), config_info.clone(),
nat_test.clone(), nat_test.clone(),
callback.clone(), callback.clone(),
config.use_channel_type,
punch_sender, punch_sender,
peer_nat_info_map.clone(), peer_nat_info_map.clone(),
external_route.clone(), external_route.clone(),
@@ -198,8 +198,7 @@ impl Vnt {
let nat_test = nat_test.clone(); let nat_test = nat_test.clone();
let device_list = device_list.clone(); let device_list = device_list.clone();
let current_device = current_device.clone(); let current_device = current_device.clone();
let use_channel_type = config.use_channel_type; if !config.use_channel_type.is_only_relay() {
if !use_channel_type.is_only_relay() {
// 定时nat探测 // 定时nat探测
maintain::retrieve_nat_type( maintain::retrieve_nat_type(
&scheduler, &scheduler,
@@ -222,7 +221,6 @@ impl Vnt {
config_info, config_info,
punch, punch,
callback, callback,
use_channel_type,
); );
}); });
} }
@@ -253,7 +251,6 @@ pub fn start<Call: VntCallback>(
config_info: BaseConfigInfo, config_info: BaseConfigInfo,
punch: Punch, punch: Punch,
callback: Call, callback: Call,
use_channel_type: UseChannelType,
) { ) {
// 定时心跳 // 定时心跳
maintain::heartbeat( maintain::heartbeat(
@@ -275,13 +272,15 @@ pub fn start<Call: VntCallback>(
callback, callback,
); );
// 定时客户端中继检测 // 定时客户端中继检测
maintain::client_relay( if !context.use_channel_type().is_only_p2p() {
&scheduler, maintain::client_relay(
context.clone(), &scheduler,
current_device.clone(), context.clone(),
device_list.clone(), current_device.clone(),
client_cipher.clone(), device_list.clone(),
); client_cipher.clone(),
);
}
// 定时地址探测 // 定时地址探测
maintain::addr_request( maintain::addr_request(
&scheduler, &scheduler,
@@ -290,7 +289,7 @@ pub fn start<Call: VntCallback>(
server_cipher.clone(), server_cipher.clone(),
config_info.clone(), config_info.clone(),
); );
if !use_channel_type.is_only_relay() { if !context.use_channel_type().is_only_relay() {
// 定时打洞 // 定时打洞
maintain::punch( maintain::punch(
&scheduler, &scheduler,
+4 -7
View File
@@ -15,7 +15,7 @@ use tun::Device;
use crate::channel::context::Context; use crate::channel::context::Context;
use crate::channel::punch::NatInfo; use crate::channel::punch::NatInfo;
use crate::channel::{Route, RouteKey, UseChannelType}; use crate::channel::{Route, RouteKey};
use crate::cipher::Cipher; use crate::cipher::Cipher;
use crate::external_route::AllowExternalRoute; use crate::external_route::AllowExternalRoute;
use crate::handle::recv_data::PacketHandler; use crate::handle::recv_data::PacketHandler;
@@ -35,7 +35,6 @@ use crate::protocol::{
pub struct ClientPacketHandler { pub struct ClientPacketHandler {
device: Arc<Device>, device: Arc<Device>,
client_cipher: Cipher, client_cipher: Cipher,
use_channel_type: UseChannelType,
punch_sender: SyncSender<(Ipv4Addr, NatInfo)>, punch_sender: SyncSender<(Ipv4Addr, NatInfo)>,
peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>, peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>,
nat_test: NatTest, nat_test: NatTest,
@@ -48,7 +47,6 @@ impl ClientPacketHandler {
pub fn new( pub fn new(
device: Arc<Device>, device: Arc<Device>,
client_cipher: Cipher, client_cipher: Cipher,
use_channel_type: UseChannelType,
punch_sender: SyncSender<(Ipv4Addr, NatInfo)>, punch_sender: SyncSender<(Ipv4Addr, NatInfo)>,
peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>, peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>,
nat_test: NatTest, nat_test: NatTest,
@@ -58,7 +56,6 @@ impl ClientPacketHandler {
Self { Self {
device, device,
client_cipher, client_cipher,
use_channel_type,
punch_sender, punch_sender,
peer_nat_info_map, peer_nat_info_map,
nat_test, nat_test,
@@ -189,7 +186,7 @@ impl ClientPacketHandler {
context.route_table.add_route(source, route); context.route_table.add_route(source, route);
} }
ControlPacket::PunchRequest => { ControlPacket::PunchRequest => {
if self.use_channel_type.is_only_relay() { if context.use_channel_type().is_only_relay() {
return Ok(()); return Ok(());
} }
//回应 //回应
@@ -203,7 +200,7 @@ impl ClientPacketHandler {
context.route_table.add_route_if_absent(source, route); context.route_table.add_route_if_absent(source, route);
} }
ControlPacket::PunchResponse => { ControlPacket::PunchResponse => {
if self.use_channel_type.is_only_relay() { if context.use_channel_type().is_only_relay() {
return Ok(()); return Ok(());
} }
let route = Route::from(route_key, 1, 199); let route = Route::from(route_key, 1, 199);
@@ -237,7 +234,7 @@ impl ClientPacketHandler {
net_packet: NetPacket<&mut [u8]>, net_packet: NetPacket<&mut [u8]>,
route_key: RouteKey, route_key: RouteKey,
) -> io::Result<()> { ) -> io::Result<()> {
if self.use_channel_type.is_only_relay() { if context.use_channel_type().is_only_relay() {
return Ok(()); return Ok(());
} }
let source = net_packet.source(); let source = net_packet.source();
+1 -3
View File
@@ -12,7 +12,7 @@ use tun::Device;
use crate::channel::context::Context; use crate::channel::context::Context;
use crate::channel::handler::RecvChannelHandler; use crate::channel::handler::RecvChannelHandler;
use crate::channel::punch::NatInfo; use crate::channel::punch::NatInfo;
use crate::channel::{RouteKey, UseChannelType}; use crate::channel::RouteKey;
use crate::cipher::Cipher; use crate::cipher::Cipher;
#[cfg(feature = "server_encrypt")] #[cfg(feature = "server_encrypt")]
use crate::cipher::RsaCipher; use crate::cipher::RsaCipher;
@@ -60,7 +60,6 @@ impl<Call: VntCallback> RecvDataHandler<Call> {
config_info: BaseConfigInfo, config_info: BaseConfigInfo,
nat_test: NatTest, nat_test: NatTest,
callback: Call, callback: Call,
use_channel_type: UseChannelType,
punch_sender: SyncSender<(Ipv4Addr, NatInfo)>, punch_sender: SyncSender<(Ipv4Addr, NatInfo)>,
peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>, peer_nat_info_map: Arc<RwLock<HashMap<Ipv4Addr, NatInfo>>>,
external_route: ExternalRoute, external_route: ExternalRoute,
@@ -83,7 +82,6 @@ impl<Call: VntCallback> RecvDataHandler<Call> {
let client = ClientPacketHandler::new( let client = ClientPacketHandler::new(
device.clone(), device.clone(),
client_cipher, client_cipher,
use_channel_type,
punch_sender, punch_sender,
peer_nat_info_map, peer_nat_info_map,
nat_test, nat_test,
+3 -1
View File
@@ -151,7 +151,9 @@ pub fn base_handle(
client_cipher.encrypt_ipv4(&mut net_packet)?; client_cipher.encrypt_ipv4(&mut net_packet)?;
//优先发到直连到地址 //优先发到直连到地址
if context.send_by_id(net_packet.buffer(), &dest_ip).is_err() { if context.send_by_id(net_packet.buffer(), &dest_ip).is_err() {
context.send_default(net_packet.buffer(), current_device.connect_server)?; if !context.use_channel_type().is_only_p2p() {
context.send_default(net_packet.buffer(), current_device.connect_server)?;
}
} }
return Ok(()); return Ok(());
} }