Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

Space mesh nodes no longer probe the local network their network refuses (#6518)

ddupontd327deb3f373
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/context.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/context.rs
index 6fbd5ada35..603d89fba5 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/context.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/context.rs
@@ -632,6 +632,11 @@ impl ServerContext {
);
// A machine attached at run time is a Space: it runs no gangs.
mesh.gang_home = None;
+ // ... on the default network, which refuses the local network: its
+ // node does not probe the computers' local addresses (the network would
+ // refuse each probe and count it as something the Space tried). A
+ // network that opens the local network says so with `CUA_MESH_LAN=1`.
+ mesh.lan = std::env::var("CUA_MESH_LAN").is_ok_and(|v| v == "1");
tokio::spawn(crate::mesh_host::run(mesh, stop.clone()));
tokio::spawn(cua_relay::client::run(join, stop));
Ok(())
@@ -682,6 +687,7 @@ impl ServerContext {
recheck: crate::mesh_host::GATE_RECHECK,
gang_home: home,
gang_wait: crate::mesh_host::GANG_WAIT,
+ lan: true,
}
}
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/mesh_host.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/mesh_host.rs
index 6b59a86683..cf2ac34315 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/mesh_host.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/mesh_host.rs
@@ -262,6 +262,12 @@ pub struct MeshHostConfig {
pub gang_home: Option<PathBuf>,
/// How long a gang port stream waits to be served ([`GANG_WAIT`]).
pub gang_wait: Duration,
+ /// Whether the node may use paths on the local network. A Space on the
+ /// default network (internet only) turns it off: its mesh node then never
+ /// pings a computer's local address, which that network would refuse and
+ /// count as something the Space tried. The relay and internet paths carry
+ /// the traffic. `CUA_MESH_LAN=0` turns it off for any node.
+ pub lan: bool,
}
impl std::fmt::Debug for MeshHostConfig {
@@ -451,6 +457,7 @@ async fn start(cfg: &MeshHostConfig) -> cua_mesh::Result<Mesh> {
auth,
);
mc.name = cfg.name.clone();
+ mc.lan = mc.lan && cfg.lan;
mc.approved_path = Some(cfg.dir.join("approved.json"));
let env = cfg.env_token.clone();
let id = cfg.machine_id.clone();
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/tests/gang_mesh_e2e.rs b/libs/cua-spacesd/crates/cua-spacesd-server/tests/gang_mesh_e2e.rs
index 45a8e8062a..086723212f 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/tests/gang_mesh_e2e.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/tests/gang_mesh_e2e.rs
@@ -157,6 +157,7 @@ impl World {
recheck: cua_spacesd_server::mesh_host::GATE_RECHECK,
gang_home: Some(home.to_owned()),
gang_wait: cua_spacesd_server::mesh_host::GANG_WAIT,
+ lan: true,
};
tokio::spawn(cua_spacesd_server::mesh_host::run(cfg, self.stop.clone()));
let sock = cua_mesh::gang::control_path(home);
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_gang.rs b/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_gang.rs
index e239328be1..8a38b18f1e 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_gang.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_gang.rs
@@ -122,6 +122,7 @@ impl World {
recheck: cua_spacesd_server::mesh_host::GATE_RECHECK,
gang_home: Some(home.to_owned()),
gang_wait: wait,
+ lan: true,
};
tokio::spawn(cua_spacesd_server::mesh_host::run(cfg, self.stop.clone()));
}
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_gate.rs b/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_gate.rs
index 21282cb7fb..8e9f75b528 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_gate.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_gate.rs
@@ -67,6 +67,7 @@ fn config(relay: &str, dir: &std::path::Path, on: Arc<AtomicBool>) -> MeshHostCo
recheck: Duration::from_millis(50),
gang_home: None,
gang_wait: cua_spacesd_server::mesh_host::GANG_WAIT,
+ lan: true,
}
}
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_metering.rs b/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_metering.rs
index 05b7cc4add..602f1629fe 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_metering.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/tests/mesh_metering.rs
@@ -148,6 +148,7 @@ impl World {
recheck: cua_spacesd_server::mesh_host::GATE_RECHECK,
gang_home: None,
gang_wait: cua_spacesd_server::mesh_host::GANG_WAIT,
+ lan: true,
};
self._dirs.push(dir);
tokio::spawn(cua_spacesd_server::mesh_host::run(cfg, self.stop.clone()));
diff --git a/libs/cua/crates/cua-mesh/src/engine.rs b/libs/cua/crates/cua-mesh/src/engine.rs
index 6c45beb108..66a0538f48 100644
--- a/libs/cua/crates/cua-mesh/src/engine.rs
+++ b/libs/cua/crates/cua-mesh/src/engine.rs
@@ -292,6 +292,11 @@ pub(crate) struct Engine {
relay_only: AtomicBool,
/// [`Prefer`], as its `u8`.
prefer: AtomicU8,
+ /// Whether the local network may be used at all (default). Off for a node
+ /// whose network refuses it (a Space on the internet-only default): it
+ /// then never pings, answers on or advertises a local address, so the
+ /// refusing network sees nothing to refuse.
+ lan_allowed: AtomicBool,
peers_changed: tokio::sync::watch::Sender<u64>,
}
@@ -318,6 +323,7 @@ impl Engine {
disco_kick: Notify::new(),
relay_only: AtomicBool::new(relay_only),
prefer: AtomicU8::new(Prefer::Auto.to_u8()),
+ lan_allowed: AtomicBool::new(true),
peers_changed,
});
let out: Arc<dyn Outbound> = engine.clone();
@@ -424,12 +430,45 @@ impl Engine {
self.forget_paths();
}
+ /// Allows or forbids paths on the local network. Forbidding drops what
+ /// was learned on it (a best path included) and stops advertising local
+ /// addresses; paths across the internet and the relay are untouched.
+ pub(crate) fn set_lan_allowed(&self, on: bool) {
+ self.lan_allowed.store(on, Ordering::SeqCst);
+ if !on {
+ for p in self.peers() {
+ let mut ps = p.path.lock().expect("path");
+ ps.candidates.retain(|a, _| !is_lan(a));
+ ps.pending.retain(|_, (a, _)| !is_lan(a));
+ if ps.best.is_some_and(|(a, _, _)| is_lan(&a)) {
+ ps.best = None;
+ }
+ }
+ let kept: Vec<SocketAddr> = self
+ .local_endpoints
+ .read()
+ .expect("endpoints")
+ .iter()
+ .copied()
+ .filter(|a| !is_lan(a))
+ .collect();
+ self.set_local_endpoints(kept);
+ }
+ }
+
+ /// Whether `addr` may be sent to or pinged: not a local-network address
+ /// while the local network is forbidden ([`Self::set_lan_allowed`]).
+ fn may_use(&self, addr: &SocketAddr) -> bool {
+ self.lan_allowed.load(Ordering::Relaxed) || !is_lan(addr)
+ }
+
/// Whether relay-only is on (a pin or `CUA_MESH_RELAY_ONLY`).
pub(crate) fn relay_only(&self) -> bool {
self.relay_only.load(Ordering::Relaxed)
}
- pub(crate) fn set_local_endpoints(&self, endpoints: Vec<SocketAddr>) {
+ pub(crate) fn set_local_endpoints(&self, mut endpoints: Vec<SocketAddr>) {
+ endpoints.retain(|a| self.may_use(a));
let changed = {
let mut eps = self.local_endpoints.write().expect("endpoints");
let changed = *eps != endpoints;
@@ -472,7 +511,7 @@ impl Engine {
// Keep learned candidates; refresh advertised ones.
path.candidates
.retain(|_, c| !c.advertised || c.last_pong.is_some());
- for e in &info.endpoints {
+ for e in info.endpoints.iter().filter(|e| self.may_use(e)) {
path.candidates.entry(*e).or_default().advertised = true;
}
}
@@ -508,7 +547,7 @@ impl Engine {
);
let sealer = Sealer::new(&self.identity.disco, &info.disco_key);
let mut path = PathState::default();
- for e in &info.endpoints {
+ for e in info.endpoints.iter().filter(|e| self.may_use(e)) {
path.candidates.entry(*e).or_default().advertised = true;
}
Peer {
@@ -564,6 +603,14 @@ impl Engine {
// ---- sending ----
fn send_udp(&self, dst: SocketAddr, data: &[u8]) -> io::Result<()> {
+ // The one place a datagram leaves: nothing goes to a local address
+ // while the network refuses them (not even a reply to a ping).
+ if !self.may_use(&dst) {
+ return Err(io::Error::new(
+ io::ErrorKind::PermissionDenied,
+ "the local network is off for this node",
+ ));
+ }
let s = self.sockets.read().expect("sockets");
let s = s
.as_ref()
@@ -717,7 +764,7 @@ impl Engine {
if let Path::Direct(addr) = path {
// Authenticated traffic from an endpoint: worth probing.
let mut ps = peer.path.lock().expect("path");
- if ps.best.is_none() && !ps.candidates.contains_key(&addr) {
+ if ps.best.is_none() && !ps.candidates.contains_key(&addr) && self.may_use(&addr) {
ps.candidates.insert(addr, Candidate::default());
drop(ps);
self.disco_kick.notify_one();
@@ -764,6 +811,7 @@ impl Engine {
};
if let Path::Direct(a) = path
&& !self.relay_only.load(Ordering::Relaxed)
+ && self.may_use(&a)
{
let mut ps = peer.path.lock().expect("path");
let fresh = !ps.candidates.contains_key(&a);
@@ -828,7 +876,7 @@ impl Engine {
drop(ps);
if !src.ip().is_unspecified() {
let mut eps = self.local_endpoints.write().expect("endpoints");
- if !eps.contains(&src) && eps.len() < 32 {
+ if !eps.contains(&src) && eps.len() < 32 && self.may_use(&src) {
// Peer-reflexive: how the peer sees us.
eps.push(src);
}
@@ -842,7 +890,7 @@ impl Engine {
// peer is pinging us right now, so both NATs open.
let targets: Vec<SocketAddr> = {
let mut ps = peer.path.lock().expect("path");
- for e in &endpoints {
+ for e in endpoints.iter().filter(|e| self.may_use(e)) {
ps.candidates.entry(*e).or_default();
}
ps.candidates.keys().copied().collect()
@@ -858,6 +906,9 @@ impl Engine {
}
fn ping(&self, peer: &Peer, addr: SocketAddr) {
+ if !self.may_use(&addr) {
+ return;
+ }
let tx: disco::TxId = rand::random();
{
let mut ps = peer.path.lock().expect("path");
@@ -1178,3 +1229,115 @@ mod prefer_tests {
assert_eq!(Prefer::parse("nope"), None);
}
}
+
+#[cfg(test)]
+mod lan_tests {
+ use super::*;
+ use crate::proto::NodeKind;
+
+ fn a(s: &str) -> SocketAddr {
+ s.parse().unwrap()
+ }
+
+ fn info(endpoints: Vec<SocketAddr>) -> PeerInfo {
+ let id = NodeIdentity::generate().unwrap();
+ PeerInfo {
+ node_key: id.public(),
+ disco_key: id.disco.public(),
+ quic_pin: id.cert_pin(),
+ kind: NodeKind::Machine,
+ machine_id: Some("m1".into()),
+ name: "m1".into(),
+ endpoints,
+ online: true,
+ version: crate::proto::VERSION,
+ grant: None,
+ }
+ }
+
+ fn candidates(e: &Engine) -> Vec<SocketAddr> {
+ let mut c: Vec<SocketAddr> = e
+ .peers()
+ .iter()
+ .flat_map(|p| {
+ p.path
+ .lock()
+ .unwrap()
+ .candidates
+ .keys()
+ .copied()
+ .collect::<Vec<_>>()
+ })
+ .collect();
+ c.sort();
+ c
+ }
+
+ /// A Space whose network refuses the local network (the internet-only default):
+ /// its node never learns, pings, answers on or advertises a local address, so
+ /// there is no packet for that network to refuse and count.
+ #[tokio::test]
+ async fn a_node_that_may_not_use_the_local_network_never_touches_it() {
+ let e = Engine::new(NodeIdentity::generate().unwrap(), 0, false)
+ .await
+ .unwrap();
+ e.set_lan_allowed(false);
+ e.set_peers(vec![info(vec![
+ a("192.168.0.20:41641"),
+ a("10.1.2.3:1"),
+ a("203.0.113.9:41641"),
+ ])]);
+ // Only the internet endpoint is a candidate.
+ assert_eq!(candidates(&e), vec![a("203.0.113.9:41641")]);
+ // Nothing leaves for a local address, whoever asks (a ping, a reply, the tunnel).
+ for lan in [
+ "192.168.0.20:41641",
+ "10.1.2.3:1",
+ "169.254.1.1:5",
+ "[fd00::1]:9",
+ ] {
+ let err = e.send_udp(a(lan), b"x").unwrap_err();
+ assert_eq!(err.kind(), io::ErrorKind::PermissionDenied, "{lan}");
+ }
+ let peer = e.peers().remove(0);
+ e.ping(&peer, a("192.168.0.20:41641"));
+ assert!(peer.path.lock().unwrap().pending.is_empty());
+ // Our own local addresses are not advertised either.
+ e.set_local_endpoints(vec![a("192.168.64.2:4000"), a("198.51.100.4:4000")]);
+ assert_eq!(
+ *e.local_endpoints.read().unwrap(),
+ vec![a("198.51.100.4:4000")]
+ );
+ }
+
+ /// Turning it off later forgets what was learned on the local network.
+ #[tokio::test]
+ async fn forbidding_the_local_network_later_drops_what_it_learned() {
+ let e = Engine::new(NodeIdentity::generate().unwrap(), 0, false)
+ .await
+ .unwrap();
+ e.set_peers(vec![info(vec![
+ a("192.168.0.20:41641"),
+ a("203.0.113.9:41641"),
+ ])]);
+ assert_eq!(candidates(&e).len(), 2);
+ {
+ let peer = e.peers().remove(0);
+ peer.path.lock().unwrap().best = Some((
+ a("192.168.0.20:41641"),
+ Duration::from_millis(1),
+ Instant::now(),
+ ));
+ }
+ e.set_lan_allowed(false);
+ assert_eq!(candidates(&e), vec![a("203.0.113.9:41641")]);
+ assert!(e.peers()[0].path.lock().unwrap().best.is_none());
+ // And allowed (the default) it is back to what it was.
+ e.set_lan_allowed(true);
+ assert_ne!(
+ e.send_udp(a("192.168.0.20:41641"), b"x")
+ .map_err(|e| e.kind()),
+ Err(io::ErrorKind::PermissionDenied)
+ );
+ }
+}
diff --git a/libs/cua/crates/cua-mesh/src/node.rs b/libs/cua/crates/cua-mesh/src/node.rs
index 8265f605cb..9a03002fdc 100644
--- a/libs/cua/crates/cua-mesh/src/node.rs
+++ b/libs/cua/crates/cua-mesh/src/node.rs
@@ -62,6 +62,12 @@ pub struct MeshConfig {
pub stun: Vec<String>,
/// Never use direct paths (tests; `CUA_MESH_RELAY_ONLY=1`).
pub relay_only: bool,
+ /// Use paths on the local network (default; `CUA_MESH_LAN=0` turns it
+ /// off). Off for a node whose network refuses the local network (a
+ /// Space on the internet-only default): it neither pings nor
+ /// advertises local addresses, so that network sees nothing of the
+ /// mesh to block, and the relay and internet paths carry the traffic.
+ pub lan: bool,
/// The mesh UDP port of the peers' QUIC media service: client forwards
/// send media datagrams to it, and a machine serves it with
/// [`Mesh::serve_udp`] on the same number. Default
@@ -105,6 +111,7 @@ impl MeshConfig {
approved_path: None,
stun: Vec::new(),
relay_only: std::env::var("CUA_MESH_RELAY_ONLY").is_ok_and(|v| v == "1"),
+ lan: !std::env::var("CUA_MESH_LAN").is_ok_and(|v| v == "0" || v == "off"),
media_udp_port: MEDIA_UDP_PORT,
reconnect_speedup: 1,
connect_timeout: CONNECT_TIMEOUT,
@@ -364,6 +371,7 @@ impl Mesh {
let engine = Engine::new(identity.clone(), cfg.listen_port, cfg.relay_only)
.await
.map_err(|e| Error::Io(format!("udp: {e}")))?;
+ engine.set_lan_allowed(cfg.lan);
let prefer = crate::engine::Prefer::from_env();
if prefer != crate::engine::Prefer::Auto {
engine.set_prefer(prefer);
@@ -1016,6 +1024,12 @@ impl Mesh {
self.inner.engine.set_relay_only(on);
}
+ /// Allows or forbids paths on the local network from now on (see
+ /// [`MeshConfig::lan`]); forbidding forgets the ones learned.
+ pub fn set_lan(&self, on: bool) {
+ self.inner.engine.set_lan_allowed(on);
+ }
+
/// Pins the path to every peer ([`crate::engine::Prefer`]); `auto`
/// lifts the pin.
pub fn set_prefer(&self, prefer: crate::engine::Prefer) {
diff --git a/libs/cua/crates/cua-mesh/tests/mesh.rs b/libs/cua/crates/cua-mesh/tests/mesh.rs
index 46fe70d2eb..be7b9b0bab 100644
--- a/libs/cua/crates/cua-mesh/tests/mesh.rs
+++ b/libs/cua/crates/cua-mesh/tests/mesh.rs
@@ -150,6 +150,53 @@ async fn streams_go_direct_and_carry_data() {
assert_eq!(seen.grant.unwrap().account, "acct-1");
}
+/// A node whose network refuses the local network (`lan = false`) still
+/// carries streams, and none of its paths is a local-network one.
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+async fn a_node_without_the_local_network_still_carries_streams() {
+ let dir = tempfile::tempdir().unwrap();
+ let coord = FakeCoordinator::start(Mode::Account).await;
+ let start = |kind: NodeKind, name: &'static str| {
+ let coord = &coord;
+ let dir = dir.path().to_path_buf();
+ async move {
+ let mut headers = Vec::new();
+ if kind == NodeKind::Machine {
+ headers.push(("x-cua-machine-id".to_owned(), format!("machine-{name}")));
+ }
+ let mut cfg = MeshConfig::new(
+ coord.url.clone(),
+ kind,
+ Some(dir.join(format!("{name}.json"))),
+ static_auth(headers),
+ );
+ cfg.name = name.to_owned();
+ cfg.stun = vec![coord.stun.to_string()];
+ cfg.lan = false;
+ cfg.reconnect_speedup = FAST_RECONNECT;
+ cfg.approved_path = Some(dir.join(format!("{name}-approved.json")));
+ let m = Mesh::start(cfg).await.expect("start");
+ m.wait_ready(READY).await.expect("ready");
+ m
+ }
+ };
+ let machine = start(NodeKind::Machine, "m").await;
+ let client = start(NodeKind::Client, "c").await;
+ serve_echo(&machine);
+ wait_peer(&client, &machine.node_key()).await;
+ let mut s = client
+ .open_stream(&machine.node_key(), "echo", None)
+ .await
+ .unwrap();
+ roundtrip(&mut s, 256 * 1024).await;
+ tokio::time::sleep(Duration::from_secs(3)).await;
+ for m in [&machine, &client] {
+ for p in m.status().peers {
+ assert_ne!(p.path_kind, "lan", "{p:?}");
+ }
+ }
+}
+
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn the_relay_carries_only_ciphertext_when_direct_fails() {
let dir = tempfile::tempdir().unwrap();