079924d19a
- 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.
180 lines
5.3 KiB
Dart
180 lines
5.3 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../core/servers/machine_access.dart';
|
|
import '../../core/servers/server_catalog.dart';
|
|
import '../../core/servers/server_catalog_store.dart';
|
|
|
|
/// Saved configurations are committed before switching the app's connection.
|
|
/// A failed save leaves the old selection and its live socket unchanged.
|
|
class ServerConnections extends ChangeNotifier {
|
|
ServerConnections({
|
|
required this.store,
|
|
required this.onSelected,
|
|
this.replaceRemovedPrimary = false,
|
|
});
|
|
|
|
final ServerCatalogStore store;
|
|
final Future<void> Function(SavedServer?) onSelected;
|
|
|
|
/// Native hosts need a pipelines/runs machine: removing the primary hands
|
|
/// that role to the next saved machine instead of leaving the app without
|
|
/// an endpoint. Browsers keep following their page origin instead.
|
|
final bool replaceRemovedPrimary;
|
|
|
|
/// Signs saved machines in independently; set by the app root when the
|
|
/// chat pool exists. Settings asks a new machine for its own password.
|
|
MachineAuthenticator? authenticator;
|
|
ServerCatalog _catalog = ServerCatalog();
|
|
bool _busy = false;
|
|
String? _error;
|
|
bool _disposed = false;
|
|
|
|
ServerCatalog get catalog => _catalog;
|
|
String? _newChatServerId;
|
|
String? get newChatServerId => _newChatServerId;
|
|
|
|
/// A browser navigation may carry only this non-secret create intent.
|
|
void restoreNewChatIntent(String id) {
|
|
if (id == _catalog.active?.id) _newChatServerId = id;
|
|
}
|
|
|
|
bool takeNewChatIntent(String? id) {
|
|
if (id == null || id != _newChatServerId || id != _catalog.active?.id) {
|
|
return false;
|
|
}
|
|
_newChatServerId = null;
|
|
return true;
|
|
}
|
|
|
|
/// Keep the intent outside the app that selecting another machine disposes.
|
|
Future<bool> requestNewChat(String id) async {
|
|
if (_busy || _disposed) return false;
|
|
_newChatServerId = id;
|
|
final selected = await _change(
|
|
() => _catalog.select(id),
|
|
activate: true,
|
|
rollbackSelection: true,
|
|
);
|
|
if (!selected) _newChatServerId = null;
|
|
return selected;
|
|
}
|
|
|
|
bool get busy => _busy;
|
|
String? get error => _error;
|
|
|
|
/// Browsers always connect to their page origin, even after Back or a failed
|
|
/// cross-origin navigation. Do not label another saved profile as active.
|
|
void alignBrowserOrigin(Uri endpoint) {
|
|
String origin(Uri uri) =>
|
|
uri.replace(scheme: uri.scheme == 'wss' ? 'https' : 'http').origin;
|
|
final current = origin(endpoint);
|
|
String? matched;
|
|
for (final server in _catalog.servers) {
|
|
if (server.endpoints.any((url) {
|
|
final candidate = Uri.parse(url);
|
|
return origin(candidate) == current &&
|
|
candidate.path == endpoint.path &&
|
|
candidate.query == endpoint.query;
|
|
})) {
|
|
matched = server.id;
|
|
break;
|
|
}
|
|
}
|
|
_catalog = ServerCatalog(servers: _catalog.servers, selectedId: matched);
|
|
_notify();
|
|
}
|
|
|
|
Future<bool> persist() => _change(() => _catalog, activate: false);
|
|
|
|
Future<void> load({String legacyEndpoint = ''}) async {
|
|
try {
|
|
final loaded = await store.load(legacyEndpoint: legacyEndpoint);
|
|
if (!_disposed) _catalog = loaded;
|
|
} catch (error) {
|
|
if (!_disposed) _error = 'Saved servers could not be read: $error';
|
|
}
|
|
_notify();
|
|
}
|
|
|
|
Future<bool> save(SavedServer server, {bool activate = true}) => _change(
|
|
() {
|
|
final next = _catalog.upsert(server);
|
|
return _catalog.active == null ? next.select(server.id) : next;
|
|
},
|
|
activate:
|
|
activate &&
|
|
(_catalog.active?.id == server.id || _catalog.active == null),
|
|
);
|
|
|
|
Future<bool> select(String id, {bool activate = true}) {
|
|
if (_busy || _disposed) return Future.value(false);
|
|
_newChatServerId = null;
|
|
return _change(
|
|
() => _catalog.select(id),
|
|
activate: activate,
|
|
rollbackSelection: true,
|
|
);
|
|
}
|
|
|
|
Future<bool> remove(String id) => _change(() {
|
|
final removedPrimary = _catalog.active?.id == id;
|
|
var next = _catalog.remove(id);
|
|
if (replaceRemovedPrimary && removedPrimary && next.servers.isNotEmpty) {
|
|
next = next.select(next.servers.first.id);
|
|
}
|
|
return next;
|
|
}, activate: _catalog.active?.id == id);
|
|
|
|
Future<bool> _change(
|
|
ServerCatalog Function() change, {
|
|
required bool activate,
|
|
bool rollbackSelection = false,
|
|
}) async {
|
|
if (_busy || _disposed) return false;
|
|
// A corrupt catalog is never silently replaced by an empty configuration.
|
|
if (_error?.startsWith('Saved servers could not be read:') ?? false) {
|
|
return false;
|
|
}
|
|
_busy = true;
|
|
final previous = _catalog;
|
|
_error = null;
|
|
_notify();
|
|
try {
|
|
final next = change();
|
|
if (!await store.save(next)) {
|
|
throw StateError('The server configuration could not be saved.');
|
|
}
|
|
if (_disposed) return false;
|
|
_catalog = next;
|
|
_notify();
|
|
if (activate) {
|
|
try {
|
|
await onSelected(next.active);
|
|
} catch (_) {
|
|
if (rollbackSelection && await store.save(previous) && !_disposed) {
|
|
_catalog = previous;
|
|
}
|
|
rethrow;
|
|
}
|
|
}
|
|
return true;
|
|
} catch (error) {
|
|
if (!_disposed) _error = '$error';
|
|
return false;
|
|
} finally {
|
|
_busy = false;
|
|
_notify();
|
|
}
|
|
}
|
|
|
|
void _notify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
}
|