feat(net): annoncer l'adresse LAN quand un VPN prend la route par défaut
CI / Formatting, tests and builds (push) Successful in 2m59s

L'adresse du QR code et des candidats ICE était celle de la route par
défaut: un VPN en tunnel complet faisait annoncer l'adresse du tunnel,
injoignable depuis la tablette. Quand cette adresse appartient à un
tunnel (type de lien none, PPP, IP dans IP, GRE, ou TAP), l'hôte annonce
la première interface physique avec une adresse RFC 1918. Le split
tunnel ne change rien. Le journal nomme l'interface du candidat retenu.

Le cas du client VPN qui coupe le LAN et de l'isolation AP est décrit
dans docs/hote.md.

Refs #40

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guillaume ASSIERandClaude Opus 5.5 committed 2026-10-30 15:47:25 +01:00
1 parent 0592c56340
commit ae5c76a51c
6 files changed
+208 -10

No files matched your search

Generated
+1
View File
@@ -1799,6 +1799,7 @@ dependencies = [
name = "rs-rtc"
version = "0.5.0"
dependencies = [
"if-addrs",
"rs-codec",
"str0m",
"thiserror",
+1
View File
@@ -10,6 +10,7 @@ repository.workspace = true
[dependencies]
rs-codec = { path = "../rs-codec" }
str0m = "0.24.1"
if-addrs = "0.15.0"
thiserror.workspace = true
tracing.workspace = true
tokio = { version = "1.54.1", features = ["rt", "net", "time", "sync", "macros"] }
+2 -9
View File
@@ -70,6 +70,7 @@ pub async fn run(
tracing::info!(
event = "ice_candidate_selected",
local = %destination,
interface = crate::lan::interface_of(destination.ip()).as_deref().unwrap_or("?"),
remote = ?peer,
"ICE and DTLS established"
);
@@ -196,15 +197,7 @@ fn send_frame(
}
}
/// LAN address of this machine that a browser can reach.
///
/// Chrome ignores a `127.0.0.1` candidate: take the address of the interface
/// carrying the default route.
pub fn local_candidate_address() -> Option<std::net::IpAddr> {
let socket = std::net::UdpSocket::bind("0.0.0.0:0").ok()?;
socket.connect("192.0.2.1:9").ok()?;
Some(socket.local_addr().ok()?.ip())
}
pub use crate::lan::local_candidate_address;
/// Opens the media socket and returns the advertised address.
///
+180
View File
@@ -0,0 +1,180 @@
//! The LAN address to advertise, even when a VPN holds the default route (#40).
//!
//! The address advertised in the QR code and in the ICE candidates used to be
//! the source address of the default route. A VPN that takes that route
//! (WireGuard, OpenVPN, a corporate client) then made the host advertise its
//! tunnel address, which no tablet on the LAN can reach: the page would not
//! load, or ICE would stay in `checking`.
//!
//! The rule: the default route's address, unless it belongs to a tunnel; then
//! the first physical interface that has a private IPv4 address. A split
//! tunnel leaves the default route on the LAN and changes nothing.
use std::net::IpAddr;
/// An interface address, as the system lists it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Candidate {
pub interface: String,
pub address: IpAddr,
/// Tunnel or point-to-point link: a VPN, never the LAN.
pub tunnel: bool,
/// Backed by a device (Ethernet, Wi-Fi), as opposed to a bridge or a
/// virtual link (Docker, libvirt).
pub physical: bool,
}
/// LAN address of this machine that a browser can reach. Chrome ignores a
/// `127.0.0.1` candidate, a tablet cannot reach a tunnel address.
pub fn local_candidate_address() -> Option<IpAddr> {
choose(default_route_address(), &candidates())
}
/// The interface carrying `address`, for the logs (#40).
pub fn interface_of(address: IpAddr) -> Option<String> {
candidates()
.into_iter()
.find(|c| c.address == address)
.map(|c| c.interface)
}
/// The choice, without the system: see the module documentation.
pub fn choose(routed: Option<IpAddr>, candidates: &[Candidate]) -> Option<IpAddr> {
let routed_tunnel =
routed.is_some_and(|ip| candidates.iter().any(|c| c.address == ip && c.tunnel));
if !routed_tunnel {
return routed;
}
let lan = |c: &&Candidate| !c.tunnel && is_private_v4(c.address);
candidates
.iter()
.filter(lan)
.find(|c| c.physical)
.or_else(|| candidates.iter().find(lan))
.map(|c| c.address)
.or(routed)
}
/// The source address of the default route. A UDP `connect` sends nothing:
/// it only asks the kernel which address it would use. The destination is
/// reserved for documentation (RFC 5737).
fn default_route_address() -> Option<IpAddr> {
let socket = std::net::UdpSocket::bind("0.0.0.0:0").ok()?;
socket.connect("192.0.2.1:9").ok()?;
Some(socket.local_addr().ok()?.ip())
}
fn candidates() -> Vec<Candidate> {
if_addrs::get_if_addrs()
.unwrap_or_default()
.into_iter()
.filter(|i| !i.is_loopback())
.map(|i| Candidate {
tunnel: is_tunnel(&i.name),
physical: is_physical(&i.name),
address: i.ip(),
interface: i.name,
})
.collect()
}
/// Kernel link types of tunnels: `none` (tun, WireGuard), PPP, IP in IP, SIT,
/// GRE. A TAP device looks like Ethernet: its `tun_flags` gives it away.
fn is_tunnel(name: &str) -> bool {
let sys = std::path::Path::new("/sys/class/net").join(name);
let link_type = std::fs::read_to_string(sys.join("type"))
.ok()
.and_then(|t| t.trim().parse::<u32>().ok());
match link_type {
Some(65534 | 512 | 768 | 776 | 778) => true,
Some(_) => sys.join("tun_flags").exists(),
// Outside Linux, or no sysfs: the usual names.
None => [
"tun",
"tap",
"wg",
"ppp",
"utun",
"ipsec",
"tailscale",
"zt",
]
.iter()
.any(|prefix| name.starts_with(prefix)),
}
}
fn is_physical(name: &str) -> bool {
std::path::Path::new("/sys/class/net")
.join(name)
.join("device")
.exists()
}
/// RFC 1918 only: 100.64.0.0/10, the carrier-grade NAT range that Tailscale
/// and NetBird use, is never taken for the LAN.
fn is_private_v4(ip: IpAddr) -> bool {
matches!(ip, IpAddr::V4(v4) if v4.is_private())
}
#[cfg(test)]
mod tests {
use super::*;
fn candidate(interface: &str, ip: &str, tunnel: bool, physical: bool) -> Candidate {
Candidate {
interface: interface.into(),
address: ip.parse().unwrap(),
tunnel,
physical,
}
}
fn machine() -> Vec<Candidate> {
vec![
candidate("docker0", "172.17.0.1", false, false),
candidate("wlp0s20f3", "192.168.1.20", false, true),
candidate("wg0", "10.8.0.2", true, false),
]
}
#[test]
fn the_default_route_wins_when_it_is_the_lan() {
let lan: IpAddr = "192.168.1.20".parse().unwrap();
assert_eq!(choose(Some(lan), &machine()), Some(lan));
}
#[test]
fn a_vpn_on_the_default_route_gives_way_to_the_physical_lan() {
let tunnel: IpAddr = "10.8.0.2".parse().unwrap();
// Docker comes first in the list, and is private too: the physical
// interface is preferred.
assert_eq!(
choose(Some(tunnel), &machine()),
Some("192.168.1.20".parse().unwrap())
);
}
#[test]
fn without_a_lan_the_routed_address_stays() {
let tunnel: IpAddr = "10.8.0.2".parse().unwrap();
let only = vec![candidate("wg0", "10.8.0.2", true, false)];
assert_eq!(choose(Some(tunnel), &only), Some(tunnel));
assert_eq!(choose(None, &only), None);
}
#[test]
fn this_machine_has_an_answer_that_is_not_a_tunnel() {
let Some(chosen) = local_candidate_address() else {
return;
};
let tunnel = candidates().iter().any(|c| c.address == chosen && c.tunnel);
let lan_exists = candidates()
.iter()
.any(|c| !c.tunnel && is_private_v4(c.address));
assert!(
!tunnel || !lan_exists,
"{chosen} is a tunnel while a LAN exists"
);
}
}
+1
View File
@@ -11,6 +11,7 @@
//! source (see `rs-codec`).
pub mod driver;
pub mod lan;
pub mod session;
pub use driver::{Command, bind_media_socket, local_candidate_address, run};
+23 -1
View File
@@ -79,7 +79,26 @@ l'avertissement demande de faire scanner le nouveau QR code. L'ancienne URL ne m
part: aucun mécanisme ne peut l'y faire suivre sans nom local (#25).
Sans interface choisie, l'URL suit la route par défaut, et un changement est signalé de la même
façon.
façon. L'annonce `rustscreen.local` (#25) suit la même adresse; le QR code garde l'IP.
## Derrière un VPN (#40)
Un VPN en **split tunnel** laisse la route par défaut sur le LAN: rien ne change. Un VPN qui prend
la route par défaut (WireGuard, OpenVPN, client d'entreprise en tunnel complet) faisait annoncer
l'adresse du tunnel, que la tablette ne joint pas. Désormais, quand l'adresse de la route par défaut
appartient à un tunnel (type de lien `none`, PPP, IP dans IP, GRE, ou `tun_flags` pour un TAP),
l'hôte annonce la première interface physique qui a une adresse privée RFC 1918, et à défaut la
première interface non tunnel. La plage 100.64.0.0/10 de Tailscale et NetBird n'est jamais prise
pour le LAN. Choisir l'interface dans les réglages (#33) reste le moyen de tout fixer à la main.
Aucun candidat STUN public, ni par défaut ni en option: seuls des candidats `host` sont annoncés, et
le journal nomme l'interface du candidat retenu (`ice_candidate_selected`, champ `interface`).
Un client de VPN qui coupe le trafic du LAN (« bloquer le réseau local », kill switch) ou un point
d'accès qui isole ses clients (Wi-Fi invité, isolation AP) empêchent toute connexion entre la
tablette et l'hôte: aucun logiciel ne contourne cela. Les signes: la page ne charge pas par
l'adresse affichée, alors qu'elle charge sur l'hôte. Remède: autoriser l'accès au réseau local dans
le client VPN, ou passer par un réseau sans isolation.
## Appairage (#19)
@@ -189,3 +208,6 @@ est resté éteint.
demande un clic: non automatisable ici);
- un scan de QR code sur iOS et Android, et VP9 dans Chromium et Safari (seul Firefox a décodé);
- le contraste AA est calculé sur les couleurs déclarées, pas mesuré à l'écran.
- un VPN en tunnel complet réel (la règle de choix d'adresse est testée sur des listes
d'interfaces; les espaces de noms réseau, qui permettraient de la jouer sans root, sont bloqués
sur la machine de développement).