简体   繁体   English

使用ssh2模块在node.js中设置sftp服务器

[英]Setting up a sftp server in node.js using ssh2 module

I would like to setup a sftp server using node.js. 我想使用node.js设置一个sftp服务器。

I looked at ssh2 module and seems like a good fit to start a sftp server. 我看了ssh2模块,似乎很适合启动sftp服务器。

I also looked at sftp-stream api , but I am not able to figure out options on how to authenticate a user to my server, which directory would be the root for sftp 我还查看了sftp-stream api ,但是我无法找出有关如何向服务器验证用户身份的选项,该目录将是sftp的根目录

If you want to create an SFTP server that also handles authentication and everything (and not just an SFTP server as the subsystem for an OpenSSH server), you need to also code up the ssh2 server portion too. 如果要创建一个同时处理身份验证和所有操作的SFTP服务器(而不仅仅是作为OpenSSH服务器子系统的SFTP服务器),则还需要对ssh2服务器部分进行编码。 Here's a simple example that only allows password authentication and only starting sftp sessions: 这是一个简单的示例,仅允许密码身份验证并且仅启动sftp会话:

var fs = require('fs');
var ssh2 = require('ssh2'),
    Server = ssh2.Server;
var OPEN_MODE = ssh2.SFTP_OPEN_MODE,
    STATUS_CODE = ssh2.SFTP_STATUS_CODE;

new Server({
  privateKey: fs.readFileSync('host.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.on('sftp', function(accept, reject) {
        console.log('Client SFTP session');
        var openFiles = {};
        var handleCount = 0;
        // `sftpStream` is an `SFTPStream` instance in server mode
        var sftpStream = accept();
        sftpStream.on('OPEN', function(reqid, filename, flags, attrs) {
          // only allow opening /tmp/foo.txt for writing
          if (filename !== '/tmp/foo.txt' || !(flags & OPEN_MODE.WRITE))
            return sftpStream.status(reqid, STATUS_CODE.FAILURE);
          // create a fake handle to return to the client, this could easily
          // be a real file descriptor number for example if actually opening
          // the file on the disk
          var handle = new Buffer(4);
          openFiles[handleCount] = true;
          handle.writeUInt32BE(handleCount++, 0, true);
          sftpStream.handle(reqid, handle);
          console.log('Opening file for write')
        }).on('WRITE', function(reqid, handle, offset, data) {
          if (handle.length !== 4 || !openFiles[handle.readUInt32BE(0, true)])
            return sftpStream.status(reqid, STATUS_CODE.FAILURE);
          // fake the write
          sftpStream.status(reqid, STATUS_CODE.OK);
          var inspected = require('util').inspect(data);
          console.log('Write to file at offset %d: %s', offset, inspected);
        }).on('CLOSE', function(reqid, handle) {
          var fnum;
          if (handle.length !== 4 || !openFiles[(fnum = handle.readUInt32BE(0, true))])
            return sftpStream.status(reqid, STATUS_CODE.FAILURE);
          delete openFiles[fnum];
          sftpStream.status(reqid, STATUS_CODE.OK);
          console.log('Closing file');
        });
      });
    });
  }).on('end', function() {
    console.log('Client disconnected');
  });
}).listen(0, '127.0.0.1', function() {
  console.log('Listening on port ' + this.address().port);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM