Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
send in answer to DO COMPRESS2; before it, everything after their connect screens, MSSP report
included, arrived as zlib read as text.
-->
<PackageVersion Include="TelnetNegotiationCore" Version="4.0.0" />
<PackageVersion Include="TelnetNegotiationCore" Version="4.6.0" />

<!--
No logging, hosting or DI *abstractions* here. The crawler is an in-process BackgroundService in
Expand Down
21 changes: 19 additions & 2 deletions src/MUI.Crawl/Telnet/WatchedProtocols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,28 @@ public override ValueTask OnEnabledAsync()
}
}

internal sealed class Mxp(Action<string> note) : MXPProtocol
/// <remarks>
/// The one plugin here that also answers. A server's <c>&lt;SUPPORT&gt;</c> is a question asked of
/// this client, and CoffeeMud holds its connect screen for three seconds waiting for the reply
/// (<c>DefaultSession.HandshakeCallback</c>, <c>HANDSHAKE_MXPPAUSE</c>). Unanswered, the probe
/// settled on the questions themselves, stored them as the game's connect screen, and read the
/// real screen into the WHO phase. The answer is the truth: this client renders nothing, so every
/// entry asked about is refused, and a bare <c>&lt;SUPPORT&gt;</c> gets a reply that claims nothing.
/// It goes out on a secure line, which a server reads as protocol rather than as typing.
/// </remarks>
internal sealed class Mxp : MXPProtocol
{
private readonly Action<string> _note;

public Mxp(Action<string> note)
{
_note = note;
OnMxpSupportRequested(asked => SendSupportsAsync([], asked));
}

public override ValueTask OnEnabledAsync()
{
note(ProtocolName);
_note(ProtocolName);
return base.OnEnabledAsync();
}
}
Expand Down
241 changes: 241 additions & 0 deletions tests/MUI.Crawl.Tests/Telnet/MxpSupportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using MUI.Crawl;

namespace MUI.Crawl.Tests;

