Skip to content

Commit

Permalink
refactor(commands): commands now store all info about themselves
Browse files Browse the repository at this point in the history
Makes it easier to modify a command

Each command exports an object:
{
  directive: string or array of commands that call this handler
  handler: function to process the command
  syntax: string of how to call the command
  description: human readable explaination of command
  flags: optional object of flags
}
  • Loading branch information
trs committed Mar 8, 2017
1 parent f6d1a38 commit 795c3d7
Show file tree
Hide file tree
Showing 84 changed files with 769 additions and 708 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ __whitelist__ : `[]`

__file_format__ : `ls`
> Format to use for [file stat](https://nodejs.org/api/fs.html#fs_class_fs_stats) responses (such as with the `LIST` command).
Possible values:
- `ls` : [bin/ls format](https://cr.yp.to/ftp/list/binls.html)
- `ep` : [Easily Parsed LIST format](https://cr.yp.to/ftp/list/eplf.html)
- `function` : pass in your own format function, returning a string:
Possible values:
`ls` : [bin/ls format](https://cr.yp.to/ftp/list/binls.html)
`ep` : [Easily Parsed LIST format](https://cr.yp.to/ftp/list/eplf.html)
`function` : pass in your own format function, returning a string:
`function (fileStats) { ... }`

__log__ : `bunyan`
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"ftp",
"ftp-server",
"ftp-srv",
"ftp-svr",
"ftpd",
"server"
],
Expand Down
3 changes: 0 additions & 3 deletions src/commands/allo.js

This file was deleted.

19 changes: 0 additions & 19 deletions src/commands/auth.js

This file was deleted.

6 changes: 0 additions & 6 deletions src/commands/cdup.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/commands/cwd.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/commands/dele.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/commands/feat.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/commands/help.js

This file was deleted.

11 changes: 7 additions & 4 deletions src/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const _ = require('lodash');
const when = require('when');

const REGISTRY = require('./registry');

class FtpCommands {
constructor(connection) {
console.log(REGISTRY)
this.connection = connection;
this.registry = require('./registry');
this.previousCommand = {};
this.blacklist = _.get(this.connection, 'server.options.blacklist', []).map(cmd => _.upperCase(cmd));
this.whitelist = _.get(this.connection, 'server.options.whitelist', []).map(cmd => _.upperCase(cmd));
Expand All @@ -14,7 +16,7 @@ class FtpCommands {
const log = this.connection.log.child({command});
log.trace('Handle command');

if (!this.registry.hasOwnProperty(command.directive)) {
if (!REGISTRY.hasOwnProperty(command.directive)) {
return this.connection.reply(402, 'Command not allowed');
}

Expand All @@ -26,8 +28,9 @@ class FtpCommands {
return this.connection.reply(502, 'Command not whitelisted');
}

const commandRegister = this.registry[command.directive];
if (!commandRegister.no_auth && !this.connection.authenticated) {
const commandRegister = REGISTRY[command.directive];
const commandFlags = _.get(commandRegister, 'flags', {});
if (!commandFlags.no_auth && !this.connection.authenticated) {
return this.connection.reply(530);
}

Expand Down
53 changes: 0 additions & 53 deletions src/commands/list.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/commands/mdtm.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/commands/mkd.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/commands/mode.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/commands/noop.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/commands/opts.js

This file was deleted.

19 changes: 0 additions & 19 deletions src/commands/pass.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/commands/pasv.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/commands/port.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/commands/pwd.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/commands/quit.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/commands/registration/allo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
directive: 'ALLO',
handler: function () {
return this.reply(202);
},
syntax: '{{cmd}}',
description: 'Allocate sufficient disk space to receive a file',
flags: {
obsolete: true
}
};
10 changes: 10 additions & 0 deletions src/commands/registration/appe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const stor = require('./stor').handler;

module.exports = {
directive: 'APPE',
handler: function (args) {
return stor.call(this, args);
},
syntax: '{{cmd}} [path]',
description: 'Append to a file'
}
27 changes: 27 additions & 0 deletions src/commands/registration/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const _ = require('lodash');

module.exports = {
directive: 'AUTH',
handler: function ({command} = {}) {
const method = _.upperCase(command._[1]);

switch (method) {
case 'TLS': return handleTLS.call(this);
case 'SSL': return handleSSL.call(this);
default: return this.reply(504);
}
},
syntax: '{{cmd}} [type]',
description: 'Set authentication mechanism',
flags: {
no_auth: true
}
}

function handleTLS() {
return this.reply(504);
}

function handleSSL() {
return this.reply(504);
}
11 changes: 11 additions & 0 deletions src/commands/registration/cdup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const cwd = require('./cwd').handler;

module.exports = {
directive: ['CDUP', 'XCUP'],
handler: function(args) {
args.command._ = [args.command._[0], '..'];
return cwd.call(this, args);
},
syntax: '{{cmd}}',
description: 'Change to Parent Directory'
}
Loading

0 comments on commit 795c3d7

Please sign in to comment.