本文介绍了创建一个能够处理远程转发的节点 SSH2 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过对 staskoverflow、Google 和官方 Node SSH2 存储库 的一些研究,我仍然无法创建与远程端口转发一起使用的节点 SSH2 服务器...

After some research on staskoverflow, Google and official Node SSH2 repository, I still can not create a Node SSH2 server which work with remote port forwarding...

实际上,我可以使用远程服务器上的标准 SSH 守护程序和客户端上的此命令执行我想要的操作:

Actually I can do what I want with standard SSH daemon on distant server and this command on client side :

ssh -R 8100:localsite.tld:80 sub.distantserver.com

来自 sub.distantserver.com:8100 的所有流量都重定向到 localsite.tld:80.(端口 22,传统的 SSH 守护进程).

All traffic from sub.distantserver.com:8100 is redirect to localsite.tld:80. (port 22, traditional SSH daemon).

我唯一的目标是通过端口 21 上的节点 SSH2 服务器来实现这一点:

My only goal is to achieve this with a Node SSH2 Server on port 21 :

ssh -R 8100:localsite.tld:80 [email protected] -p 21
password: bar

当它起作用时,我可以对用户进行一些检查并启动其他一些进程;)

When it'll work, I can do some check on the user and start some other process ;)

基于官方的 Github 问题示例,我尝试了这样的操作,就像一个 POC 来捕获流,但它在不存在的 forwardIn 上失败.

Basing on official Github issues example I try something like this, just like a POC to catch stream but it fail on forwardIn that does not exists.

var fs = require('fs');
var crypto = require('crypto');
var inspect = require('util').inspect;

var buffersEqual = require('buffer-equal-constant-time');
var ssh2 = require('ssh2');
var utils = ssh2.utils;

new ssh2.Server({
  hostKeys: [fs.readFileSync('/etc/ssh/ssh_host_rsa_key')]
}, function(client) {
  console.log('Client connected!');

  client.on('authentication', function(ctx) {
    if (ctx.method === 'password'
        && ctx.username === 'foo'
        && ctx.password === 'bar')
      ctx.accept();
    else
      ctx.reject();
  }).on('ready', function() {
    console.log('Client authenticated!');

    client.on('session', function(accept, reject) {
      var session = accept();
      session.once('exec', function(accept, reject, info) {
        console.log('Client wants to execute: ' + inspect(info.command));
        var stream = accept();
        stream.stderr.write('Oh no, the dreaded errors!\n');
        stream.write('Just kidding about the errors!\n');
        stream.exit(0);
        stream.end();
      });
    });
    client.on('request', function(accept, reject, name, info) {
        console.log(info);
        if (name === 'tcpip-forward') {
        accept();
        setTimeout(function() {
          console.log('Sending incoming tcpip forward');
          client.forwardIn(info.bindAddr,
                           info.bindPort,
                           function(err, stream) {
                                if (err)
                                  return;
                                stream.end('hello world\n');
                            });
        }, 1000);
      } else {
        reject();
      }
    });
  });
}).listen(21, '0.0.0.0', function() {
  console.log('Listening on port ' + this.address().port);
});

有谁知道如何实现一个简单的常规SSH转发服务器端?

Does anybody know how to achieve a simple conventional SSH forward server side ?

谢谢!

推荐答案

在作者的帮助下找到了解决方案:Github 官方解决方案

Found a solution with author's help :Official Github solution on issue

let fs = require('fs'),
  inspect = require('util').inspect,
  ssh2 = require('ssh2'),
  net = require('net');

new ssh2.Server({
  hostKeys: [fs.readFileSync('/etc/ssh/ssh_host_rsa_key')]
}, client => {
  console.log('Client connected!');
  client
    .on('authentication', ctx => {
      if (
        ctx.method === 'password'
        && ctx.username === 'foo'
        && ctx.password === 'bar'
      ) {
        ctx.accept();
      } else {
        ctx.reject();
      }
    })
    .on('ready', () => {
      console.log('Client authenticated!');
      client
        .on('session', (accept, reject) => {
          let session = accept();
          session.on('shell', function(accept, reject) {
            let stream = accept();
          });
        })
        .on('request', (accept, reject, name, info) => {
          if (name === 'tcpip-forward') {
            accept();
            net.createServer(function(socket) {
              socket.setEncoding('utf8');
              client.forwardOut(
                info.bindAddr, info.bindPort,
                socket.remoteAddress, socket.remotePort,
                (err, upstream) => {
                  if (err) {
                    socket.end();
                    return console.error('not working: ' + err);
                  }
                  upstream.pipe(socket).pipe(upstream);
                });
            }).listen(info.bindPort);
          } else {
            reject();
          }
        });
    });
}).listen(21, '0.0.0.0', function() {
  console.log('Listening on port ' + server.address().port);
});

这篇关于创建一个能够处理远程转发的节点 SSH2 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 05:31