diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-08-29 14:49:11 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-08-29 14:49:11 +0200 |
| commit | be57cf9c3b499c8e59a94d13059f16f1456fcae1 (patch) | |
| tree | 674e7d32ea6c1def0a4b86e40bad5eda3ba04c1d /packages/meshbay-node/src/meshbay_node/transport | |
| parent | c4813d1aaf1f4fdf3eb9dc07909f324f5745544d (diff) | |
| download | meshbay-be57cf9c3b499c8e59a94d13059f16f1456fcae1.tar.gz | |
perf(node): filter ICE interfaces to eliminate 5s STUN timeout on VPN/virtual adapters
aioice sends STUN binding requests from every IPv4 interface and waits up
to 5 seconds for all to complete. On a machine with Tailscale (wt0), the
STUN request never gets a response, adding a fixed 5-second penalty to
every WebRTC connection — measured at 6 s total (vs 1-2 s without it).
Auto-exclude virtual/VPN adapters (tailscale, virbr, docker, veth, podman,
cni) and CGNAT-range IPs (100.64.0.0/10). Operator can override with
ice_interfaces in node.toml [node] section for explicit control.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/transport')
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/transport/ice_filter.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/transport/ice_filter.py b/packages/meshbay-node/src/meshbay_node/transport/ice_filter.py new file mode 100644 index 0000000..a91724e --- /dev/null +++ b/packages/meshbay-node/src/meshbay_node/transport/ice_filter.py @@ -0,0 +1,82 @@ +""" +Filter network interfaces for ICE candidate gathering. + +aioice enumerates every interface and sends a STUN binding request from each +IPv4 address. On a machine with a Tailscale (wt0), libvirt (virbr*), or +Docker (docker*, veth*) interface, the STUN request often cannot reach the +server and burns the full 5-second asyncio.wait timeout — measured at 6 s +end-to-end on a Fedora laptop with Tailscale. + +This module patches aioice.ice.get_host_addresses so the gather runs against +the interfaces the operator actually wants. Two modes: + + ice_interfaces = ["wlp0s20f3"] → explicit include-list, nothing else + ice_interfaces = [] → auto-exclude virtual/VPN adapters + +Auto-excluded adapter name patterns: tailscale*, wt*, virbr*, docker*, +veth*, br-*, podman*, cni*. +""" + +import ipaddress +import logging +import re + +import aioice.ice + +log = logging.getLogger(__name__) + +_VIRTUAL_ADAPTER_RE = re.compile( + r"^(tailscale|wt|virbr|docker|veth|br-|podman|cni)", + re.IGNORECASE, +) + +_original_get_host_addresses = aioice.ice.get_host_addresses + + +def _is_cgnat(addr: str) -> bool: + """Tailscale uses 100.64.0.0/10 (RFC 6598 CGNAT).""" + try: + ip = ipaddress.ip_address(addr) + return ip in ipaddress.ip_network("100.64.0.0/10") + except ValueError: + return False + + +def install(include: list[str] | None = None) -> None: + """Monkey-patch aioice.ice.get_host_addresses with a filtered version.""" + import ifaddr + + include_set = set(include) if include else None + + def filtered_get_host_addresses( + use_ipv4: bool = True, use_ipv6: bool = True, + ) -> list[str]: + addresses: list[str] = [] + for adapter in ifaddr.get_adapters(): + if include_set is not None: + if adapter.name not in include_set: + continue + elif _VIRTUAL_ADAPTER_RE.match(adapter.name): + continue + + for ip in adapter.ips: + if isinstance(ip.ip, str) and use_ipv4 and ip.ip != "127.0.0.1": + if include_set is None and _is_cgnat(ip.ip): + continue + addresses.append(ip.ip) + elif use_ipv6 and ip.ip[0] != "::1" and ip.ip[2] == 0: + addresses.append(ip.ip[0]) + return addresses + + aioice.ice.get_host_addresses = filtered_get_host_addresses + + if include_set: + log.info("ICE interfaces (explicit): %s", ", ".join(sorted(include_set))) + else: + all_adapters = [a.name for a in ifaddr.get_adapters()] + excluded = [ + n for n in all_adapters + if _VIRTUAL_ADAPTER_RE.match(n) + ] + if excluded: + log.info("ICE auto-excluded interfaces: %s", ", ".join(excluded)) |