Skip to content

Commit 4cf7c9a

Browse files
committed
Merge branch 'padding' of https://github.com/scott-xu/SSH.NET into des
2 parents 5e3482d + 2c31a72 commit 4cf7c9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+994
-202
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,9 @@ dotnet_code_quality.CA1828.api_surface = all
704704
# Similar to MA0053, but does not support public types and types that define (new) virtual members.
705705
dotnet_diagnostic.CA1852.severity = none
706706

707+
# CA1848: don't enforce LoggerMessage pattern
708+
dotnet_diagnostic.CA1848.severity = suggestion
709+
707710
# CA1859: Change return type for improved performance
708711
#
709712
# By default, this diagnostic is only reported for private members.

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ The repository makes use of continuous integration (CI) with GitHub Actions to v
3434

3535
## Good to know
3636

37-
### TraceSource logging
37+
### Logging
3838

39-
The Debug build of SSH.NET contains rudimentary logging functionality via `System.Diagnostics.TraceSource`. See `Renci.SshNet.Abstractions.DiagnosticAbstraction` for usage examples.
39+
The tests always log to the console. See the [Logging documentation](https://sshnet.github.io/SSH.NET/logging.html) on how to set a custom `ILoggerFactory`.
4040

4141
### Wireshark
4242

Directory.Packages.props

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
8-
<PackageVersion Include="BouncyCastle.Cryptography" Version="2.4.0" />
8+
<PackageVersion Include="BouncyCastle.Cryptography" Version="2.5.0" />
99
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
1010
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
1111
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1">
@@ -15,6 +15,9 @@
1515
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.163" />
1616
<!-- Must be kept at version 1.0.0 or higher, see https://github.com/sshnet/SSH.NET/pull/1288 for details. -->
1717
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" />
18+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
19+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
20+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
1821
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
1922
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.2" />
2023
<PackageVersion Include="MSTest.TestFramework" Version="3.6.2" />

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The main types provided by this library are:
6262
## Additional Documentation
6363

6464
* [Further examples](https://sshnet.github.io/SSH.NET/examples.html)
65+
* [Logging](https://sshnet.github.io/SSH.NET/logging.html)
6566
* [API browser](https://sshnet.github.io/SSH.NET/api/Renci.SshNet.html)
6667

6768
## Encryption Methods
@@ -101,17 +102,21 @@ The main types provided by this library are:
101102
* OpenSSL PKCS#8 PEM format ("BEGIN PRIVATE KEY", "BEGIN ENCRYPTED PRIVATE KEY")
102103
* ssh.com format ("BEGIN SSH2 ENCRYPTED PRIVATE KEY")
103104
* OpenSSH key format ("BEGIN OPENSSH PRIVATE KEY")
105+
* PuTTY private key format ("PuTTY-User-Key-File-2", "PuTTY-User-Key-File-3")
104106
* DSA in
105107
* OpenSSL traditional PEM format ("BEGIN DSA PRIVATE KEY")
106108
* OpenSSL PKCS#8 PEM format ("BEGIN PRIVATE KEY", "BEGIN ENCRYPTED PRIVATE KEY")
107109
* ssh.com format ("BEGIN SSH2 ENCRYPTED PRIVATE KEY")
110+
* PuTTY private key format ("PuTTY-User-Key-File-2", "PuTTY-User-Key-File-3")
108111
* ECDSA 256/384/521 in
109112
* OpenSSL traditional PEM format ("BEGIN EC PRIVATE KEY")
110113
* OpenSSL PKCS#8 PEM format ("BEGIN PRIVATE KEY", "BEGIN ENCRYPTED PRIVATE KEY")
111114
* OpenSSH key format ("BEGIN OPENSSH PRIVATE KEY")
115+
* PuTTY private key format ("PuTTY-User-Key-File-2", "PuTTY-User-Key-File-3")
112116
* ED25519 in
113117
* OpenSSL PKCS#8 PEM format ("BEGIN PRIVATE KEY", "BEGIN ENCRYPTED PRIVATE KEY")
114118
* OpenSSH key format ("BEGIN OPENSSH PRIVATE KEY")
119+
* PuTTY private key format ("PuTTY-User-Key-File-2", "PuTTY-User-Key-File-3")
115120

116121
Private keys in OpenSSL traditional PEM format can be encrypted using one of the following cipher methods:
117122
* DES-EDE3-CBC
@@ -122,7 +127,7 @@ Private keys in OpenSSL traditional PEM format can be encrypted using one of the
122127

123128
Private keys in OpenSSL PKCS#8 PEM format can be encrypted using any cipher method BouncyCastle supports.
124129

125-
Private keys in ssh.com format can be encrypted using one of the following cipher methods:
130+
Private keys in ssh.com format can be encrypted using the following cipher method:
126131
* 3des-cbc
127132

128133
Private keys in OpenSSH key format can be encrypted using one of the following cipher methods:
@@ -137,6 +142,9 @@ Private keys in OpenSSH key format can be encrypted using one of the following c
137142
* aes256-gcm<span></span>@openssh.com
138143
* chacha20-poly1305<span></span>@openssh.com
139144

145+
Private keys in PuTTY private key format can be encrypted using the following cipher method:
146+
* aes256-cbc
147+
140148
## Host Key Algorithms
141149

142150
**SSH.NET** supports the following host key algorithms:

docfx/logging.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Logging
2+
=================
3+
4+
SSH.NET uses the [Microsoft.Extensions.Logging](https://learn.microsoft.com/dotnet/core/extensions/logging) API to log diagnostic messages. In order to access the log messages of SSH.NET in your own application for diagnosis, register your own `ILoggerFactory` before using the SSH.NET APIs, for example:
5+
6+
```cs
7+
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
8+
{
9+
builder.SetMinimumLevel(LogLevel.Debug);
10+
builder.AddConsole();
11+
});
12+
13+
Renci.SshNet.SshNetLoggingConfiguration.InitializeLogging(loggerFactory);
14+
15+
All messages by SSH.NET are logged under the `Renci.SshNet` category.

src/Renci.SshNet/Abstractions/DiagnosticAbstraction.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/Renci.SshNet/BaseClient.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66

7-
using Renci.SshNet.Abstractions;
7+
using Microsoft.Extensions.Logging;
8+
89
using Renci.SshNet.Common;
910
using Renci.SshNet.Messages.Transport;
1011

@@ -20,6 +21,7 @@ public abstract class BaseClient : IBaseClient
2021
/// </summary>
2122
private readonly bool _ownsConnectionInfo;
2223

24+
private readonly ILogger _logger;
2325
private readonly IServiceFactory _serviceFactory;
2426
private readonly object _keepAliveLock = new object();
2527
private TimeSpan _keepAliveInterval;
@@ -190,6 +192,7 @@ private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionI
190192
_connectionInfo = connectionInfo;
191193
_ownsConnectionInfo = ownsConnectionInfo;
192194
_serviceFactory = serviceFactory;
195+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
193196
_keepAliveInterval = Timeout.InfiniteTimeSpan;
194197
}
195198

@@ -343,7 +346,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
343346
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
344347
public void Disconnect()
345348
{
346-
DiagnosticAbstraction.Log("Disconnecting client.");
349+
_logger.LogInformation("Disconnecting client.");
347350

348351
CheckDisposed();
349352

@@ -442,7 +445,7 @@ protected virtual void Dispose(bool disposing)
442445

443446
if (disposing)
444447
{
445-
DiagnosticAbstraction.Log("Disposing client.");
448+
_logger.LogDebug("Disposing client.");
446449

447450
Disconnect();
448451

src/Renci.SshNet/Channels/Channel.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using System.Net.Sockets;
33
using System.Threading;
44

5-
using Renci.SshNet.Abstractions;
5+
using Microsoft.Extensions.Logging;
6+
67
using Renci.SshNet.Common;
78
using Renci.SshNet.Messages;
89
using Renci.SshNet.Messages.Connection;
@@ -18,6 +19,7 @@ internal abstract class Channel : IChannel
1819
private readonly Lock _messagingLock = new Lock();
1920
private readonly uint _initialWindowSize;
2021
private readonly ISession _session;
22+
private readonly ILogger _logger;
2123
private EventWaitHandle _channelClosedWaitHandle = new ManualResetEvent(initialState: false);
2224
private EventWaitHandle _channelServerWindowAdjustWaitHandle = new ManualResetEvent(initialState: false);
2325
private uint? _remoteWindowSize;
@@ -81,6 +83,7 @@ protected Channel(ISession session, uint localChannelNumber, uint localWindowSiz
8183
LocalChannelNumber = localChannelNumber;
8284
LocalPacketSize = localPacketSize;
8385
LocalWindowSize = localWindowSize;
86+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger(GetType());
8487

8588
session.ChannelWindowAdjustReceived += OnChannelWindowAdjust;
8689
session.ChannelDataReceived += OnChannelData;
@@ -555,7 +558,7 @@ protected virtual void Close()
555558
var closeWaitResult = _session.TryWait(_channelClosedWaitHandle, ConnectionInfo.ChannelCloseTimeout);
556559
if (closeWaitResult != WaitResult.Success)
557560
{
558-
DiagnosticAbstraction.Log(string.Format("Wait for channel close not successful: {0:G}.", closeWaitResult));
561+
_logger.LogInformation("Wait for channel close not successful: {CloseWaitResult}", closeWaitResult);
559562
}
560563
}
561564
}

src/Renci.SshNet/Channels/ChannelDirectTcpip.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Net.Sockets;
44
using System.Threading;
55

6+
using Microsoft.Extensions.Logging;
7+
68
using Renci.SshNet.Abstractions;
79
using Renci.SshNet.Common;
810
using Renci.SshNet.Messages.Connection;
@@ -15,7 +17,7 @@ namespace Renci.SshNet.Channels
1517
internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
1618
{
1719
private readonly Lock _socketLock = new Lock();
18-
20+
private readonly ILogger _logger;
1921
private EventWaitHandle _channelOpen = new AutoResetEvent(initialState: false);
2022
private EventWaitHandle _channelData = new AutoResetEvent(initialState: false);
2123
private IForwardedPort _forwardedPort;
@@ -31,6 +33,7 @@ internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip
3133
public ChannelDirectTcpip(ISession session, uint localChannelNumber, uint localWindowSize, uint localPacketSize)
3234
: base(session, localChannelNumber, localWindowSize, localPacketSize)
3335
{
36+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ChannelDirectTcpip>();
3437
}
3538

3639
/// <summary>
@@ -157,8 +160,7 @@ private void ShutdownSocket(SocketShutdown how)
157160
}
158161
catch (SocketException ex)
159162
{
160-
// TODO: log as warning
161-
DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);
163+
_logger.LogInformation(ex, "Failure shutting down socket");
162164
}
163165
}
164166
}

src/Renci.SshNet/Channels/ChannelForwardedTcpip.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Threading;
66
#endif
77

8+
using Microsoft.Extensions.Logging;
9+
810
using Renci.SshNet.Abstractions;
911
using Renci.SshNet.Common;
1012
using Renci.SshNet.Messages.Connection;
@@ -17,6 +19,7 @@ namespace Renci.SshNet.Channels
1719
internal sealed class ChannelForwardedTcpip : ServerChannel, IChannelForwardedTcpip
1820
{
1921
private readonly Lock _socketShutdownAndCloseLock = new Lock();
22+
private readonly ILogger _logger;
2023
private Socket _socket;
2124
private IForwardedPort _forwardedPort;
2225

@@ -45,6 +48,7 @@ internal ChannelForwardedTcpip(ISession session,
4548
remoteWindowSize,
4649
remotePacketSize)
4750
{
51+
_logger = SshNetLoggingConfiguration.LoggerFactory.CreateLogger<ChannelForwardedTcpip>();
4852
}
4953

5054
/// <summary>
@@ -142,8 +146,7 @@ private void ShutdownSocket(SocketShutdown how)
142146
}
143147
catch (SocketException ex)
144148
{
145-
// TODO: log as warning
146-
DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);
149+
_logger.LogInformation(ex, "Failure shutting down socket");
147150
}
148151
}
149152
}

0 commit comments

Comments
 (0)