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.
388 lines
12 KiB
Dart
388 lines
12 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../core/session/server_session.dart';
|
|
import '../../core/transport/server_url.dart';
|
|
import '../../core/servers/server_probe.dart';
|
|
import '../../ui/adaptive/host.dart';
|
|
import 'discovery/discovered_server.dart';
|
|
import 'discovery/discovery_panel.dart';
|
|
import 'discovery/server_discovery.dart';
|
|
|
|
/// Reaches a server and reports what it says about this device's session.
|
|
typedef ServerCheck = Future<SessionState> Function(Uri endpoint);
|
|
|
|
/// Signs in with the web password; null on success, else the reason.
|
|
typedef ServerSignIn = Future<String?> Function(Uri endpoint, String password);
|
|
|
|
/// The phone's first run: which server, then its password. Both are kept, so
|
|
/// later launches open the app directly.
|
|
class OnboardingFlow extends StatefulWidget {
|
|
const OnboardingFlow({
|
|
super.key,
|
|
required this.onDone,
|
|
this.initialServer = '',
|
|
this.startAtPassword = false,
|
|
this.check,
|
|
this.probe,
|
|
this.signIn = ServerSession.signIn,
|
|
this.saveServer,
|
|
this.saveServerProfile,
|
|
this.saveMachine,
|
|
this.savedServers,
|
|
this.serverAddresses = const [],
|
|
this.discovery,
|
|
});
|
|
|
|
/// Called with the signed-in server's endpoint.
|
|
final ValueChanged<Uri> onDone;
|
|
|
|
/// Pre-filled server address (the saved one, or the build's default).
|
|
final String initialServer;
|
|
|
|
/// The saved session was refused: only the password is asked again.
|
|
final bool startAtPassword;
|
|
|
|
/// Reaches an address; [probe] also says why one could not be used. With
|
|
/// neither, the device's own session probe is used.
|
|
final ServerCheck? check;
|
|
final ServerProbe? probe;
|
|
final ServerSignIn signIn;
|
|
final Future<void> Function(String url)? saveServer;
|
|
final Future<void> Function(String url, String name)? saveServerProfile;
|
|
|
|
/// Saves every chosen route to one machine, the reached one first. Takes
|
|
/// precedence over [saveServerProfile] when set.
|
|
final Future<void> Function(List<String> endpoints, String name)? saveMachine;
|
|
final Widget? savedServers;
|
|
final List<Uri> serverAddresses;
|
|
|
|
/// Network discovery; null hides the scan when no service was supplied.
|
|
final ServerDiscovery? discovery;
|
|
|
|
@override
|
|
State<OnboardingFlow> createState() => _OnboardingFlowState();
|
|
}
|
|
|
|
class _OnboardingFlowState extends State<OnboardingFlow> {
|
|
late final TextEditingController _server = TextEditingController(
|
|
text: widget.initialServer,
|
|
);
|
|
final TextEditingController _password = TextEditingController();
|
|
final TextEditingController _name = TextEditingController();
|
|
late bool _passwordStep = widget.startAtPassword;
|
|
Uri? _endpoint;
|
|
bool _busy = false;
|
|
String? _error;
|
|
|
|
/// The routes a discovered machine was chosen with; empty when typed.
|
|
List<Uri> _discoveredRoutes = const [];
|
|
|
|
ServerProbe get _probe =>
|
|
widget.probe ??
|
|
(widget.check == null
|
|
? ServerSession.probe
|
|
: probeFromCheck(widget.check!));
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (widget.startAtPassword) _endpoint = _parse(widget.initialServer);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_server.dispose();
|
|
_password.dispose();
|
|
_name.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Uri? _parse(String text) {
|
|
try {
|
|
return Uri.parse(ServerUrl.validate(text).wsUrl);
|
|
} on FormatException {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<void> _continue() async {
|
|
String? problem;
|
|
Uri? endpoint;
|
|
try {
|
|
endpoint = Uri.parse(ServerUrl.validate(_server.text).wsUrl);
|
|
} on FormatException catch (error) {
|
|
problem = error.message;
|
|
}
|
|
if (endpoint == null) {
|
|
setState(() => _error = problem);
|
|
return;
|
|
}
|
|
setState(() {
|
|
_busy = true;
|
|
_error = null;
|
|
});
|
|
// Typing another address drops the discovered machine's other routes.
|
|
final routes = _discoveredRoutes.contains(endpoint)
|
|
? _discoveredRoutes
|
|
: const <Uri>[];
|
|
final candidates = [
|
|
endpoint,
|
|
...routes.where((candidate) => candidate != endpoint),
|
|
if (widget.serverAddresses.contains(endpoint))
|
|
...widget.serverAddresses.where((candidate) => candidate != endpoint),
|
|
];
|
|
final result = await probeServerAddresses(
|
|
candidates,
|
|
probe: _probe,
|
|
isCurrent: () => mounted,
|
|
);
|
|
endpoint = result.endpoint;
|
|
final state = result.state;
|
|
if (!mounted) return;
|
|
if (state == SessionState.unreachable) {
|
|
setState(() {
|
|
_busy = false;
|
|
_error =
|
|
result.reason ??
|
|
'Could not reach that server. Check the address and network.';
|
|
});
|
|
return;
|
|
}
|
|
final name = _name.text.trim().isEmpty ? endpoint.host : _name.text.trim();
|
|
try {
|
|
if (widget.saveMachine != null) {
|
|
await widget.saveMachine!([
|
|
endpoint.toString(),
|
|
for (final route in routes)
|
|
if (route != endpoint) route.toString(),
|
|
], name);
|
|
} else if (widget.saveServerProfile != null) {
|
|
await widget.saveServerProfile!(endpoint.toString(), name);
|
|
} else {
|
|
await widget.saveServer?.call(endpoint.toString());
|
|
}
|
|
} catch (error) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_busy = false;
|
|
_error = '$error';
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
if (!mounted) return;
|
|
if (state == SessionState.signedIn) {
|
|
widget.onDone(endpoint);
|
|
return;
|
|
}
|
|
setState(() {
|
|
_busy = false;
|
|
_endpoint = endpoint;
|
|
_passwordStep = true;
|
|
});
|
|
}
|
|
|
|
void _useDiscovered(DiscoveredServer server) {
|
|
setState(() {
|
|
_server.text = server.endpoint.toString();
|
|
_discoveredRoutes = server.endpoints;
|
|
_name.text = server.name;
|
|
_error = null;
|
|
});
|
|
}
|
|
|
|
Future<void> _signIn() async {
|
|
final endpoint = _endpoint;
|
|
if (endpoint == null || _password.text.isEmpty) return;
|
|
setState(() {
|
|
_busy = true;
|
|
_error = null;
|
|
});
|
|
final error = await widget.signIn(endpoint, _password.text);
|
|
if (!mounted) return;
|
|
if (error != null) {
|
|
setState(() {
|
|
_busy = false;
|
|
_error = error;
|
|
});
|
|
return;
|
|
}
|
|
widget.onDone(endpoint);
|
|
}
|
|
|
|
void _back() => setState(() {
|
|
_passwordStep = false;
|
|
_error = null;
|
|
_password.clear();
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final cupertino = usesCupertinoHost(context);
|
|
final title = _passwordStep ? 'Sign in' : 'Connect to Pipelines';
|
|
final lead = _passwordStep
|
|
? 'Enter the password chosen when Pipelines was installed on '
|
|
'${_endpoint?.host ?? 'the server'}.'
|
|
: 'Enter the address of your Pipelines server, for example '
|
|
'wss://server.example/connect.';
|
|
final field = _passwordStep
|
|
? _field(
|
|
cupertino,
|
|
key: const Key('onboarding-password'),
|
|
controller: _password,
|
|
hint: 'Password',
|
|
obscure: true,
|
|
onSubmitted: _signIn,
|
|
)
|
|
: _field(
|
|
cupertino,
|
|
key: const Key('onboarding-server'),
|
|
controller: _server,
|
|
hint: 'wss://server/connect',
|
|
keyboard: TextInputType.url,
|
|
onSubmitted: _continue,
|
|
);
|
|
final action = _passwordStep ? _signIn : _continue;
|
|
final label = _passwordStep ? 'Sign in' : 'Continue';
|
|
final button = cupertino
|
|
? CupertinoButton.filled(
|
|
key: const Key('onboarding-next'),
|
|
onPressed: _busy ? null : action,
|
|
child: _busy ? const CupertinoActivityIndicator() : Text(label),
|
|
)
|
|
: FilledButton(
|
|
key: const Key('onboarding-next'),
|
|
onPressed: _busy ? null : action,
|
|
child: _busy
|
|
? const SizedBox(
|
|
width: 18,
|
|
height: 18,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: Text(label),
|
|
);
|
|
final errorColor = cupertino
|
|
? CupertinoColors.systemRed.resolveFrom(context)
|
|
: Theme.of(context).colorScheme.error;
|
|
final body = SafeArea(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 420),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
title,
|
|
style: cupertino
|
|
? CupertinoTheme.of(
|
|
context,
|
|
).textTheme.navLargeTitleTextStyle
|
|
: Theme.of(context).textTheme.headlineSmall,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(lead),
|
|
const SizedBox(height: 24),
|
|
field,
|
|
if (!_passwordStep) ...[
|
|
const SizedBox(height: 12),
|
|
_field(
|
|
cupertino,
|
|
key: const Key('onboarding-server-name'),
|
|
controller: _name,
|
|
hint: 'Machine name',
|
|
onSubmitted: _continue,
|
|
),
|
|
if (_discoveredRoutes.length > 1) ...[
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'Also saved as backup routes to this machine: '
|
|
'${_discoveredRoutes.where((r) => r.toString() != _server.text).join(', ')}',
|
|
key: const Key('onboarding-routes'),
|
|
),
|
|
],
|
|
],
|
|
if (_error != null) ...[
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
_error!,
|
|
key: const Key('onboarding-error'),
|
|
style: TextStyle(color: errorColor),
|
|
),
|
|
],
|
|
const SizedBox(height: 16),
|
|
button,
|
|
if (!_passwordStep && widget.discovery != null) ...[
|
|
const SizedBox(height: 16),
|
|
DiscoveryPanel(
|
|
discovery: widget.discovery!,
|
|
enabled: !_busy,
|
|
onSelect: _useDiscovered,
|
|
),
|
|
],
|
|
if (_passwordStep && !widget.startAtPassword) ...[
|
|
const SizedBox(height: 8),
|
|
cupertino
|
|
? CupertinoButton(
|
|
onPressed: _busy ? null : _back,
|
|
child: const Text('Change server'),
|
|
)
|
|
: TextButton(
|
|
onPressed: _busy ? null : _back,
|
|
child: const Text('Change server'),
|
|
),
|
|
],
|
|
if (widget.savedServers != null) ...[
|
|
const SizedBox(height: 24),
|
|
widget.savedServers!,
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
return cupertino
|
|
? CupertinoPageScaffold(child: body)
|
|
: Scaffold(body: body);
|
|
}
|
|
|
|
Widget _field(
|
|
bool cupertino, {
|
|
required Key key,
|
|
required TextEditingController controller,
|
|
required String hint,
|
|
required VoidCallback onSubmitted,
|
|
bool obscure = false,
|
|
TextInputType? keyboard,
|
|
}) {
|
|
if (cupertino) {
|
|
return CupertinoTextField(
|
|
key: key,
|
|
controller: controller,
|
|
placeholder: hint,
|
|
obscureText: obscure,
|
|
autocorrect: false,
|
|
keyboardType: keyboard,
|
|
autofocus: true,
|
|
onSubmitted: (_) => onSubmitted(),
|
|
);
|
|
}
|
|
return TextField(
|
|
key: key,
|
|
controller: controller,
|
|
obscureText: obscure,
|
|
autocorrect: false,
|
|
keyboardType: keyboard,
|
|
autofocus: true,
|
|
decoration: InputDecoration(
|
|
labelText: hint,
|
|
border: const OutlineInputBorder(),
|
|
),
|
|
onSubmitted: (_) => onSubmitted(),
|
|
);
|
|
}
|
|
}
|