Files
pipelines/app/lib/features/onboarding/discovery/browser_server_discovery.dart
T
alan 079924d19a Recover from zero servers, sign each machine in on its own, offer every route
- Removing the primary machine hands pipelines/runs to the next saved machine
  on native hosts; removing the last machine returns to the connect screen
  instead of stranding the app on "Server endpoint not configured".
- Add server probes the new machine and asks for its own password in the same
  page; Settings rows show Connected / Sign in required / Unreachable with the
  reason and offer Sign in; no other machine's credential is ever reused.
- Unreachable addresses explain why: a TLS failure against an IP points at the
  Tailscale name, a non-Pipelines answer points at the port.
- Discovered machines list every advertised route (LAN, Tailscale IP, Tailscale
  name) as a checklist; chosen routes are saved as one machine.
- tool/connection_probe.dart replays the native login and websocket against
  real addresses for diagnosis.
2026-11-08 23:59:20 +00:00

85 lines
3.1 KiB
Dart

import 'dart:convert';
import '../../../core/transport/server_url.dart';
import 'discovered_server.dart';
import 'server_discovery.dart';
import 'browser_discovery_fetch_stub.dart'
if (dart.library.js_interop) 'browser_discovery_fetch_web.dart'
as impl;
/// The authenticated web front door browses its LAN on the browser's behalf.
class BrowserServerDiscovery implements ServerDiscovery {
BrowserServerDiscovery({Future<String> Function()? fetch})
: _fetch = fetch ?? impl.fetchDiscovery;
final Future<String> Function() _fetch;
@override
Stream<List<DiscoveredServer>> scan() async* {
final body = await _fetch().timeout(const Duration(seconds: 8));
if (body.length > 262144) {
throw const FormatException('Discovery response is too large.');
}
final envelope = jsonDecode(body);
if (envelope is! Map || envelope['discovered_servers'] is! List) {
throw const FormatException(
'This server did not return network discovery results.',
);
}
final servers = <Uri, DiscoveredServer>{};
for (final item in (envelope['discovered_servers'] as List).take(256)) {
if (item is! Map ||
item['endpoint'] is! String ||
item['name'] is! String) {
continue;
}
try {
final validated = ServerUrl.validate(item['endpoint'] as String);
final uri = Uri.parse(validated.wsUrl);
final name = (item['name'] as String).trim();
if (name.isEmpty ||
name.length > 256 ||
uri.hasQuery ||
uri.hasFragment) {
continue;
}
servers[uri] = DiscoveredServer(
name: name,
host: uri.host,
port: validated.port,
secure: uri.scheme == 'wss',
path: uri.path,
passwordRequired: item['auth'] != 'none',
version: item['version'] is String ? item['version'] as String : null,
addresses: _routes(uri, item['addresses']),
);
} on FormatException {
// One malformed announcement must not hide other machines.
}
}
yield servers.values.toList();
}
/// Every advertised route, the LAN [primary] first; older servers send none.
static List<DiscoveredAddress> _routes(Uri primary, Object? raw) {
final routes = <DiscoveredAddress>[
DiscoveredAddress(route: DiscoveredRoute.lan, endpoint: primary),
];
if (raw is! List) return routes;
for (final item in raw.take(8)) {
if (item is! Map || item['endpoint'] is! String) continue;
final route = DiscoveredAddress.routeNamed(item['kind'] as String?);
if (route == null) continue;
try {
final uri = Uri.parse(
ServerUrl.validate(item['endpoint'] as String).wsUrl,
);
if (uri.hasQuery || uri.hasFragment) continue;
final address = DiscoveredAddress(route: route, endpoint: uri);
if (!routes.contains(address) && uri != primary) routes.add(address);
} on FormatException {
// Skip one malformed route; the machine is still reachable by others.
}
}
return routes;
}
}