7ecc12b747
Server: internal/discovery publishes a DNS-SD record (hashicorp/mdns) with the port LAN clients dial (the web proxy via -mdns-port, which the supervisor passes, or a non-loopback -addr) and TXT v, name, path, tls, auth. On by default; -mdns=false or PIPELINES_MDNS=off disables it. Failures are logged and never block startup; the advert is withdrawn on shutdown. App: the Connect to Pipelines screen gets 'Find servers on this network' behind a ServerDiscovery interface (Bonsoir: Android NSD with multicast lock, iOS Bonjour with NSLocalNetworkUsageDescription and NSBonjourServices). Tapping a server fills the address and connects; manual entry stays. Web has no scan.
86 lines
2.8 KiB
Dart
86 lines
2.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:bonsoir/bonsoir.dart';
|
|
|
|
import 'discovered_server.dart';
|
|
import 'server_discovery.dart';
|
|
|
|
/// DNS-SD browsing through the platform service: Android NSD (Bonsoir holds
|
|
/// the Wi-Fi multicast lock while browsing) and Apple's Network framework
|
|
/// (needs NSLocalNetworkUsageDescription and NSBonjourServices).
|
|
class BonsoirServerDiscovery implements ServerDiscovery {
|
|
BonsoirServerDiscovery({this.type = pipelinesServiceType});
|
|
|
|
final String type;
|
|
|
|
@override
|
|
Stream<List<DiscoveredServer>> scan() {
|
|
late final StreamController<List<DiscoveredServer>> controller;
|
|
BonsoirDiscovery? discovery;
|
|
StreamSubscription<BonsoirDiscoveryEvent>? events;
|
|
final found = <String, DiscoveredServer>{};
|
|
|
|
void publish() {
|
|
if (!controller.isClosed) controller.add(found.values.toList());
|
|
}
|
|
|
|
Future<void> start() async {
|
|
try {
|
|
final browser = BonsoirDiscovery(type: type);
|
|
discovery = browser;
|
|
await browser.initialize();
|
|
if (controller.isClosed) return;
|
|
events = browser.eventStream?.listen(
|
|
(event) {
|
|
switch (event) {
|
|
case BonsoirDiscoveryServiceFoundEvent(:final service):
|
|
unawaited(service.resolve(browser.serviceResolver));
|
|
case BonsoirDiscoveryServiceResolvedEvent(:final service) ||
|
|
BonsoirDiscoveryServiceUpdatedEvent(:final service):
|
|
final server = DiscoveredServer.fromRecord(
|
|
instance: service.name,
|
|
addresses: service.hostAddresses,
|
|
hostname: service.hostname,
|
|
port: service.port,
|
|
attributes: service.attributes,
|
|
);
|
|
if (server != null) {
|
|
found[service.name] = server;
|
|
publish();
|
|
}
|
|
case BonsoirDiscoveryServiceLostEvent(:final service):
|
|
if (found.remove(service.name) != null) publish();
|
|
default:
|
|
break;
|
|
}
|
|
},
|
|
onError: (Object error) {
|
|
if (!controller.isClosed) controller.addError(error);
|
|
},
|
|
);
|
|
await browser.start();
|
|
publish();
|
|
} catch (error, stack) {
|
|
if (!controller.isClosed) controller.addError(error, stack);
|
|
}
|
|
}
|
|
|
|
controller = StreamController<List<DiscoveredServer>>(
|
|
onListen: () => unawaited(start()),
|
|
onCancel: () async {
|
|
await events?.cancel();
|
|
final browser = discovery;
|
|
if (browser != null && !browser.isStopped) {
|
|
try {
|
|
await browser.stop();
|
|
} catch (_) {
|
|
// Already stopped by the platform; nothing else to release.
|
|
}
|
|
}
|
|
await controller.close();
|
|
},
|
|
);
|
|
return controller.stream;
|
|
}
|
|
}
|