Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js - ftp-srv - Simple ftp Server Example needed

The last days I'm trying to get a simple ftp-server running in NodeJS.

I found a package named "ftp-srv" with some documentation here: https://www.npmjs.com/package/ftp-srv

Inspire by the code-snipptes there I wrote a small script:

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

const hostname = '0.0.0.0';
const port = 1111

const ftpServer = new FtpSrv ( 'ftp://' + hostname + ':' + port,
{ anonymous: true, greeting : [ "Hello Jong", "Wie gehts?" ] } );

ftpServer.on ( 'login', ( data, resolve, reject ) =>
{
  console.log ( 'data: '    + data );
  console.log ( 'resolve: ' + resolve );
  console.log ( 'reject: '  + reject );

});

ftpServer.on ( 'client-error', (connection, context, error) =>
{
  console.log ( 'connection: ' +  connection );
  console.log ( 'context: '    +  context );
  console.log ( 'error: '      +  error );
});


ftpServer.listen()
.then(() =>
{
  console.log ( `Server running at http://${hostname}:${port}/` );
});

I can start the Script with "node ftpserver.js" and it runs without a Problem. If I connect with a FTP-Client Software it seems to connect, but waits for the "Welcome Message" and hangs at that point.

I provided the "greeting"-Variable but that text is not send to the client.

I searched a lot in google but couldnt find any working Examples for "ftp-srv".

I think the point where I have to fill in some code is here:

ftpServer.on ( 'login', ( data, resolve, reject ) =>
{
  // HERE
});

It would help me a lot if anyone could provide some Example-code to get a working client connection and to overcome the greeting message.

--- Edit ---

The Advice from jcaron helped a lot, Im one step further now. These are my changes:

[...]
ftpServer.on ( 'login', ( data, resolve, reject ) =>
{
  resolve ( { root: '/home/peter/apps/ftpfiles' } );
});
[...]

When I connect with a client now, the client tries to read the remote directory. After 3 tries the client get a timeout.

It seems like the client cant access the directory '/home/peter/apps/ftpfiles'. I can say that it exists and has read/write permissions by the user which I start my "ftpserver.js"-Script.

I tried some things with a fs-object instead of root:[dir], but always see the same behaviour.

Can anyone help?

--- Edit ---

In addition simply changed

const hostname = '0.0.0.0';
#to local address:
const hostname = '192.x.y.z';

and it worked for me.

like image 761
Peter Avatar asked Sep 15 '17 08:09

Peter


1 Answers

When you receive the login event, you need to call either resolve or reject based on what you decided what the result of the authentication.

If you consider the login info is correct, call resolve, passing it an object with the relevant details, for instance:

resolve({root: '/path/to/files/accessible/via/ftp'})

Also note that if you are testing locally on a private network, you should probably use you local IP or 127.0.0.1 as the hostname. 0.0.0.0 makes it use the external IP address.

like image 71
jcaron Avatar answered Oct 16 '22 08:10

jcaron