KaramJaber
KaramJaber

Reputation: 893

Error 502 "Command Not Supported" when uploading a file using FTP NodeJS

I'm trying to create FTP server using ftp-srv library and then to upload a file to it using FTP client library jsftp.

All the functions(Rename,ls,etc..) are working except the upload function which fails with the error 502 command not supported.

The FTP server code:

const ftpServer = require('ftp-srv'); 

const startFtpServer = async (host, port, user, pass) => {  


  const server = new ftpServer(`ftp://${host}:${port}`, { 
    pasv_range: '8400-8500', // change this to an open port range for your app 
    greeting:"Hello to my custom server",
  }); 

  server.on('login', ({ connection, username, password }, resolve, reject) => { 
    if (1) { 
      // If connected, add a handler to confirm file uploads 
      connection.on('STOR', (error, fileName) => { 
        if (error) { 
          console.error(`FTP server error: could not receive file ${fileName} for upload ${error}`); 
        } 
        console.info(`FTP server: upload successfully received - ${fileName}`); 
      }); 
      resolve({ root: 'C:/Users/kjaber/Desktop/serv' }); 
    } else { 
      reject(new Error('Unable to authenticate with FTP server: bad username or password')); 
    } 
  }); 

  server.on('client-error', ({ context, error }) => { 
    console.error(`FTP server error: error interfacing with client ${context} ${error} on ftp://${host}:${port} ${JSON.stringify(error)}`); 
  }); 

  const closeFtpServer = async () => { 
    await server.close(); 
  }; 

  // The types are incorrect here - listen returns a promise 

  await server.listen(); 


  return { 
    shutdownFunc: async () => { 
      // server.close() returns a promise - another incorrect type 
      await closeFtpServer(); 
    }, 
  }; 
}; 

startFtpServer("127.0.0.1","60000","anonymous","[email protected]");

This open an FTP server to port 21.

The FTP client:

var fs = require("fs");

var ftp = new jsftp({
host: "127.0.0.1",
port: 21, // defaults to 21
user: "a", // defaults to "anonymous"
pass: "b" // defaults to "@anonymous"
});

ftp.auth("aaaa", "bbbb", function(hadErr) {
if (!hadErr)
console.log("auth succesfull")
});


ftp.raw("quit", (err, data) => {
  if (err) {
    return console.error(err);
  }

  console.log("Bye!");
});

ftp.put('C:/Users/kjaber/Desktop/Console/bbb.txt', "/", err => {
  console.log(err)
  if (!err) {
    console.log("File transferred successfully!");
  }
});

After running the client i get the error "Error: 502 Command not supported" . I tried to use FileZella as FTP client and the upload succeeded but using the above client code it doesn't work. Any idea why ?

Upvotes: 0

Views: 3260

Answers (1)

keyo
keyo

Reputation: 91

I had the same issue. You need to whitelist commands, e.g.

const ftpServer = new FtpSrv({ url,
    pasv_url: myHostname,
    whitelist: ['STOR', 'USER', 'PASS', 'TYPE', 'RETR', 'PASV', 'QUIT'],
    anonymous: false,
    greeting : [ "Hello, I'm FTP server" ] } );

Upvotes: 2

Related Questions