Skip to content

Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.

One network at a time; invites wait out a route switch; proxy names looked up again; LF checksums on Windows (#99)

Lars Talian8efddd4c47be
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index f2e8a7e..7da1497 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -51,7 +51,8 @@ jobs:
$name = "reason-network-${{ matrix.target }}.zip"
New-Item -ItemType Directory dist | Out-Null
Compress-Archive target/${{ matrix.target }}/release/reason-network.exe dist/$name
- "$((Get-FileHash dist/$name -Algorithm SHA256).Hash.ToLower()) $name" | Out-File -Encoding ascii dist/$name.sha256
+ # One LF-terminated line, as shasum writes it (Out-File would end it in CRLF, which `shasum -c` refuses).
+ Set-Content -NoNewline -Encoding ascii dist/$name.sha256 "$((Get-FileHash dist/$name -Algorithm SHA256).Hash.ToLower()) $name`n"
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: reason-network-${{ matrix.target }}
diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs
index 4374e7b..f0eca38 100644
--- a/src/daemon/mod.rs
+++ b/src/daemon/mod.rs
@@ -108,6 +108,9 @@ pub struct Daemon {
arrived: Notify,
git_cache: Mutex<HashMap<String, (Instant, GitInfo)>>,
net: RwLock<Option<Arc<Net>>>,
+ /// Held while the network starts, stops or is swapped, and while an invite is made: one at a time, so two networks
+ /// never run on this machine's key, and an invite isn't made on one that is going away.
+ net_lock: tokio::sync::Mutex<()>,
/// Recent message times per sender, for the rate limit.
rate: Mutex<HashMap<String, VecDeque<Instant>>>,
/// When a notification from each sender was last shown here.
@@ -206,6 +209,7 @@ impl Daemon {
arrived: Notify::new(),
git_cache: Mutex::new(HashMap::new()),
net: RwLock::new(None),
+ net_lock: tokio::sync::Mutex::new(()),
rate: Mutex::new(HashMap::new()),
shown: Mutex::new(HashMap::new()),
asks: Mutex::new(HashMap::new()),
diff --git a/src/daemon/teamops.rs b/src/daemon/teamops.rs
index 738bcb5..fe5fbaf 100644
--- a/src/daemon/teamops.rs
+++ b/src/daemon/teamops.rs
@@ -49,19 +49,36 @@ impl Daemon {
}
}
- /// The network again, on the way to the relays that gets through now (see `net::route_loop`).
- pub(crate) async fn restart_net(self: &Arc<Self>) {
+ /// The network again, on the way to the relays that gets through now (see `net::route_loop`), if `net` is still
+ /// the one running: not if it was replaced or stopped meanwhile (a join under way).
+ pub(crate) async fn restart_net(self: &Arc<Self>, net: &Arc<Net>) {
+ let _one = self.net_lock.lock().await;
+ if !self.net().is_some_and(|n| Arc::ptr_eq(&n, net)) {
+ return;
+ }
+ if let Err(e) = self.start_net_now().await {
+ log(&format!("network not started: {e:#}"));
+ }
+ }
+
+ pub(super) async fn start_net(self: &Arc<Self>) -> Result<()> {
+ let _one = self.net_lock.lock().await;
+ self.start_net_now().await
+ }
+
+ /// Stops the network, if one runs (with `net_lock` held).
+ async fn stop_net_now(&self) {
let old = self.net.write().unwrap().take();
if let Some(net) = old {
net.shutdown().await;
}
- if let Err(e) = self.start_net().await {
- log(&format!("network not started: {e:#}"));
- }
}
- pub(super) async fn start_net(self: &Arc<Self>) -> Result<()> {
+ /// Starts the team's network in place of the one running, if any (with `net_lock` held). Invite codes made here
+ /// that are still good are listened for on it.
+ async fn start_net_now(self: &Arc<Self>) -> Result<()> {
let team = self.team_state().ok_or_else(|| anyhow!("not in a team"))?;
+ self.stop_net_now().await;
let relay = team.meta.body.relay.as_deref();
let endpoint = net::bind(self.key.clone(), relay, self.cfg.lan()).await?;
// Last known presence of each machine, so `who --all` shows offline teammates after a restart.
@@ -262,10 +279,8 @@ impl Daemon {
}
// A team of one that nobody joined: switching loses nothing. It stays until the join succeeds.
solo = true;
- let old = self.net.write().unwrap().take();
- if let Some(net) = old {
- net.shutdown().await;
- }
+ let _one = self.net_lock.lock().await;
+ self.stop_net_now().await;
}
let res = self.join_with(invite_text, &person, proof).await;
if let Err(e) = &res {
@@ -397,6 +412,8 @@ impl Daemon {
/// Makes an invite: a code (this machine listens for it until it is used or expires) and the equivalent
/// long ticket (which any teammate's machine can redeem).
pub(super) async fn invite(&self, me: bool, hours: u64) -> Result<InviteInfo> {
+ // Made on the network that runs once a swap under way is done, and listened for there.
+ let _one = self.net_lock.lock().await;
let net =
self.net().ok_or_else(|| anyhow!("not connected to a team yet (run `{CLI} init` or `{CLI} join`)"))?;
net.online(Duration::from_secs(5)).await;
diff --git a/src/net.rs b/src/net.rs
index 131e293..1767a7f 100644
--- a/src/net.rs
+++ b/src/net.rs
@@ -124,6 +124,8 @@ pub struct Net {
relays: Vec<RelayUrl>,
/// The proxy the relays are reached through, or none for directly.
proxy: Option<RelayUrl>,
+ /// The proxy's address as looked up for the endpoint.
+ at: Option<RelayUrl>,
router: Mutex<Option<Router>>,
/// Endpoints listening for joins on behalf of invite codes, by their (code-derived) id.
invites: Mutex<HashMap<EndpointId, Router>>,
@@ -153,32 +155,60 @@ pub async fn relays_for(relay: Option<&str>) -> Result<Vec<RelayUrl>> {
})
}
-/// Where the way to the relays that last got through is kept: present when that was the proxy.
+/// The relays last reached through the proxy, one per line: those are tried that way first.
fn route_file() -> std::path::PathBuf {
crate::paths::home().join("relay-proxy")
}
-/// The proxy to reach `relays` through: one is set (see [`proxy_for`]) and it is what last got through. Otherwise
-/// relays are reached directly, as without a proxy: a proxy variable can be stale (a VPN's, a debugging proxy's) while
-/// the internet is right there.
+fn kept(file: &std::path::Path) -> Vec<String> {
+ let text = std::fs::read_to_string(file).unwrap_or_default();
+ text.lines().map(str::trim).filter(|l| !l.is_empty()).map(String::from).collect()
+}
+
+/// Whether `relays` were last reached through the proxy.
+fn proxied(file: &std::path::Path, relays: &[RelayUrl]) -> bool {
+ let kept = kept(file);
+ relays.iter().any(|r| kept.iter().any(|k| k == r.as_str()))
+}
+
+/// The proxy to reach `relays` through: one is set (see [`proxy_for`]) and it is what last got through to them.
+/// Otherwise relays are reached directly, as without a proxy: a proxy variable can be stale (a VPN's, a debugging
+/// proxy's) while the internet is right there.
fn route(relays: &[RelayUrl]) -> Option<RelayUrl> {
- route_file().exists().then(|| proxy_for(relays)).flatten()
+ proxied(&route_file(), relays).then(|| proxy_for(relays)).flatten()
}
-fn keep_route(proxy: &Option<RelayUrl>) {
- let r = match proxy {
- Some(_) => std::fs::write(route_file(), "the relays last answered through the proxy\n"),
- None => std::fs::remove_file(route_file())
- .or_else(|e| if e.kind() == std::io::ErrorKind::NotFound { Ok(()) } else { Err(e) }),
- };
- if let Err(e) = r {
+/// Keeps how `relays` were reached: through `proxy`, or directly. Other relays' entries (another team's own relay, or
+/// the default ones) stay as they are.
+fn keep_route(proxy: &Option<RelayUrl>, relays: &[RelayUrl]) {
+ if let Err(e) = keep_in(&route_file(), proxy.is_some(), relays) {
log(&format!("couldn't keep the way to the relays: {e}"));
}
}
+fn keep_in(file: &std::path::Path, proxied: bool, relays: &[RelayUrl]) -> std::io::Result<()> {
+ let mut kept = kept(file);
+ kept.retain(|k| !relays.iter().any(|r| r.as_str() == k));
+ if proxied {
+ kept.extend(relays.iter().map(|r| r.to_string()));
+ }
+ if !kept.is_empty() {
+ return std::fs::write(file, kept.join("\n") + "\n");
+ }
+ std::fs::remove_file(file).or_else(|e| if e.kind() == std::io::ErrorKind::NotFound { Ok(()) } else { Err(e) })
+}
+
+/// An endpoint, with how it reaches its relays: `proxy` as set (none: directly), and `at`, the proxy's address as
+/// looked up for it.
+pub struct Bound {
+ pub endpoint: Endpoint,
+ proxy: Option<RelayUrl>,
+ at: Option<RelayUrl>,
+}
+
/// An endpoint on the team's own relay (`relay`) or the default ones, the way that last got through, at once. A
/// running network that stays off its relays tries the other way itself (see `route_loop`).
-pub async fn bind(key: SecretKey, relay: Option<&str>, lan: bool) -> Result<Endpoint> {
+pub async fn bind(key: SecretKey, relay: Option<&str>, lan: bool) -> Result<Bound> {
let relays = relays_for(relay).await?;
endpoint(&key, &relays, relay.is_some(), lan, route(&relays)).await
}
@@ -189,7 +219,7 @@ pub async fn bind_settled(key: SecretKey, relay: Option<&str>, lan: bool) -> Res
let relays = relays_for(relay).await?;
let own = relay.is_some();
let first = route(&relays);
- let endpoint = self::endpoint(&key, &relays, own, lan, first.clone()).await?;
+ let endpoint = self::endpoint(&key, &relays, own, lan, first.clone()).await?.endpoint;
let Some(proxy) = proxy_for(&relays) else { return Ok((endpoint, relays)) };
if tokio::time::timeout(FIRST_WAY, endpoint.online()).await.is_ok() {
return Ok((endpoint, relays));
@@ -201,15 +231,18 @@ pub async fn bind_settled(key: SecretKey, relay: Option<&str>, lan: bool) -> Res
return Ok((endpoint, relays));
}
endpoint.close().await;
- keep_route(&other);
+ keep_route(&other, &relays);
log(&format!("on a relay {}", way(&other)));
- Ok((self::endpoint(&key, &relays, own, lan, other).await?, relays))
+ Ok((self::endpoint(&key, &relays, own, lan, other).await?.endpoint, relays))
}
/// Whether a relay answers the `proxy` way, asked by a throwaway endpoint that publishes nothing (this machine's own
/// stays as it is).
async fn reaches(relays: &[RelayUrl], proxy: &Option<RelayUrl>) -> bool {
- let Ok(probe) = endpoint(&SecretKey::generate(), relays, true, false, proxy.clone()).await else { return false };
+ let Ok(Bound { endpoint: probe, .. }) = endpoint(&SecretKey::generate(), relays, true, false, proxy.clone()).await
+ else {
+ return false;
+ };
let reached = tokio::time::timeout(OTHER_WAY, probe.online()).await.is_ok();
probe.close().await;
reached
@@ -221,7 +254,7 @@ async fn endpoint(
own: bool,
lan: bool,
proxy: Option<RelayUrl>,
-) -> Result<Endpoint> {
+) -> Result<Bound> {
// The default relays, with n0's address lookup still: that is how a teammate on an older Reason Network (on n0's
// relays) and this machine find each other, and what four-word codes need. The team's own relay and nothing of
// n0's: none of its relays, no publishing or looking up addresses in its DNS; teammates are dialed through it.
@@ -229,15 +262,19 @@ async fn endpoint(
let mut b = if own { Endpoint::builder(presets::Minimal) } else { Endpoint::builder(presets::N0) }
.secret_key(key.clone())
.relay_mode(relay_mode);
- if let Some(p) = proxy {
- b = b.proxy_url(looked_up(p).await.into());
+ let at = match &proxy {
+ Some(p) => Some(looked_up(p.clone()).await),
+ None => None,
+ };
+ if let Some(at) = &at {
+ b = b.proxy_url(at.clone().into());
}
if lan && own {
b = b.address_lookup(NoRelays(MdnsAddressLookup::builder().build(key.public())?));
} else if lan {
b = b.address_lookup(MdnsAddressLookup::builder());
}
- Ok(b.bind().await?)
+ Ok(Bound { endpoint: b.bind().await?, proxy, at })
}
/// An http proxy's name looked up the way curl and everything else on this machine looks it up (hosts file, the
@@ -249,7 +286,8 @@ async fn looked_up(proxy: RelayUrl) -> RelayUrl {
if proxy.scheme() != "http" || host.starts_with('[') || host.parse::<std::net::IpAddr>().is_ok() {
return proxy;
}
- let found = tokio::time::timeout(Duration::from_secs(5), tokio::net::lookup_host((host.to_string(), port))).await;
+ // Bounded: a resolver that hangs mustn't hold up the network's start (the name is then left to iroh).
+ let found = tokio::time::timeout(Duration::from_secs(2), tokio::net::lookup_host((host.to_string(), port))).await;
// One address goes on: IPv4 if there is one (a name for this machine lists ::1 first, where a proxy often isn't).
let ips: Vec<std::net::IpAddr> = found.ok().and_then(|r| r.ok()).into_iter().flatten().map(|a| a.ip()).collect();
let Some(&ip) = ips.iter().find(|ip| ip.is_ipv4()).or(ips.first()) else { return proxy };
@@ -365,16 +403,32 @@ async fn route_loop(net: Weak<Net>) {
wait = FIRST_WAY;
continue;
};
- let other = if this.proxy.is_some() { None } else { Some(proxy) };
- if !reaches(&this.relays, &other).await || on() || this.endpoint.is_closed() {
- wait = (wait * 2).min(Duration::from_secs(60));
+ // Through the proxy, its name may stand for another address by now: the same way, looked up afresh.
+ let fresh = match &this.proxy {
+ Some(p) => Some(looked_up(p.clone()).await).filter(|at| Some(at) != this.at.as_ref()),
+ None => None,
+ };
+ let (other, note) = match fresh {
+ Some(at) if reaches(&this.relays, &Some(at.clone())).await => {
+ (this.proxy.clone(), format!("{}, looked up again", way(&Some(at))))
+ }
+ _ => {
+ let other = if this.proxy.is_some() { None } else { Some(proxy) };
+ let note = way(&other);
+ if !reaches(&this.relays, &other).await {
+ wait = (wait * 2).min(Duration::from_secs(60));
+ continue;
+ }
+ (other, note)
+ }
+ };
+ if on() || this.endpoint.is_closed() {
continue;
}
- log(&format!("no relay {} ({}), but {}: switching", way(&this.proxy), unreached(&this.endpoint), way(&other)));
- keep_route(&other);
+ log(&format!("no relay {} ({}), but {note}: switching", way(&this.proxy), unreached(&this.endpoint)));
+ keep_route(&other, &this.relays);
let Some(daemon) = this.daemon.upgrade() else { return };
- drop(this);
- daemon.restart_net().await;
+ daemon.restart_net(&this).await;
return;
}
}
@@ -638,11 +692,12 @@ impl ProtocolHandler for JoinProto {
impl Net {
pub fn start(
daemon: &Arc<Daemon>,
- endpoint: Endpoint,
+ bound: Bound,
relay: Option<&str>,
remote: HashMap<EndpointId, Remote>,
) -> Arc<Net> {
let relays: Vec<RelayUrl> = relay.map_or_else(crate::relays::current, |u| u.parse().into_iter().collect());
+ let Bound { endpoint, proxy, at } = bound;
let net = Arc::new_cyclic(|weak: &Weak<Net>| Net {
me: endpoint.id(),
router: Mutex::new(Some(
@@ -653,8 +708,9 @@ impl Net {
)),
invites: Default::default(),
endpoint,
- relays: relays.clone(),
- proxy: route(&relays),
+ relays,
+ proxy,
+ at,
daemon: Arc::downgrade(daemon),
links: Default::default(),
// Saved by an older build, maybe, which tidied less.
@@ -711,7 +767,7 @@ impl Net {
relay: Option<&str>,
lan: bool,
) -> Result<()> {
- let endpoint = bind(key, relay, lan).await?;
+ let endpoint = bind(key, relay, lan).await?.endpoint;
let id = endpoint.id();
let router = Router::builder(endpoint).accept(JOIN_ALPN, JoinProto(Arc::downgrade(self), Some(id))).spawn();
self.invites.lock().unwrap().insert(id, router);
@@ -1047,6 +1103,27 @@ fn tidy_presence(mut p: Presence) -> Presence {
mod tests {
use super::*;
+ #[test]
+ fn the_way_to_relays_is_kept_for_those_relays_only() {
+ let file = std::env::temp_dir().join(format!("reason-network-route-{}", ulid::Ulid::generate()));
+ let url = |u: &str| -> RelayUrl { u.parse().unwrap() };
+ let default = [url("https://relay-sfo1.example.com"), url("https://relay-fra1.example.com")];
+ let own = [url("https://relay.team.example.com")];
+ assert!(!proxied(&file, &default), "nothing kept: directly");
+ keep_in(&file, true, &own).unwrap();
+ assert!(proxied(&file, &own) && !proxied(&file, &default), "a team relay's way isn't the default relays'");
+ keep_in(&file, true, &default).unwrap();
+ assert!(proxied(&file, &default) && proxied(&file, &[url("https://relay-sfo1.example.com")]));
+ assert!(proxied(&file, &[url("https://relay-new.example.com"), default[1].clone()]), "a list that grew");
+ keep_in(&file, false, &default).unwrap();
+ assert!(!proxied(&file, &default) && proxied(&file, &own), "directly again, for those relays only");
+ keep_in(&file, false, &own).unwrap();
+ assert!(!file.exists(), "nothing left to keep");
+ std::fs::write(&file, "the relays last answered through the proxy\n").unwrap();
+ assert!(!proxied(&file, &default), "an earlier build's file names no relay");
+ let _ = std::fs::remove_file(&file);
+ }
+
#[test]
fn relays_go_through_the_proxy_curl_would_use() {
let relay = ["relay-fra1.compute.reasonmachines.com"];
@@ -1185,7 +1262,7 @@ mod tests {
#[tokio::test]
async fn a_link_naming_only_this_machine_says_so() {
let key = SecretKey::generate();
- let endpoint = bind(key.clone(), Some("https://relay.example.com"), false).await.unwrap();
+ let endpoint = bind(key.clone(), Some("https://relay.example.com"), false).await.unwrap().endpoint;
let req = JoinRequest {
team: String::new(),
secret: String::new(),
@@ -1204,7 +1281,7 @@ mod tests {
#[tokio::test]
async fn a_link_naming_this_machine_and_offline_ones_says_to_ask_an_online_teammate() {
let key = SecretKey::generate();
- let endpoint = bind(key.clone(), Some("https://relay.example.com"), false).await.unwrap();
+ let endpoint = bind(key.clone(), Some("https://relay.example.com"), false).await.unwrap().endpoint;
let req = JoinRequest {
team: String::new(),
secret: String::new(),