/// The DNS-SD service type the server advertises (internal/discovery). const pipelinesServiceType = '_pipelines._tcp'; /// How one route reaches a discovered machine. enum DiscoveredRoute { /// An address on the network the browsing host shares with the machine. lan, /// The machine's tailnet IP; the same port, encrypted by Tailscale. tailscale, /// An HTTPS origin fronting the same server, usually its tailnet DNS name. public, } /// One way to dial a discovered machine. class DiscoveredAddress { const DiscoveredAddress({required this.route, required this.endpoint}); final DiscoveredRoute route; final Uri endpoint; /// What the choice list calls this route. String get label => switch (route) { DiscoveredRoute.lan => 'Local network', DiscoveredRoute.tailscale => 'Tailscale IP', DiscoveredRoute.public => endpoint.scheme == 'wss' ? 'Tailscale name (HTTPS)' : 'Public name', }; static DiscoveredRoute? routeNamed(String? kind) => switch (kind) { 'lan' => DiscoveredRoute.lan, 'tailscale' => DiscoveredRoute.tailscale, 'public' => DiscoveredRoute.public, _ => null, }; @override bool operator ==(Object other) => other is DiscoveredAddress && other.route == route && other.endpoint == endpoint; @override int get hashCode => Object.hash(route, endpoint); @override String toString() => '$label: $endpoint'; } /// A Pipelines server found on the local network. /// /// [endpoint] is the first route; [addresses] lists every advertised route to /// this same machine so the operator can choose which ones to save. class DiscoveredServer { const DiscoveredServer({ required this.name, required this.host, required this.port, this.secure = false, this.path = '/connect', this.passwordRequired = true, this.version, List? addresses, }) : _routes = addresses; /// Builds a server from a resolved DNS-SD record: the service [instance] /// name, the resolved [addresses] (and SRV [hostname] when no address is /// usable), the SRV [port] and the TXT [attributes]. Returns null when there /// is nothing to dial. static DiscoveredServer? fromRecord({ required String instance, required List addresses, String? hostname, required int port, Map attributes = const {}, }) { final host = preferredHost(addresses) ?? _bare(hostname); if (host == null || port <= 0 || port > 65535) return null; final path = attributes['path'] ?? ''; final name = attributes['name'] ?? ''; final secure = attributes['tls'] == '1'; final cleanPath = path.startsWith('/') ? path : '/connect'; final routes = [ DiscoveredAddress( route: DiscoveredRoute.lan, endpoint: Uri( scheme: secure ? 'wss' : 'ws', host: host, port: port, path: cleanPath, ), ), ]; for (final value in (attributes['ts'] ?? '').split(',')) { final ip = value.trim(); if (!isTailscaleIPv4(ip) || routes.length > 5) continue; routes.add( DiscoveredAddress( route: DiscoveredRoute.tailscale, endpoint: Uri( scheme: secure ? 'wss' : 'ws', host: ip, port: port, path: cleanPath, ), ), ); } final public = publicEndpoint(attributes['public'], path: cleanPath); if (public != null) { routes.add( DiscoveredAddress(route: DiscoveredRoute.public, endpoint: public), ); } return DiscoveredServer( name: name.isEmpty ? instance : name, host: host, port: port, secure: secure, path: cleanPath, passwordRequired: attributes['auth'] != 'none', version: attributes['v'], addresses: routes, ); } final String name; final String host; final int port; final bool secure; final String path; final bool passwordRequired; final String? version; final List? _routes; /// The websocket endpoint the connect screen dials. Uri get endpoint => Uri(scheme: secure ? 'wss' : 'ws', host: host, port: port, path: path); /// Every advertised route to this machine, the primary first. List get addresses => _routes ?? [DiscoveredAddress(route: DiscoveredRoute.lan, endpoint: endpoint)]; /// The routes' endpoints, in order. List get endpoints => [for (final a in addresses) a.endpoint]; /// "host:port" as shown under the name. String get address => endpoint.authority; /// The same machine with only the [chosen] routes, the first one primary. DiscoveredServer choosing(List chosen) { if (chosen.isEmpty) return this; final first = chosen.first.endpoint; return DiscoveredServer( name: name, host: first.host, port: first.hasPort ? first.port : (first.scheme == 'wss' ? 443 : 80), secure: first.scheme == 'wss', path: first.path, passwordRequired: passwordRequired, version: version, addresses: List.unmodifiable(chosen), ); } /// Picks the address a phone can dial: IPv4 first, then a routable IPv6 /// address. Link-local IPv6 needs a zone the URL cannot carry. static String? preferredHost(List addresses) { final values = [for (final value in addresses) value.trim()]; for (final value in values) { final bytes = _ipv4(value); if (bytes != null && bytes[0] != 127) return value; } for (final value in values) { final bytes = _ipv6(value); if (bytes == null) continue; final loopback = bytes.sublist(0, 15).every((byte) => byte == 0) && bytes[15] == 1; final linkLocal = bytes[0] == 0xfe && (bytes[1] & 0xc0) == 0x80; if (!loopback && !linkLocal) return value; } return null; } /// Tailscale assigns IPv4 addresses from 100.64.0.0/10. static bool isTailscaleIPv4(String value) { final bytes = _ipv4(value); return bytes != null && bytes[0] == 100 && (bytes[1] & 0xc0) == 0x40; } /// The ws(s) endpoint for an advertised HTTP(S) origin, or null when the /// value is not a bare origin. static Uri? publicEndpoint(String? raw, {String path = '/connect'}) { final value = raw?.trim() ?? ''; if (value.isEmpty || value.length > 200) return null; final uri = Uri.tryParse(value); if (uri == null || (uri.scheme != 'https' && uri.scheme != 'http') || uri.host.isEmpty || uri.userInfo.isNotEmpty || (uri.path.isNotEmpty && uri.path != '/') || uri.hasQuery || uri.hasFragment) { return null; } return Uri( scheme: uri.scheme == 'https' ? 'wss' : 'ws', host: uri.host, port: uri.hasPort ? uri.port : null, path: path, ); } static List? _ipv4(String value) { if (!RegExp(r'^\d{1,3}(\.\d{1,3}){3}$').hasMatch(value)) return null; try { return Uri.parseIPv4Address(value); } on FormatException { return null; } } static List? _ipv6(String value) { if (!value.contains(':') || value.contains('%')) return null; try { return Uri.parseIPv6Address(value); } on FormatException { return null; } } static String? _bare(String? hostname) { final value = hostname?.trim().replaceFirst(RegExp(r'\.$'), '') ?? ''; return value.isEmpty ? null : value; } @override bool operator ==(Object other) => other is DiscoveredServer && other.name == name && other.endpoint == endpoint && other.passwordRequired == passwordRequired && other.version == version && _sameRoutes(other.addresses); bool _sameRoutes(List other) { if (other.length != addresses.length) return false; for (var i = 0; i < other.length; i++) { if (other[i] != addresses[i]) return false; } return true; } @override int get hashCode => Object.hash(name, endpoint, passwordRequired, version, addresses.length); }