Apply similar treatment to PipeStream as #1322 did to ShellStream
PipeStream now behaves much more Stream-like. In particular, it performs partial
reads (instead of blocking until a certain amount of data is available), blocks
until data is available (instead of returning 0 prematurely) and removes the
Stream-unlike properties `BlockLastReadBuffer` and `MaxBufferLength`.
Sadly I gave up trying to make a benchmark compatible with all the quirks of the
previous implementation, but a dumb throughput test (reading and writing simultaneously)
shows about 5.2GB/s with this implementation compared to 140MB/s previously.
Some cleanup of its usage in the library followed.
Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
* Enable nullable on NetConf/Scp/SshClient
* fix formatting
* improve directoryCounter check
* disable nullable warnings on old frameworks
since the libraries are missing a lot of nullable attributes
in the old frameworks, this causes a lot of false positive
nullable warnings.
Simply disable these warnings for old frameworks.
* fix flaky Sftp_BeginUploadFile test
this test can randomly fail because it assumes that the callback
has been called when the AsyncWaitHandle was set. But this is not
necessarily the case because AsyncResult.SetAsCompleted does it
the other way around.
example: https://ci.appveyor.com/project/drieseng/ssh-net/builds/49831002/job/1237d4lg46j22pf0
* use ManualResetEventSlim
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* fix flaky ReceiveOnServerSocketShouldReturnZero test
ReceiveOnServerSocketShouldReturnZero assumed that the disconnect
already happened on the server side, but it never waits for this.
This could cause ServerSocket.Receive to still return valid data
and fail the test.
example: https://ci.appveyor.com/project/drieseng/ssh-net/builds/49836561/job/wen5tjd1c7wgxrfh
* Update test/Renci.SshNet.Tests/Classes/SessionTest_Connected_Disconnect.cs
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* Preparation to enforce formatting and style in CI
- enabled IDE0055 to enforce formatting on build
- disabled SA1137 and SA1025 because they are already
covered by IDE0055
- disabled SA1021 because it conflicts with csharp_space_after_cast
* Cleanup formatting and style on codebase
This commit has no manual changes, it is the result
of running "dotnet format whitespace" and "dotnet format style"
* new formatting fixes after merge
* appveyor: set git autocrlf
as suggested by sharwell to hopefully fix Windows CI.
* use autocrlf input
to hopefully fix Linux tests
* fix formatting
* use autocrlf input for Linux only
* set csharp_space_after_cast to false
* Revert SA1021 suppression
---------
Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
- Update README to point to https://sshnet.github.io/SSH.NET rather than the CHM file
- Add a CONTRIBUTING.md with some how-tos
For https://sshnet.github.io/SSH.NET:
- Use the repo README as the landing page to keep them in sync
- Combine the API-per-TFM pages to just one "API" page
- Add a couple more examples
- Add a GitHub link in the header.
* Tweak AsyncSocketListener in tests
Some CI runs have been crashing lately from within AsyncSocketListener.
This moves a bit of disconnection & exception handling around. It probably won't
fix the underlying tests which would have otherwise failed, but it might stop the
process from outright crashing.
Also reference the same code for the integration tests.
* Simplify the diff
* Stabilise a couple of tests
* Use BCL for RSA
* Restore benchmark (with quirks for old code)
* Fixup benchmark (remove compatibility with old code)
* cosmetic tweaks
* Add a regression test for #1388
* Fix SocketAsyncEventArgsAwaitable calling continuation multiple times
* Use Interlocked.Exchange
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* Add timeouts when reading from sockets in Socks5Connector
* Add a Socks5 timeout test for a connection reply
---------
Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* Implement OpenSSH strict key exchange extension
* The pseudo-algorithm
is only valid in the initial SSH2_MSG_KEXINIT and MUST be ignored
if they are present in subsequent SSH2_MSG_KEXINIT packets.
* Only send strict kex pseudo algorithm for the first kex.
Strictly disable non-kex massages in strict kex mode.
* Unit tests for strict kex
* More unit tests
* More unit tests
* Correct file name
* Update SessionTest_ConnectingBase.cs
* More unit tests
* Delete SessionTest_Connecting_ServerSendsMaxIgnoreMessagesBeforeKexInit.cs
* Add a comment about throwing exception when inbound sequence number is about to wrap during init kex.
* Delete SessionTest_Connecting_ServerSendsDebugMessageAfterKexInit_NoStrictKex.cs
* Fix build
* Update test/Renci.SshNet.Tests/Classes/SessionTest_Connected.cs
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* Init AeadCipher
* Move AeadCipher to parent folder. Move EncryptBlock/DecryptBlock from SymmetricCipher to BlockCipher
* simplify parameter name
* Implement AesGcmCipher
* Update README
* Remove protected IV from AeadCipher; Set offset to outbound sequence just like other ciphers
* Rename associatedData to packetLengthField
* Use Span<byte> to avoid unnecessary allocations
* Use `Span` to improve performance when `IncrementCounter()`
* Add `IsAead` property to `CipherInfo`. Include packet length field and tag field in offset and length when call AesGcm's `Decrypt(...)` method. Do not determine HMAC if cipher is AesGcm during kex.
* Fix build
* Fix UT
* Check `AesGcm.IsSupported` before add to the `Encryptions` collection.
Guard AES-GCM with `NET6_0_OR_GREATER`.
Insert AES-GCM ciphers right after AES-CTR ciphers but before AES-CBC ciphers, which is similar with OpenSSH:
```
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
```
Although Dictionary's order is not defined, from observation, it is in the same order with add. Anyway that would be another topic.
* Suppress CA1859 "Use concrete types when possible for improved performance" for `ConnectionInfo.Encryptions`.
Test `Aes128Gcm` and `Aes256Gcm` only when `NET6_0_OR_GREATER`
* Update xml doc comments. Do not treat AesGcmCipher separately in Session.cs
* Fix build
* Fix build
* Update the comment as ChaCha20Poly1305 uses a separated key to encypt the packet length and the size it 4.
* Update src/Renci.SshNet/Security/Cryptography/Cipher.cs
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* Make `AesGcmCipher` internal.
Assert offset when decrypt.
* Fix nullable error in build
* typos
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* Integrate `ZLibStream` from .NET 6.0+ with SSH.NET.
* OpenSSH server does not support zlib (pre-auth); OpenSSH client still supports zlib (pre-auth)
* Correct compression algorithm name; Update README.md
* Integrate `ZLibStream` from .NET 6.0+ with SSH.NET.
* OpenSSH server does not support zlib (pre-auth); OpenSSH client still supports zlib (pre-auth)
* Correct compression algorithm name; Update README.md
* Test the compression by upload/download file
* Refactor compression.
* Move delayed compression logic to base class.
* seal Zlib
* update unit test
* update unit test to see if it can trigger integration test
* Flush zlibStream
* Fix integration test
* update test
* Update ConnectionInfo.cs
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* Update README.md
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* debugging CI
* apt-get
* print memory
* x
* x
* Remove some unbounded random.Next() calls
Some of which are causing giant array allocations unnecessarily. Should stabilise CI.
* cleanup
* Handle unknown channel messages correctly
See discussion #1218 . Some servers send custom channel messages
like 'keepalive@proftpd.org' as keep alive messages. This currently
causes a NotSupportedException.
According to the spec https://datatracker.ietf.org/doc/html/rfc4254#section-5.4 :
"If the request is not recognized or is not
supported for the channel, SSH_MSG_CHANNEL_FAILURE is returned."
Send a failure message back instead of throwing an exception.
* consider WantReply before sending failure reply
* Use RemoteChannelNumber for failure message
* fix wrong ChannelNumber in SshCommand Channel Response
not directly related to the PR, was noticed during Code Review.
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* SftpClient: handle the SFTP session being closed by the server
If the server closes the SFTP session but keeps the TCP connection open,
this currently causes IsConnected to return true, but any operation
fails with "the session is not open".
SftpClient.IsConnected now also check sftpSession.IsOpen. Connect()
and ConnectAsync() were reworked to take into account that the Session
may already/still be open, but the SFTP session may not. This is needed
so a reconnect works.
fixes#843 and #1153
* Always re-create session
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
Co-authored-by: Igor Milavec <igor.milavec@gmail.com>
* Fixed warnings in tests.
* Fixed warnings in main project.
* Fix public key.
* Further warning cleanup.
* Muted remaining warnings.
* Final cleanup.
* Fix warning.
* Fix test classes.
* Fix more internals.
* Revert "internal" in test projects
* Revert more internals.
* Revert all non-analyzer changes.
---------
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
* Fix CancelAsync Cause Deadlock
* Fix CancelAsync Cause Deadlock
* Support manual cancelling if exit-signal does not cancel
* Fix switch with duplicate case
* Revert wait exit response, use existing OperationCancelledException
* Not executing callback when command is cancelled
* Add `Closed` event to `ShellStream`. Lib consumer could hook to this event to detect if channel is closed by server **in time**.
* handle `Closed` event on different thread. No need to block original thread.
* Updated dependencies.
* Reverted Microsoft.Bcl.AsyncInterfaces back to version 1.0.0.
Also added a note so we no longer try to upgrade it.
* Reverted back to Moq.
---------
Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
* Added guard clauses to various timeouts to ensure they don't exceed an Int32 in milliseconds.
* Fixed guard clauses.
* Updated build tags.
* Added guard clauses to various timeouts to ensure they don't exceed an Int32 in milliseconds.
* Fixed tests.
* Added additional tests.
* Replaced NoWarn with .editorconfig setting
* Fixed references to parameter names.
* Restore Write/Flush tests
* Restore write buffer to ShellStream and painfully fix the mocks
---------
Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>