Repository navigation
Expand file tree
/
Copy pathTelnetSession.cs
More file actions
280 lines (267 loc) · 10.4 KB
/
Copy pathTelnetSession.cs
File metadata and controls
280 lines (267 loc) · 10.4 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System.Net.Sockets;
using System.Threading.Channels;
using VelaShell.PluginSdk.Logging;
using VelaShell.PluginSdk.Network;
using VelaShell.PluginSdk.Protocols;
namespace VelaShell.Plugin.Telnet;
/// <summary>
/// 一条 Telnet 会话:TCP 之上的 <see cref="IProtocolTerminalSession" />。
/// <para>
/// 结构是一条**单出口管道**:后台泵读套接字 → 交给 <see cref="TelnetNegotiator" /> 消化协议字节 →
/// 净数据写进通道;<see cref="ReadAsync" /> 只从通道取。本地回显也写同一个通道 ——
/// 这样"没有远端回显时用户仍看得见自己敲的字"不必等下一次网络数据到达
/// (若把回显攒在旁路缓冲里,阻塞在套接字读上的读取方根本不会醒)。
/// </para>
/// <para>
/// 写侧串行化(<see cref="_writeGate" />):用户按键、NAWS 子协商、协商应答都是写,
/// 交织会把一个帧撕成两半 —— 对端看到的就是半个 IAC 序列。
/// </para>
/// </summary>
internal sealed class TelnetSession : IProtocolTerminalSession
{
private readonly Stream _stream;
private readonly TelnetNegotiator _negotiator;
private readonly TelnetConfig _config;
private readonly IPluginLogger _log;
private readonly CancellationTokenSource _lifetime = new();
private readonly SemaphoreSlim _writeGate = new(1, 1);
private readonly Channel<byte[]> _output = Channel.CreateUnbounded<byte[]>(
new UnboundedChannelOptions { SingleReader = true, SingleWriter = false });
private ReadOnlyMemory<byte> _carry;
private Task? _pump;
private volatile bool _disposed;
private TelnetSession(Stream stream, TelnetConfig config, IPluginLogger log)
{
_stream = stream;
_config = config;
_log = log;
_negotiator = new(config);
}
/// <summary>建立连接并发出第一批协商;端点不可达时抛 <see cref="ProtocolConnectionException" />。</summary>
/// <param name="host">主机名或 IP。</param>
/// <param name="port">端口。</param>
/// <param name="config">协商策略。</param>
/// <param name="options">终端初始参数。</param>
/// <param name="log">插件日志。</param>
/// <param name="network">宿主的出站网络能力(按宿主代理设置拨号)。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>已就绪的会话。</returns>
public static async Task<TelnetSession> ConnectAsync(
string host,
int port,
TelnetConfig config,
ProtocolTerminalOptions options,
IPluginLogger log,
INetworkApi network,
CancellationToken cancellationToken)
{
// 由宿主按「设置 → 网络代理」拨号(直连或经 HTTP / SOCKS5 代理),插件不自己开 socket。
// Nagle 会把单个按键攒到 40ms 后再发,交互式终端上是肉眼可见的迟滞:NoDelay 默认开着。
HostTcpConnection connection;
try
{
connection = await network.ConnectTcpAsync(host, port, new TcpConnectOptions { NoDelay = true }, cancellationToken)
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw;
}
catch (NetworkProxyException ex)
{
// 宿主给的代理报错已是界面语言,并点名了代理与目标。
throw new ProtocolConnectionException(ex.Message, ex);
}
catch (Exception ex)
{
throw new ProtocolConnectionException($"{host}:{port} — {ex.Message}", ex);
}
Stream stream = connection.Stream;
var session = new TelnetSession(stream, config, log);
byte[] hello = session._negotiator.BuildInitialRequests(options.Columns, options.Rows);
try
{
await stream.WriteAsync(hello, cancellationToken).ConfigureAwait(false);
await stream.FlushAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
await session.DisposeAsync().ConfigureAwait(false);
throw new ProtocolConnectionException($"{host}:{port} — {ex.Message}", ex);
}
session._pump = Task.Run(() => session.PumpAsync(), CancellationToken.None);
return session;
}
/// <inheritdoc />
public async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
if (buffer.IsEmpty)
{
return 0;
}
if (_carry.IsEmpty)
{
try
{
byte[] chunk = await _output.Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
_carry = chunk;
}
catch (ChannelClosedException)
{
return 0; // 泵已收尾 = 会话结束。
}
}
int copied = Math.Min(buffer.Length, _carry.Length);
_carry[..copied].CopyTo(buffer);
_carry = _carry[copied..];
return copied;
}
/// <inheritdoc />
public async ValueTask WriteAsync(ReadOnlyMemory<byte> data, CancellationToken cancellationToken = default)
{
if (_disposed || data.IsEmpty)
{
return;
}
byte[] encoded = _negotiator.EncodeOutbound(data.Span);
await SendAsync(encoded, cancellationToken).ConfigureAwait(false);
// 本地回显:对端没接管回显时,用户敲的字得由我们显示出来,否则就是"打字看不见"。
// 回显的是**用户原始字节**,不是编码后的线上字节(那里可能刚补过 LF、双写过 0xFF)。
if (ShouldEchoLocally())
{
_ = _output.Writer.TryWrite(data.ToArray());
}
}
/// <inheritdoc />
public async ValueTask ResizeAsync(int columns, int rows, CancellationToken cancellationToken = default)
{
if (_disposed)
{
return;
}
if (_negotiator.BuildWindowSize(columns, rows) is { Length: > 0 } frame)
{
await SendAsync(frame, cancellationToken).ConfigureAwait(false);
}
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
if (_disposed)
{
return;
}
_disposed = true;
await _lifetime.CancelAsync().ConfigureAwait(false);
try
{
_stream.Dispose(); // 唤醒阻塞在读上的泵。
}
catch (Exception ex)
{
_log.Debug($"Closing telnet socket threw: {ex.Message}");
}
if (_pump is { } pump)
{
// 泵只做套接字读与通道写,取消后必然很快返回;仍加超时兜底,
// 绝不让一次关标签卡在这里(关闭路径由宿主在后台调,但也不该无限等)。
// 用 WaitAsync 而不是 WhenAny(Task.Delay):后者即便泵先返回,那个定时器
// 也会一直跑到点(CA2027);终端会话可能开开关关很多次,定时器就攒起来了。
try
{
await pump.WaitAsync(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
}
catch (TimeoutException)
{
// 泵卡在某处收不回来:关闭路径不为它停留,后面的句柄照常释放。
_log.Debug("Telnet read pump did not finish within the shutdown timeout.");
}
}
_ = _output.Writer.TryComplete();
_stream.Dispose();
_writeGate.Dispose();
_lifetime.Dispose();
}
/// <summary>本地回显策略:auto 时只在对端未启用 ECHO 的情况下回显。</summary>
private bool ShouldEchoLocally() => _config.LocalEcho switch
{
TelnetLocalEcho.On => true,
TelnetLocalEcho.Off => false,
_ => !_negotiator.RemoteEcho
};
private async Task SendAsync(byte[] payload, CancellationToken cancellationToken)
{
if (payload.Length == 0)
{
return;
}
using var linked = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _lifetime.Token);
await _writeGate.WaitAsync(linked.Token).ConfigureAwait(false);
try
{
await _stream.WriteAsync(payload, linked.Token).ConfigureAwait(false);
await _stream.FlushAsync(linked.Token).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or ObjectDisposedException or SocketException)
{
// 连接已断:读侧的 EOF 才是权威结论,这里只丢弃这一次写。
_log.Debug($"Telnet write failed: {ex.Message}");
}
finally
{
// Dispose 与写并发时信号量可能已释放,吞掉这一次。
try
{
_ = _writeGate.Release();
}
catch (ObjectDisposedException)
{
}
}
}
/// <summary>
/// 读泵:套接字 → 协商状态机 → 净数据进通道。任何异常都归一化成"结束"
/// (完成通道即 EOF),掉线不该以异常形式冒到宿主的读循环里。
/// </summary>
private async Task PumpAsync()
{
byte[] buffer = new byte[16384];
List<byte> data = [];
List<byte> responses = [];
try
{
while (!_lifetime.IsCancellationRequested)
{
int read = await _stream.ReadAsync(buffer, _lifetime.Token).ConfigureAwait(false);
if (read <= 0)
{
break; // 对端关闭。
}
data.Clear();
responses.Clear();
_negotiator.Process(buffer.AsSpan(0, read), data, responses);
if (responses.Count > 0)
{
await SendAsync([.. responses], _lifetime.Token).ConfigureAwait(false);
}
if (data.Count > 0)
{
_ = _output.Writer.TryWrite([.. data]);
}
}
}
catch (Exception ex) when (ex is OperationCanceledException or IOException
or ObjectDisposedException or SocketException)
{
// 正常收尾路径:关标签、对端 RST、进程退出。
}
catch (Exception ex)
{
_log.Warn($"Telnet read pump stopped: {ex.Message}");
}
finally
{
_ = _output.Writer.TryComplete();
}
}
}