Compare commits
47 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88f02cd498 | ||
|
|
2cc5d54d7f | ||
|
|
f127d0e7b6 | ||
|
|
13048a96bd | ||
|
|
f6355e66c3 | ||
|
|
6e79e958cc | ||
|
|
db7d88f411 | ||
|
|
323ee62110 | ||
|
|
1e446a7801 | ||
|
|
977fbd4190 | ||
|
|
d5d1b98b04 | ||
|
|
df0a4d640c | ||
|
|
73274191fe | ||
|
|
37c3da3a62 | ||
|
|
9bece5f946 | ||
|
|
83947142df | ||
|
|
c54045e0b9 | ||
|
|
cf71243729 | ||
|
|
7fb43a5790 | ||
|
|
e99059125e | ||
|
|
954e9a1252 | ||
|
|
2b9e163958 | ||
|
|
c6a49d2191 | ||
|
|
14e5f87cc3 | ||
|
|
580b8d6eae | ||
|
|
a75d63df92 | ||
|
|
301ae110e8 | ||
|
|
4d69b48466 | ||
|
|
ec010697bb | ||
|
|
cf3d543f1a | ||
|
|
69bec2b01c | ||
|
|
2eac41d127 | ||
|
|
eb32f93fc6 | ||
|
|
095423606e | ||
|
|
61cf1bda39 | ||
|
|
75f847ed5d | ||
|
|
ad4b32fc13 | ||
|
|
be3c57bed0 | ||
|
|
dc7dd1075c | ||
|
|
543e6cc1cc | ||
|
|
5c1f8f7a65 | ||
|
|
557995a1a9 | ||
|
|
45eca5afe0 | ||
|
|
695e594d97 | ||
|
|
97b55fc92c | ||
|
|
577066850b | ||
|
|
0ec989cf1e |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,5 +2,4 @@ node_modules/
|
||||
|
||||
dist/
|
||||
reports/
|
||||
.env
|
||||
npm-debug.log
|
||||
|
||||
@@ -2,6 +2,10 @@ language: node_js
|
||||
node_js:
|
||||
- "6"
|
||||
|
||||
env:
|
||||
FTP_URL: ftp://127.0.0.1:8880
|
||||
PASV_RANGE: 8881
|
||||
|
||||
install: npm install
|
||||
|
||||
script:
|
||||
|
||||
260
README.md
260
README.md
@@ -1,28 +1,32 @@
|
||||
# ftp-srv [](https://badge.fury.io/js/ftp-srv) [](https://travis-ci.org/stewarttylerr/ftp-srv) [](https://github.com/semantic-release/semantic-release) [](http://commitizen.github.io/cz-cli/)
|
||||

