/// The DNS-SD service type the server advertises (internal/discovery). const pipelinesServiceType = '_pipelines._tcp'; /// A Pipelines server found on the local network. class DiscoveredServer { const DiscoveredServer({ required this.name, required this.host, required this.port, this.secure = false, this.path = '/connect', this.passwordRequired = true, this.version, }); /// 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'] ?? ''; return DiscoveredServer( name: name.isEmpty ? instance : name, host: host, port: port, secure: attributes['tls'] == '1', path: path.startsWith('/') ? path : '/connect', passwordRequired: attributes['auth'] != 'none', version: attributes['v'], ); } final String name; final String host; final int port; final bool secure; final String path; final bool passwordRequired; final String? version; /// The websocket endpoint the connect screen dials. Uri get endpoint => Uri(scheme: secure ? 'wss' : 'ws', host: host, port: port, path: path); /// "host:port" as shown under the name. String get address => endpoint.authority; /// 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; } 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; @override int get hashCode => Object.hash(name, endpoint, passwordRequired, version); }