我正在尝试使用node.js获得一个简单的sftp-client。我正在使用ssh2(Reference)。但是一旦我使用FileZilla连接,我得到这个:
Command: pwd
Response: Current directory is: "/server/"
Status: Directory listing successful
Status: Retrieving directory listing...
Command: ls
Status: Listing directory /server/
Error: Reading directory .: malformed FXP_NAME packet
Status: Directory listing successful
这是我的代码:
session.on('sftp', function(accept, reject) {
console.log('Client SFTP session');
var openFiles = {};
var handleCount = 0;
// `sftpStream` is an `SFTPStream` instance in server mode
// see: https://github.com/mscdex/ssh2-streams/blob/master/SFTPStream.md
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 & ssh2.SFTP_OPEN_MODE.WRITE))
return sftpStream.status(reqid, ssh2.SFTP_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, ssh2.SFTP_STATUS_CODE.FAILURE);
// fake the write
sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.OK);
var inspected = require('util').inspect(data);
console.log('Write to file at offset %d: %s', offset, inspected);
}).on('REALPATH', function (reqID,path) {
console.log('Opening dir: ' + path);
sftpStream.name(reqID,{filename:"/server/"});
}).on('OPENDIR', function(reqid,path) {
console.log("Opening dir: " + path);
sftpStream.handle(reqid, new Buffer(path, "binary"));
}).on('READDIR', function(reqID,path) {
console.log("Opening dir: " + path);
sftpStream.name(reqID,[{filename:".",longname:"."}]);
}).on('CLOSE', function(reqid, handle) {
var fnum;
if (handle.length !== 4 || !openFiles[(fnum = handle.readUInt32BE(0, true))])
return sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.FAILURE);
delete openFiles[fnum];
sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.OK);
console.log('Closing file');
});
});
关于此的纪录片非常糟糕,所以我决定问你。好像我在READDIR事件上做错了。
答案 0 :(得分:0)
问题在于.name()
目前存在错误。在此期间,您可以在传递给attrs: {}
的对象中明确设置.name()
作为解决方法。