TerminalStudio / dartssh2

SSH and SFTP client written in pure Dart, aiming to be feature-rich as well as easy to use.
https://pub.dev/packages/dartssh2
MIT License
207 stars 53 forks source link

Connection blocks main thread #23

Open Nihildum opened 2 years ago

Nihildum commented 2 years ago

For some reasons the use of dartssh2 blocks the main thread of my flutter application. This happens when I connect to the server and when I run a simple command on the server e.g. cat file.txt which is very small (like 100 characters). I run my application with a debugger and stopped it during the hangs. For the connection it seems that the loading of the ssh key takes quite some time, for the cat I not that sure.

Since dart has just one thread und there are no async calls in between, the main application hangs. Is there something I can to to prevent the hangs?

Connect to the server: F (bcrypt.dart:1242) Blowfish.encipher (bcrypt.dart:1154) Blowfish.expand0state (bcrypt.dart:1193) bcrypt_hash (bcrypt.dart:1267) bcrypt_pbkdf (bcrypt.dart:1343) OpenSSHKeyPairs._decryptPrivateKeyBlob (ssh_key_pair.dart:213) OpenSSHKeyPairs.getPrivateKeys (ssh_key_pair.dart:138) SSHKeyPair.fromPem (ssh_key_pair.dart:24) ServerConnection._connect (connection.dart:23)

Run command cat: int.+ (integers.dart:8) TweetNaCl._M_off (tweetnacl.dart:1184) TweetNaCl._M (tweetnacl.dart:803) TweetNaCl.crypto_scalarmult (tweetnacl.dart:1394) TweetNaCl.crypto_scalarmult_base (tweetnacl.dart:1416) _ScalarMult.scalseMultBase (kex_x25519.dart:63) new SSHKexX25519 (kex_x25519.dart:18) SSHTransport._handleMessageKexInit (ssh_transport.dart:714) SSHTransport._handleMessage (ssh_transport.dart:637) SSHTransport._processPackets (ssh_transport.dart:317) SSHTransport._processVersionExchange (ssh_transport.dart:300) SSHTransport._processData (ssh_transport.dart:257) SSHTransport._onSocketData (ssh_transport.dart:237) _rootRunUnary (zone.dart:1434) _CustomZone.runUnary (zone.dart:1335) _CustomZone.runUnaryGuarded (zone.dart:1244) _BufferingStreamSubscription._sendData (stream_impl.dart:341) _BufferingStreamSubscription._add (stream_impl.dart:271) _SyncStreamControllerDispatch._sendData (stream_controller.dart:774) _StreamController._add (stream_controller.dart:648) _StreamController.add (stream_controller.dart:596) _Socket._onData (socket_patch.dart:2314) _rootRunUnary (zone.dart:1442) _CustomZone.runUnary (zone.dart:1335) _CustomZone.runUnaryGuarded (zone.dart:1244) _BufferingStreamSubscription._sendData (stream_impl.dart:341) _BufferingStreamSubscription._add (stream_impl.dart:271) _SyncStreamControllerDispatch._sendData (stream_controller.dart:774) _StreamController._add (stream_controller.dart:648) _StreamController.add (stream_controller.dart:596) new _RawSocket. (socket_patch.dart:1839) _NativeSocket.issueReadEvent.issue (socket_patch.dart:1322) _microtaskLoop (schedule_microtask.dart:40) _startMicrotaskLoop (schedule_microtask.dart:49)
Nihildum commented 2 years ago

P.S.: My ssh key is an 4096 RSA key. With https://pub.dev/packages/ssh2 I did not had these problem, but there the release version of my App was not working properly.

xtyxtyx commented 2 years ago

The algorithm used in encrypted private keys is bcrypt, which is a password hashing algorithm and is slow by design. You can use the compute function to move the computation to other isolates, as described in README.

Nihildum commented 2 years ago

Thanks for the answer. Using the compute to load the key solver the issue with the connection.

What do you mean in "bcrypt, which is a password hashing algorithm and is slow by design" with "slow by design". Slow in order to take long or slow because it needs a lot of computations?

Nevertheless, the application still hangs on the cat of the file.

vanlooverenkoen commented 7 months ago

@Nihildum did you find a solution? I have the same issue with any interaction of the package

Nihildum commented 7 months ago

I switched technology in my project. The ssh connection to the server was used in first prototype. When I implemented a real server with protobuf, I no longer neded the dartssh2 package.

vanlooverenkoen commented 7 months ago

Unfortunately I don't have that option

vanlooverenkoen commented 7 months ago

@xtyxtyx I am just using username & onPasswordRequest. Is there a way to prevent the main thread from blocking? Or do you have an idea on how we can prevent this in general?