forked from HarryCordewener/TelnetNegotiationCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockPipelineClient.cs
More file actions
61 lines (52 loc) · 1.96 KB
/
MockPipelineClient.cs
File metadata and controls
61 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System.Net.Sockets;
using TelnetNegotiationCore.Interpreters;
using System.Text;
using TelnetNegotiationCore.Models;
using Microsoft.Extensions.Logging;
using TelnetNegotiationCore.Builders;
using TelnetNegotiationCore.Protocols;
namespace TelnetNegotiationCore.TestClient;
public class MockPipelineClient(ILogger<MockPipelineClient> logger, ITelnetInterpreterFactory telnetFactory)
{
public static async ValueTask WriteBackAsync(byte[] writeback, Encoding encoding, TelnetInterpreter t) =>
await Task.Run(() => Console.WriteLine(encoding.GetString(writeback.AsSpan())));
public ValueTask SignalGMCPAsync((string module, string writeback) val)
{
logger.LogDebug("GMCP Signal: {Module}: {WriteBack}", val.module, val.writeback);
return ValueTask.CompletedTask;
}
public ValueTask SignalMSSPAsync(MSSPConfig val)
{
logger.LogDebug("New MSSP: {@MSSPConfig}", val);
return ValueTask.CompletedTask;
}
public async ValueTask SignalPromptAsync() =>
await Task.Run(() => logger.LogDebug("Prompt"));
public async Task StartAsync(string address, int port)
{
var client = new TcpClient(address, port);
var (telnet, readTask) = await telnetFactory.CreateBuilder()
.OnSubmit(WriteBackAsync)
.AddPlugin<NAWSProtocol>()
.AddPlugin<GMCPProtocol>()
.OnGMCPMessage(SignalGMCPAsync)
.AddPlugin<MSSPProtocol>()
.OnMSSP(SignalMSSPAsync)
.AddPlugin<TerminalTypeProtocol>()
.AddPlugin<CharsetProtocol>()
.AddPlugin<EORProtocol>()
.OnPrompt(SignalPromptAsync)
.AddPlugin<SuppressGoAheadProtocol>()
.BuildAndStartAsync(client);
// readTask completes when the server closes the connection.
// Log any unexpected errors from the network read loop.
_ = readTask.ContinueWith(
t => logger.LogError(t.Exception, "Network read loop ended with an error"),
TaskContinuationOptions.OnlyOnFaulted);
while (true)
{
var read = Console.ReadLine() ?? string.Empty;
await telnet.SendPromptAsync(telnet.CurrentEncoding.GetBytes(read));
}
}
}