using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Layout; using Avalonia.Media; using Grpc.Core; namespace Strata.Desktop; public sealed class ConnectionWindow : Window { private readonly TextBox host = Field("SSH host або alias"); private readonly TextBox user = Field("Користувач · необов’язково"); private readonly TextBox sshPort = Field("SSH порт", "22"); private readonly TextBox nodePort = Field("Порт запущеного node", "18766"); private readonly TextBox identity = Field("Абсолютний шлях до ключа · необов’язково"); private readonly TextBox token = new() { PlaceholderText = "Ключ доступу node", PasswordChar = '●', MaxLength = 4096 }; private readonly TextBlock status = new() { TextWrapping = TextWrapping.Wrap, Foreground = Brush.Parse("#9DABB9") }; private readonly Button local = new() { Content = "Цей комп’ютер", Padding = new Thickness(18, 12) }; private readonly Button connect = new() { Content = "Підключитися через SSH", Padding = new Thickness(18, 12) }; private readonly CancellationTokenSource lifetime = new(); private bool transferred; public ConnectionWindow(IClassicDesktopStyleApplicationLifetime desktop) { Title = "Strata · Підключення"; Width = 650; Height = 730; MinWidth = 540; MinHeight = 600; Background = Brush.Parse("#10151C"); var content = new StackPanel { Margin = new Thickness(32), Spacing = 14 }; content.Children.Add(new TextBlock { Text = "Де працюватиме Strata?", FontSize = 27, Foreground = Brushes.White }); content.Children.Add(new TextBlock { Text = "Переглядайте обладнання цього комп’ютера або підключіть віддалений вузол.", TextWrapping = TextWrapping.Wrap }); content.Children.Add(local); content.Children.Add(new Separator()); content.Children.Add(new TextBlock { Text = "Віддалений вузол · SSH", FontSize = 19, Foreground = Brush.Parse("#82DFCB") }); content.Children.Add(host); content.Children.Add(user); var ports = new Grid { ColumnDefinitions = new ColumnDefinitions("*,14,*") }; var sshColumn = new StackPanel { Spacing = 5 }; sshColumn.Children.Add(new TextBlock { Text = "SSH порт", FontSize = 12 }); sshColumn.Children.Add(sshPort); var nodeColumn = new StackPanel { Spacing = 5 }; nodeColumn.Children.Add(new TextBlock { Text = "Порт node", FontSize = 12 }); nodeColumn.Children.Add(nodePort); ports.Children.Add(sshColumn); Grid.SetColumn(nodeColumn, 2); ports.Children.Add(nodeColumn); content.Children.Add(ports); content.Children.Add(identity); content.Children.Add(token); content.Children.Add(new TextBlock { Text = "Вузол має бути запущений, SSH-ключ сервера — уже довірений. Ключ доступу node зберігається лише в пам’яті цієї сесії.", TextWrapping = TextWrapping.Wrap, FontSize = 12, Foreground = Brush.Parse("#9DABB9") }); content.Children.Add(connect); content.Children.Add(status); Content = new ScrollViewer { Content = content }; local.Click += async (_, _) => await Open(desktop, false); connect.Click += async (_, _) => await Open(desktop, true); Closed += (_, _) => { lifetime.Cancel(); token.Text = ""; if (!transferred) CoreClient.Close(); }; } private async Task Open(IClassicDesktopStyleApplicationLifetime desktop, bool remote) { local.IsEnabled = connect.IsEnabled = false; status.Text = remote ? "Підключення через SSH та перевірка доступу…" : "Запуск локального вузла…"; try { CoreClient.RemoteConnection? selection = null; if (remote) { if (!int.TryParse(sshPort.Text, out var sp) || !int.TryParse(nodePort.Text, out var np)) throw new InvalidOperationException("Порти мають бути числами від 1 до 65535."); static string? Optional(string? text) => string.IsNullOrWhiteSpace(text) ? null : text.Trim(); selection = new(host.Text?.Trim() ?? "", Optional(user.Text), sp, Optional(identity.Text), np, token.Text ?? ""); } await CoreClient.OpenSelection(selection, lifetime.Token); if (lifetime.IsCancellationRequested) { CoreClient.Close(); return; } token.Text = ""; var main = new MainWindow(); desktop.MainWindow = main; transferred = true; main.Show(); Close(); } catch (OperationCanceledException) { status.Text = "Підключення скасовано або перевищено час очікування."; } catch (RpcException e) { status.Text = e.StatusCode == StatusCode.Unauthenticated ? "Ключ доступу node не підійшов. Виправте його та повторіть." : "Вузол недоступний. Перевірте SSH-доступ, довіру до сервера та порт node."; } catch (InvalidOperationException e) { status.Text = e.Message; } catch (Exception) { status.Text = "Не вдалося підключитися. Перевірте параметри та доступність Rust core / OpenSSH."; } finally { local.IsEnabled = connect.IsEnabled = true; } } private static TextBox Field(string hint, string? value = null) => new() { PlaceholderText = hint, Text = value, MaxLength = 4096 }; }