feat(relay): add --relay / PIXELPASS_RELAY override

Both host and viewer hardcoded presets::N0, pinning every session to the
bundled relays (which on iroh rc.0 are the canary-grade defaults). Add a
shared common::endpoint::bind() that keeps N0's DNS discovery + crypto but
swaps in a RelayMode::Custom single-relay map when --relay (or the
PIXELPASS_RELAY env var, so GUI children inherit it) is set.

Lets users point at a self-hosted relay or staging today; the production
relays (*.relay.iroh.network) speak a newer protocol that rc.0 rejects
("invalid iroh-relay version header"), so they only become usable — and
the default — after an iroh GA bump. Verified: override connects cleanly
through staging; bad URLs are rejected before any network work.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 15:59:36 -04:00
parent 32131b0ccb
commit eb077d81f0
6 changed files with 66 additions and 14 deletions
+42
View File
@@ -0,0 +1,42 @@
//! Shared iroh endpoint construction for the host and viewer.
//!
//! Both sides bind an endpoint with the same ALPN; the only knob is the relay.
use std::str::FromStr;
use anyhow::{Context, Result};
use iroh::endpoint::presets;
use iroh::{Endpoint, RelayMap, RelayMode, RelayUrl};
use super::alpn::ALPN;
/// Environment variable consulted when `--relay` isn't passed. Lets the GUI's
/// child processes and scripted runs inherit a relay choice without a flag.
pub const RELAY_ENV: &str = "PIXELPASS_RELAY";
/// Resolve the relay override: explicit `--relay` wins, else `PIXELPASS_RELAY`,
/// else `None` (use the bundled defaults).
pub fn relay_override(flag: Option<&str>) -> Option<String> {
flag.map(str::to_owned)
.or_else(|| std::env::var(RELAY_ENV).ok().filter(|s| !s.trim().is_empty()))
}
/// Bind the iroh endpoint with our ALPN.
///
/// With no `relay` override we use [`presets::N0`] — n0 DNS discovery, the
/// library's default relays, and the chosen crypto provider. With an override
/// we keep all of that but swap in a single custom relay via
/// [`RelayMode::Custom`]; this is how a user gets off the rc's bundled
/// (canary-grade) relays or points at a self-hosted one. Discovery is
/// unchanged, so peers still resolve each other by endpoint id.
pub async fn bind(relay: Option<&str>) -> Result<Endpoint> {
let mut builder = Endpoint::builder(presets::N0).alpns(vec![ALPN.to_vec()]);
if let Some(url) = relay {
let url = RelayUrl::from_str(url)
.with_context(|| format!("invalid relay URL {url:?} (expected e.g. https://relay.example/)"))?;
builder = builder.relay_mode(RelayMode::Custom(RelayMap::from(url)));
}
builder.bind().await.context("failed to bind the iroh endpoint")
}
+1
View File
@@ -2,6 +2,7 @@ pub mod alpn;
pub mod bandwidth;
pub mod config;
pub mod deps;
pub mod endpoint;
pub mod display;
pub mod output;
pub mod process;