mscdex / mscdex/ssh2

SSH2 connection failed in production - Timed out while waiting for handshake

Open
#1,464 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
JavaScript
Stars
5.8k
Forks
734
PR merge metrics
No merged PRs in 30d

Description

I have the following function in my service to connect to my server using ssh:

  async executeSSHCommand(command: string): Promise<string> {
    const conn = new Client();
    const host = this._configService.get('bbb.host', { infer: true });
    const privateKey = this._configService.get('bbb.privateKey', { infer: true });
    const passphrase = this._configService.get('bbb.passphrase', { infer: true })?.replace(/\\n/g, '\n');

    // console.log('Connecting with:', {
    //   host,
    //   user: 'root',
    //   hasKey: privateKey,
    //   passphrase: passphrase,
    // });

    return new Promise((resolve, reject) => {
      conn.once('ready', () => {
        conn.exec(command, (err, stream) => {
          if (err) return reject(err);

          let output = '';
          stream.on('data', (data) => (output += data.toString()));
          stream.on('close', (code) => {
            conn.end(); // kończymy połączenie dopiero po zakończeniu komendy
            if (code !== 0) return reject(`Command failed with code: ${code}`);
            resolve(output);
          });
        });
      });

      conn.on('error', (err) => reject(err));
      conn.connect({ host, port: 22, username: 'root', privateKey, passphrase });
    });
  }

Unfortunately, while it works fine in development, it does not work in production (digital ocean app) - I have an error:

Timed out while waiting for handshake

Any ideas how to fix that?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the conn.connect entry point in the reported function and review the ssh2 connection and handshake behavior. Compare the host, port, key, passphrase, and network conditions between development and the DigitalOcean production environment. Done means identifying a reproducible cause for the production-only handshake timeout or confirming that it is external to ssh2.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.