Public Access
Add the Avalonia app and the xterm renderer, and fix two real bugs
The terminal works end to end. A new integration test drives a real sshd in a container through a real PTY, the real pump, the real loopback WebSocket with its token and origin checks, and a ClientWebSocket standing in for the page: the login banner arrives, typed input round-trips, and `stty size` reports the 100x30 the session asked for. The only untested link left is xterm drawing bytes it was handed. The WebView is de-risked on Windows, which was the plan's largest risk. Not by assertion: with the app running there is an established TCP connection from msedgewebview2 to the data plane port, so WebView2 launched, navigated to the loopback page, executed terminal.js, and completed the WebSocket handshake against the real token and origin checks. Linux remains unproven and the package's own release notes now corroborate the concern -- Linux uses a WPE backend, and it ships a NativeWebDialog described as useful where embedded WebViews may be unavailable. Two bugs found by building it, both of which would have shipped: - ShellStream.Write buffers and needs an explicit Flush. Without one a keystroke is accepted, reported as written, and never reaches the remote: the terminal displays output perfectly and simply stops responding to input. SSH.NET's own WriteLine flushes, which is why the earlier spike never hit it. Found by isolating the pump against real SSH and reading BytesRead=51 -- banner and prompt through, nothing after. - The Windows app manifest needs a supportedOS list, or Avalonia's native control host fails outright and the terminal never starts. Also fixed a genuinely flaky test I happened to catch: SyncCursorTests tampered with the *last* base64url character, whose low bits the decoder ignores when the input length is not a multiple of three -- so a tampered cursor sometimes decoded to identical bytes and verified. It failed roughly one run in thirty, depending on a random key. Now tampers the penultimate character, which is fully significant at every length; 40 consecutive runs are clean. xterm 6.0.0 plus the fit and webgl addons are vendored as UMD bundles rather than built with npm, so a clean clone needs only the .NET SDK. Provenance and licences are recorded next to them, along with the UMD global names terminal.js depends on -- a bundle that switched to ES modules would load without error and leave Terminal undefined. The renderer acknowledges output from term.write's completion callback, not on receipt. Acknowledging early would return flow-control credit for bytes the screen has not caught up with, which is the one thing the credit window exists to measure. TerminalWorkspace moved into DodoSSH.Client.Terminal: it has no Avalonia dependency, and having it there is what let the end-to-end test exist at all. 404 tests pass, zero warnings on a clean rebuild, format clean.
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using DodoSSH.Client.App.Terminal;
|
||||
using DodoSSH.Client.Ssh;
|
||||
using DodoSSH.Client.Terminal;
|
||||
|
||||
namespace DodoSSH.Client.App.ViewModels;
|
||||
|
||||
/// <summary>
|
||||
/// The shell: connect to a host, and surface host key trust decisions.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Hosts are typed in directly for now. Reading them from the encrypted vault needs the local cache
|
||||
/// and the sync client, which are the next pieces; this exists to prove the terminal path end to end
|
||||
/// and is deliberately obvious about being temporary rather than looking like a finished feature.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The two host key states are modelled separately and behave differently, which is the point. An
|
||||
/// unknown host offers a Trust button. A changed key offers nothing — see
|
||||
/// <see cref="SshHostKeyMismatchException"/> for why there is no "continue anyway" here.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal sealed partial class MainWindowViewModel(
|
||||
TerminalWorkspace workspace,
|
||||
IKnownHostStore knownHosts) : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string host = "127.0.0.1";
|
||||
|
||||
[ObservableProperty]
|
||||
private int port = 22;
|
||||
|
||||
[ObservableProperty]
|
||||
private string username = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string password = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string status = "Enter a host and connect.";
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isConnecting;
|
||||
|
||||
/// <summary>The key awaiting the user's decision, or null when there is none.</summary>
|
||||
[ObservableProperty]
|
||||
private HostKeyPresentation? pendingHostKey;
|
||||
|
||||
/// <summary>Set when a pinned key changed, which is a dead end rather than a prompt.</summary>
|
||||
[ObservableProperty]
|
||||
private string? hostKeyMismatch;
|
||||
|
||||
/// <summary>Where the embedded browser should navigate.</summary>
|
||||
public Uri TerminalPageUrl => workspace.PageUrl;
|
||||
|
||||
/// <summary>Whether the trust prompt should be visible.</summary>
|
||||
public bool HasPendingHostKey => PendingHostKey is not null;
|
||||
|
||||
/// <summary>Whether the mismatch banner should be visible.</summary>
|
||||
public bool HasHostKeyMismatch => HostKeyMismatch is not null;
|
||||
|
||||
[RelayCommand]
|
||||
private async Task ConnectAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Username))
|
||||
{
|
||||
Status = "A username is required.";
|
||||
return;
|
||||
}
|
||||
|
||||
IsConnecting = true;
|
||||
PendingHostKey = null;
|
||||
HostKeyMismatch = null;
|
||||
Status = $"Connecting to {Host}:{Port}…";
|
||||
|
||||
try
|
||||
{
|
||||
// The renderer has to be attached first: the transport drops frames when nothing is
|
||||
// connected, so a session opened earlier would lose its SessionOpened frame and then
|
||||
// stream output at a terminal that was never created.
|
||||
await workspace.WaitForRendererAsync().ConfigureAwait(true);
|
||||
|
||||
var request = new SshConnectionRequest(
|
||||
Host,
|
||||
Port,
|
||||
Username,
|
||||
new SshPasswordCredential(Password));
|
||||
|
||||
await workspace
|
||||
.OpenSessionAsync(request, TerminalSize.Default, cancellationToken)
|
||||
.ConfigureAwait(true);
|
||||
|
||||
Status = $"Connected to {Host}:{Port}.";
|
||||
}
|
||||
catch (SshHostKeyUnknownException exception)
|
||||
{
|
||||
// First contact. The user has to decide, and they need the fingerprint to do it.
|
||||
PendingHostKey = exception.Presentation;
|
||||
Status = "This host has not been seen before.";
|
||||
}
|
||||
catch (SshHostKeyMismatchException exception)
|
||||
{
|
||||
HostKeyMismatch = exception.Message;
|
||||
Status = "The host key has changed. The connection was refused.";
|
||||
}
|
||||
catch (Exception exception) when (exception is not OperationCanceledException)
|
||||
{
|
||||
Status = exception.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsConnecting = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Pins the offered key and retries.</summary>
|
||||
[RelayCommand]
|
||||
private async Task TrustHostKeyAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (PendingHostKey is not { } presentation)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await knownHosts.TrustAsync(presentation, cancellationToken).ConfigureAwait(true);
|
||||
|
||||
PendingHostKey = null;
|
||||
|
||||
await ConnectAsync(cancellationToken).ConfigureAwait(true);
|
||||
}
|
||||
|
||||
/// <summary>Dismisses the trust prompt without pinning anything.</summary>
|
||||
[RelayCommand]
|
||||
private void RejectHostKey()
|
||||
{
|
||||
PendingHostKey = null;
|
||||
Status = "The host key was not trusted, so nothing was connected.";
|
||||
}
|
||||
|
||||
partial void OnPendingHostKeyChanged(HostKeyPresentation? value) =>
|
||||
OnPropertyChanged(nameof(HasPendingHostKey));
|
||||
|
||||
partial void OnHostKeyMismatchChanged(string? value) =>
|
||||
OnPropertyChanged(nameof(HasHostKeyMismatch));
|
||||
}
|
||||
Reference in New Issue
Block a user