-
-
Notifications
You must be signed in to change notification settings - Fork 978
Expand file tree
/
Copy pathSshClient.cs
More file actions
346 lines (294 loc) · 14.4 KB
/
SshClient.cs
File metadata and controls
346 lines (294 loc) · 14.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Text;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
/// <inheritdoc cref="ISshClient" />
public class SshClient : BaseClient, ISshClient
{
/// <summary>
/// Holds the list of forwarded ports.
/// </summary>
private readonly List<ForwardedPort> _forwardedPorts;
/// <summary>
/// Holds a value indicating whether the current instance is disposed.
/// </summary>
/// <value>
/// <see langword="true"/> if the current instance is disposed; otherwise, <see langword="false"/>.
/// </value>
private bool _isDisposed;
private MemoryStream? _inputStream;
/// <inheritdoc />
public IEnumerable<ForwardedPort> ForwardedPorts
{
get
{
return _forwardedPorts.AsReadOnly();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="SshClient" /> class.
/// </summary>
/// <param name="connectionInfo">The connection info.</param>
/// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>
public SshClient(ConnectionInfo connectionInfo)
: this(connectionInfo, ownsConnectionInfo: false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SshClient"/> class.
/// </summary>
/// <param name="host">Connection host.</param>
/// <param name="port">Connection port.</param>
/// <param name="username">Authentication username.</param>
/// <param name="password">Authentication password.</param>
/// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
public SshClient(string host, int port, string username, string password)
#pragma warning disable CA2000 // Dispose objects before losing scope
: this(new PasswordConnectionInfo(host, port, username, password), ownsConnectionInfo: true)
#pragma warning restore CA2000 // Dispose objects before losing scope
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SshClient"/> class.
/// </summary>
/// <param name="host">Connection host.</param>
/// <param name="username">Authentication username.</param>
/// <param name="password">Authentication password.</param>
/// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>
public SshClient(string host, string username, string password)
: this(host, ConnectionInfo.DefaultPort, username, password)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SshClient"/> class.
/// </summary>
/// <param name="host">Connection host.</param>
/// <param name="port">Connection port.</param>
/// <param name="username">Authentication username.</param>
/// <param name="keyFiles">Authentication private key file(s) .</param>
/// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.MinPort"/> and <see cref="IPEndPoint.MaxPort"/>.</exception>
[SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
public SshClient(string host, int port, string username, params IPrivateKeySource[] keyFiles)
: this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), ownsConnectionInfo: true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SshClient"/> class.
/// </summary>
/// <param name="host">Connection host.</param>
/// <param name="username">Authentication username.</param>
/// <param name="keyFiles">Authentication private key file(s) .</param>
/// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is <see langword="null"/> or contains only whitespace characters.</exception>
public SshClient(string host, string username, params IPrivateKeySource[] keyFiles)
: this(host, ConnectionInfo.DefaultPort, username, keyFiles)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SshClient"/> class.
/// </summary>
/// <param name="connectionInfo">The connection info.</param>
/// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
/// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>
/// <remarks>
/// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, then the
/// connection info will be disposed when this instance is disposed.
/// </remarks>
private SshClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
: this(connectionInfo, ownsConnectionInfo, new ServiceFactory())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SshClient"/> class.
/// </summary>
/// <param name="connectionInfo">The connection info.</param>
/// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
/// <param name="serviceFactory">The factory to use for creating new services.</param>
/// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <see langword="null"/>.</exception>
/// <remarks>
/// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, then the
/// connection info will be disposed when this instance is disposed.
/// </remarks>
internal SshClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
: base(connectionInfo, ownsConnectionInfo, serviceFactory)
{
_forwardedPorts = new List<ForwardedPort>();
}
/// <summary>
/// Called when client is disconnecting from the server.
/// </summary>
protected override void OnDisconnecting()
{
base.OnDisconnecting();
foreach (var port in _forwardedPorts)
{
port.Stop();
}
}
/// <inheritdoc />
public void AddForwardedPort(ForwardedPort port)
{
ThrowHelper.ThrowIfNull(port);
EnsureSessionIsOpen();
AttachForwardedPort(port);
_forwardedPorts.Add(port);
}
/// <inheritdoc />
public void RemoveForwardedPort(ForwardedPort port)
{
ThrowHelper.ThrowIfNull(port);
// Stop port forwarding before removing it
port.Stop();
DetachForwardedPort(port);
_ = _forwardedPorts.Remove(port);
}
private void AttachForwardedPort(ForwardedPort port)
{
if (port.Session != null && port.Session != Session)
{
throw new InvalidOperationException("Forwarded port is already added to a different client.");
}
port.Session = Session;
}
private static void DetachForwardedPort(ForwardedPort port)
{
port.Session = null;
}
/// <inheritdoc />
public ISshCommand CreateCommand(string commandText)
{
return CreateCommand(commandText, ConnectionInfo.Encoding);
}
/// <inheritdoc />
public ISshCommand CreateCommand(string commandText, Encoding encoding)
{
EnsureSessionIsOpen();
ConnectionInfo.Encoding = encoding;
return new SshCommand(Session!, commandText, encoding);
}
/// <inheritdoc />
public ISshCommand RunCommand(string commandText)
{
var cmd = CreateCommand(commandText);
_ = cmd.Execute();
return cmd;
}
/// <inheritdoc />
public Shell CreateShell(Stream input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint>? terminalModes, int bufferSize)
{
EnsureSessionIsOpen();
return new Shell(Session, input, output, extendedOutput, terminalName, columns, rows, width, height, terminalModes, bufferSize);
}
/// <inheritdoc />
public Shell CreateShell(Stream input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes)
{
return CreateShell(input, output, extendedOutput, terminalName, columns, rows, width, height, terminalModes, 1024);
}
/// <inheritdoc />
public Shell CreateShell(Stream input, Stream output, Stream extendedOutput)
{
return CreateShell(input, output, extendedOutput, string.Empty, 0, 0, 0, 0, terminalModes: null, 1024);
}
/// <inheritdoc />
public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint>? terminalModes, int bufferSize)
{
/*
* TODO Issue #1224: let shell dispose of input stream when we own the stream!
*/
_inputStream = new MemoryStream();
using (var writer = new StreamWriter(_inputStream, encoding, bufferSize: 1024, leaveOpen: true))
{
writer.Write(input);
writer.Flush();
}
_ = _inputStream.Seek(0, SeekOrigin.Begin);
return CreateShell(_inputStream, output, extendedOutput, terminalName, columns, rows, width, height, terminalModes, bufferSize);
}
/// <inheritdoc />
public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes)
{
return CreateShell(encoding, input, output, extendedOutput, terminalName, columns, rows, width, height, terminalModes, 1024);
}
/// <inheritdoc />
public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput)
{
return CreateShell(encoding, input, output, extendedOutput, string.Empty, 0, 0, 0, 0, terminalModes: null, 1024);
}
/// <inheritdoc />
public Shell CreateShellNoTerminal(Stream input, Stream output, Stream extendedOutput, int bufferSize = -1)
{
EnsureSessionIsOpen();
return new Shell(Session, input, output, extendedOutput, bufferSize);
}
/// <inheritdoc />
public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize)
{
return CreateShellStream(terminalName, columns, rows, width, height, bufferSize, terminalModeValues: null);
}
/// <inheritdoc />
public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize, IDictionary<TerminalModes, uint>? terminalModeValues)
{
EnsureSessionIsOpen();
return ServiceFactory.CreateShellStream(Session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize);
}
/// <inheritdoc />
public ShellStream CreateShellStreamNoTerminal(int bufferSize = -1)
{
EnsureSessionIsOpen();
return ServiceFactory.CreateShellStreamNoTerminal(Session, bufferSize);
}
/// <summary>
/// Stops forwarded ports.
/// </summary>
protected override void OnDisconnected()
{
base.OnDisconnected();
for (var i = _forwardedPorts.Count - 1; i >= 0; i--)
{
var port = _forwardedPorts[i];
DetachForwardedPort(port);
_forwardedPorts.RemoveAt(i);
}
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_isDisposed)
{
return;
}
if (disposing)
{
if (_inputStream != null)
{
_inputStream.Dispose();
_inputStream = null;
}
_isDisposed = true;
}
}
private void EnsureSessionIsOpen()
{
if (Session is null)
{
throw new SshConnectionException("Client not connected.");
}
}
}
}