feat(cli): add cli support for ftp-srv

This commit is contained in:
Edin Mujagic
2018-05-05 14:48:50 +02:00
committed by Tyler Stewart
parent 3a7b3d4570
commit 87f3ae79a1
3 changed files with 114 additions and 1 deletions

View File

@@ -258,6 +258,16 @@ __Used in:__ `STOU`
See [CONTRIBUTING.md](CONTRIBUTING.md).
## CLI
Command-line interface
```bash
$ ftp-srv [opts] [path]
$ ftp-srv --help
```
path => defaults to pwd
<!--[]-->

98
bin/ftp-srv Executable file
View File

@@ -0,0 +1,98 @@
#!/usr/bin/env node
const yargs = require('yargs');
const path = require('path');
const FtpSrv = require('../src');
const args = setupYargs();
setupCredentials();
startFtpServer();
function setupYargs() {
return yargs
.option('address', {
alias: 'a',
default: 'ftp://0.0.0.0:9876',
describe: '{ftp|ftps}://{ip-address}:[port]',
type: 'string'
})
.option('username', {
describe: 'Blank for anonymous',
default: '',
type: 'string'
})
.option('password', {
default: '',
type: 'string'
})
.option('read-only', {
describe: 'Disable upload',
boolean: true,
default: false
})
.option('credentials', {
alias: 'c',
describe: 'Load user & pass from json file',
default: ''
})
.argv;
}
function setupCredentials() {
if (args.username.length === 0 && args.credentials.length > 0) {
const credentials = require(path.resolve(args.credentials));
if (credentials.username) {
args.username = credentials.username;
}
if (credentials.password) {
args.password = credentials.password;
}
}
}
function getPathToServe(args_) {
const path = args_['_'];
if (path.length === 0) {
return process.cwd();
} else {
return path[0];
}
}
function getBlacklistedForReadonlyCommands(isReadOnly) {
if (isReadOnly) {
return ['allo', 'appe', 'dele', 'mkd', 'rmd', 'rnfr', 'rnto', 'stor', 'stru']
.map(item => item.toUpperCase());
}
return [];
}
function startFtpServer() {
const ftpServer = new FtpSrv(args.address);
ftpServer.on('login', (data, resolve, reject) => {
const isAnonymous = args.username === '';
if (isAnonymous || (data.username === args.username && data.password === args.password)) {
resolve({
root: getPathToServe(args),
blacklist: getBlacklistedForReadonlyCommands(args.readOnly)
});
} else {
reject(new Error('Invalid username or password'));
}
});
ftpServer.listen();
}
process.on('SIGINT', function () {
logger.info(colors.red('ftp-srv stopped.'));
process.exit();
});
process.on('SIGTERM', function () {
logger.info(colors.red('ftp-srv stopped.'));
process.exit();
});

View File

@@ -14,6 +14,7 @@
"license": "MIT",
"files": [
"src",
"bin",
"ftp-srv.d.ts"
],
"main": "ftp-srv.js",
@@ -38,6 +39,9 @@
"verify:js:watch": "chokidar 'src/**/*.js' 'test/**/*.js' 'config/**/*.js' -c 'npm run verify:js:fix' --initial --silent",
"verify:watch": "npm run verify:js:watch --silent"
},
"bin": {
"ftp-srv": "./bin/ftp-srv"
},
"release": {
"verifyConditions": "condition-circle"
},
@@ -55,7 +59,8 @@
"ip": "^1.1.5",
"lodash": "^4.17.10",
"moment": "^2.22.1",
"uuid": "^3.2.1"
"uuid": "^3.2.1",
"yargs": "^11.0.0"
},
"devDependencies": {
"@icetee/ftp": "^1.0.2",