Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About using this module to test the connection process #103

Open
TakamiChie opened this issue Feb 11, 2023 · 1 comment
Open

About using this module to test the connection process #103

TakamiChie opened this issue Feb 11, 2023 · 1 comment
Assignees

Comments

@TakamiChie
Copy link

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

@agebrock
Copy link
Owner

Hi there, this is not really ssh-tunnel related and more a question you should ask in the ssh2 project.
However I like your approach and had a quick look, I will investigate a bit further, if this would be a nice way to test the project.

I advice you to have a look into the following post :
mscdex/ssh2#698

There is an working example for an SSH2 server with port forwarding support.

it would be nice to get your feedback once you are done emulating.

Thx for the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants