sanketbajoria / ssh2-promise

ssh with promise/async await and typescript support
https://www.npmjs.com/package/ssh2-promise
MIT License
148 stars 25 forks source link

How to get the retry count? #69

Open rehanvdm opened 3 years ago

rehanvdm commented 3 years ago

My application requires knowledge of when a SSH Connection + Tunnels are retrying.

The close and disconnect events do not fire and I have spent a lot of time trying to figure out how to get either an event or a flag by inspecting private variables on the class. But it shows connected every time. My procedure:

  1. Connected to the internet
  2. Connect SSH + Tunnels, test and it works.
  3. Disconnect from the internet
  4. ?? Need some event to know that we are retrying the connection ??
  5. Connect to the internet
  6. Test and it still works

If anyone has any advice or help, would be much appreciated.

rehanvdm commented 3 years ago

Related to #23

The reason I couldn't find a way was because of how the event emitter does not reregister events when it creates the new connection using the retry logic. I couldn't find/see a way around this, the sshConnection.ts class to callback to the index.ts to close the connection, remove the event listeners and then reattach them for when it creates the new connection in the retry logic here https://github.com/sanketbajoria/ssh2-promise/blob/5e3674da5bdf0e4c39a2df5e98b4a5acd771ee8e/src/sshConnection.ts#L280

I am disabling the retry logic, forking this repo and then adding a this.close(); just above the reject. I could for the life of me also not catch that.

https://github.com/sanketbajoria/ssh2-promise/blob/5e3674da5bdf0e4c39a2df5e98b4a5acd771ee8e/src/sshConnection.ts#L286

This closes the connection but still non of the original events are fired, but in my use case that monitors the local ports this is fine. As soon as I see the port is available and my program thinks it is in use, then I know something closed it and I must retry.