connecting_chan, includes the IPInfo

This commit is contained in:
alex 2025-10-30 15:33:09 +08:00
parent e003584463
commit 9861c4e850
4 changed files with 31 additions and 15 deletions

View File

@ -4,7 +4,6 @@ mod pb;
mod tcp;
mod utils;
use std::collections::HashMap;
use std::{sync::atomic::AtomicU8, time::Duration};
use std::net::{SocketAddr, ToSocketAddrs};
@ -25,7 +24,12 @@ use sdlan_sn_rs::{
utils::{create_or_load_uuid, get_sdlan_sock_from_socketaddr, Result, SDLanError},
};
use crate::network::Node;
#[derive(Clone)]
pub enum ConnectionInfo {
ConnState(ConnectionState),
IPInfo(String),
}
#[derive(Clone)]
#[repr(u8)]
@ -40,7 +44,7 @@ pub async fn run_sdlan(
sender: std::sync::mpsc::Sender<bool>,
install_channel: &str,
connecting_chan: Option<Sender<ConnectionState>>, // start_stop_sender: Sender<String>,
connecting_chan: Option<Sender<ConnectionInfo>>, // start_stop_sender: Sender<String>,
// start_stop_receiver: Receiver<String>,
) -> Result<()> {
let (start_stop_sender, start_stop_chan) = channel(20);
@ -56,6 +60,7 @@ pub async fn run_sdlan(
args.tos,
start_stop_sender,
args.mtu,
connecting_chan.clone(),
)
.await
{

View File

@ -14,7 +14,7 @@ use crate::pb::{
};
use crate::tcp::{init_tcp_conn, EventType, NakMsgCode, NatType, PacketType, SdlanTcp};
use crate::utils::{send_to_sock, CommandLine};
use crate::ConnectionState;
use crate::{ConnectionInfo, ConnectionState};
use sdlan_sn_rs::config::AF_INET;
use sdlan_sn_rs::peer::{SdlanSock, V6Info};
use sdlan_sn_rs::utils::{get_current_timestamp, ip_to_string, is_multi_broadcast, rsa_decrypt};
@ -67,6 +67,9 @@ async fn handle_tcp_message(msg: SdlanTcp) {
.ip
.net_addr
.store(dev.net_addr, Ordering::Relaxed);
if let Some(ref chan) = edge.connection_chan {
let _ = chan.send(ConnectionInfo::IPInfo(ip)).await;
}
/*
let mac = match dev.mac.try_into() {
Err(_) => NULL_MAC,
@ -247,7 +250,7 @@ pub async fn async_main(
args: CommandLine,
start_stop_chan: Receiver<StartStopInfo>,
cancel: CancellationToken,
connecting_chan: Option<Sender<ConnectionState>>,
connecting_chan: Option<Sender<ConnectionInfo>>,
) -> Result<()> {
// let _ = PidRecorder::new(".pid");
@ -339,7 +342,7 @@ pub async fn async_main(
|| async {
edge.set_authorized(false, vec![]);
},
|msg| handle_tcp_message(msg),
|msg| handle_tcp_message(msg),
edge.tcp_pong.clone(),
// tcp_pong,
start_stop_chan,

View File

@ -10,7 +10,7 @@ use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot;
use tracing::{debug, error};
use crate::get_base_dir;
use crate::{ConnectionInfo, get_base_dir};
use crate::pb::{
encode_to_tcp_message, encode_to_udp_message, SdlEmpty, SdlStunProbe, SdlStunProbeReply,
};
@ -37,6 +37,7 @@ pub async fn init_edge(
tos: u32,
start_stop: Sender<StartStopInfo>,
mtu: u32,
connecting_chan: Option<Sender<ConnectionInfo>>,
) -> Result<()> {
// gen public key
let rsa_path = format!("{}/.client", get_base_dir());
@ -79,6 +80,7 @@ pub async fn init_edge(
tcp_pong.clone(),
start_stop,
mtu,
connecting_chan,
);
do_init_edge(edge)?;
@ -120,6 +122,7 @@ pub struct Node {
pub tcp_pong: Arc<AtomicU64>,
start_stop_sender: Sender<StartStopInfo>,
pub connection_chan: Option<Sender<ConnectionInfo>>,
// user token info
pub _token: Mutex<String>,
@ -257,6 +260,7 @@ impl Node {
tcp_pong: Arc<AtomicU64>,
start_stop: Sender<StartStopInfo>,
mtu: u32,
connecting_chan: Option<Sender<ConnectionInfo>>,
) -> Self {
let mode = if cfg!(not(feature = "tun")) {
Mode::Tap
@ -271,6 +275,7 @@ impl Node {
network_code: Mutex::new(network_code.to_owned()),
start_stop_sender: start_stop,
connection_chan: connecting_chan,
tcp_pong,

View File

@ -22,7 +22,7 @@ use tracing::error;
use crate::config::TCP_PING_TIME;
use crate::network::StartStopInfo;
use crate::tcp::read_a_packet;
use crate::ConnectionState;
use crate::{ConnectionInfo, ConnectionState};
use super::tcp_codec::SdlanTcp;
@ -37,7 +37,7 @@ pub struct ReadWriteActor {
// actor收到数据之后发送给上层的发送端口,接收端由handle保存
from_tcp: Sender<SdlanTcp>,
cancel: CancellationToken,
connecting_chan: Option<Sender<ConnectionState>>,
connecting_chan: Option<Sender<ConnectionInfo>>,
ipv6_network_restarter: Option<Sender<bool>>,
}
@ -48,7 +48,7 @@ impl ReadWriteActor {
from_tcp: Sender<SdlanTcp>,
connected: Arc<AtomicBool>,
pong_time: Arc<AtomicU64>,
connecting_chan: Option<Sender<ConnectionState>>,
connecting_chan: Option<Sender<ConnectionInfo>>,
ipv6_network_restarter: Option<Sender<bool>>,
) -> Self {
Self {
@ -81,7 +81,8 @@ impl ReadWriteActor {
let mut start_pkt_id = None;
loop {
if let Some(ref connecting_chan) = self.connecting_chan {
let _ = connecting_chan.send(ConnectionState::NotConnected).await;
let state = ConnectionInfo::ConnState(ConnectionState::NotConnected);
let _ = connecting_chan.send(state).await;
}
self.connected.store(false, Ordering::Relaxed);
if !started {
@ -117,7 +118,8 @@ impl ReadWriteActor {
}
if let Some(ref connecting_chan) = self.connecting_chan {
let _ = connecting_chan.send(ConnectionState::Connecting).await;
let state = ConnectionInfo::ConnState(ConnectionState::Connecting);
let _ = connecting_chan.send(state).await;
}
debug!("try connecting...");
@ -141,7 +143,8 @@ impl ReadWriteActor {
on_connected(&mut stream, start_pkt_id.take()).await;
if let Some(ref connecting_chan) = self.connecting_chan {
let _ = connecting_chan.send(ConnectionState::Connected).await;
let state = ConnectionInfo::ConnState(ConnectionState::Connected);
let _ = connecting_chan.send(state).await;
}
if let Some(ref ipv6_restarter) = self.ipv6_network_restarter {
let _ = ipv6_restarter.send(true).await;
@ -264,7 +267,7 @@ impl ReadWriterHandle {
pong_time: Arc<AtomicU64>,
start_stop_chan: Receiver<StartStopInfo>,
// cancel: CancellationToken,
connecting_chan: Option<Sender<ConnectionState>>,
connecting_chan: Option<Sender<ConnectionInfo>>,
ipv6_network_restarter: Option<Sender<bool>>,
) -> Self
where
@ -321,7 +324,7 @@ pub fn init_tcp_conn<'a, T, T3, T2, F, F2>(
pong_time: Arc<AtomicU64>,
// cancel: CancellationToken,
start_stop_chan: Receiver<StartStopInfo>,
connecting_chan: Option<Sender<ConnectionState>>,
connecting_chan: Option<Sender<ConnectionInfo>>,
ipv6_network_restarter: Option<Sender<bool>>,
) where
T: for<'b> Fn(&'b mut TcpStream, Option<u32>) -> BoxFuture<'b, ()> + Send + 'static,