Files
wanderbooks/crates/relay/src/main.rs
T
swaitsandClaude Fable 5.2 13adeac382 fix(relay): one address or network cannot stop the relay for the others
A review found ways for one cheap attacker to stop the service.

- Line handshakes were limited only by a global pool: one address could
  finish a hello, stall, and repeat. A handshake now holds a place for its
  address (4) and network (64) from the relay's host name to the verdict.
- Claims were counted per full address and against the certificate
  authority's weekly 50: one IPv6 /64 could spend all 50 with throwaway
  keys. Addresses now count by IPv6 /64 and /48 and IPv4 address and /24;
  claiming a name is limited by a separate safety ceiling (5 000 a day) and
  by those prefix limits, and the authority's weekly budget is charged when
  a name first publishes a proof record (once per name, renewals free,
  durable in the registry), under per-prefix limits of its own.
- IPv4 callers on a dual-stack listener (::ffff:a.b.c.d) shared one slot:
  every table now makes the address canonical first.
- The pool of unfinished hellos also counts per /48 and /24, and a
  reserve is kept for addresses that held a proven line lately (the server
  name is not known before the hello, so it is for who calls, not what is
  asked).
- A static account-binding secret was served to anyone with a name: the
  list file may no longer carry one (refused at load), a binding comes only
  from a per-app source, and an authority that needs one and has none is
  served without and marked not usable.
- Blocking now reaches a live line: the next publish and a sweep every
  10 s close it.
- Unclaimed, held-by-another-key and blocked names answer a stranger the
  same.
- A hostile app and caller could make a line hold 13 MiB: yamux fixes each
  stream's window at 256 KiB, so the stream limits are 8+2+4 (3.5 MiB), and
  the splice has a minimum rate in place of a per-write stall check.
- DNS answers truncated over the per-address rate, has a global ceiling,
  answers CAA from the authority list (issue per authority, dns-01 only),
  and passes over a Windows UDP connection reset without sleeping.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 09:52:54 -07:00

109 lines
3.8 KiB
Rust

//! `wanderbooks-relay`: run the relay from a configuration file.
//!
//! ```text
//! wanderbooks-relay <config.toml>
//! wanderbooks-relay <config.toml> block <four-word-name>
//! wanderbooks-relay <config.toml> unblock <four-word-name>
//! ```
use std::process::ExitCode;
use std::sync::atomic::Ordering;
use std::time::Duration;
use relay::authorities::AuthorityList;
use relay::names::Name;
use relay::registry::Registry;
use relay::{Config, Relay, tls};
fn fail(msg: &str) -> ExitCode {
eprintln!("wanderbooks-relay: {msg}");
ExitCode::FAILURE
}
#[tokio::main]
async fn main() -> ExitCode {
let args: Vec<String> = std::env::args().skip(1).collect();
let Some(path) = args.first() else {
return fail("usage: wanderbooks-relay <config.toml> [block|unblock <name>]");
};
let text = match std::fs::read_to_string(path) {
Ok(t) => t,
Err(e) => return fail(&format!("{path}: {e}")),
};
let cfg = match Config::from_toml(&text) {
Ok(c) => c,
Err(e) => return fail(&format!("{path}: {e}")),
};
let words: Vec<&str> = args.iter().map(String::as_str).collect();
if let [_, verb @ ("block" | "unblock"), name] = words.as_slice() {
let Ok(name) = Name::parse(name) else {
return fail("a name is four words from the list, joined by hyphens");
};
let registry = match Registry::open(&cfg.registry_path, 4) {
Ok(r) => r,
Err(e) => return fail(&e.to_string()),
};
return match registry.set_blocked(&name, *verb == "block").await {
Ok(true) => {
println!("{verb}ed {name}");
ExitCode::SUCCESS
}
Ok(false) => fail("no such name"),
Err(e) => fail(&e.to_string()),
};
}
if args.len() != 1 {
return fail("usage: wanderbooks-relay <config.toml> [block|unblock <name>]");
}
let tls = match tls::server_config_from_files(&cfg.tls_cert, &cfg.tls_key) {
Ok(t) => t,
Err(e) => return fail(&e),
};
// No per-app binding source is installed: an authority that needs an
// external account binding is listed, served without one and marked
// not usable until a deployment supplies a `BindingSource`. The list
// file never carries a binding secret (it is refused at load).
let authorities = match &cfg.authorities_path {
Some(p) => match AuthorityList::from_file(p.clone()) {
Ok(l) => l,
Err(e) => return fail(&e.to_string()),
},
None => AuthorityList::empty(),
};
let relay = match Relay::start(cfg, tls, authorities).await {
Ok(r) => r,
Err(e) => return fail(&format!("could not start: {e}")),
};
eprintln!(
"wanderbooks-relay: router on {}, DNS on {} (udp) and {} (tcp)",
relay.router_addr(),
relay.dns_udp_addr(),
relay.dns_tcp_addr()
);
let shared = relay.shared().clone();
let stats = tokio::spawn(async move {
let mut tick = tokio::time::interval(Duration::from_secs(60));
loop {
tick.tick().await;
let m = &shared.metrics;
eprintln!(
"lines {} routed {} unknown_or_offline {} shed {} hello_timeouts {} bad_hello {} claims {}",
m.lines_up.load(Ordering::Relaxed),
m.routed.load(Ordering::Relaxed),
m.unknown_or_offline.load(Ordering::Relaxed),
m.shed.load(Ordering::Relaxed),
m.hello_timeouts.load(Ordering::Relaxed),
m.bad_hello.load(Ordering::Relaxed),
m.claims.load(Ordering::Relaxed),
);
}
});
let _ = tokio::signal::ctrl_c().await;
eprintln!("wanderbooks-relay: shutting down");
stats.abort();
relay.shutdown().await;
ExitCode::SUCCESS
}