|
||||
|
||||
[](https://badge.fury.io/js/ftp-srv) [](https://travis-ci.org/trs/ftp-srv) [](https://github.com/semantic-release/semantic-release) [](http://commitizen.github.io/cz-cli/)
|
||||
|
||||
<!--[RM_DESCRIPTION]-->
|
||||
> Modern, extensible FTP Server
|
||||
|
||||
<!--[]-->
|
||||
|
||||
---
|
||||
|
||||
- [Overview](#overview)
|
||||
- [Features](#features)
|
||||
- [Install](#install)
|
||||
- [Usage](#usage)
|
||||
- [API](#api)
|
||||
- [Events](#events)
|
||||
- [File System](#file-system)
|
||||
- [API](#api)
|
||||
- [Events](#events)
|
||||
- [Supported Commands](#supported-commands)
|
||||
- [File System](#file-system)
|
||||
- [Contributing](#contributing)
|
||||
- [License](#license)
|
||||
|
||||
## Overview
|
||||
> `ftp-srv` is designed to be easy, exensible, and modern.
|
||||
> Configuration is very minimal for a basic FTP server,
|
||||
but can easily grow to fit a larger scale project.
|
||||
`ftp-srv` is a modern and extensible FTP server designed to be simple yet configurable.
|
||||
|
||||
## Features
|
||||
- Supports passive and active connections
|
||||
- Allows extensible [file systems](#file-system) on a per connection basis
|
||||
- Extensible [file systems](#file-system) per connection
|
||||
- Passive and active transfers
|
||||
- [Explicit](https://en.wikipedia.org/wiki/FTPS#Explicit) & [Implicit](https://en.wikipedia.org/wiki/FTPS#Implicit) TLS connections
|
||||
|
||||
## Install
|
||||
`npm install ftp-srv --save`
|
||||
@@ -33,9 +37,10 @@ but can easily grow to fit a larger scale project.
|
||||
// Quick start
|
||||
|
||||
const FtpSvr = require('ftp-srv');
|
||||
const ftpServer = new FtpSvr(url, [{ options ... }]);
|
||||
const ftpServer = new FtpSvr('ftp://0.0.0.0:9876', { options ... });
|
||||
|
||||
ftpServer.on('...', (data, resolve, reject) => { ... })
|
||||
ftpServer.on('login', (data, resolve, reject) => { ... });
|
||||
...
|
||||
|
||||
ftpServer.listen()
|
||||
.then(() => { ... });
|
||||
@@ -43,127 +48,158 @@ ftpServer.listen()
|
||||
|
||||
## API
|
||||
|
||||
#### new FtpSrv(url, [options])
|
||||
### `new FtpSrv(url, [{options}])`
|
||||
#### url
|
||||
[URL string](https://nodejs.org/api/url.html#url_url_strings_and_url_objects) indicating the protocol, hostname, and port to listen on for connections.
|
||||
Supported protocols:
|
||||
- `ftp` Plain FTP
|
||||
- `ftps` Implicit FTP over TLS
|
||||
|
||||
- __url__ :: `ftp://127.0.0.1:21`
|
||||
- A full href url, indicating the protocol, and external IP with port to listen for connections.
|
||||
- Supported protocols:
|
||||
- `ftp`
|
||||
- To accept external connections, the hostname must be the box's external IP address. This can be fetched automatically by setting the hostname to `0.0.0.0`.
|
||||
- __options__ :: `{}`
|
||||
- __pasv_range__ :: `22`
|
||||
- Starting port or min - max range to accept passive connections
|
||||
- Ports will be queried for an unused port in the range to use for the connection.
|
||||
- If none are found, the connection cannot be established
|
||||
- If an integer is supplied: will indicate the minimum allowable port
|
||||
- If a range is defined (`3000-3100`): only ports within that range will be used
|
||||
- __anonymous__ :: `false`
|
||||
- If true, will authenticate connections after passing the `USER` command. Passwords will not be required.
|
||||
- __blacklist__ :: `[]`
|
||||
- Array of commands to be blacklisted globally
|
||||
- `['RMD', 'RNFR', 'RNTO']`
|
||||
- A connection sending one of these commands will be replied with code `502`
|
||||
- __whitelist__ :: `[]`
|
||||
- If set, only commands within this array are allowed
|
||||
- A connection sending any other command will be replied to with code `502`
|
||||
- __file_format__ :: `ls`
|
||||
- Set the format to use for file stat queries, such as `LIST`
|
||||
- Possible values include:
|
||||
- `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 a function as the parameter to use your own
|
||||
- Only one argument is passed in: a node [file stat](https://nodejs.org/api/fs.html#fs_class_fs_stats) object with additional file `name` parameter
|
||||
- __log__ :: `bunyan.createLogger()`
|
||||
- A [bunyan logger](https://github.com/trentm/node-bunyan) instance
|
||||
- By default, one is created, but a custom instance can be passed in as well
|
||||
_Note:_ The hostname must be the external IP address to accept external connections. Setting the hostname to `0.0.0.0` will automatically set the external IP.
|
||||
__Default:__ `"ftp://127.0.0.1:21"`
|
||||
|
||||
#### options
|
||||
|
||||
##### `pasv_range`
|
||||
A starting port (eg `8000`) or a range (eg `"8000-9000"`) to accept passive connections.
|
||||
This range is then queried for an available port to use when required.
|
||||
__Default:__ `22`
|
||||
|
||||
##### `greeting`
|
||||
A human readable array of lines or string to send when a client connects.
|
||||
__Default:__ `null`
|
||||
|
||||
##### `tls`
|
||||
Node [TLS secure context object](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options) used for implicit (`ftps` protocol) or explicit (`AUTH TLS`) connections.
|
||||
__Default:__ `false`
|
||||
|
||||
##### `anonymous`
|
||||
If true, will allow clients to authenticate using the username `anonymous`, not requiring a password from the user.
|
||||
Can also set as a string which allows users to authenticate using the username provided.
|
||||
The `login` event is then sent with the provided username and `@anonymous` as the password.
|
||||
__Default:__ `false`
|
||||
|
||||
##### `blacklist`
|
||||
Array of commands that are not allowed.
|
||||
Response code `502` is sent to clients sending one of these commands.
|
||||
__Example:__ `['RMD', 'RNFR', 'RNTO']` will not allow users to delete directories or rename any files.
|
||||
__Default:__ `[]`
|
||||
|
||||
##### `whitelist`
|
||||
Array of commands that are only allowed.
|
||||
Response code `502` is sent to clients sending any other command.
|
||||
__Default:__ `[]`
|
||||
|
||||
##### `file_format`
|
||||
Sets the format to use for file stat queries such as `LIST`.
|
||||
__Default:__ `"ls"`
|
||||
__Allowable 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 () {}` A custom function returning a format or promise for one.
|
||||
- Only one argument is passed in: a node [file stat](https://nodejs.org/api/fs.html#fs_class_fs_stats) object with additional file `name` parameter
|
||||
|
||||
##### `log`
|
||||
A [bunyan logger](https://github.com/trentm/node-bunyan) instance. Created by default.
|
||||
|
||||
## Events
|
||||
|
||||
#### "login" ({connection, username, password}, resolve, reject)
|
||||
> Occurs after `PASS` command is set, or after `USER` if `anonymous` is `true`
|
||||
The `FtpSvr` class extends the [node net.Server](https://nodejs.org/api/net.html#net_class_net_server). Some custom events can be resolved or rejected, such as `login`.
|
||||
|
||||
- __connection__
|
||||
- Instance of the FTP client
|
||||
- __username__
|
||||
- Username provided in the `USER` command
|
||||
- __password__
|
||||
- Password provided in the `PASS` command
|
||||
- Only provided if `anonymous` is set to `false`
|
||||
- __resolve ({fs, cwd, blacklist, whitelist})__
|
||||
- __fs__ _[optional]_
|
||||
- Optional file system class for connection to use
|
||||
- See [File System](#file-system) for implementation details
|
||||
- __cwd__ _[optional]_
|
||||
- If `fs` not provided, will set the starting directory for the connection
|
||||
- __blacklist__ _[optional]_
|
||||
- Commands that are forbidden for this connection only
|
||||
- __whitelist__ _[optional]_
|
||||
- If set, this connection will only be able to use the provided commands
|
||||
- __reject (error)__
|
||||
- __error__
|
||||
- Error object
|
||||
### `login`
|
||||
```js
|
||||
on('login', {connection, username, password}, resolve, reject) => { ... }
|
||||
```
|
||||
|
||||
Occurs when a client is attempting to login. Here you can resolve the login request by username and password.
|
||||
|
||||
`connection` [client class object](src/connection.js)
|
||||
`username` string of username from `USER` command
|
||||
`password` string of password from `PASS` command
|
||||
`resolve` takes an object of arguments:
|
||||
- `fs`
|
||||
- Set a custom file system class for this connection to use.
|
||||
- See [File System](#file-system) for implementation details.
|
||||
- `root`
|
||||
- If `fs` is not provided, this will set the root directory for the connection.
|
||||
- The user cannot traverse lower than this directory.
|
||||
- `cwd`
|
||||
- If `fs` is not provided, will set the starting directory for the connection
|
||||
- This is relative to the `root` directory.
|
||||
- `blacklist`
|
||||
- Commands that are forbidden for only this connection
|
||||
- `whitelist`
|
||||
- If set, this connection will only be able to use the provided commands
|
||||
|
||||
`reject` takes an error object
|
||||
|
||||
### `client-error`
|
||||
```js
|
||||
on('client-error', {connection, context, error}) => { ... }
|
||||
```
|
||||
|
||||
Occurs when an error arises in the client connection.
|
||||
|
||||
`connection` [client class object](src/connection.js)
|
||||
`context` string of where the error occured
|
||||
`error` error object
|
||||
|
||||
## Supported Commands
|
||||
|
||||
See the [command registry](src/commands/registration) for a list of all implemented FTP commands.
|
||||
|
||||
## File System
|
||||
> The default file system can be overriden to use your own implementation. This can allow for virtual file systems and more.
|
||||
> Each connection can be given it's own file system depending on the user.
|
||||
The default file system can be overwritten to use your own implementation.
|
||||
This can allow for virtual file systems, and more.
|
||||
Each connection can set it's own file system based on the user.
|
||||
|
||||
#### Functions
|
||||
`currentDirectory()`
|
||||
Returns a string of the current working directory
|
||||
Custom file systems can implement the following variables depending on the developers needs.
|
||||
|
||||
> Used in: `PWD`
|
||||
### Methods
|
||||
#### [`currentDirectory()`](src/fs.js#L29)
|
||||
Returns a string of the current working directory
|
||||
__Used in:__ `PWD`
|
||||
|
||||
`get(fileName)`
|
||||
Returns a file stat object of file or directory
|
||||
#### [`get(fileName)`](src/fs.js#L33)
|
||||
Returns a file stat object of file or directory
|
||||
__Used in:__ `STAT`, `SIZE`, `RNFR`, `MDTM`
|
||||
|
||||
> Used in: `STAT`, `SIZE`, `RNFR`, `MDTM`
|
||||
#### [`list(path)`](src/fs.js#L39)
|
||||
Returns array of file and directory stat objects
|
||||
__Used in:__ `LIST`, `STAT`
|
||||
|
||||
`list(path)`
|
||||
Returns array of file and directory stat objects
|
||||
#### [`chdir(path)`](src/fs.js#L56)
|
||||
Returns new directory relative to current directory
|
||||
__Used in:__ `CWD`, `CDUP`
|
||||
|
||||
> Used in `LIST`, `STAT`
|
||||
#### [`mkdir(path)`](src/fs.js#L96)
|
||||
Returns a path to a newly created directory
|
||||
__Used in:__ `MKD`
|
||||
|
||||
`chdir(path)`
|
||||
Returns new directory relative to cwd
|
||||
|
||||
> Used in `CWD`, `CDUP`
|
||||
|
||||
`mkdir(path)`
|
||||
Return a path to a newly created directory
|
||||
|
||||
> Used in `MKD`
|
||||
|
||||
`write(fileName, options)`
|
||||
#### [`write(fileName, {append = false})`](src/fs.js#L68)
|
||||
Returns a writable stream
|
||||
Options:
|
||||
`append` if true, append to existing file
|
||||
Options: `append` if true, append to existing file
|
||||
__Used in:__ `STOR`, `APPE`
|
||||
|
||||
> Used in `STOR`, `APPE`
|
||||
#### [`read(fileName)`](src/fs.js#L75)
|
||||
Returns a readable stream
|
||||
__Used in:__ `RETR`
|
||||
|
||||
`read(fileName)`
|
||||
Returns a readable stream
|
||||
#### [`delete(path)`](src/fs.js#L87)
|
||||
Delete a file or directory
|
||||
__Used in:__ `DELE`
|
||||
|
||||
> Used in `RETR`
|
||||
#### [`rename(from, to)`](src/fs.js#L102)
|
||||
Rename a file or directory
|
||||
__Used in:__ `RNFR`, `RNTO`
|
||||
|
||||
`delete(path)`
|
||||
Delete a file or directory
|
||||
#### [`chmod(path)`](src/fs.js#L108)
|
||||
Modify a file or directory's permissions
|
||||
__Used in:__ `SITE CHMOD`
|
||||
|
||||
> Used in `DELE`
|
||||
|
||||
`rename(from, to)`
|
||||
Rename a file or directory
|
||||
|
||||
> Used in `RNFR`, `RNTO`
|
||||
|
||||
`chmod(path)`
|
||||
Modify a file or directory's permissions
|
||||
|
||||
> Used in `SITE CHMOD`
|
||||
|
||||
`getUniqueName()`
|
||||
Return a unique file name to write to
|
||||
|
||||
> Used in `STOU`
|
||||
#### [`getUniqueName()`](src/fs.js#L113)
|
||||
Returns a unique file name to write to
|
||||
__Used in:__ `STOU`
|
||||
|
||||
<!--[RM_CONTRIBUTING]-->
|
||||
## Contributing
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// Use JS to support loading of threshold data from external file
|
||||
var coverageConfig = {
|
||||
instrumentation: {
|
||||
root: 'src/'
|
||||
root: 'src/',
|
||||
excludes: ['errors.js']
|
||||
},
|
||||
check: require('./thresholds.json'),
|
||||
reporting: {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
test/**/*.spec.js
|
||||
--reporter list
|
||||
--no-timeouts
|
||||
--reporter mocha-pretty-bunyan-nyan
|
||||
--ui bdd
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
{
|
||||
"global": {
|
||||
"statements": 70,
|
||||
"branches": 60,
|
||||
"functions": 80,
|
||||
"lines": 80
|
||||
"statements": 90,
|
||||
"branches": 80,
|
||||
"functions": 90,
|
||||
"lines": 90
|
||||
},
|
||||
"each": {
|
||||
"statements": 0,
|
||||
"branches": 0,
|
||||
"functions": 0,
|
||||
"lines": 0
|
||||
"statements": 70,
|
||||
"branches": 40,
|
||||
"functions": 60,
|
||||
"lines": 70
|
||||
}
|
||||
}
|
||||
|
||||
43
logo.html
Normal file
43
logo.html
Normal file
@@ -0,0 +1,43 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700" rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
|
||||
rel="stylesheet">
|
||||
<style>
|
||||
body {
|
||||
padding: 0px;
|
||||
}
|
||||
h1 {
|
||||
display: flex;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-size: 73px;
|
||||
font-family: 'Source Code Pro', monospace;
|
||||
font-weight: bold;
|
||||
line-height: 0.9em;
|
||||
letter-spacing: -3px;
|
||||
color: #444;
|
||||
text-shadow:
|
||||
1px 0px 1px #ccc, 0px 1px 1px #eee,
|
||||
2px 1px 1px #ccc, 1px 2px 1px #eee,
|
||||
3px 2px 1px #ccc, 2px 3px 1px #eee,
|
||||
4px 3px 1px #ccc, 3px 4px 1px #eee,
|
||||
5px 4px 1px #ccc, 4px 5px 1px #eee,
|
||||
6px 5px 1px #ccc, 5px 6px 1px #eee,
|
||||
7px 6px 1px #ccc;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
h1 > i {
|
||||
font-size: 42px !important;
|
||||
color: #03A9F4;
|
||||
align-self: center;
|
||||
padding-top: 15px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1><i class="material-icons">markunread_mailbox</i>ftp-srv</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -14,7 +14,7 @@
|
||||
"main": "src/index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/stewarttylerr/ftp-srv"
|
||||
"url": "https://github.com/trs/ftp-srv"
|
||||
},
|
||||
"scripts": {
|
||||
"pre-release": "npm-run-all verify test:coverage build ",
|
||||
@@ -47,9 +47,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"bunyan": "^1.8.9",
|
||||
"date-fns": "^1.28.2",
|
||||
"lodash": "^4.17.4",
|
||||
"minimist-string": "^1.0.2",
|
||||
"moment": "^2.18.1",
|
||||
"uuid": "^3.0.1",
|
||||
"when": "^3.7.8"
|
||||
},
|
||||
@@ -68,6 +67,7 @@
|
||||
"husky": "0.13.1",
|
||||
"istanbul": "0.4.5",
|
||||
"mocha": "3.2.0",
|
||||
"mocha-pretty-bunyan-nyan": "^1.0.4",
|
||||
"npm-run-all": "4.0.1",
|
||||
"rimraf": "2.5.4",
|
||||
"semantic-release": "^6.3.2",
|
||||
|
||||
@@ -11,9 +11,25 @@ class FtpCommands {
|
||||
this.whitelist = _.get(this.connection, 'server.options.whitelist', []).map(cmd => _.upperCase(cmd));
|
||||
}
|
||||
|
||||
parse(message) {
|
||||
const [directive, ...args] = message.replace(/"/g, '').split(' ');
|
||||
const command = {
|
||||
directive: _.chain(directive).trim().toUpper().value(),
|
||||
arg: _.compact(args).join(' ') || null,
|
||||
raw: message
|
||||
};
|
||||
return command;
|
||||
}
|
||||
|
||||
handle(command) {
|
||||
const log = this.connection.log.child({command});
|
||||
log.trace('Handle command');
|
||||
if (typeof command === 'string') command = this.parse(command);
|
||||
|
||||
// Obfuscate password from logs
|
||||
const logCommand = _.clone(command);
|
||||
if (logCommand.directive === 'PASS') logCommand.arg = '********';
|
||||
|
||||
const log = this.connection.log.child({directive: command.directive});
|
||||
log.trace({command: logCommand}, 'Handle command');
|
||||
|
||||
if (!REGISTRY.hasOwnProperty(command.directive)) {
|
||||
return this.connection.reply(402, 'Command not allowed');
|
||||
|
||||
@@ -5,6 +5,6 @@ module.exports = {
|
||||
handler: function (args) {
|
||||
return stor.call(this, args);
|
||||
},
|
||||
syntax: '{{cmd}} [path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Append to a file'
|
||||
};
|
||||
|
||||
@@ -1,27 +1,41 @@
|
||||
const _ = require('lodash');
|
||||
const tls = require('tls');
|
||||
|
||||
module.exports = {
|
||||
directive: 'AUTH',
|
||||
handler: function ({command} = {}) {
|
||||
const method = _.upperCase(command._[1]);
|
||||
const method = _.upperCase(command.arg);
|
||||
|
||||
switch (method) {
|
||||
case 'TLS': return handleTLS.call(this);
|
||||
case 'SSL': return handleSSL.call(this);
|
||||
default: return this.reply(504);
|
||||
}
|
||||
},
|
||||
syntax: '{{cmd}} [type]',
|
||||
syntax: '{{cmd}} <type>',
|
||||
description: 'Set authentication mechanism',
|
||||
flags: {
|
||||
no_auth: true
|
||||
no_auth: true,
|
||||
feat: 'AUTH TLS'
|
||||
}
|
||||
};
|
||||
|
||||
function handleTLS() {
|
||||
return this.reply(504);
|
||||
}
|
||||
if (!this.server._tls) return this.reply(504);
|
||||
|
||||
function handleSSL() {
|
||||
return this.reply(504);
|
||||
return this.reply(234)
|
||||
.then(() => {
|
||||
const secureContext = tls.createSecureContext(this.server._tls);
|
||||
const secureSocket = new tls.TLSSocket(this.commandSocket, {
|
||||
isServer: true,
|
||||
secureContext
|
||||
});
|
||||
['data', 'timeout', 'end', 'close', 'drain', 'error'].forEach(event => {
|
||||
function forwardEvent() {
|
||||
this.emit.apply(this, arguments);
|
||||
}
|
||||
secureSocket.on(event, forwardEvent.bind(this.commandSocket, event));
|
||||
});
|
||||
this.commandSocket = secureSocket;
|
||||
this.secure = true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ const cwd = require('./cwd').handler;
|
||||
module.exports = {
|
||||
directive: ['CDUP', 'XCUP'],
|
||||
handler: function (args) {
|
||||
args.command._ = [args.command._[0], '..'];
|
||||
args.command.arg = '..';
|
||||
return cwd.call(this, args);
|
||||
},
|
||||
syntax: '{{cmd}}',
|
||||
|
||||
@@ -7,7 +7,7 @@ module.exports = {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.chdir) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
return when.try(this.fs.chdir.bind(this.fs), command._[1])
|
||||
return when.try(this.fs.chdir.bind(this.fs), command.arg)
|
||||
.then(cwd => {
|
||||
const path = cwd ? `"${escapePath(cwd)}"` : undefined;
|
||||
return this.reply(250, path);
|
||||
@@ -17,6 +17,6 @@ module.exports = {
|
||||
return this.reply(550, err.message);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}}[path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Change working directory'
|
||||
};
|
||||
|
||||
@@ -6,15 +6,15 @@ module.exports = {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.delete) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
return when.try(this.fs.delete.bind(this.fs), command._[1])
|
||||
return when.try(this.fs.delete.bind(this.fs), command.arg)
|
||||
.then(() => {
|
||||
return this.reply(250);
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(550);
|
||||
return this.reply(550, err.message);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Delete file'
|
||||
};
|
||||
|
||||
22
src/commands/registration/eprt.js
Normal file
22
src/commands/registration/eprt.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const _ = require('lodash');
|
||||
const ActiveConnector = require('../../connector/active');
|
||||
|
||||
const FAMILY = {
|
||||
1: 4,
|
||||
2: 6
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
directive: 'EPRT',
|
||||
handler: function ({command} = {}) {
|
||||
this.connector = new ActiveConnector(this);
|
||||
const [protocol, ip, port] = _.compact(command.arg.split('|'));
|
||||
const family = FAMILY[protocol];
|
||||
if (!family) return this.reply(502, 'Unknown network protocol');
|
||||
|
||||
return this.connector.setupConnection(ip, port, family)
|
||||
.then(() => this.reply(200));
|
||||
},
|
||||
syntax: '{{cmd}} |<protocol>|<address>|<port>|',
|
||||
description: 'Specifies an address and port to which the server should connect'
|
||||
};
|
||||
16
src/commands/registration/epsv.js
Normal file
16
src/commands/registration/epsv.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const PassiveConnector = require('../../connector/passive');
|
||||
|
||||
module.exports = {
|
||||
directive: 'EPSV',
|
||||
handler: function () {
|
||||
this.connector = new PassiveConnector(this);
|
||||
return this.connector.setupServer()
|
||||
.then(server => {
|
||||
const {port} = server.address();
|
||||
|
||||
return this.reply(229, `EPSV OK (|||${port}|)`);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [<protocol>]',
|
||||
description: 'Initiate passive mode'
|
||||
};
|
||||
@@ -11,7 +11,9 @@ module.exports = {
|
||||
return feats;
|
||||
}, [])
|
||||
.map(feat => ` ${feat}`);
|
||||
return this.reply(211, 'Extensions supported', ...features, 'END');
|
||||
return features.length
|
||||
? this.reply(211, 'Extensions supported', ...features, 'End')
|
||||
: this.reply(211, 'No features');
|
||||
},
|
||||
syntax: '{{cmd}}',
|
||||
description: 'Get the feature list implemented by the server',
|
||||
|
||||
@@ -4,7 +4,7 @@ module.exports = {
|
||||
directive: 'HELP',
|
||||
handler: function ({command} = {}) {
|
||||
const registry = require('../registry');
|
||||
const directive = _.upperCase(command._[1]);
|
||||
const directive = _.upperCase(command.arg);
|
||||
if (directive) {
|
||||
if (!registry.hasOwnProperty(directive)) return this.reply(502, `Unknown command ${directive}.`);
|
||||
|
||||
@@ -16,7 +16,7 @@ module.exports = {
|
||||
return this.reply(211, 'Supported commands:', ...supportedCommands, 'Use "HELP [command]" for syntax help.');
|
||||
}
|
||||
},
|
||||
syntax: '{{cmd}} [command(optional)]',
|
||||
syntax: '{{cmd}} [<command>]',
|
||||
description: 'Returns usage documentation on a command if specified, else a general help document is returned',
|
||||
flags: {
|
||||
no_auth: true
|
||||
|
||||
@@ -13,7 +13,7 @@ module.exports = {
|
||||
const simple = command.directive === 'NLST';
|
||||
|
||||
let dataSocket;
|
||||
const directory = command._[1] || '.';
|
||||
const directory = command.arg || '.';
|
||||
return this.connector.waitForConnection()
|
||||
.then(socket => {
|
||||
this.commandSocket.pause();
|
||||
@@ -48,13 +48,13 @@ module.exports = {
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(err.code || 451, err.message || 'No directory');
|
||||
return this.reply(451, err.message || 'No directory');
|
||||
})
|
||||
.finally(() => {
|
||||
this.connector.end();
|
||||
this.commandSocket.resume();
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [path(optional)]',
|
||||
syntax: '{{cmd}} [<path>]',
|
||||
description: 'Returns information of a file or directory if specified, else information of the current working directory is returned'
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const when = require('when');
|
||||
const format = require('date-fns/format');
|
||||
const moment = require('moment');
|
||||
|
||||
module.exports = {
|
||||
directive: 'MDTM',
|
||||
@@ -7,17 +7,17 @@ module.exports = {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.get) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
return when.try(this.fs.get.bind(this.fs), command._[1])
|
||||
return when.try(this.fs.get.bind(this.fs), command.arg)
|
||||
.then(fileStat => {
|
||||
const modificationTime = format(fileStat.mtime, 'YYYYMMDDHHmmss.SSS');
|
||||
const modificationTime = moment.utc(fileStat.mtime).format('YYYYMMDDHHmmss.SSS');
|
||||
return this.reply(213, modificationTime);
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(550);
|
||||
return this.reply(550, err.message);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Return the last-modified time of a specified file',
|
||||
flags: {
|
||||
feat: 'MDTM'
|
||||
|
||||
@@ -7,16 +7,16 @@ module.exports = {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.mkdir) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
return when.try(this.fs.mkdir.bind(this.fs), command._[1])
|
||||
return when.try(this.fs.mkdir.bind(this.fs), command.arg)
|
||||
.then(dir => {
|
||||
const path = dir ? `"${escapePath(dir)}"` : undefined;
|
||||
return this.reply(257, path);
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(550);
|
||||
return this.reply(550, err.message);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}}[path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Make directory'
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
module.exports = {
|
||||
directive: 'MODE',
|
||||
handler: function ({command} = {}) {
|
||||
return this.reply(/^S$/i.test(command._[1]) ? 200 : 504);
|
||||
return this.reply(/^S$/i.test(command.arg) ? 200 : 504);
|
||||
},
|
||||
syntax: '{{cmd}} [mode]',
|
||||
syntax: '{{cmd}} <mode>',
|
||||
description: 'Sets the transfer mode (Stream, Block, or Compressed)',
|
||||
flags: {
|
||||
obsolete: true
|
||||
|
||||
@@ -5,6 +5,6 @@ module.exports = {
|
||||
handler: function (args) {
|
||||
return list.call(this, args);
|
||||
},
|
||||
syntax: '{{cmd}} [path(optional)]',
|
||||
syntax: '{{cmd}} [<path>]',
|
||||
description: 'Returns a list of file names in a specified directory'
|
||||
};
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
const _ = require('lodash');
|
||||
|
||||
module.exports = {
|
||||
directive: 'PASS',
|
||||
handler: function ({log, command} = {}) {
|
||||
if (!this.username) return this.reply(503);
|
||||
if (this.username && this.authenticated &&
|
||||
_.get(this, 'server.options.anonymous') === true) return this.reply(230);
|
||||
if (this.authenticated) return this.reply(202);
|
||||
|
||||
// 332 : require account name (ACCT)
|
||||
|
||||
const password = command._[1];
|
||||
const password = command.arg;
|
||||
if (!password) return this.reply(501, 'Must provide password');
|
||||
return this.login(this.username, password)
|
||||
.then(() => {
|
||||
return this.reply(230);
|
||||
@@ -19,7 +17,7 @@ module.exports = {
|
||||
return this.reply(530, err.message || 'Authentication failed');
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [password]',
|
||||
syntax: '{{cmd}} <password>',
|
||||
description: 'Authentication password',
|
||||
flags: {
|
||||
no_auth: true
|
||||
|
||||
13
src/commands/registration/pbsz.js
Normal file
13
src/commands/registration/pbsz.js
Normal file
@@ -0,0 +1,13 @@
|
||||
module.exports = {
|
||||
directive: 'PBSZ',
|
||||
handler: function ({command} = {}) {
|
||||
if (!this.server._tls || !this.secure) return this.reply(202, 'Not suppored');
|
||||
this.bufferSize = parseInt(command.arg, 10);
|
||||
return this.reply(200, this.bufferSize === 0 ? 'OK' : 'Buffer too large: PBSZ=0');
|
||||
},
|
||||
syntax: '{{cmd}}',
|
||||
description: 'Protection Buffer Size',
|
||||
flags: {
|
||||
no_auth: true
|
||||
}
|
||||
};
|
||||
@@ -5,7 +5,7 @@ module.exports = {
|
||||
directive: 'PORT',
|
||||
handler: function ({command} = {}) {
|
||||
this.connector = new ActiveConnector(this);
|
||||
const rawConnection = _.get(command, '_[1]', '').split(',');
|
||||
const rawConnection = _.get(command, 'arg', '').split(',');
|
||||
if (rawConnection.length !== 6) return this.reply(425);
|
||||
|
||||
const ip = rawConnection.slice(0, 4).join('.');
|
||||
@@ -15,6 +15,6 @@ module.exports = {
|
||||
return this.connector.setupConnection(ip, port)
|
||||
.then(() => this.reply(200));
|
||||
},
|
||||
syntax: '{{cmd}} x,x,x,x,y,y',
|
||||
syntax: '{{cmd}} <x>,<x>,<x>,<x>,<y>,<y>',
|
||||
description: 'Specifies an address and port to which the server should connect'
|
||||
};
|
||||
|
||||
22
src/commands/registration/prot.js
Normal file
22
src/commands/registration/prot.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const _ = require('lodash');
|
||||
|
||||
module.exports = {
|
||||
directive: 'PROT',
|
||||
handler: function ({command} = {}) {
|
||||
if (!this.server._tls || !this.secure) return this.reply(202, 'Not suppored');
|
||||
if (!this.bufferSize && typeof this.bufferSize !== 'number') return this.reply(503);
|
||||
|
||||
switch (_.toUpper(command.arg)) {
|
||||
case 'P': return this.reply(200, 'OK');
|
||||
case 'C':
|
||||
case 'S':
|
||||
case 'E': return this.reply(536, 'Not supported');
|
||||
default: return this.reply(504);
|
||||
}
|
||||
},
|
||||
syntax: '{{cmd}}',
|
||||
description: 'Data Channel Protection Level',
|
||||
flags: {
|
||||
no_auth: true
|
||||
}
|
||||
};
|
||||
@@ -12,7 +12,7 @@ module.exports = {
|
||||
this.commandSocket.pause();
|
||||
dataSocket = socket;
|
||||
})
|
||||
.then(() => when.try(this.fs.read.bind(this.fs), command._[1]))
|
||||
.then(() => when.try(this.fs.read.bind(this.fs), command.arg))
|
||||
.then(stream => {
|
||||
return when.promise((resolve, reject) => {
|
||||
dataSocket.on('error', err => stream.emit('error', err));
|
||||
@@ -29,13 +29,13 @@ module.exports = {
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(551);
|
||||
return this.reply(551, err.message);
|
||||
})
|
||||
.finally(() => {
|
||||
this.connector.end();
|
||||
this.commandSocket.resume();
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Retrieve a copy of the file'
|
||||
};
|
||||
|
||||
@@ -5,6 +5,6 @@ module.exports = {
|
||||
handler: function (args) {
|
||||
return dele.call(this, args);
|
||||
},
|
||||
syntax: '{{cmd}} [path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Remove a directory'
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@ module.exports = {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.get) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
const fileName = command._[1];
|
||||
const fileName = command.arg;
|
||||
return when.try(this.fs.get.bind(this.fs), fileName)
|
||||
.then(() => {
|
||||
this.renameFrom = fileName;
|
||||
@@ -14,9 +14,9 @@ module.exports = {
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(550);
|
||||
return this.reply(550, err.message);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [name]',
|
||||
syntax: '{{cmd}} <name>',
|
||||
description: 'Rename from'
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ module.exports = {
|
||||
if (!this.fs.rename) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
const from = this.renameFrom;
|
||||
const to = command._[1];
|
||||
const to = command.arg;
|
||||
|
||||
return when.try(this.fs.rename.bind(this.fs), from, to)
|
||||
.then(() => {
|
||||
@@ -17,12 +17,12 @@ module.exports = {
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(550);
|
||||
return this.reply(550, err.message);
|
||||
})
|
||||
.finally(() => {
|
||||
delete this.renameFrom;
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [name]',
|
||||
syntax: '{{cmd}} <name>',
|
||||
description: 'Rename to'
|
||||
};
|
||||
|
||||
@@ -4,7 +4,8 @@ module.exports = function ({log, command} = {}) {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.chmod) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
const [, mode, fileName] = command._;
|
||||
const [mode, ...fileNameParts] = command.arg.split(' ');
|
||||
const fileName = fileNameParts.join(' ');
|
||||
return when.try(this.fs.chmod.bind(this.fs), fileName, parseInt(mode, 8))
|
||||
.then(() => {
|
||||
return this.reply(200);
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
const _ = require('lodash');
|
||||
const when = require('when');
|
||||
|
||||
module.exports = {
|
||||
directive: 'SITE',
|
||||
handler: function ({log, command} = {}) {
|
||||
const registry = require('./registry');
|
||||
let [, subverb, ...subparameters] = command._;
|
||||
subverb = _.upperCase(subverb);
|
||||
const subLog = log.child({subverb});
|
||||
const subCommand = this.commands.parse(command.arg);
|
||||
const subLog = log.child({subverb: subCommand.directive});
|
||||
|
||||
if (!registry.hasOwnProperty(subverb)) return this.reply(502);
|
||||
if (!registry.hasOwnProperty(subCommand.directive)) return this.reply(502);
|
||||
|
||||
const subCommand = {
|
||||
_: [subverb, ...subparameters],
|
||||
directive: subverb
|
||||
};
|
||||
const handler = registry[subverb].handler.bind(this);
|
||||
const handler = registry[subCommand.directive].handler.bind(this);
|
||||
return when.try(handler, { log: subLog, command: subCommand });
|
||||
},
|
||||
syntax: '{{cmd}} [subVerb] [subParams]',
|
||||
syntax: '{{cmd}} <subVerb> [...<subParams>]',
|
||||
description: 'Sends site specific commands to remote server'
|
||||
};
|
||||
|
||||
@@ -6,16 +6,16 @@ module.exports = {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.get) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
return when.try(this.fs.get.bind(this.fs), command._[1])
|
||||
return when.try(this.fs.get.bind(this.fs), command.arg)
|
||||
.then(fileStat => {
|
||||
return this.reply(213, {message: fileStat.size});
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(550);
|
||||
return this.reply(550, err.message);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Return the size of a file',
|
||||
flags: {
|
||||
feat: 'SIZE'
|
||||
|
||||
@@ -6,7 +6,7 @@ module.exports = {
|
||||
directive: 'STAT',
|
||||
handler: function (args = {}) {
|
||||
const {log, command} = args;
|
||||
const path = _.get(command, '_[1]');
|
||||
const path = _.get(command, 'arg');
|
||||
if (path) {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.get) return this.reply(402, 'Not supported by file system');
|
||||
@@ -33,12 +33,12 @@ module.exports = {
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(450);
|
||||
return this.reply(450, err.message);
|
||||
});
|
||||
} else {
|
||||
return this.reply(211, 'Status OK');
|
||||
}
|
||||
},
|
||||
syntax: '{{cmd}} [path(optional)]',
|
||||
syntax: '{{cmd}} [<path>]',
|
||||
description: 'Returns the current status'
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ module.exports = {
|
||||
if (!this.fs.write) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
const append = command.directive === 'APPE';
|
||||
const fileName = command._[1];
|
||||
const fileName = command.arg;
|
||||
|
||||
let dataSocket;
|
||||
return this.connector.waitForConnection()
|
||||
@@ -32,13 +32,13 @@ module.exports = {
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(553);
|
||||
return this.reply(550, err.message);
|
||||
})
|
||||
.finally(() => {
|
||||
this.connector.end();
|
||||
this.commandSocket.resume();
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} [path]',
|
||||
syntax: '{{cmd}} <path>',
|
||||
description: 'Store data as a file at the server site'
|
||||
};
|
||||
|
||||
@@ -8,14 +8,14 @@ module.exports = {
|
||||
if (!this.fs) return this.reply(550, 'File system not instantiated');
|
||||
if (!this.fs.get || !this.fs.getUniqueName) return this.reply(402, 'Not supported by file system');
|
||||
|
||||
const fileName = args.command._[1];
|
||||
const fileName = args.command.arg;
|
||||
return when.try(() => {
|
||||
return when.try(this.fs.get.bind(this.fs), fileName)
|
||||
.then(() => when.try(this.fs.getUniqueName.bind(this.fs)))
|
||||
.catch(() => when.resolve(fileName));
|
||||
})
|
||||
.then(name => {
|
||||
args.command._[1] = name;
|
||||
args.command.arg = name;
|
||||
return stor.call(this, args);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
module.exports = {
|
||||
directive: 'STRU',
|
||||
handler: function ({command} = {}) {
|
||||
return this.reply(/^F$/i.test(command._[1]) ? 200 : 504);
|
||||
return this.reply(/^F$/i.test(command.arg) ? 200 : 504);
|
||||
},
|
||||
syntax: '{{cmd}} [structure]',
|
||||
syntax: '{{cmd}} <structure>',
|
||||
description: 'Set file transfer structure',
|
||||
flags: {
|
||||
obsolete: true
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const _ = require('lodash');
|
||||
|
||||
const ENCODING_TYPES = {
|
||||
A: 'utf-8',
|
||||
A: 'utf8',
|
||||
I: 'binary',
|
||||
L: 'binary'
|
||||
};
|
||||
@@ -9,12 +9,12 @@ const ENCODING_TYPES = {
|
||||
module.exports = {
|
||||
directive: 'TYPE',
|
||||
handler: function ({command} = {}) {
|
||||
const encoding = _.upperCase(command._[1]);
|
||||
const encoding = _.upperCase(command.arg);
|
||||
if (!ENCODING_TYPES.hasOwnProperty(encoding)) return this.reply(501);
|
||||
|
||||
this.encoding = ENCODING_TYPES[encoding];
|
||||
return this.reply(200);
|
||||
},
|
||||
syntax: '{{cmd}} [mode]',
|
||||
description: 'Set the transfer mode, binary (I) or utf-8 (A)'
|
||||
syntax: '{{cmd}} <mode>',
|
||||
description: 'Set the transfer mode, binary (I) or utf8 (A)'
|
||||
};
|
||||
|
||||
@@ -2,23 +2,25 @@ module.exports = {
|
||||
directive: 'USER',
|
||||
handler: function ({log, command} = {}) {
|
||||
if (this.username) return this.reply(530, 'Username already set');
|
||||
if (this.authenticated) return this.reply(230);
|
||||
|
||||
this.username = command._[1];
|
||||
if (!this.username) return this.reply(501, 'Must send username requirement');
|
||||
this.username = command.arg;
|
||||
if (!this.username) return this.reply(501, 'Must provide username');
|
||||
|
||||
if (this.server.options.anonymous === true) {
|
||||
if (this.server.options.anonymous === true && this.username === 'anonymous' ||
|
||||
this.username === this.server.options.anonymous) {
|
||||
return this.login(this.username, '@anonymous')
|
||||
.then(() => {
|
||||
return this.reply(230);
|
||||
})
|
||||
.catch(err => {
|
||||
log.error(err);
|
||||
return this.reply(530, err || 'Authentication failed');
|
||||
return this.reply(530, err.message || 'Authentication failed');
|
||||
});
|
||||
}
|
||||
return this.reply(331);
|
||||
},
|
||||
syntax: '{{cmd}} [username]',
|
||||
syntax: '{{cmd}} <username>',
|
||||
description: 'Authentication username',
|
||||
flags: {
|
||||
no_auth: true
|
||||
|
||||
@@ -20,6 +20,7 @@ const commands = [
|
||||
require('./registration/pasv'),
|
||||
require('./registration/port'),
|
||||
require('./registration/pwd'),
|
||||
require('./registration/quit'),
|
||||
require('./registration/retr'),
|
||||
require('./registration/rmd'),
|
||||
require('./registration/rnfr'),
|
||||
@@ -32,7 +33,9 @@ const commands = [
|
||||
require('./registration/stru'),
|
||||
require('./registration/syst'),
|
||||
require('./registration/type'),
|
||||
require('./registration/user')
|
||||
require('./registration/user'),
|
||||
require('./registration/pbsz'),
|
||||
require('./registration/prot')
|
||||
];
|
||||
|
||||
const registry = commands.reduce((result, cmd) => {
|
||||
|
||||
@@ -2,7 +2,6 @@ const _ = require('lodash');
|
||||
const uuid = require('uuid');
|
||||
const when = require('when');
|
||||
const sequence = require('when/sequence');
|
||||
const parseCommandString = require('minimist-string');
|
||||
|
||||
const BaseConnector = require('./connector/base');
|
||||
const FileSystem = require('./fs');
|
||||
@@ -13,27 +12,20 @@ const DEFAULT_MESSAGE = require('./messages');
|
||||
class FtpConnection {
|
||||
constructor(server, options) {
|
||||
this.server = server;
|
||||
this.commandSocket = options.socket;
|
||||
this.id = uuid.v4();
|
||||
this.log = options.log.child({id: this.id});
|
||||
this.log = options.log.child({id: this.id, ip: this.ip});
|
||||
this.commands = new Commands(this);
|
||||
this.encoding = 'utf-8';
|
||||
this.encoding = 'utf8';
|
||||
this.bufferSize = false;
|
||||
|
||||
this.connector = new BaseConnector(this);
|
||||
|
||||
this.commandSocket = options.socket;
|
||||
this.commandSocket.on('error', err => {
|
||||
this.server.server.emit('error', {connection: this, error: err});
|
||||
});
|
||||
this.commandSocket.on('data', data => {
|
||||
const messages = _.compact(data.toString('utf-8').split('\r\n'));
|
||||
const handleMessage = message => {
|
||||
const command = parseCommandString(message);
|
||||
command.directive = _.upperCase(command._[0]);
|
||||
return this.commands.handle(command);
|
||||
};
|
||||
|
||||
return sequence(messages.map(message => handleMessage.bind(this, message)));
|
||||
this.log.error(err, 'Client error');
|
||||
this.server.emit('client-error', {connection: this, context: 'commandSocket', error: err});
|
||||
});
|
||||
this.commandSocket.on('data', this._handleData.bind(this));
|
||||
this.commandSocket.on('timeout', () => {});
|
||||
this.commandSocket.on('close', () => {
|
||||
if (this.connector) this.connector.end();
|
||||
@@ -41,13 +33,25 @@ class FtpConnection {
|
||||
});
|
||||
}
|
||||
|
||||
_handleData(data) {
|
||||
const messages = _.compact(data.toString('utf8').split('\r\n'));
|
||||
this.log.trace(messages);
|
||||
return sequence(messages.map(message => this.commands.handle.bind(this.commands, message)));
|
||||
}
|
||||
|
||||
get ip() {
|
||||
try {
|
||||
return this.dataSocket ? this.dataSocket.remoteAddress : this.commandSocket.remoteAddress;
|
||||
} catch (ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
close(code = 421, message = 'Closing connection') {
|
||||
return when(() => {
|
||||
if (code) return this.reply(code, message);
|
||||
})
|
||||
.then(() => {
|
||||
if (this.commandSocket) this.commandSocket.end();
|
||||
});
|
||||
return when
|
||||
.resolve(code)
|
||||
.then(_code => _code && this.reply(_code, message))
|
||||
.then(() => this.commandSocket && this.commandSocket.end());
|
||||
}
|
||||
|
||||
login(username, password) {
|
||||
@@ -56,10 +60,10 @@ class FtpConnection {
|
||||
if (!loginListeners || !loginListeners.length) {
|
||||
if (!this.server.options.anoymous) throw new errors.GeneralError('No "login" listener setup', 500);
|
||||
} else {
|
||||
return this.server.emit('login', {connection: this, username, password});
|
||||
return this.server.emitPromise('login', {connection: this, username, password});
|
||||
}
|
||||
})
|
||||
.then(({root = '/', cwd = '/', fs, blacklist = [], whitelist = []} = {}) => {
|
||||
.then(({root, cwd, fs, blacklist = [], whitelist = []} = {}) => {
|
||||
this.authenticated = true;
|
||||
this.commands.blacklist = _.concat(this.commands.blacklist, blacklist);
|
||||
this.commands.whitelist = _.concat(this.commands.whitelist, whitelist);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const {Socket} = require('net');
|
||||
const tls = require('tls');
|
||||
const when = require('when');
|
||||
const Connector = require('./base');
|
||||
|
||||
@@ -17,7 +18,7 @@ class Active extends Connector {
|
||||
.then(() => this.dataSocket);
|
||||
}
|
||||
|
||||
setupConnection(host, port) {
|
||||
setupConnection(host, port, family = 4) {
|
||||
const closeExistingServer = () => this.dataSocket ?
|
||||
when(this.dataSocket.destroy()) :
|
||||
when.resolve();
|
||||
@@ -26,8 +27,18 @@ class Active extends Connector {
|
||||
.then(() => {
|
||||
this.dataSocket = new Socket();
|
||||
this.dataSocket.setEncoding(this.encoding);
|
||||
this.dataSocket.connect({ host, port }, () => {
|
||||
this.dataSocket.on('error', err => this.server.emit('client-error', {connection: this.connection, context: 'dataSocket', error: err}));
|
||||
this.dataSocket.connect({ host, port, family }, () => {
|
||||
this.dataSocket.pause();
|
||||
|
||||
if (this.connection.secure) {
|
||||
const secureContext = tls.createSecureContext(this.server._tls);
|
||||
const secureSocket = new tls.TLSSocket(this.dataSocket, {
|
||||
isServer: true,
|
||||
secureContext
|
||||
});
|
||||
this.dataSocket = secureSocket;
|
||||
}
|
||||
this.dataSocket.connected = true;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,14 +4,20 @@ const errors = require('../errors');
|
||||
class Connector {
|
||||
constructor(connection) {
|
||||
this.connection = connection;
|
||||
this.server = connection.server;
|
||||
this.log = connection.log;
|
||||
|
||||
this.dataSocket = null;
|
||||
this.dataServer = null;
|
||||
this.type = false;
|
||||
}
|
||||
|
||||
get log() {
|
||||
return this.connection.log;
|
||||
}
|
||||
|
||||
get server() {
|
||||
return this.connection.server;
|
||||
}
|
||||
|
||||
waitForConnection() {
|
||||
return when.reject(new errors.ConnectorError('No connector setup, send PASV or PORT'));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
const net = require('net');
|
||||
const tls = require('tls');
|
||||
const when = require('when');
|
||||
|
||||
const Connector = require('./base');
|
||||
const findPort = require('../helpers/find-port');
|
||||
const errors = require('../errors');
|
||||
@@ -11,9 +13,7 @@ class Passive extends Connector {
|
||||
}
|
||||
|
||||
waitForConnection({timeout = 5000, delay = 250} = {}) {
|
||||
if (!this.dataServer) {
|
||||
return when.reject(new errors.ConnectorError('Passive server not setup'));
|
||||
}
|
||||
if (!this.dataServer) return when.reject(new errors.ConnectorError('Passive server not setup'));
|
||||
return when.iterate(
|
||||
() => {},
|
||||
() => this.dataServer && this.dataServer.listening && this.dataSocket && this.dataSocket.connected,
|
||||
@@ -30,10 +30,7 @@ class Passive extends Connector {
|
||||
return closeExistingServer()
|
||||
.then(() => this.getPort())
|
||||
.then(port => {
|
||||
this.dataSocket = null;
|
||||
this.dataServer = net.createServer({pauseOnConnect: true});
|
||||
this.dataServer.maxConnections = 1;
|
||||
this.dataServer.on('connection', socket => {
|
||||
const connectionHandler = socket => {
|
||||
if (this.connection.commandSocket.remoteAddress !== socket.remoteAddress) {
|
||||
this.log.error({
|
||||
pasv_connection: socket.remoteAddress,
|
||||
@@ -46,14 +43,29 @@ class Passive extends Connector {
|
||||
}
|
||||
this.log.debug({port}, 'Passive connection fulfilled.');
|
||||
|
||||
this.dataSocket = socket;
|
||||
if (this.connection.secure) {
|
||||
const secureContext = tls.createSecureContext(this.server._tls);
|
||||
const secureSocket = new tls.TLSSocket(socket, {
|
||||
isServer: true,
|
||||
secureContext
|
||||
});
|
||||
this.dataSocket = secureSocket;
|
||||
} else {
|
||||
this.dataSocket = socket;
|
||||
}
|
||||
this.dataSocket.connected = true;
|
||||
this.dataSocket.setEncoding(this.connection.encoding);
|
||||
this.dataSocket.on('error', err => this.server.emit('client-error', {connection: this.connection, context: 'dataSocket', error: err}));
|
||||
this.dataSocket.on('close', () => {
|
||||
this.log.debug('Passive connection closed');
|
||||
this.end();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
this.dataSocket = null;
|
||||
this.dataServer = net.createServer({ pauseOnConnect: true }, connectionHandler);
|
||||
this.dataServer.maxConnections = 1;
|
||||
this.dataServer.on('error', err => this.server.emit('client-error', {connection: this.connection, context: 'dataServer', error: err}));
|
||||
this.dataServer.on('close', () => {
|
||||
this.log.debug('Passive server closed');
|
||||
this.dataServer = null;
|
||||
|
||||
74
src/fs.js
74
src/fs.js
@@ -8,22 +8,22 @@ const fs = whenNode.liftAll(syncFs);
|
||||
const errors = require('./errors');
|
||||
|
||||
class FileSystem {
|
||||
constructor(connection, {
|
||||
root = '/',
|
||||
cwd = '/'
|
||||
} = {}) {
|
||||
constructor(connection, { root, cwd } = {}) {
|
||||
this.connection = connection;
|
||||
this.cwd = cwd;
|
||||
this.root = root;
|
||||
this.cwd = cwd || nodePath.sep;
|
||||
this.root = root || process.cwd();
|
||||
}
|
||||
|
||||
_resolvePath(path) {
|
||||
const pathParts = {
|
||||
root: this.root,
|
||||
base: nodePath.resolve(this.cwd, path)
|
||||
_resolvePath(path = '') {
|
||||
const isFromRoot = _.startsWith(path, '/') || _.startsWith(path, nodePath.sep);
|
||||
const cwd = isFromRoot ? nodePath.sep : this.cwd || nodePath.sep;
|
||||
const serverPath = nodePath.join(nodePath.sep, cwd, path);
|
||||
const fsPath = nodePath.join(this.root, serverPath);
|
||||
|
||||
return {
|
||||
serverPath,
|
||||
fsPath
|
||||
};
|
||||
path = nodePath.format(pathParts);
|
||||
return path;
|
||||
}
|
||||
|
||||
currentDirectory() {
|
||||
@@ -31,17 +31,17 @@ class FileSystem {
|
||||
}
|
||||
|
||||
get(fileName) {
|
||||
const path = this._resolvePath(fileName);
|
||||
return fs.stat(path)
|
||||
const {fsPath} = this._resolvePath(fileName);
|
||||
return fs.stat(fsPath)
|
||||
.then(stat => _.set(stat, 'name', fileName));
|
||||
}
|
||||
|
||||
list(path = '.') {
|
||||
path = this._resolvePath(path);
|
||||
return fs.readdir(path)
|
||||
const {fsPath} = this._resolvePath(path);
|
||||
return fs.readdir(fsPath)
|
||||
.then(fileNames => {
|
||||
return when.map(fileNames, fileName => {
|
||||
const filePath = nodePath.join(path, fileName);
|
||||
const filePath = nodePath.join(fsPath, fileName);
|
||||
return fs.access(filePath, syncFs.constants.F_OK)
|
||||
.then(() => {
|
||||
return fs.stat(filePath)
|
||||
@@ -54,60 +54,60 @@ class FileSystem {
|
||||
}
|
||||
|
||||
chdir(path = '.') {
|
||||
path = this._resolvePath(path);
|
||||
return fs.stat(path)
|
||||
const {fsPath, serverPath} = this._resolvePath(path);
|
||||
return fs.stat(fsPath)
|
||||
.tap(stat => {
|
||||
if (!stat.isDirectory()) throw new errors.FileSystemError('Not a valid directory');
|
||||
})
|
||||
.then(() => {
|
||||
this.cwd = path.replace(new RegExp(`^${this.root}`), '') || '/';
|
||||
this.cwd = serverPath;
|
||||
return this.currentDirectory();
|
||||
});
|
||||
}
|
||||
|
||||
write(fileName, {append = false} = {}) {
|
||||
const path = this._resolvePath(fileName);
|
||||
const stream = syncFs.createWriteStream(path, {flags: !append ? 'w+' : 'a+'});
|
||||
stream.on('error', () => fs.unlink(path));
|
||||
const {fsPath} = this._resolvePath(fileName);
|
||||
const stream = syncFs.createWriteStream(fsPath, {flags: !append ? 'w+' : 'a+'});
|
||||
stream.on('error', () => fs.unlink(fsPath));
|
||||
return stream;
|
||||
}
|
||||
|
||||
read(fileName) {
|
||||
const path = this._resolvePath(fileName);
|
||||
return fs.stat(path)
|
||||
const {fsPath} = this._resolvePath(fileName);
|
||||
return fs.stat(fsPath)
|
||||
.tap(stat => {
|
||||
if (stat.isDirectory()) throw new errors.FileSystemError('Cannot read a directory');
|
||||
})
|
||||
.then(() => {
|
||||
const stream = syncFs.createReadStream(path, {flags: 'r'});
|
||||
const stream = syncFs.createReadStream(fsPath, {flags: 'r'});
|
||||
return stream;
|
||||
});
|
||||
}
|
||||
|
||||
delete(path) {
|
||||
path = this._resolvePath(path);
|
||||
return fs.stat(path)
|
||||
const {fsPath} = this._resolvePath(path);
|
||||
return fs.stat(fsPath)
|
||||
.then(stat => {
|
||||
if (stat.isDirectory()) return fs.rmdir(path);
|
||||
else return fs.unlink(path);
|
||||
if (stat.isDirectory()) return fs.rmdir(fsPath);
|
||||
else return fs.unlink(fsPath);
|
||||
});
|
||||
}
|
||||
|
||||
mkdir(path) {
|
||||
path = this._resolvePath(path);
|
||||
return fs.mkdir(path)
|
||||
.then(() => path);
|
||||
const {fsPath} = this._resolvePath(path);
|
||||
return fs.mkdir(fsPath)
|
||||
.then(() => fsPath);
|
||||
}
|
||||
|
||||
rename(from, to) {
|
||||
const fromPath = this._resolvePath(from);
|
||||
const toPath = this._resolvePath(to);
|
||||
const {fsPath: fromPath} = this._resolvePath(from);
|
||||
const {fsPath: toPath} = this._resolvePath(to);
|
||||
return fs.rename(fromPath, toPath);
|
||||
}
|
||||
|
||||
chmod(path, mode) {
|
||||
path = this._resolvePath(path);
|
||||
return fs.chmod(path, mode);
|
||||
const {fsPath} = this._resolvePath(path);
|
||||
return fs.chmod(fsPath, mode);
|
||||
}
|
||||
|
||||
getUniqueName() {
|
||||
|
||||
@@ -1,52 +1,54 @@
|
||||
const _ = require('lodash');
|
||||
const dateFns = require('date-fns');
|
||||
const moment = require('moment');
|
||||
const errors = require('../errors');
|
||||
|
||||
const FORMATS = {
|
||||
ls,
|
||||
ep
|
||||
};
|
||||
|
||||
module.exports = function (fileStat, format = 'ls') {
|
||||
if (typeof format === 'function') return format(fileStat);
|
||||
|
||||
const formats = {
|
||||
ls: ls,
|
||||
ep: ep
|
||||
};
|
||||
if (!formats.hasOwnProperty(format)) {
|
||||
if (!FORMATS.hasOwnProperty(format)) {
|
||||
throw new errors.FileSystemError('Bad file stat formatter');
|
||||
}
|
||||
return formats[format](fileStat);
|
||||
return FORMATS[format](fileStat);
|
||||
};
|
||||
|
||||
function ls(fileStat) {
|
||||
const now = moment.utc();
|
||||
const mtime = moment.utc(new Date(fileStat.mtime));
|
||||
const dateFormat = now.diff(mtime, 'months') < 6 ? 'MMM DD HH:mm' : 'MMM DD YYYY';
|
||||
|
||||
return [
|
||||
fileStat.mode !== null
|
||||
? [
|
||||
fileStat.isDirectory() ? 'd' : '-',
|
||||
400 & fileStat.mode ? 'r' : '-',
|
||||
200 & fileStat.mode ? 'w' : '-',
|
||||
100 & fileStat.mode ? 'x' : '-',
|
||||
40 & fileStat.mode ? 'r' : '-',
|
||||
20 & fileStat.mode ? 'w' : '-',
|
||||
10 & fileStat.mode ? 'x' : '-',
|
||||
4 & fileStat.mode ? 'r' : '-',
|
||||
2 & fileStat.mode ? 'w' : '-',
|
||||
1 & fileStat.mode ? 'x' : '-'
|
||||
].join('')
|
||||
: fileStat.isDirectory() ? 'drwxr-xr-x' : '-rwxr-xr-x',
|
||||
fileStat.mode ? [
|
||||
fileStat.isDirectory() ? 'd' : '-',
|
||||
fileStat.mode & 256 ? 'r' : '-',
|
||||
fileStat.mode & 128 ? 'w' : '-',
|
||||
fileStat.mode & 64 ? 'x' : '-',
|
||||
fileStat.mode & 32 ? 'r' : '-',
|
||||
fileStat.mode & 16 ? 'w' : '-',
|
||||
fileStat.mode & 8 ? 'x' : '-',
|
||||
fileStat.mode & 4 ? 'r' : '-',
|
||||
fileStat.mode & 2 ? 'w' : '-',
|
||||
fileStat.mode & 1 ? 'x' : '-'
|
||||
].join('') : fileStat.isDirectory() ? 'drwxr-xr-x' : '-rwxr-xr-x',
|
||||
'1',
|
||||
fileStat.uid,
|
||||
fileStat.gid,
|
||||
fileStat.uid || 1,
|
||||
fileStat.gid || 1,
|
||||
_.padStart(fileStat.size, 12),
|
||||
_.padStart(dateFns.format(fileStat.mtime, 'MMM DD HH:mm'), 12),
|
||||
_.padStart(mtime.format(dateFormat), 12),
|
||||
fileStat.name
|
||||
].join(' ');
|
||||
}
|
||||
|
||||
function ep(fileStat) {
|
||||
const facts = [
|
||||
const facts = _.compact([
|
||||
fileStat.dev && fileStat.ino ? `i${fileStat.dev.toString(16)}.${fileStat.ino.toString(16)}` : null,
|
||||
fileStat.size ? `s${fileStat.size}` : null,
|
||||
fileStat.mtime ? `m${dateFns.format(dateFns.parse(fileStat.mtime), 'X')}` : null,
|
||||
fileStat.mode ? `up${fileStat.mode.toString(8).substr(fileStat.mode.toString(8).length - 3)}` : null,
|
||||
fileStat.isDirectory() ? 'r' : '/'
|
||||
].join(',');
|
||||
fileStat.mtime ? `m${moment.utc(new Date(fileStat.mtime)).format('X')}` : null,
|
||||
fileStat.mode ? `up${(fileStat.mode & 4095).toString(8)}` : null,
|
||||
fileStat.isDirectory() ? '/' : 'r'
|
||||
]).join(',');
|
||||
return `+${facts}\t${fileStat.name}`;
|
||||
}
|
||||
|
||||
@@ -2,15 +2,15 @@ const net = require('net');
|
||||
const when = require('when');
|
||||
const errors = require('../errors');
|
||||
|
||||
module.exports = function (min = 22, max = undefined) {
|
||||
module.exports = function (min = 1, max = undefined) {
|
||||
return when.promise((resolve, reject) => {
|
||||
let port = min;
|
||||
let checkPort = min;
|
||||
let portCheckServer = net.createServer();
|
||||
portCheckServer.maxConnections = 0;
|
||||
portCheckServer.on('error', () => {
|
||||
if (!max || port < max) {
|
||||
port = port + 1;
|
||||
portCheckServer.listen(port);
|
||||
if (checkPort < 65535 && (!max || checkPort < max)) {
|
||||
checkPort = checkPort + 1;
|
||||
portCheckServer.listen(checkPort);
|
||||
} else {
|
||||
reject(new errors.GeneralError('Unable to find open port', 500));
|
||||
}
|
||||
@@ -22,6 +22,6 @@ module.exports = function (min = 22, max = undefined) {
|
||||
resolve(port);
|
||||
});
|
||||
});
|
||||
portCheckServer.listen(port);
|
||||
portCheckServer.listen(checkPort);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ module.exports = function (hostname) {
|
||||
if (response.statusCode !== 200) {
|
||||
return reject(new errors.GeneralError('Unable to resolve hostname', response.statusCode));
|
||||
}
|
||||
response.setEncoding('utf-8');
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', chunk => {
|
||||
ip += chunk;
|
||||
});
|
||||
|
||||
96
src/index.js
96
src/index.js
@@ -3,6 +3,8 @@ const when = require('when');
|
||||
const nodeUrl = require('url');
|
||||
const buyan = require('bunyan');
|
||||
const net = require('net');
|
||||
const tls = require('tls');
|
||||
const fs = require('fs');
|
||||
|
||||
const Connection = require('./connection');
|
||||
const resolveHost = require('./helpers/resolve-host');
|
||||
@@ -15,28 +17,51 @@ class FtpServer {
|
||||
pasv_range: 22,
|
||||
file_format: 'ls',
|
||||
blacklist: [],
|
||||
whitelist: []
|
||||
whitelist: [],
|
||||
greeting: null,
|
||||
tls: false
|
||||
}, options);
|
||||
this._greeting = this.setupGreeting(this.options.greeting);
|
||||
this._features = this.setupFeaturesMessage();
|
||||
this._tls = this.setupTLS(this.options.tls);
|
||||
|
||||
delete this.options.greeting;
|
||||
delete this.options.tls;
|
||||
|
||||
this.connections = {};
|
||||
this.log = this.options.log;
|
||||
this.url = nodeUrl.parse(url || 'ftp://127.0.0.1:21');
|
||||
this.server = net.createServer({pauseOnConnect: true}, socket => {
|
||||
|
||||
const serverConnectionHandler = socket => {
|
||||
let connection = new Connection(this, {log: this.log, socket});
|
||||
this.connections[connection.id] = connection;
|
||||
|
||||
socket.on('close', () => this.disconnectClient(connection.id));
|
||||
|
||||
const greeting = this.getGreetingMessage();
|
||||
const features = this.getFeaturesMessage();
|
||||
return connection.reply(220, greeting, features)
|
||||
const greeting = this._greeting || [];
|
||||
const features = this._features || 'Ready';
|
||||
return connection.reply(220, ...greeting, features)
|
||||
.finally(() => socket.resume());
|
||||
});
|
||||
this.server.on('error', err => {
|
||||
this.log.error(err);
|
||||
});
|
||||
};
|
||||
const serverOptions = _.assign(this.isTLS ? this._tls : {}, { pauseOnConnect: true });
|
||||
|
||||
this.server = (this.isTLS ? tls : net).createServer(serverOptions, serverConnectionHandler);
|
||||
this.server.on('error', err => this.log.error(err, '[Event] error'));
|
||||
if (this.isTLS) {
|
||||
this.server.on('tlsClientError', err => this.log.error(err, '[Event] tlsClientError'));
|
||||
}
|
||||
this.on = this.server.on.bind(this.server);
|
||||
this.once = this.server.once.bind(this.server);
|
||||
this.listeners = this.server.listeners.bind(this.server);
|
||||
|
||||
process.on('SIGTERM', () => this.close());
|
||||
process.on('SIGINT', () => this.close());
|
||||
process.on('SIGBREAK', () => this.close());
|
||||
process.on('SIGHUP', () => this.close());
|
||||
}
|
||||
|
||||
get isTLS() {
|
||||
return this.url.protocol === 'ftps:' && this._tls;
|
||||
}
|
||||
|
||||
listen() {
|
||||
@@ -46,25 +71,44 @@ class FtpServer {
|
||||
return when.promise((resolve, reject) => {
|
||||
this.server.listen(this.url.port, err => {
|
||||
if (err) return reject(err);
|
||||
this.log.info({port: this.url.port}, 'Listening');
|
||||
this.log.info({
|
||||
protocol: this.url.protocol.replace(/\W/g, ''),
|
||||
ip: this.url.hostname,
|
||||
port: this.url.port
|
||||
}, 'Listening');
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
emit(action, ...data) {
|
||||
emitPromise(action, ...data) {
|
||||
const defer = when.defer();
|
||||
const params = _.concat(data, [defer.resolve, defer.reject]);
|
||||
this.server.emit(action, ...params);
|
||||
return defer.promise;
|
||||
}
|
||||
|
||||
getGreetingMessage() {
|
||||
return null;
|
||||
emit(action, ...data) {
|
||||
this.server.emit(action, ...data);
|
||||
}
|
||||
|
||||
getFeaturesMessage() {
|
||||
setupTLS(_tls) {
|
||||
if (!tls) return false;
|
||||
return _.assign(_tls, {
|
||||
cert: _tls.cert ? fs.readFileSync(_tls.cert) : undefined,
|
||||
key: _tls.key ? fs.readFileSync(_tls.key) : undefined,
|
||||
ca: _tls.ca ? Array.isArray(_tls.ca) ? _tls.ca.map(_ca => fs.readFileSync(_ca)) : [fs.readFileSync(_tls.ca)] : undefined
|
||||
});
|
||||
}
|
||||
|
||||
setupGreeting(greet) {
|
||||
if (!greet) return [];
|
||||
const greeting = Array.isArray(greet) ? greet : greet.split('\n');
|
||||
return greeting;
|
||||
}
|
||||
|
||||
setupFeaturesMessage() {
|
||||
let features = [];
|
||||
if (this.options.anonymous) features.push('a');
|
||||
|
||||
@@ -75,32 +119,28 @@ class FtpServer {
|
||||
return features.length ? features.join(' ') : 'Ready';
|
||||
}
|
||||
|
||||
setGreeting(greeting) {
|
||||
if (typeof greeting === 'string') {
|
||||
this.options.greeting = greeting;
|
||||
} else {
|
||||
greeting.then(greet => {
|
||||
this.options.greeting = greet;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
disconnectClient(id) {
|
||||
return when.promise(resolve => {
|
||||
const client = this.connections[id];
|
||||
if (!client) return resolve();
|
||||
delete this.connections[id];
|
||||
client.close(0);
|
||||
resolve();
|
||||
try {
|
||||
client.close(0);
|
||||
} catch (err) {
|
||||
this.log.error(err, 'Error closing connection', {id});
|
||||
} finally {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
close() {
|
||||
this.log.info('Server closing...');
|
||||
this.server.maxConnections = 0;
|
||||
return when.map(Object.keys(this.connections), id => this.disconnectClient(id))
|
||||
.then(() => when.promise((resolve, reject) => {
|
||||
.then(() => when.promise(resolve => {
|
||||
this.server.close(err => {
|
||||
if (err) return reject(err);
|
||||
if (err) this.log.error(err, 'Error closing server');
|
||||
resolve();
|
||||
});
|
||||
}));
|
||||
|
||||
12
test/cert/server.crt
Normal file
12
test/cert/server.crt
Normal file
@@ -0,0 +1,12 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIBtTCCAR4CCQDsFyLCxvy4qzANBgkqhkiG9w0BAQUFADAfMQswCQYDVQQGEwJD
|
||||
QTEQMA4GA1UECBMHQWxiZXJ0YTAeFw0xNzA1MDgyMzQzMjNaFw0xODA1MDgyMzQz
|
||||
MjNaMB8xCzAJBgNVBAYTAkNBMRAwDgYDVQQIEwdBbGJlcnRhMIGfMA0GCSqGSIb3
|
||||
DQEBAQUAA4GNADCBiQKBgQD2uOwjV7Nb7ubBsK/tHZQ5hUpaQ9QD9Jo8qj7DBOia
|
||||
C4xbwpF6w+ZDf5OnkR7Hl3QlcofbkXKkLWmG0Mm5wvFA6kYW6D8vMT5Di7+eksf/
|
||||
agkklBnRtdoBb3lsbMbo3/EXijwHbCKbm+sTTe7dwGK/w6p782K/kgHVWk+L58O7
|
||||
rQIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAOkX32keFpo0kKQKpeZgxVYjvn4/Voy6
|
||||
6oLsj7jJYq3oZts1dX6kHVpLEbF9sWKB2iz7nqz7pSN1ATq0IL/5rcxvNwiL4Idv
|
||||
F8CCBvsBui+0gwX755NJK/L57a5i8yQ5HC65NujGAA4I5+2x8HlefMVuBpEYjzQ2
|
||||
6lW2OJJ8xtP/
|
||||
-----END CERTIFICATE-----
|
||||
10
test/cert/server.csr
Normal file
10
test/cert/server.csr
Normal file
@@ -0,0 +1,10 @@
|
||||
-----BEGIN CERTIFICATE REQUEST-----
|
||||
MIIBXjCByAIBADAfMQswCQYDVQQGEwJDQTEQMA4GA1UECBMHQWxiZXJ0YTCBnzAN
|
||||
BgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA9rjsI1ezW+7mwbCv7R2UOYVKWkPUA/Sa
|
||||
PKo+wwTomguMW8KResPmQ3+Tp5Eex5d0JXKH25FypC1phtDJucLxQOpGFug/LzE+
|
||||
Q4u/npLH/2oJJJQZ0bXaAW95bGzG6N/xF4o8B2wim5vrE03u3cBiv8Oqe/Niv5IB
|
||||
1VpPi+fDu60CAwEAAaAAMA0GCSqGSIb3DQEBBQUAA4GBACzo+Wecs3CTbItrugdL
|
||||
pP4crsRs+HJljWA0e+WEGKhcd1FjrcLBr4WqzHFQJWHOTz2vM5PiKXPZk9crLxWa
|
||||
Y8kMhU6eQPnCM6+7Gffm32+VS1ipNlhzHyYsjYpgC3ROElqo0J5M5sas4lbaamr+
|
||||
FnlyRjrPSUcFdcbPL6ozND3e
|
||||
-----END CERTIFICATE REQUEST-----
|
||||
15
test/cert/server.key
Normal file
15
test/cert/server.key
Normal file
@@ -0,0 +1,15 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICXQIBAAKBgQD2uOwjV7Nb7ubBsK/tHZQ5hUpaQ9QD9Jo8qj7DBOiaC4xbwpF6
|
||||
w+ZDf5OnkR7Hl3QlcofbkXKkLWmG0Mm5wvFA6kYW6D8vMT5Di7+eksf/agkklBnR
|
||||
tdoBb3lsbMbo3/EXijwHbCKbm+sTTe7dwGK/w6p782K/kgHVWk+L58O7rQIDAQAB
|
||||
AoGANRPVYUkVwfpkVFkBj/5kC/fb5g1fiDZQFCr/846Tx8giOv9hssqAOBczGcKD
|
||||
n6a6iu/XwGnLAvzuDd3O+BKzObrKV36u9HfvCxohKaKvhPg3lBlJ5fFq/UNBoLv8
|
||||
eHz0GUGGoCxwJBAV43ojV1GdyRZ7vdmYw2hzltsHIp7UDqECQQD7yltCCJm3+gcw
|
||||
p+Sde0+M9CubkTETPwpd3XPu6Bs5f2nxNVj6RAInPEBr5//5UY7Q9BBEHrDPsq0j
|
||||
/+gsSlWZAkEA+tjf9qRXk9JHoN3PD0xLNEgUZAsQwDic29jxb3xrGkuUjCebKRuC
|
||||
FU2sAfNgDp+MyG1iyAoZcySzH2Dp3+v7NQJAaBwBo8oelT2in3GsS5ljCSskpMxh
|
||||
+E1Gog0hFJWQPDP8wCmIwuI/6a02Def9pT8dyDRCTYhLH3YHtSzo+Pc7cQJBAJ4G
|
||||
XiD8qwc+o00eLsEOaRoIhn/30JenknmVE5QOJ1KrZmtc0Ax3fd15zvBzp4HO1Vu2
|
||||
PVKTujClYApWfT9JZDkCQQC3Ne79bb5WSsGNbg4eT+FWde4hkdpheBsWraEDN1Pp
|
||||
NanupXMPNP0EduAQ1O+oPRiZ5pG38MQYcPZHTtlULoiO
|
||||
-----END RSA PRIVATE KEY-----
|
||||
18
test/cert/server.key.org
Normal file
18
test/cert/server.key.org
Normal file
@@ -0,0 +1,18 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
Proc-Type: 4,ENCRYPTED
|
||||
DEK-Info: DES-EDE3-CBC,A253DC8068106194
|
||||
|
||||
jrYyhujaDsASrOfb2kn5Tvb0mRyIRsz7gVdjwlUdF2+lPdA/w6Os/NQBo1BIUAJp
|
||||
lfS5KDTwiE3QPgrBXUNgpy71Yr+MSzmsYWdonGlXGtchohQKXpxtL3qOpczX3ERR
|
||||
0AZninOKOYLw9+pe/tLNZI78DHxN1X0qTXS56RFydlW3XZbnl2Ux9CGuaCVq6vwh
|
||||
yzr9H+XTqeh95wTfdXkRdFRSTyUuJ72cvMsBFRDhz60epDmUDo1XDf844BpXXfcU
|
||||
kQoXHEtNkWZqzsc4ClOopp3Bgtd7eYoOLQluyovHgXzjtsur0xeMkHn9uTfkJ+IM
|
||||
cYMS71ZKbMePS7XBt3YPLBvVXNcyYhWUP7VdGYxXTPqd1AVWciDB9S5EvfMxnnZz
|
||||
O4M4ejxV8S7fF/cGju+sRzXx9oHPfo091Q3XKV1hrsUcF+ULrA8A8rHr64bDJ3wp
|
||||
luhekzwb+5yFfZDj9XUuGMD6pSWYoWB8Jmk8cxVsdZPtGXbTQHFL9/+UZ65wSGpj
|
||||
CjTLuFyVhY8pliynZH80vsNeRycdfmx93XoLqfS4xwEmI5v/MGUF24eTpF1/VIa5
|
||||
oKDrVuERdXAn4JBKeaMratrl6p1BhkPe7VNnMUFw3U+C4x+QHISxbboUJiTcCe1C
|
||||
pT6+YYkxQJ88rKunSEXkQYt6LeYSDg8Dw5y5Oq68DmW2Rp1m4ptTbqk3+uh83vzV
|
||||
Ff0JnfNfT80GHD3T5hMgizZal2vV8DeH1WAwPzpNaeV6wTy6MSgRLgQ89cCQ0TXV
|
||||
0GYnCZCaoXA2ldvbB3vW3fweOTr8Mp7aSl4s8K0R8sT3eief9/SyWA==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
95
test/commands/index.spec.js
Normal file
95
test/commands/index.spec.js
Normal file
@@ -0,0 +1,95 @@
|
||||
const {expect} = require('chai');
|
||||
const when = require('when');
|
||||
const bunyan = require('bunyan');
|
||||
const sinon = require('sinon');
|
||||
|
||||
const FtpCommands = require('../../src/commands');
|
||||
|
||||
describe('FtpCommands', function () {
|
||||
let sandbox;
|
||||
let commands;
|
||||
let mockConnection = {
|
||||
authenticated: false,
|
||||
log: bunyan.createLogger({name: 'FtpCommands'}),
|
||||
reply: () => when.resolve({}),
|
||||
server: {
|
||||
options: {
|
||||
blacklist: ['allo']
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
|
||||
commands = new FtpCommands(mockConnection);
|
||||
|
||||
sandbox.spy(mockConnection, 'reply');
|
||||
sandbox.spy(commands, 'handle');
|
||||
sandbox.spy(commands, 'parse');
|
||||
});
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
describe('parse', function () {
|
||||
it('no args: test', () => {
|
||||
const cmd = commands.parse('test');
|
||||
expect(cmd.directive).to.equal('TEST');
|
||||
expect(cmd.arg).to.equal(null);
|
||||
expect(cmd.raw).to.equal('test');
|
||||
});
|
||||
|
||||
it('one arg: test arg', () => {
|
||||
const cmd = commands.parse('test arg');
|
||||
expect(cmd.directive).to.equal('TEST');
|
||||
expect(cmd.arg).to.equal('arg');
|
||||
expect(cmd.raw).to.equal('test arg');
|
||||
});
|
||||
|
||||
it('two args: test arg1 arg2', () => {
|
||||
const cmd = commands.parse('test arg1 arg2');
|
||||
expect(cmd.directive).to.equal('TEST');
|
||||
expect(cmd.arg).to.equal('arg1 arg2');
|
||||
expect(cmd.raw).to.equal('test arg1 arg2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('handle', function () {
|
||||
it('fails with unsupported command', () => {
|
||||
return commands.handle('bad')
|
||||
.then(() => {
|
||||
expect(mockConnection.reply.callCount).to.equal(1);
|
||||
expect(mockConnection.reply.args[0][0]).to.equal(402);
|
||||
});
|
||||
});
|
||||
|
||||
it('fails with blacklisted command', () => {
|
||||
return commands.handle('allo')
|
||||
.then(() => {
|
||||
expect(mockConnection.reply.callCount).to.equal(1);
|
||||
expect(mockConnection.reply.args[0][0]).to.equal(502);
|
||||
expect(mockConnection.reply.args[0][1]).to.match(/blacklisted/);
|
||||
});
|
||||
});
|
||||
|
||||
it('fails with non whitelisted command', () => {
|
||||
commands.whitelist.push('USER');
|
||||
return commands.handle('auth')
|
||||
.then(() => {
|
||||
expect(mockConnection.reply.callCount).to.equal(1);
|
||||
expect(mockConnection.reply.args[0][0]).to.equal(502);
|
||||
expect(mockConnection.reply.args[0][1]).to.match(/whitelisted/);
|
||||
});
|
||||
});
|
||||
|
||||
it('fails due to being unauthenticated', () => {
|
||||
return commands.handle('stor')
|
||||
.then(() => {
|
||||
expect(mockConnection.reply.callCount).to.equal(1);
|
||||
expect(mockConnection.reply.args[0][0]).to.equal(530);
|
||||
expect(mockConnection.reply.args[0][1]).to.match(/authentication/);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -12,7 +12,7 @@ describe(CMD, function () {
|
||||
end: () => when.resolve()
|
||||
}
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -8,7 +8,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -6,9 +6,12 @@ const CMD = 'AUTH';
|
||||
describe(CMD, function () {
|
||||
let sandbox;
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
reply: () => when.resolve(),
|
||||
server: {
|
||||
_tls: {}
|
||||
}
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -19,17 +22,18 @@ describe(CMD, function () {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('TLS // not supported', done => {
|
||||
cmdFn({command: {_: [CMD, 'TLS'], directive: CMD}})
|
||||
it('TLS // supported', done => {
|
||||
cmdFn({command: { arg: 'TLS', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(504);
|
||||
expect(mockClient.reply.args[0][0]).to.equal(234);
|
||||
expect(mockClient.secure).to.equal(true);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('SSL // not supported', done => {
|
||||
cmdFn({command: {_: [CMD, 'SSL'], directive: CMD}})
|
||||
cmdFn({command: { arg: 'SSL', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(504);
|
||||
done();
|
||||
@@ -38,7 +42,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('bad // bad', done => {
|
||||
cmdFn({command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({command: { arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(504);
|
||||
done();
|
||||
@@ -13,7 +13,7 @@ describe(CMD, function () {
|
||||
chdir: () => when.resolve()
|
||||
}
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -26,7 +26,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('.. // successful', done => {
|
||||
cmdFn({log, command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({log, command: {directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(250);
|
||||
expect(mockClient.fs.chdir.args[0][0]).to.equal('..');
|
||||
@@ -10,7 +10,7 @@ describe(CMD, function () {
|
||||
reply: () => {},
|
||||
fs: { chdir: () => {} }
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -25,7 +25,7 @@ describe(CMD, function () {
|
||||
describe('// check', function () {
|
||||
it('fails on no fs', done => {
|
||||
const badMockClient = { reply: () => {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -37,7 +37,7 @@ describe(CMD, function () {
|
||||
|
||||
it('fails on no fs chdir command', done => {
|
||||
const badMockClient = { reply: () => {}, fs: {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -49,7 +49,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('test // successful', done => {
|
||||
cmdFn({log, command: {_: [CMD, 'test'], directive: CMD}})
|
||||
cmdFn({log, command: { arg: 'test', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(250);
|
||||
expect(mockClient.fs.chdir.args[0][0]).to.equal('test');
|
||||
@@ -61,7 +61,7 @@ describe(CMD, function () {
|
||||
it('test // successful', done => {
|
||||
mockClient.fs.chdir.restore();
|
||||
sandbox.stub(mockClient.fs, 'chdir').resolves('/test');
|
||||
cmdFn({log, command: {_: [CMD, 'test'], directive: CMD}})
|
||||
cmdFn({log, command: { arg: 'test', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(250);
|
||||
expect(mockClient.fs.chdir.args[0][0]).to.equal('test');
|
||||
@@ -74,7 +74,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.chdir.restore();
|
||||
sandbox.stub(mockClient.fs, 'chdir').rejects(new Error('Bad'));
|
||||
|
||||
cmdFn({log, command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({log, command: { arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
expect(mockClient.fs.chdir.args[0][0]).to.equal('bad');
|
||||
@@ -10,7 +10,7 @@ describe(CMD, function () {
|
||||
reply: () => {},
|
||||
fs: { delete: () => {} }
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -25,7 +25,7 @@ describe(CMD, function () {
|
||||
describe('// check', function () {
|
||||
it('fails on no fs', done => {
|
||||
const badMockClient = { reply: () => {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -37,7 +37,7 @@ describe(CMD, function () {
|
||||
|
||||
it('fails on no fs delete command', done => {
|
||||
const badMockClient = { reply: () => {}, fs: {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -49,7 +49,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('test // successful', done => {
|
||||
cmdFn({log, command: {_: [CMD, 'test'], directive: CMD}})
|
||||
cmdFn({log, command: { arg: 'test', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(250);
|
||||
expect(mockClient.fs.delete.args[0][0]).to.equal('test');
|
||||
@@ -62,7 +62,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.delete.restore();
|
||||
sandbox.stub(mockClient.fs, 'delete').rejects(new Error('Bad'));
|
||||
|
||||
cmdFn({log, command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({log, command: { arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
expect(mockClient.fs.delete.args[0][0]).to.equal('bad');
|
||||
@@ -8,7 +8,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -20,7 +20,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('// successful', done => {
|
||||
cmdFn({command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({command: { directive: CMD }})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(211);
|
||||
done();
|
||||
@@ -29,7 +29,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('help // successful', done => {
|
||||
cmdFn({command: {_: [CMD, 'help'], directive: CMD}})
|
||||
cmdFn({command: { arg: 'help', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(214);
|
||||
done();
|
||||
@@ -38,7 +38,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('help // successful', done => {
|
||||
cmdFn({command: {_: [CMD, 'allo'], directive: CMD}})
|
||||
cmdFn({command: { arg: 'allo', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(214);
|
||||
done();
|
||||
@@ -47,7 +47,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('bad // unsuccessful', done => {
|
||||
cmdFn({command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({command: { arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(502);
|
||||
done();
|
||||
@@ -19,7 +19,7 @@ describe(CMD, function () {
|
||||
pause: () => {}
|
||||
}
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -51,7 +51,7 @@ describe(CMD, function () {
|
||||
describe('// check', function () {
|
||||
it('fails on no fs', done => {
|
||||
const badMockClient = { reply: () => {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -63,7 +63,7 @@ describe(CMD, function () {
|
||||
|
||||
it('fails on no fs list command', done => {
|
||||
const badMockClient = { reply: () => {}, fs: {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -75,7 +75,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('. // successful', done => {
|
||||
cmdFn({log, command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({log, command: {directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(150);
|
||||
expect(mockClient.reply.args[1][1]).to.have.property('raw');
|
||||
@@ -91,7 +91,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.list.restore();
|
||||
sandbox.stub(mockClient.fs, 'list').rejects(new Error());
|
||||
|
||||
cmdFn({log, command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({log, command: {directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(451);
|
||||
done();
|
||||
@@ -102,7 +102,7 @@ describe(CMD, function () {
|
||||
it('. // unsuccessful (timeout)', done => {
|
||||
sandbox.stub(mockClient.connector, 'waitForConnection').returns(when.reject(new when.TimeoutError()));
|
||||
|
||||
cmdFn({log, command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({log, command: {directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(425);
|
||||
done();
|
||||
@@ -10,7 +10,7 @@ describe(CMD, function () {
|
||||
reply: () => {},
|
||||
fs: { get: () => {} }
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -25,7 +25,7 @@ describe(CMD, function () {
|
||||
describe('// check', function () {
|
||||
it('fails on no fs', done => {
|
||||
const badMockClient = { reply: () => {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -37,7 +37,7 @@ describe(CMD, function () {
|
||||
|
||||
it('fails on no fs get command', done => {
|
||||
const badMockClient = { reply: () => {}, fs: {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -49,7 +49,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('. // successful', done => {
|
||||
cmdFn({log, command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({log, command: {directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(213);
|
||||
//expect(mockClient.reply.args[0][1]).to.equal('20111010172411.000');
|
||||
@@ -62,7 +62,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.get.restore();
|
||||
sandbox.stub(mockClient.fs, 'get').rejects(new Error());
|
||||
|
||||
cmdFn({log, command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({log, command: {directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
done();
|
||||
@@ -10,7 +10,7 @@ describe(CMD, function () {
|
||||
reply: () => {},
|
||||
fs: { mkdir: () => {} }
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -25,7 +25,7 @@ describe(CMD, function () {
|
||||
describe('// check', function () {
|
||||
it('fails on no fs', done => {
|
||||
const badMockClient = { reply: () => {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -37,7 +37,7 @@ describe(CMD, function () {
|
||||
|
||||
it('fails on no fs mkdir command', done => {
|
||||
const badMockClient = { reply: () => {}, fs: {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -49,7 +49,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('test // successful', done => {
|
||||
cmdFn({log, command: {_: [CMD, 'test'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'test', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(257);
|
||||
expect(mockClient.fs.mkdir.args[0][0]).to.equal('test');
|
||||
@@ -61,7 +61,7 @@ describe(CMD, function () {
|
||||
it('test // successful', done => {
|
||||
mockClient.fs.mkdir.restore();
|
||||
sandbox.stub(mockClient.fs, 'mkdir').resolves('test');
|
||||
cmdFn({log, command: {_: [CMD, 'test'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'test', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(257);
|
||||
expect(mockClient.fs.mkdir.args[0][0]).to.equal('test');
|
||||
@@ -74,7 +74,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.mkdir.restore();
|
||||
sandbox.stub(mockClient.fs, 'mkdir').rejects(new Error('Bad'));
|
||||
|
||||
cmdFn({log, command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
expect(mockClient.fs.mkdir.args[0][0]).to.equal('bad');
|
||||
@@ -8,7 +8,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -20,7 +20,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('S // successful', done => {
|
||||
cmdFn({command: {_: [CMD, 'S']}})
|
||||
cmdFn({command: {arg: 'S'}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
done();
|
||||
@@ -29,7 +29,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('Q // unsuccessful', done => {
|
||||
cmdFn({command: {_: [CMD, 'Q']}})
|
||||
cmdFn({command: {arg: 'Q'}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(504);
|
||||
done();
|
||||
@@ -19,7 +19,7 @@ describe(CMD, function () {
|
||||
pause: () => {}
|
||||
}
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -49,7 +49,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('. // successful', done => {
|
||||
cmdFn({log, command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({log, command: {directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(150);
|
||||
expect(mockClient.reply.args[1][1]).to.have.property('raw');
|
||||
@@ -8,7 +8,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -8,7 +8,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -10,9 +10,9 @@ describe(CMD, function () {
|
||||
reply: () => {},
|
||||
login: () => {},
|
||||
server: { options: { anonymous: false } },
|
||||
username: 'user'
|
||||
username: 'anonymous'
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -25,21 +25,21 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('pass // successful', done => {
|
||||
cmdFn({log, command: {_: [CMD, 'pass'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'pass', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(230);
|
||||
expect(mockClient.login.args[0]).to.eql(['user', 'pass']);
|
||||
expect(mockClient.login.args[0]).to.eql(['anonymous', 'pass']);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('// successful (anonymous)', done => {
|
||||
it('// successful (already authenticated)', done => {
|
||||
mockClient.server.options.anonymous = true;
|
||||
mockClient.authenticated = true;
|
||||
cmdFn({log, command: {_: [CMD], directive: CMD}})
|
||||
cmdFn({log, command: {directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(230);
|
||||
expect(mockClient.reply.args[0][0]).to.equal(202);
|
||||
expect(mockClient.login.callCount).to.equal(0);
|
||||
mockClient.server.options.anonymous = false;
|
||||
mockClient.authenticated = false;
|
||||
@@ -52,7 +52,7 @@ describe(CMD, function () {
|
||||
mockClient.login.restore();
|
||||
sandbox.stub(mockClient, 'login').rejects('bad');
|
||||
|
||||
cmdFn({log, command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(530);
|
||||
done();
|
||||
@@ -64,7 +64,7 @@ describe(CMD, function () {
|
||||
mockClient.login.restore();
|
||||
sandbox.stub(mockClient, 'login').rejects({});
|
||||
|
||||
cmdFn({log, command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(530);
|
||||
done();
|
||||
@@ -74,7 +74,7 @@ describe(CMD, function () {
|
||||
|
||||
it('bad // unsuccessful', done => {
|
||||
delete mockClient.username;
|
||||
cmdFn({log, command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(503);
|
||||
done();
|
||||
56
test/commands/registration/pbsz.spec.js
Normal file
56
test/commands/registration/pbsz.spec.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const when = require('when');
|
||||
const {expect} = require('chai');
|
||||
const sinon = require('sinon');
|
||||
|
||||
const CMD = 'PBSZ';
|
||||
describe(CMD, function () {
|
||||
let sandbox;
|
||||
const mockClient = {
|
||||
reply: () => when.resolve(),
|
||||
server: {},
|
||||
secure: true
|
||||
};
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
|
||||
sandbox.spy(mockClient, 'reply');
|
||||
});
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('// unsuccessful', done => {
|
||||
cmdFn()
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(202);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('// successful', done => {
|
||||
mockClient.server._tls = {};
|
||||
|
||||
cmdFn({command: {arg: '0'}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
expect(mockClient.bufferSize).to.equal(0);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('// successful', done => {
|
||||
mockClient.server._tls = {};
|
||||
|
||||
cmdFn({command: {arg: '10'}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
expect(mockClient.bufferSize).to.equal(10);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@ const when = require('when');
|
||||
const {expect} = require('chai');
|
||||
const sinon = require('sinon');
|
||||
|
||||
const ActiveConnector = require('../../src/connector/active');
|
||||
const ActiveConnector = require('../../../src/connector/active');
|
||||
|
||||
const CMD = 'PORT';
|
||||
describe(CMD, function () {
|
||||
@@ -10,7 +10,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -32,7 +32,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('// unsuccessful | invalid argument', done => {
|
||||
cmdFn({ command: { _: [CMD, '1,2,3,4,5'] } })
|
||||
cmdFn({ command: { arg: '1,2,3,4,5' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(425);
|
||||
done();
|
||||
@@ -41,7 +41,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('// successful', done => {
|
||||
cmdFn({ command: { _: [CMD, '192,168,0,100,137,214'] } })
|
||||
cmdFn({ command: { arg: '192,168,0,100,137,214' } })
|
||||
.then(() => {
|
||||
const [ip, port] = ActiveConnector.prototype.setupConnection.args[0];
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
72
test/commands/registration/prot.spec.js
Normal file
72
test/commands/registration/prot.spec.js
Normal file
@@ -0,0 +1,72 @@
|
||||
const when = require('when');
|
||||
const {expect} = require('chai');
|
||||
const sinon = require('sinon');
|
||||
|
||||
const CMD = 'PROT';
|
||||
describe(CMD, function () {
|
||||
let sandbox;
|
||||
const mockClient = {
|
||||
reply: () => when.resolve(),
|
||||
server: {},
|
||||
secure: true
|
||||
};
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
|
||||
sandbox.spy(mockClient, 'reply');
|
||||
});
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('// unsuccessful', done => {
|
||||
cmdFn()
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(202);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('// unsuccessful - no bufferSize', done => {
|
||||
mockClient.server._tls = {};
|
||||
|
||||
cmdFn({command: {arg: 'P'}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(503);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('// successful', done => {
|
||||
mockClient.bufferSize = 0;
|
||||
|
||||
cmdFn({command: {arg: 'p'}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('// unsuccessful - unsupported', done => {
|
||||
cmdFn({command: {arg: 'C'}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(536);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('// unsuccessful - unknown', done => {
|
||||
cmdFn({command: {arg: 'QQ'}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(504);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
});
|
||||
@@ -10,7 +10,7 @@ describe(CMD, function () {
|
||||
reply: () => {},
|
||||
fs: { currentDirectory: () => {} }
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -25,7 +25,7 @@ describe(CMD, function () {
|
||||
describe('// check', function () {
|
||||
it('fails on no fs', done => {
|
||||
const badMockClient = { reply: () => {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -37,7 +37,7 @@ describe(CMD, function () {
|
||||
|
||||
it('fails on no fs currentDirectory command', done => {
|
||||
const badMockClient = { reply: () => {}, fs: {} };
|
||||
const badCmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient);
|
||||
sandbox.stub(badMockClient, 'reply').resolves();
|
||||
badCmdFn()
|
||||
.then(() => {
|
||||
@@ -49,7 +49,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('// successful', done => {
|
||||
cmdFn({log, command: {_: [CMD, 'test'], directive: CMD}})
|
||||
cmdFn({log, command: { arg: 'test', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(257);
|
||||
done();
|
||||
@@ -61,7 +61,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.currentDirectory.restore();
|
||||
sandbox.stub(mockClient.fs, 'currentDirectory').resolves('/test');
|
||||
|
||||
cmdFn({log, command: {_: [CMD, 'test'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'test', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(257);
|
||||
done();
|
||||
@@ -73,7 +73,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.currentDirectory.restore();
|
||||
sandbox.stub(mockClient.fs, 'currentDirectory').rejects(new Error('Bad'));
|
||||
|
||||
cmdFn({log, command: {_: [CMD, 'bad'], directive: CMD}})
|
||||
cmdFn({log, command: {arg: 'bad', directive: CMD}})
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
done();
|
||||
@@ -7,7 +7,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
close: () => {}
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -7,7 +7,7 @@ describe(CMD, function () {
|
||||
let sandbox;
|
||||
const mockLog = { error: () => {} };
|
||||
const mockClient = { reply: () => when.resolve() };
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -50,7 +50,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.get.restore();
|
||||
sandbox.stub(mockClient.fs, 'get').rejects(new Error('test'));
|
||||
|
||||
cmdFn({ log: mockLog, command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ log: mockLog, command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
done();
|
||||
@@ -59,7 +59,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('test // successful', done => {
|
||||
cmdFn({ log: mockLog, command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ log: mockLog, command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.fs.get.args[0][0]).to.equal('test');
|
||||
expect(mockClient.reply.args[0][0]).to.equal(350);
|
||||
@@ -7,7 +7,7 @@ describe(CMD, function () {
|
||||
let sandbox;
|
||||
const mockLog = { error: () => {} };
|
||||
const mockClient = { reply: () => when.resolve() };
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -62,7 +62,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.rename.restore();
|
||||
sandbox.stub(mockClient.fs, 'rename').rejects(new Error('test'));
|
||||
|
||||
cmdFn({ log: mockLog, command: { _: [CMD, 'new'] } })
|
||||
cmdFn({ log: mockLog, command: { arg: 'new' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
done();
|
||||
@@ -71,7 +71,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('new // successful', done => {
|
||||
cmdFn({ command: { _: [CMD, 'new'] } })
|
||||
cmdFn({ command: { arg: 'new' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(250);
|
||||
expect(mockClient.fs.rename.args[0]).to.eql(['test', 'new']);
|
||||
@@ -7,7 +7,7 @@ describe(CMD, function () {
|
||||
let sandbox;
|
||||
const mockLog = { error: () => {} };
|
||||
const mockClient = { reply: () => when.resolve() };
|
||||
const cmdFn = require(`../../../src/commands/registration/site/${CMD.toLowerCase()}`).bind(mockClient);
|
||||
const cmdFn = require(`../../../../src/commands/registration/site/${CMD.toLowerCase()}`).bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -49,7 +49,7 @@ describe(CMD, function () {
|
||||
mockClient.fs.chmod.restore();
|
||||
sandbox.stub(mockClient.fs, 'chmod').rejects(new Error('test'));
|
||||
|
||||
cmdFn({ log: mockLog, command: { _: [CMD, '777', 'test'] } })
|
||||
cmdFn({ log: mockLog, command: { arg: '777 test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(500);
|
||||
done();
|
||||
@@ -58,7 +58,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('777 test // successful', done => {
|
||||
cmdFn({ log: mockLog, command: { _: [CMD, '777', 'test'] } })
|
||||
cmdFn({ log: mockLog, command: { arg: '777 test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.fs.chmod.args[0]).to.eql(['test', 511]);
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
@@ -7,7 +7,7 @@ describe(CMD, function () {
|
||||
let sandbox;
|
||||
const mockLog = { error: () => {} };
|
||||
const mockClient = { reply: () => when.resolve() };
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -47,7 +47,7 @@ describe(CMD, function () {
|
||||
it('// unsuccessful | file get fails', done => {
|
||||
sandbox.stub(mockClient.fs, 'get').rejects(new Error('test'));
|
||||
|
||||
cmdFn({ log: mockLog, command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ log: mockLog, command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
done();
|
||||
@@ -56,7 +56,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('// successful', done => {
|
||||
cmdFn({ command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(213);
|
||||
done();
|
||||
@@ -7,7 +7,7 @@ describe(CMD, function () {
|
||||
let sandbox;
|
||||
const mockLog = { error: () => {} };
|
||||
const mockClient = { reply: () => when.resolve() };
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -35,7 +35,7 @@ describe(CMD, function () {
|
||||
it('// unsuccessful | no file system', done => {
|
||||
delete mockClient.fs;
|
||||
|
||||
cmdFn({ command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(550);
|
||||
done();
|
||||
@@ -46,7 +46,7 @@ describe(CMD, function () {
|
||||
it('// unsuccessful | file system does not have functions', done => {
|
||||
mockClient.fs = {};
|
||||
|
||||
cmdFn({ command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(402);
|
||||
done();
|
||||
@@ -57,7 +57,7 @@ describe(CMD, function () {
|
||||
it('// unsuccessful | file get fails', done => {
|
||||
sandbox.stub(mockClient.fs, 'get').rejects(new Error('test'));
|
||||
|
||||
cmdFn({ log: mockLog, command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ log: mockLog, command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(450);
|
||||
done();
|
||||
@@ -85,7 +85,7 @@ describe(CMD, function () {
|
||||
isDirectory: () => false
|
||||
});
|
||||
|
||||
cmdFn({ command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(212);
|
||||
done();
|
||||
@@ -132,7 +132,7 @@ describe(CMD, function () {
|
||||
isDirectory: () => true
|
||||
});
|
||||
|
||||
cmdFn({ command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(213);
|
||||
done();
|
||||
@@ -2,7 +2,7 @@ const when = require('when');
|
||||
const {expect} = require('chai');
|
||||
const sinon = require('sinon');
|
||||
|
||||
const stor = require('../../src/commands/registration/stor');
|
||||
const stor = require('../../../src/commands/registration/stor');
|
||||
|
||||
const CMD = 'STOU';
|
||||
describe(CMD, function () {
|
||||
@@ -10,7 +10,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -56,12 +56,12 @@ describe(CMD, function () {
|
||||
mockClient.fs.get.restore();
|
||||
sandbox.stub(mockClient.fs, 'get').rejects({});
|
||||
|
||||
cmdFn({ command: { _: [CMD, 'good'] } })
|
||||
cmdFn({ command: { arg: 'good' } })
|
||||
.then(() => {
|
||||
const call = stor.handler.call.args[0][1];
|
||||
expect(call).to.have.property('command');
|
||||
expect(call.command).to.have.property('_');
|
||||
expect(call.command._).to.eql([CMD, 'good']);
|
||||
expect(call.command).to.have.property('arg');
|
||||
expect(call.command.arg).to.eql('good');
|
||||
expect(mockClient.fs.getUniqueName.callCount).to.equal(0);
|
||||
done();
|
||||
})
|
||||
@@ -69,12 +69,12 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('// successful | generates unique name', done => {
|
||||
cmdFn({ command: { _: [CMD, 'bad'] } })
|
||||
cmdFn({ command: { arg: 'bad' } })
|
||||
.then(() => {
|
||||
const call = stor.handler.call.args[0][1];
|
||||
expect(call).to.have.property('command');
|
||||
expect(call.command).to.have.property('_');
|
||||
expect(call.command._).to.eql([CMD, '4']);
|
||||
expect(call.command).to.have.property('arg');
|
||||
expect(call.command.arg).to.eql('4');
|
||||
expect(mockClient.fs.getUniqueName.callCount).to.equal(1);
|
||||
done();
|
||||
})
|
||||
@@ -8,7 +8,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -20,7 +20,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('// successful', done => {
|
||||
cmdFn({command: { _: [CMD, 'F'] } })
|
||||
cmdFn({command: { arg: 'F' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
done();
|
||||
@@ -29,7 +29,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('// unsuccessful', done => {
|
||||
cmdFn({command: { _: [CMD, 'X'] } })
|
||||
cmdFn({command: { arg: 'X' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(504);
|
||||
done();
|
||||
@@ -8,7 +8,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -8,7 +8,7 @@ describe(CMD, function () {
|
||||
const mockClient = {
|
||||
reply: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -21,17 +21,17 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('A // successful', done => {
|
||||
cmdFn({ command: { _: [CMD, 'A'] } })
|
||||
cmdFn({ command: { arg: 'A' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
expect(mockClient.encoding).to.equal('utf-8');
|
||||
expect(mockClient.encoding).to.equal('utf8');
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('I // successful', done => {
|
||||
cmdFn({ command: { _: [CMD, 'I'] } })
|
||||
cmdFn({ command: { arg: 'I' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
expect(mockClient.encoding).to.equal('binary');
|
||||
@@ -41,7 +41,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('L // successful', done => {
|
||||
cmdFn({ command: { _: [CMD, 'L'] } })
|
||||
cmdFn({ command: { arg: 'L' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(200);
|
||||
expect(mockClient.encoding).to.equal('binary');
|
||||
@@ -51,7 +51,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('X // successful', done => {
|
||||
cmdFn({ command: { _: [CMD, 'X'] } })
|
||||
cmdFn({ command: { arg: 'X' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(501);
|
||||
expect(mockClient.encoding).to.equal(null);
|
||||
@@ -13,7 +13,7 @@ describe(CMD, function () {
|
||||
server: { options: {} },
|
||||
login: () => when.resolve()
|
||||
};
|
||||
const cmdFn = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
@@ -29,7 +29,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('test // successful | prompt for password', done => {
|
||||
cmdFn({ command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(331);
|
||||
done();
|
||||
@@ -40,7 +40,7 @@ describe(CMD, function () {
|
||||
it('test // successful | anonymous login', done => {
|
||||
mockClient.server.options = {anonymous: true};
|
||||
|
||||
cmdFn({ command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ command: { arg: 'anonymous' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(230);
|
||||
expect(mockClient.login.callCount).to.equal(1);
|
||||
@@ -50,7 +50,7 @@ describe(CMD, function () {
|
||||
});
|
||||
|
||||
it('test // unsuccessful | no username provided', done => {
|
||||
cmdFn({ command: { _: [CMD] } })
|
||||
cmdFn({ command: { } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(501);
|
||||
expect(mockClient.login.callCount).to.equal(0);
|
||||
@@ -62,7 +62,7 @@ describe(CMD, function () {
|
||||
it('test // unsuccessful | already set username', done => {
|
||||
mockClient.username = 'test';
|
||||
|
||||
cmdFn({ command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(530);
|
||||
expect(mockClient.login.callCount).to.equal(0);
|
||||
@@ -71,15 +71,24 @@ describe(CMD, function () {
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('test // unsuccessful | login function rejects', done => {
|
||||
it('test // successful | regular login if anonymous is true', done => {
|
||||
mockClient.server.options = {anonymous: true};
|
||||
|
||||
mockClient.login.restore();
|
||||
sandbox.stub(mockClient, 'login').rejects(new Error('test'));
|
||||
|
||||
cmdFn({ log: mockLog, command: { _: [CMD, 'test'] } })
|
||||
cmdFn({ log: mockLog, command: { arg: 'test' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(530);
|
||||
expect(mockClient.reply.args[0][0]).to.equal(331);
|
||||
expect(mockClient.login.callCount).to.equal(0);
|
||||
done();
|
||||
})
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('test // successful | anonymous login with set username', done => {
|
||||
mockClient.server.options = {anonymous: 'sillyrabbit'};
|
||||
|
||||
cmdFn({ log: mockLog, command: { arg: 'sillyrabbit' } })
|
||||
.then(() => {
|
||||
expect(mockClient.reply.args[0][0]).to.equal(230);
|
||||
expect(mockClient.login.callCount).to.equal(1);
|
||||
done();
|
||||
})
|
||||
@@ -27,6 +27,7 @@ describe('Connector - Passive //', function () {
|
||||
sandbox = sinon.sandbox.create();
|
||||
|
||||
sandbox.spy(mockConnection, 'reply');
|
||||
sandbox.spy(mockConnection, 'close');
|
||||
|
||||
mockConnection.commandSocket.remoteAddress = '::ffff:127.0.0.1';
|
||||
mockConnection.server.options.pasv_range = '8000';
|
||||
@@ -45,7 +46,7 @@ describe('Connector - Passive //', function () {
|
||||
});
|
||||
|
||||
it('has invalid pasv range', function (done) {
|
||||
delete mockConnection.server.options.pasv_range;
|
||||
mockConnection.server.options.pasv_range = -1;
|
||||
|
||||
passive.setupServer()
|
||||
.then(() => done('should not happen'))
|
||||
@@ -84,10 +85,13 @@ describe('Connector - Passive //', function () {
|
||||
expect(passive.dataServer).to.exist;
|
||||
|
||||
const {port} = passive.dataServer.address();
|
||||
net.createConnection(port, () => {
|
||||
expect(mockConnection.reply.callCount).to.equal(1);
|
||||
expect(mockConnection.reply.args[0][0]).to.equal(550);
|
||||
done();
|
||||
net.createConnection(port);
|
||||
passive.dataServer.once('connection', () => {
|
||||
setTimeout(() => {
|
||||
expect(passive.connection.reply.callCount).to.equal(1);
|
||||
expect(passive.connection.reply.args[0][0]).to.equal(550);
|
||||
done();
|
||||
}, 100);
|
||||
});
|
||||
})
|
||||
.catch(done);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const {expect} = require('chai');
|
||||
const dateFns = require('date-fns');
|
||||
|
||||
const fileStat = require('../../src/helpers/file-stat');
|
||||
const errors = require('../../src/errors');
|
||||
@@ -9,7 +8,7 @@ describe('helpers // file-stat', function () {
|
||||
name: 'test1',
|
||||
dev: 2114,
|
||||
ino: 48064969,
|
||||
mode: 33188,
|
||||
mode: 33279,
|
||||
nlink: 1,
|
||||
uid: 85,
|
||||
gid: 100,
|
||||
@@ -17,24 +16,57 @@ describe('helpers // file-stat', function () {
|
||||
size: 527,
|
||||
blksize: 4096,
|
||||
blocks: 8,
|
||||
atime: 'Mon, 10 Oct 2011 23:24:11 GMT',
|
||||
mtime: 'Mon, 10 Oct 2011 23:24:11 GMT',
|
||||
ctime: 'Mon, 10 Oct 2011 23:24:11 GMT',
|
||||
birthtime: 'Mon, 10 Oct 2011 23:24:11 GMT',
|
||||
atime: 'Mon, 10 Oct 2017 23:24:11 GMT',
|
||||
mtime: 'Mon, 10 Oct 2017 23:24:11 GMT',
|
||||
ctime: 'Mon, 10 Oct 2017 23:24:11 GMT',
|
||||
birthtime: 'Mon, 10 Oct 2017 23:24:11 GMT',
|
||||
isDirectory: () => false
|
||||
};
|
||||
|
||||
describe.skip('format - ls //', function () {
|
||||
const STAT_OLD = {
|
||||
name: 'test2',
|
||||
dev: 2114,
|
||||
ino: 48064969,
|
||||
mode: 33279,
|
||||
nlink: 1,
|
||||
uid: 84,
|
||||
gid: 101,
|
||||
rdev: 0,
|
||||
size: 530,
|
||||
blksize: 4096,
|
||||
blocks: 8,
|
||||
atime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
mtime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
ctime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
birthtime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
isDirectory: () => false
|
||||
};
|
||||
|
||||
describe('format - ls //', function () {
|
||||
it('formats correctly', () => {
|
||||
const format = fileStat(STAT, 'ls');
|
||||
expect(format).to.equal('-rwxrw-r-- 1 85 100 527 Oct 10 17:24 test1');
|
||||
expect(format).to.equal('-rwxrwxrwx 1 85 100 527 Oct 10 23:24 test1');
|
||||
});
|
||||
|
||||
it('formats correctly for files over 6 months old', () => {
|
||||
const format = fileStat(STAT_OLD, 'ls');
|
||||
expect(format).to.equal('-rwxrwxrwx 1 84 101 530 Oct 10 2011 test2');
|
||||
});
|
||||
|
||||
it('formats without some attributes', () => {
|
||||
const format = fileStat({
|
||||
name: 'missing stuff',
|
||||
mtime: 'Mon, 10 Oct 2011 14:05:12 GMT',
|
||||
isDirectory: () => true
|
||||
}, 'ls');
|
||||
expect(format).to.equal('drwxr-xr-x 1 1 1 Oct 10 2011 missing stuff');
|
||||
});
|
||||
});
|
||||
|
||||
describe.skip('format - ep //', function () {
|
||||
describe('format - ep //', function () {
|
||||
it('formats correctly', () => {
|
||||
const format = fileStat(STAT, 'ep');
|
||||
expect(format).to.equal('+i842.2dd69c9,s527,m1318289051,up644,/ test1');
|
||||
expect(format).to.equal('+i842.2dd69c9,s527,m1507677851,up777,r test1');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ const {expect} = require('chai');
|
||||
const resolveHost = require('../../src/helpers/resolve-host');
|
||||
|
||||
describe('helpers //resolve-host', function () {
|
||||
this.timeout(4000);
|
||||
|
||||
it('fetches ip address', done => {
|
||||
const hostname = '0.0.0.0';
|
||||
resolveHost(hostname)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
/* eslint no-unused-expressions: 0 */
|
||||
require('dotenv').load();
|
||||
const {expect} = require('chai');
|
||||
const bunyan = require('bunyan');
|
||||
const fs = require('fs');
|
||||
@@ -7,28 +6,30 @@ const fs = require('fs');
|
||||
const FtpServer = require('../src');
|
||||
const FtpClient = require('ftp');
|
||||
|
||||
before(() => require('dotenv').load());
|
||||
|
||||
describe('FtpServer', function () {
|
||||
this.timeout(2000);
|
||||
let log = bunyan.createLogger({name: 'test', level: 10});
|
||||
let log = bunyan.createLogger({name: 'test'});
|
||||
let server;
|
||||
let client;
|
||||
|
||||
before(done => {
|
||||
server = new FtpServer(process.env.FTP_URL, {
|
||||
log,
|
||||
pasv_range: process.env.PASV_RANGE
|
||||
pasv_range: process.env.PASV_RANGE,
|
||||
tls: {
|
||||
key: `${process.cwd()}/test/cert/server.key`,
|
||||
cert: `${process.cwd()}/test/cert/server.crt`,
|
||||
ca: `${process.cwd()}/test/cert/server.csr`
|
||||
}
|
||||
});
|
||||
server.on('login', (data, resolve) => {
|
||||
resolve({root: process.cwd()});
|
||||
});
|
||||
process.on('SIGINT', function () {
|
||||
server.close();
|
||||
});
|
||||
|
||||
require('child_process').exec(`sudo kill $(sudo lsof -t -i:${server.url.port})`, () => {
|
||||
server.listen()
|
||||
.finally(() => done());
|
||||
});
|
||||
server.listen()
|
||||
.then(() => done());
|
||||
});
|
||||
after(() => {
|
||||
server.close();
|
||||
@@ -38,6 +39,7 @@ describe('FtpServer', function () {
|
||||
expect(server).to.exist;
|
||||
client = new FtpClient();
|
||||
client.once('ready', () => done());
|
||||
client.once('error', err => done(err));
|
||||
client.connect({
|
||||
host: server.url.hostname,
|
||||
port: server.url.port,
|
||||
@@ -62,41 +64,41 @@ describe('FtpServer', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('CWD ..', done => {
|
||||
const dir = '..';
|
||||
client.cwd(`${dir}`, (err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.a('string');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('CWD test', done => {
|
||||
const dir = 'test';
|
||||
client.cwd(`${dir}`, (err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.a('string');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('PWD', done => {
|
||||
client.pwd((err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.a('string');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('LIST .', done => {
|
||||
client.list('.', (err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.an('array');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
const runFileSystemTests = () => {
|
||||
it('CWD ..', done => {
|
||||
const dir = '..';
|
||||
client.cwd(`${dir}`, (err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.a('string');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('CWD test', done => {
|
||||
const dir = 'test';
|
||||
client.cwd(`${dir}`, (err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.a('string');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('PWD', done => {
|
||||
client.pwd((err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.a('string');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('LIST .', done => {
|
||||
client.list('.', (err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.an('array');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('STOR test.txt', done => {
|
||||
const buffer = Buffer.from('test text file');
|
||||
client.put(buffer, 'test.txt', err => {
|
||||
@@ -183,6 +185,47 @@ describe('FtpServer', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('MKD tmp', done => {
|
||||
if (fs.existsSync('./test/tmp')) {
|
||||
fs.rmdirSync('./test/tmp');
|
||||
}
|
||||
client.mkdir('tmp', err => {
|
||||
expect(err).to.not.exist;
|
||||
expect(fs.existsSync('./test/tmp')).to.equal(true);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('CWD tmp', done => {
|
||||
client.cwd('tmp', (err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.a('string');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('CDUP', done => {
|
||||
client.cdup(err => {
|
||||
expect(err).to.not.exist;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('RMD tmp', done => {
|
||||
client.rmdir('tmp', err => {
|
||||
expect(err).to.not.exist;
|
||||
expect(fs.existsSync('./test/tmp')).to.equal(false);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('CDUP', done => {
|
||||
client.cdup(err => {
|
||||
expect(err).to.not.exist;
|
||||
done();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
it('TYPE A', done => {
|
||||
@@ -201,38 +244,31 @@ describe('FtpServer', function () {
|
||||
});
|
||||
runFileSystemTests();
|
||||
|
||||
it('MKD tmp', done => {
|
||||
if (fs.existsSync('./test/tmp')) {
|
||||
fs.rmdirSync('./test/tmp');
|
||||
}
|
||||
client.mkdir('tmp', err => {
|
||||
expect(err).to.not.exist;
|
||||
expect(fs.existsSync('./test/tmp')).to.equal(true);
|
||||
done();
|
||||
it('AUTH TLS', done => {
|
||||
client.end();
|
||||
client.once('close', () => {
|
||||
client = new FtpClient();
|
||||
client.once('ready', () => done());
|
||||
client.once('error', err => done(err));
|
||||
client.connect({
|
||||
host: server.url.hostname,
|
||||
port: server.url.port,
|
||||
user: 'test',
|
||||
password: 'test',
|
||||
secure: true,
|
||||
secureOptions: {
|
||||
rejectUnauthorized: false,
|
||||
checkServerIdentity: () => undefined
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
runFileSystemTests();
|
||||
|
||||
it('CWD tmp', done => {
|
||||
client.cwd('tmp', (err, data) => {
|
||||
expect(err).to.not.exist;
|
||||
expect(data).to.be.a('string');
|
||||
done();
|
||||
it('QUIT', done => {
|
||||
client.once('close', done);
|
||||
client.logout(err => {
|
||||
expect(err).to.be.undefined;
|
||||
});
|
||||
});
|
||||
|
||||
it('CDUP', done => {
|
||||
client.cdup(err => {
|
||||
expect(err).to.not.exist;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('RMD tmp', done => {
|
||||
client.rmdir('tmp', err => {
|
||||
expect(err).to.not.exist;
|
||||
expect(fs.existsSync('./test/tmp')).to.equal(false);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
5
test/mochabunyan.opts
Normal file
5
test/mochabunyan.opts
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"mute":false,
|
||||
"level":"fatal",
|
||||
"reporter":"spec"
|
||||
}
|
||||
@@ -4,13 +4,22 @@ const bunyan = require('bunyan');
|
||||
const FtpServer = require('../src');
|
||||
|
||||
const log = bunyan.createLogger({name: 'test'});
|
||||
log.level(process.env.LOG_LEVEL || 'trace');
|
||||
const server = new FtpServer(process.env.FTP_URL, {
|
||||
log.level('trace');
|
||||
const server = new FtpServer('ftp://127.0.0.1:8880', {
|
||||
log,
|
||||
pasv_range: process.env.PASV_RANGE
|
||||
pasv_range: 8881,
|
||||
greeting: ['Welcome', 'to', 'the', 'jungle!'],
|
||||
tls: {
|
||||
key: `${process.cwd()}/test/cert/server.key`,
|
||||
cert: `${process.cwd()}/test/cert/server.crt`,
|
||||
ca: `${process.cwd()}/test/cert/server.csr`
|
||||
},
|
||||
file_format: 'ep',
|
||||
anonymous: 'sillyrabbit'
|
||||
});
|
||||
server.on('login', ({username, password}, resolve, reject) => {
|
||||
if (username === 'test' && password === 'test') resolve({ root: require('os').homedir() });
|
||||
else reject('Bad username or password');
|
||||
if (username === 'test' && password === 'test' || username === 'anonymous') {
|
||||
resolve({ root: require('os').homedir() });
|
||||
} else reject('Bad username or password');
|
||||
});
|
||||
server.listen();
|
||||
|
||||
Reference in New Issue
Block a user