Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkgs/id/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ impl SaveRateLimiter {
if let Some(last) = map.get(name) {
let elapsed = last.elapsed();
if elapsed < self.cooldown {
return Err(self.cooldown.checked_sub(elapsed).unwrap());
// checked_sub is Some exactly because elapsed < cooldown;
// ZERO is the unreachable fallback (no time remaining).
return Err(self.cooldown.checked_sub(elapsed).unwrap_or(Duration::ZERO));
}
}
Ok(())
Expand Down
5 changes: 4 additions & 1 deletion pkgs/id/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::sync::Arc;

use crate::time::unix_ms;
use crate::world_caps::{CapError, CapsReport, Decision, Request, Subscription};
use anyhow::{Context, Result, bail, ensure};
use rand::RngExt as _;
Expand All @@ -32,6 +31,10 @@ use tokio::sync::{broadcast, mpsc, oneshot, watch};
/// in [`crate::groups::worlds`] so the crate builds without this feature.
pub use crate::groups::worlds::Isolation;

/// Re-exported so `crate::world::unix_ms` keeps working: the function now
/// lives in [`crate::time`] so the crate builds without this feature.
pub(crate) use crate::time::unix_ms;

/// Bounded limits for one world.
#[derive(Clone, Copy, Debug)]
pub struct WorldLimits {
Expand Down
24 changes: 24 additions & 0 deletions pkgs/id/tests/fixtures/relay/iroh-relay.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# systemd unit for iroh-relay, shared by both modes in relay.kit.toml. Install the binary at
# /usr/local/bin/iroh-relay and the mode's config at /etc/iroh-relay/relay.toml before enabling
# the unit. The vps mode also installs iroh-relay.service.d/vps.conf, which grants the port
# capability. The tunnel mode installs none.
[Unit]
Description=iroh relay
After=network-online.target
Wants=network-online.target
# Refuse to start without the config. The relay otherwise starts silently on [::]:80 (S11).
ConditionPathExists=/etc/iroh-relay/relay.toml

[Service]
ExecStart=/usr/local/bin/iroh-relay --config-path /etc/iroh-relay/relay.toml
# The relay ignores SIGTERM and is hard-killed by it. SIGINT exits cleanly (S11).
KillSignal=SIGINT
User=iroh-relay
Group=iroh-relay
StateDirectory=iroh-relay
Environment=RUST_LOG=info
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
6 changes: 6 additions & 0 deletions pkgs/id/tests/fixtures/relay/iroh-relay.service.d/vps.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Drop-in for the vps mode (relay.kit.toml). The relay binds ports 80 and 443, which are below
# 1024, so the User=iroh-relay account needs CAP_NET_BIND_SERVICE. The tunnel mode binds only
# loopback port 3340 and installs no drop-in, so its unit grants no capability.
[Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
35 changes: 35 additions & 0 deletions pkgs/id/tests/fixtures/relay/relay.kit.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# The relay kit's modes. A mode is one way to run iroh-relay on one host.
#
# tunnel setup A, a Cloudflare Tunnel. cloudflared terminates TLS at Cloudflare's edge and
# forwards to the relay's plain HTTP origin on loopback. No inbound port, no UDP.
# vps setup B, a public IPv4 address. TLS on 443, HTTP on 80, QUIC address discovery on
# 7842/udp.
#
# Each mode's values are the ones its config file and unit must carry. tests/relay_ops.rs
# checks the committed config and unit for each mode against this file.
#
# Keys:
# config the relay config for this mode, copied to /etc/iroh-relay/relay.toml
# unit_dropin the unit drop-in to install beside iroh-relay.service, if any
# hostname the public name clients use, as https://<hostname>
# http_bind_addr the plain HTTP listener
# https_bind_addr the TLS listener; absent when the mode serves no TLS
# metrics_bind_addr the metrics listener, which must stay on loopback
# qad whether QUIC address discovery is on

[modes.tunnel]
config = "tunnel.toml"
# The Tunnel's public hostname, set in the Cloudflare dashboard to point at http://localhost:3340.
hostname = "relay.example.org"
http_bind_addr = "127.0.0.1:3340"
metrics_bind_addr = "127.0.0.1:9090"
qad = false

[modes.vps]
config = "vps.toml"
unit_dropin = "iroh-relay.service.d/vps.conf"
hostname = "relay.example.org"
http_bind_addr = "[::]:80"
https_bind_addr = "[::]:443"
metrics_bind_addr = "127.0.0.1:9090"
qad = true
18 changes: 18 additions & 0 deletions pkgs/id/tests/fixtures/relay/tunnel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# iroh-relay, Cloudflare Tunnel mode (setup A in the connectivity design). Its values are in
# relay.kit.toml under [modes.tunnel].
#
# Plain HTTP on loopback. cloudflared terminates TLS at Cloudflare's edge and forwards to
# this address, so the relay needs no certificate and no open inbound port.
#
# QUIC address discovery needs UDP, which Tunnel does not carry, so it stays off. Clients
# using this relay must build its RelayConfig with no QUIC port (RelayConfig::new(url, None)).
http_bind_addr = "127.0.0.1:3340"
enable_quic_addr_discovery = false

# The metrics endpoint defaults to [::]:9090, which is public. Keep it on loopback.
metrics_bind_addr = "127.0.0.1:9090"

# Optional abuse control. Clients must then send one of these tokens, as a Bearer header or
# as ?token=. Check the key names against iroh-relay's access config before enabling it.
# [access]
# shared_token = ["replace-with-a-long-random-token"]
29 changes: 29 additions & 0 deletions pkgs/id/tests/fixtures/relay/vps.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# iroh-relay, VPS mode (setup B in the connectivity design). Its values are in relay.kit.toml
# under [modes.vps].
#
# Needs a public IPv4 address and these open ports:
# 80/tcp ACME challenge and captive-portal check
# 443/tcp relay (WebSocket over TLS) and /ping
# 7842/udp QUIC address discovery, which lets peers find their public addresses for direct paths
#
# Replace relay.example.org and ops@example.org before use. Keep cert_dir on persistent storage:
# Let's Encrypt allows 5 certificates per identical name set per week.
http_bind_addr = "[::]:80"
enable_quic_addr_discovery = true

# Keep metrics off the public interface.
metrics_bind_addr = "127.0.0.1:9090"

[tls]
https_bind_addr = "[::]:443"
hostname = "relay.example.org"
cert_mode = "LetsEncrypt"
cert_dir = "/var/lib/iroh-relay/certs"
contact = "ops@example.org"

# Keep false while testing, so Let's Encrypt staging issues the certificate. Set true for production.
prod_tls = false

# Optional abuse control, as in tunnel.toml.
# [access]
# shared_token = ["replace-with-a-long-random-token"]
49 changes: 46 additions & 3 deletions pkgs/id/tests/relay_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
//! `relay.kit.toml` has one table per mode. `tunnel` is a Cloudflare Tunnel
//! (no inbound port, plain HTTP on loopback). `vps` is a public address (TLS on
//! 80 and 443, QUIC address discovery). A mode's unit is the shared
//! `iroh-relay.service` plus the mode's drop-in, if it has one. The kit lives in
//! the design doc, so the paths are relative to this crate.
//! `iroh-relay.service` plus the mode's drop-in, if it has one.
//!
//! The kit is vendored under `tests/fixtures/relay` so it is inside this
//! crate's source tree: the Nix flake cannot see the design doc copy at the
//! repo root (`doc/2026-10-11T03-44-33Z_design_groups_events_delegation/
//! connectivity/relay`, #238), which stays canonical. Edit the doc copy and
//! re-copy it here; `test_vendored_kit_matches_the_design_doc_copy` fails
//! whenever the doc copy is present and the two have drifted.
//!
//! Each check runs on the kit's real files, and also on mutated copies, so a
//! check that accepts everything fails.
Expand All @@ -15,11 +21,25 @@

use std::{net::SocketAddr, path::Path};

const KIT: &str = concat!(
const KIT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/relay");

/// The canonical kit, in the design doc at the repo root. Outside this
/// crate's source tree, so it does not exist in the Nix sandbox; the
/// vendored copy above is what the checks read there.
const CANONICAL_KIT: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../doc/2026-10-11T03-44-33Z_design_groups_events_delegation/connectivity/relay"
);

/// Every file in the kit, relative to the kit root.
const KIT_FILES: [&str; 5] = [
"relay.kit.toml",
"tunnel.toml",
"vps.toml",
"iroh-relay.service",
"iroh-relay.service.d/vps.conf",
];

const KIT_FILE: &str = "relay.kit.toml";
const UNIT: &str = "iroh-relay.service";
/// What both modes must run. The config path is where the unit's
Expand Down Expand Up @@ -506,3 +526,26 @@ fn test_config_check_accepts_loopback_ipv6() {
);
check_config(&config).unwrap();
}

/// The vendored kit must match the canonical design-doc copy, so the two
/// cannot drift silently. The doc copy lives outside this crate's source
/// tree and is absent in the Nix sandbox; there the vendored copy is the
/// only one and this check has nothing to compare against. When the doc
/// copy IS present, any difference fails loudly.
#[test]
fn test_vendored_kit_matches_the_design_doc_copy() {
let canonical = Path::new(CANONICAL_KIT);
if !canonical.is_dir() {
return;
}
for name in KIT_FILES {
let vendored = kit_file(name);
let original = std::fs::read_to_string(canonical.join(name))
.expect("every vendored kit file exists in the design doc copy");
assert_eq!(
vendored, original,
"vendored kit file {name} differs from the design doc copy; \
edit the doc copy and re-copy it into tests/fixtures/relay"
);
}
}
Loading