VNOX-Client/src/net/session/task.rs
loki5512344 9a20ace6e6
feat(client): TLS 1.3 control connection with SPKI pinning and LNEx v3
Replace the custom X25519/HKDF/ChaCha20 TCP layer with rustls (TLS 1.3 only,
ALPN vnox-lnex/3, no plaintext or fallback). Add a trust store (pin, trust on
first use, public CA), block changed fingerprints and identities, bind the
Ed25519 auth signature to the TLS exporter, add RU/EN trust dialog and
update the interop test and e2e voice tool.

Claude-Session: https://claude.ai/code/session_01GDjxLvBcnJqniFQGUGb1V4
2026-10-24 22:05:19 +02:00

165 lines
5.2 KiB
Rust

use tokio::sync::mpsc;
use tokio::time::sleep;
use tracing::{error, info};
use crate::identity::Identity;
use crate::net::trust::{TransportError, TrustStore};
use crate::net::types::{NetCommand, NetEvent};
use super::{
handlers::session_loop,
reconnect,
switch::{ReconnectRequested, SessionTarget},
};
pub async fn net_task(
identity: Identity,
trust: TrustStore,
mut cmd_rx: mpsc::Receiver<NetCommand>,
event_tx: mpsc::Sender<NetEvent>,
) {
let mut target: Option<SessionTarget> = None;
let mut failures: u32 = 0;
'session: loop {
let Some(mut session) = target.take() else {
match wait_for_connect(&mut cmd_rx).await {
Some(t) => {
failures = 0;
target = Some(t);
}
None => return,
}
continue;
};
if failures > 0 {
let delay = reconnect::backoff_delay(failures - 1);
let _ = event_tx
.send(NetEvent::Reconnecting {
attempt: failures,
delay_secs: delay.as_secs(),
})
.await;
let delay = sleep(delay);
tokio::pin!(delay);
loop {
tokio::select! {
_ = &mut delay => break,
command = cmd_rx.recv() => match command {
Some(NetCommand::Connect {address,auto_reconnect}) => {
session.address=address;session.auto_reconnect=auto_reconnect;
failures=0;target=Some(session);continue 'session;
}
Some(NetCommand::Disconnect) => {
failures=0;
let _=event_tx.send(NetEvent::Disconnected {reason:"disconnected".into(),will_retry:false}).await;
continue 'session;
}
None => return,
_ => {}
}
}
}
}
info!(
"connecting to {} (attempt {})",
session.address,
failures + 1
);
let restored = failures > 0;
match session_loop(
&identity,
&session.address,
&trust,
&mut session.channels,
restored,
&mut cmd_rx,
&event_tx,
)
.await
{
Ok(()) => {
failures = 0;
let _ = event_tx
.send(NetEvent::Disconnected {
reason: "disconnected".into(),
will_retry: false,
})
.await;
}
Err(e) => {
if let Some(req) = e.downcast_ref::<ReconnectRequested>() {
info!("switching node to {}", req.address);
session.address = req.address.clone();
session.auto_reconnect = req.auto_reconnect;
failures = 0;
target = Some(session);
continue;
}
error!("session ended: {e}");
// Trust failures and protocol mismatches never improve by retrying.
let transport = e.downcast_ref::<TransportError>();
if let Some(problem) = transport.and_then(TransportError::trust_problem) {
let _ = event_tx.send(NetEvent::TrustProblem(problem)).await;
}
let retry = session.auto_reconnect && transport.is_none();
let _ = event_tx
.send(NetEvent::Disconnected {
reason: e.to_string(),
will_retry: retry,
})
.await;
if retry {
failures = failures.saturating_add(1);
target = Some(session);
} else {
failures = 0;
}
}
}
}
}
async fn wait_for_connect(cmd_rx: &mut mpsc::Receiver<NetCommand>) -> Option<SessionTarget> {
loop {
match cmd_rx.recv().await {
Some(NetCommand::Connect {
address,
auto_reconnect,
}) => {
return Some(SessionTarget {
address,
auto_reconnect,
channels: vec!["general".into()],
});
}
None => return None,
Some(NetCommand::Disconnect) => continue,
_ => {}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn disconnected_cancel_does_not_disable_later_connections() {
let (sender, mut receiver) = mpsc::channel(4);
sender.send(NetCommand::Disconnect).await.unwrap();
sender
.send(NetCommand::Connect {
address: "127.0.0.1:7600".into(),
auto_reconnect: false,
})
.await
.unwrap();
assert_eq!(
wait_for_connect(&mut receiver).await.unwrap().address,
"127.0.0.1:7600"
);
}
}