/// <summary>
/// A server's MXP <c>&lt;SUPPORT&gt;</c> question is answered, and the screen it was holding back is
/// the one stored.
/// </summary>
/// <remarks>
/// CoffeeMud asks on connect and holds its connect screen up to three seconds for the reply
/// (<c>DefaultSession.HandshakeCallback</c>, <c>HANDSHAKE_MXPPAUSE</c>). Unanswered, the probe settled
/// on the questions, stored <c>&lt;SUPPORT IMAGE IMAGE.URL&gt;</c> as the game's connect screen
/// (<c>24.138.50.103:5555</c>, 2026-11-02), and the real screen arrived in the phases after it.
/// </remarks>
public class MxpSupportTests
{
private static ProbeOptions Fast() => new()
{
QuietPeriod = TimeSpan.FromMilliseconds(120),
SilenceGrace = TimeSpan.FromMilliseconds(300),
MaxPhase = TimeSpan.FromSeconds(3),
BannerPatience = TimeSpan.FromMilliseconds(300),
WhoGrace = TimeSpan.FromMilliseconds(700),
PollInterval = TimeSpan.FromMilliseconds(15),
Timeout = TimeSpan.FromSeconds(20),
MsspSettleGrace = TimeSpan.FromMilliseconds(400),
PromptHold = TimeSpan.FromMilliseconds(120),
};

[Test]
public async Task ASupportQuestionIsAnsweredAndTheScreenBehindItIsTheBanner()
{
await using var game = new HoldingGame();

var result = await new TelnetProbe(Fast()).ProbeAsync(game.Target);

await Assert.That(result.Outcome).IsEqualTo(ProbeOutcome.Answered);
await Assert.That(game.Answer).IsNotNull().Because("the server asked, and was waiting");
await Assert.That(game.Answer!.ToUpperInvariant()).Contains("-IMAGE.URL")
.Because("this client renders nothing, so it refuses what it was asked about");
await Assert.That(game.Answer!).DoesNotContain("+")
.Because("nothing is claimed that the probe does not do");
await Assert.That(result.Banner).Contains("Welcome to the Holding Pattern");
await Assert.That(result.Banner).DoesNotContain("<SUPPORT");
await Assert.That(result.MxpObserved).IsTrue();
}

/// <summary>
/// The same, as CoffeeMud actually does it: the option is agreed, no start marker follows, and the
/// question arrives on a lock-secure line.
/// </summary>
[Test]
public async Task ASupportQuestionWithNoStartMarkerIsStillAnswered()
{
await using var game = new HoldingGame { SendsStartMarker = false };

var result = await new TelnetProbe(Fast()).ProbeAsync(game.Target);

await Assert.That(result.Outcome).IsEqualTo(ProbeOutcome.Answered);
await Assert.That(game.Answer).IsNotNull().Because("the server asked, and was waiting");
await Assert.That(game.Answer!.ToUpperInvariant()).Contains("-IMAGE.URL");
await Assert.That(result.Banner).Contains("Welcome to the Holding Pattern");
await Assert.That(result.Banner).DoesNotContain("<SUPPORT");
await Assert.That(result.MxpObserved).IsTrue();
}

/// <summary>
/// Speaks just enough telnet to agree MXP, start it, and ask; then holds its real screen until it
/// is answered or the hold runs out, as CoffeeMud does.
/// </summary>
private sealed class HoldingGame : IAsyncDisposable
{
private const byte Iac = 255, Sb = 250, Se = 240, Will = 251, Do = 253, Mxp = 91;

private static readonly TimeSpan Hold = TimeSpan.FromSeconds(3);

private readonly TcpListener _listener;
private readonly CancellationTokenSource _stopping = new();
private readonly Task _serving;
private volatile string? _answer;

public HoldingGame()
{
_listener = new TcpListener(IPAddress.Loopback, 0);
_listener.Start();
_serving = ServeAsync();
}

/// <summary>
/// Whether MXP mode is started with <c>IAC SB MXP IAC SE</c>. CoffeeMud never sends it and
/// goes straight to its line-mode sequences.
/// </summary>
public bool SendsStartMarker { get; init; } = true;

public ProbeTarget Target => new(
IPAddress.Loopback.ToString(),
((IPEndPoint)_listener.LocalEndpoint).Port);

/// <summary>The client's <c>&lt;SUPPORTS&gt;</c> line, if it sent one before the hold ran out.</summary>
public string? Answer => _answer;

private async Task ServeAsync()
{
try
{
using var client = await _listener.AcceptTcpClientAsync(_stopping.Token);
await using var stream = client.GetStream();
var heard = new List<byte>();
var listening = ListenAsync(stream, heard);

await stream.WriteAsync(new byte[] { Iac, Will, Mxp }, _stopping.Token);
await SendAsync(stream, "\r\nConnecting to the Holding Pattern...\r\n");

var agreed = Stopwatch.StartNew();
while (!Contains(heard, [Iac, Do, Mxp]) && agreed.Elapsed < Hold)
{
await Task.Delay(20, _stopping.Token);
}

if (SendsStartMarker)
{
await stream.WriteAsync(new byte[] { Iac, Sb, Mxp, Iac, Se }, _stopping.Token);
}

await SendAsync(stream, "\n\u001b[6z\n\u001b[6z<SUPPORT IMAGE IMAGE.URL>\n");
await SendAsync(stream, "\n\u001b[6z\n\u001b[6z<SUPPORT>\n");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

var holding = Stopwatch.StartNew();
while (holding.Elapsed < Hold && _answer is null)
{
_answer = FindAnswer(heard);
await Task.Delay(20, _stopping.Token);
}

await SendAsync(stream, "Welcome to the Holding Pattern\r\nAccount name: ");
await listening;
}
catch (OperationCanceledException)
{
}
catch (IOException)
{
}
catch (SocketException)
{
}
}

/// <summary>Everything the client sends, until it closes or the game is disposed.</summary>
private async Task ListenAsync(NetworkStream stream, List<byte> heard)
{
var buffer = new byte[4096];

try
{
while (true)
{
var read = await stream.ReadAsync(buffer, _stopping.Token);
if (read == 0)
{
return;
}

lock (heard)
{
heard.AddRange(buffer.AsSpan(0, read).ToArray());
}
}
}
catch (OperationCanceledException)
{
}
catch (IOException)
{
}
}

private static string? FindAnswer(List<byte> heard)
{
string text;
lock (heard)
{
text = Encoding.Latin1.GetString(heard.ToArray());
}

var at = text.IndexOf("<SUPPORTS", StringComparison.OrdinalIgnoreCase);
var end = at < 0 ? -1 : text.IndexOf('>', at);
return end > 0 ? text[at..(end + 1)] : null;
}

private static bool Contains(List<byte> heard, byte[] sequence)
{
lock (heard)
{
return IndexOf(heard, sequence) >= 0;
}
}

private static int IndexOf(List<byte> heard, byte[] sequence)
{
for (var i = 0; i + sequence.Length <= heard.Count; i++)
{
var match = true;
for (var j = 0; j < sequence.Length && match; j++)
{
match = heard[i + j] == sequence[j];
}

if (match)
{
return i;
}
}

return -1;
}

private Task SendAsync(NetworkStream stream, string text) =>
stream.WriteAsync(Encoding.Latin1.GetBytes(text), _stopping.Token).AsTask();

public async ValueTask DisposeAsync()
{
await _stopping.CancelAsync();
_listener.Stop();

try
{
await _serving;
}
catch (OperationCanceledException)
{
}

_stopping.Dispose();
}
}
}
Loading