question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

server fail to bind "forwardIn" port

See original GitHub issue

Hi. Thanks mscdex for this module. I can’t understand why the code below work well when pointing to a real ssh server and fail when pointing to a node ssh2 mscdex server

I hope i’ve made a mistake and it’s a a bug or a limitation …

Requiring some serious help dude ! thanks in advance !

var ssh2 = require('ssh2');
var utils = ssh2.utils;
var fs = require('fs');
var keygen = require('ssh-keygen');
var ssh2Client = require('ssh2').Client;

// Pointing on mscdex ssh2 server
// run that
// then netstat -antp | grep 8000 -> KO
var clientDstKo = {
    ip:'127.0.0.1',
    port:23,
    username:'foo',
    password:'bar'
};

// Pointing on classical linux ssh daemon
// run that
// then netstat -antp | grep 8000 -> OK
// then telnet localhost port 8000 and everything is fine
var clientDstOk = {
    ip:'127.0.0.1',
    port:22,
    username:'realuser',
    password:'changeme'
};

// just change this to clientDstKo if you wanna reproduce the problem
var clientDst = clientDstOk;

var onClientConnected = function(client,info) {
    console.log('Server: client connected (%s)',info.ip);

    client
        .on('authentication', function(ctx) {
            if (ctx.method === 'password' && ctx.username === 'foo'&& ctx.password === 'bar') return ctx.accept();
            ctx.reject();
        })
        .on('ready', function() {
            client.on('request',function(accept,reject,name,info) {
                console.log('Server: request event fired:',name,info);
                accept();
            });
        });
};

var start = function(privateKey) {
    new ssh2.Server({
        privateKey: privateKey,
        //debug:console.log
    },onClientConnected)
        .listen(23, '127.0.0.1', function() {

            var client = new ssh2Client();

            client.on('tcp connection', function(details,accept,reject) {
                console.log('Client: tcp connection event fired, AS EXPECTED ! exiting ..',JSON.stringify(details));
                process.exit(0);
            });

            client.on('ready', function() {
                console.log('Client: ready event fired');

                this.forwardIn('localhost', 8000, function (err, portBinded) {
                    if (err) return console.log(err.message);
                    console.log('Client: server supposed to accept forwardIn port %s',portBinded);
                    console.log('Now you are supposed to see port 8000 listening on localhost, please telnet localhost 8000');
                    return true;
                });

            }).connect({
                host: clientDst.ip,
                port: clientDst.port,
                username:clientDst.username,
                password:clientDst.password,
                //debug:console.log
            });
        });
};

keygen({
    password: false,
    read: true,
    destroy:true
}, function(err, out){
    if(err) return cb(err);
    start(out.key);
});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mscdexcommented, Sep 16, 2015

Here is a simple implementation that allows any ip/port:

var net = require('net');
// ...
.on('ready', function() {
  var proxied = {};
  client.on('request',function(accept,reject,name,info) {
    console.log('Server: request event fired:', name, info);

    if (name === 'cancel-tcpip-forward') {
      var server = proxied[info.bindAddr + ':' + info.bindPort]);
      if (server) {
        server.close();
        delete proxied[info.bindAddr + ':' + info.bindPort];
        accept();
      } else
        reject();
    } else if (name === 'tcpip-forward') {
      var proxy = net.createServer(function(sock) {
        client.forwardOut(info.bindAddr,
                          info.bindPort,
                          sock.remoteAddress,
                          sock.remotePort,
                          function(err, clientStream) {
          if (err)
            return sock.end();
          clientStream.pipe(sock).pipe(clientStream);
        });
      }).on('error', function(err) {
        // Most likely could not bind to the address/port
        reject();
      }).listen(info.bindPort, info.bindAddr, function() {
        proxied[info.bindAddr + ':' + info.bindPort] = proxy;
        accept();
      });
    } else
      reject(); // Unsupported request
  }).on('end', function() {
    // Clean up any forwarded ports that were left open
    Object.keys(proxied).forEach(function(key) {
      proxied[key].close();
    });
    proxied = null;
  });
});
0reactions
franck34commented, Sep 16, 2015

wow … i have to study now !

thank you so much again. I saw many things in closed ticket speaking about what i wan’t to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix Minecraft Server Failed to Bind to Port [5 Ways]
The most common reason for Minecraft failed to bind to port error comes from the incorrect IP configuration. If you have entered something ......
Read more >
Minecraft Server Error: Failed To Bind To Port - Apex Hosting
The failed to bind to Port Minecraft server error occurs when a server attempts to use a service port (i.e 25565) that is...
Read more >
[Solution] "Failed to bind to port" - Support - Minecraft Forum
Failed to bind to port is an issue which is caused by the host operating system. Either because something is already running on...
Read more >
How to Fix 'Failed to Bind to Port' Error on Minecraft? - Appuals
Solution 2: Changing the IP Configuration · We will need a text editor to edit the server files. · Navigate to this address...
Read more >
5 Fixes for Minecraft Server Failed to Bind to Port
Minecraft's failure to bind to port is most likely due to incorrect IP settings. If another server uses the same port and IP...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found