2015-12-03 64 views
0

我想使用node.js獲得一個簡單的sftp客戶端。我使用ssh2(Reference)。但只要我使用的FileZilla連接我得到這個:sftp格式不正確的FXP_NAME數據包

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

您是否試過另一個SFTP客戶端,以防FileZilla/PSFTP過於嚴格?你可以嘗試WinSCP並將其會話日誌文件(調試2級)追加到問題中嗎? –

回答

0

問題是目前有一個.name()的錯誤。同時,您可以在傳遞給.name()的對象中明確設置attrs: {}作爲解決方法。