Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Reconnect on network changes and tidy pairing links
Bee7a0c6190312b
diff --git a/core/nectarlink-core/src/node.rs b/core/nectarlink-core/src/node.rsindex 79d7204..bb48f26 100644--- a/core/nectarlink-core/src/node.rs+++ b/core/nectarlink-core/src/node.rs@@ -616,6 +616,16 @@ impl Node {self.shared.wake_supervisor(&peer);}+ /// Tells the core the network may have changed (Wi-Fi switched, resumed+ /// from sleep). Paths are re-checked and offline devices are dialed right+ /// away instead of after their backoff. Harmless when nothing changed.+ pub async fn network_changed(&self) {+ self.shared.endpoint.network_change().await;+ for supervisor in lock(&self.shared.supervisors).values() {+ supervisor.wake.notify_one();+ }+ }+/// Devices discovered on the local network that aren't paired yet.pub fn discovered_devices(&self) -> Vec<DiscoveredDevice> {lock(&self.shared.discovered).values().cloned().collect()diff --git a/core/nectarlink-core/src/pairing.rs b/core/nectarlink-core/src/pairing.rsindex af09b4e..87f0e12 100644--- a/core/nectarlink-core/src/pairing.rs+++ b/core/nectarlink-core/src/pairing.rs@@ -14,6 +14,7 @@//! that close. Failures close with [`CLOSE_FAILED`] and nobody stores.use std::{+ net::{IpAddr, SocketAddr},sync::{Arc, Mutex},time::{Duration, Instant},};@@ -156,7 +157,43 @@ pub(crate) async fn start_host(shared: &Arc<Shared>) -> Result<PairingUri> {});let name = local_device(shared).name;- Ok(PairingUri { id: shared.id, secret, name, addrs: shared.direct_addrs().await })+ Ok(PairingUri { id: shared.id, secret, name, addrs: link_addrs(shared.direct_addrs().await) })+}++/// Most addresses a pairing link carries; more only make the QR code denser.+const MAX_LINK_ADDRS: usize = 3;++/// Picks the addresses worth putting in a pairing link: private IPv4 first+/// (what a phone on the same network almost always uses), then other IPv4,+/// then at most one IPv6. Link-local IPv6 is useless without a scope ID.+/// Discovery on the network finds the device even if none of these work.+fn link_addrs(mut addrs: Vec<SocketAddr>) -> Vec<SocketAddr> {+ let rank = |a: &SocketAddr| match a.ip() {+ IpAddr::V4(v4) if v4.is_private() => 0,+ IpAddr::V4(v4) if v4.is_loopback() || v4.is_link_local() => 3,+ IpAddr::V4(_) => 1,+ IpAddr::V6(_) => 2,+ };+ addrs.retain(|a| match a.ip() {+ IpAddr::V6(v6) => !v6.is_loopback() && !v6.is_unicast_link_local(),+ IpAddr::V4(v4) => !v4.is_loopback(),+ });+ addrs.sort_by_key(rank);+ let mut picked = Vec::new();+ let mut has_v6 = false;+ for addr in addrs {+ if addr.is_ipv6() {+ if has_v6 {+ continue;+ }+ has_v6 = true;+ }+ picked.push(addr);+ if picked.len() == MAX_LINK_ADDRS {+ break;+ }+ }+ picked}pub(crate) fn cancel(shared: &Shared) {@@ -434,3 +471,26 @@ fn fail(shared: &Shared, err: Error) -> Error {shared.emit(NodeEvent::Pairing(PairingEvent::Failed(failure)));err}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn pairing_links_carry_the_useful_addresses() {+ let addrs: Vec<SocketAddr> = [+ "[2409:40d2::1]:5000",+ "[fe80::1]:5000",+ "100.64.0.1:5000",+ "[2409:40d2::2]:5000",+ "192.168.31.121:5000",+ "127.0.0.1:5000",+ ]+ .iter()+ .map(|a| a.parse().unwrap())+ .collect();+ let picked: Vec<String> = link_addrs(addrs).iter().map(ToString::to_string).collect();+ assert_eq!(picked, ["192.168.31.121:5000", "100.64.0.1:5000", "[2409:40d2::1]:5000"]);+ assert!(link_addrs(Vec::new()).is_empty());+ }+}diff --git a/core/nectarlink-core/src/session.rs b/core/nectarlink-core/src/session.rsindex 512cbde..713aec0 100644--- a/core/nectarlink-core/src/session.rs+++ b/core/nectarlink-core/src/session.rs@@ -135,6 +135,13 @@ impl Session {on_end: impl FnOnce(Arc<Session>) + Send + 'static,) -> Arc<Session> {let peer = crate::device_id(&conn.remote_id());+ // Start from QUIC's estimate (from the handshake) until the first+ // heartbeat measures the round trip end to end.+ let initial_rtt = conn+ .paths()+ .iter()+ .find(|p| p.is_selected())+ .map_or(0, |p| p.rtt().as_millis().min(u128::from(u32::MAX)) as u32);let (outbox, outbox_rx) = mpsc::channel(OUTBOX_CAPACITY);let session = Arc::new(Session {peer,@@ -143,7 +150,7 @@ impl Session {outbox,pending: Mutex::new(HashMap::new()),next_id: AtomicU64::new(1),- rtt_ms: AtomicU32::new(0),+ rtt_ms: AtomicU32::new(initial_rtt),cancel: shared.cancel.child_token(),});