Skip to content

About using this module to test the connection process #103

@TakamiChie

Description

@TakamiChie

I am currently using this module to create an application that connects to the dstHost shown below.

flowchart LR
    A["localHost"] -->|Tunneling| B["host"]
    B -->|Tunneling| C["dstHost"]
Loading

I wanted to write test code to simulate communication at the dstHost, so I wrote the following code:

const { createTunnel } = require('tunnel-ssh');
const { Server } = require('ssh2');
const { readFileSync } = require('fs');
const net = require("net");

require("dotenv").config();
const tunnelOptions = {
  autoClose:false
};
const serverOptions = {
  host: process.env.SRC_HOST,  port: process.env.SRC_PORT
};
const sshOptions = {
  host: process.env.HOST, port: process.env.PORT,
  username: process.env.UNAME, password: process.env.PASSWORD
}
const forwardOptions = {
  srcAddr: process.env.HOST, srcPort: process.env.PORT,
  dstAddr: process.env.TO_HOST, dstPort: process.env.TO_PORT
}

const f = async () => {
  const proxy = net.createServer((client) => {
    const serverConnection = net.createConnection({host: forwardOptions.dstAddr, port: forwardOptions.dstPort});
    client.pipe(serverConnection);
  });
  const server = new Server({ hostKeys: [readFileSync('./id_rsa')] },  (client) => {
    console.log('Client connected!');

    client.on('authentication', (ctx) => {
      // authentication code
    }).on('ready', () => {
      // authenticated code
    }).on('close', () => {
      // closing code
    });
  });
  proxy.listen({host: sshOptions.host, port: sshOptions.port});
  server.listen({host: forwardOptions.dstAddr, port: forwardOptions.dstPort});

  const result = await createTunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions);
  console.log("Finished!");
  result[0].close();
}
f();

However, when I execute the above code, the log "Finished" is not displayed and the error "Error: Timed out while waiting for handshake" occurs.

How can I emulate SSH communication without causing errors?


tunnel-ssh:4.0.5
node: v19.6.0

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions