import 'login_probe.dart'; /// What a server says about this device's session. enum SessionState { signedIn, signedOut, unreachable } /// What a probe of one server address found; [reason] explains an /// unreachable address in words the operator can act on. class ServerProbeResult { const ServerProbeResult(this.state, {this.reason}); final SessionState state; final String? reason; } /// The web login page for a websocket endpoint: same host and port, http(s). Uri? loginUriFor(Uri endpoint) { final scheme = switch (endpoint.scheme) { 'wss' || 'https' => 'https', 'ws' || 'http' => 'http', _ => null, }; if (scheme == null || endpoint.host.isEmpty || endpoint.userInfo.isNotEmpty) { return null; } return Uri( scheme: scheme, host: endpoint.host, port: endpoint.hasPort ? endpoint.port : null, path: '/login', ); } /// The web login sends a signed-in visitor on and shows its form otherwise; /// anything else on that port is not a Pipelines server. ServerProbeResult interpretLoginProbe(Uri login, LoginProbe probe) { final where = '${login.host}:${login.port}'; final status = probe.status; if (status == null) { return ServerProbeResult( SessionState.unreachable, reason: probe.failure ?? 'Could not reach $where.', ); } if (status >= 300 && status < 400) { return const ServerProbeResult(SessionState.signedIn); } if (status == 200 && probe.loginPage) { return const ServerProbeResult(SessionState.signedOut); } return ServerProbeResult( SessionState.unreachable, reason: status == 200 ? '$where answered, but not with the Pipelines sign-in page. ' 'Check the port: use the one in the web app\'s address bar.' : '$where answered HTTP $status instead of the Pipelines sign-in ' 'page. Check the address and port.', ); }