Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16dbc7895c | ||
|
|
94f0b893e4 | ||
|
|
79d7bd9062 | ||
|
|
44999c714d | ||
|
|
0d9131c370 | ||
|
|
eafaf6f642 | ||
|
|
ea99e6ebbd | ||
|
|
342911eb36 | ||
|
|
0094614ee3 | ||
|
|
0773967ba5 | ||
|
|
e5820688c4 | ||
|
|
a3fa1a2f71 | ||
|
|
e540822d5b | ||
|
|
36f331d15d |
@@ -1,103 +1,97 @@
|
||||
version: 2
|
||||
|
||||
create-cache-file: &create-cache-file
|
||||
run:
|
||||
name: Setup cache
|
||||
command: echo "$NODE_VERSION" > _cache_node_version
|
||||
|
||||
package-json-cache: &package-json-cache
|
||||
key: npm-install-{{ checksum "_cache_node_version" }}-{{ checksum "package-lock.json" }}
|
||||
|
||||
base-build: &base-build
|
||||
steps:
|
||||
- checkout
|
||||
- <<: *create-cache-file
|
||||
- restore_cache:
|
||||
<<: *package-json-cache
|
||||
- run:
|
||||
name: Install
|
||||
command: npm install
|
||||
- save_cache:
|
||||
<<: *package-json-cache
|
||||
paths:
|
||||
- node_modules
|
||||
- run:
|
||||
name: Lint
|
||||
command: npm run verify:js
|
||||
- run:
|
||||
name: Test
|
||||
command: npm run test:once
|
||||
|
||||
jobs:
|
||||
test_node_10:
|
||||
build:
|
||||
docker:
|
||||
- image: circleci/node:10
|
||||
environment:
|
||||
- NODE_VERSION: 10
|
||||
<<: *base-build
|
||||
- image: &node-image circleci/node:lts
|
||||
steps:
|
||||
- checkout
|
||||
- restore_cache:
|
||||
keys:
|
||||
- &npm-cache-key npm-cache-{{ .Branch }}-{{ .Revision }}
|
||||
- npm-cache-{{ .Branch }}
|
||||
- npm-cache
|
||||
- run:
|
||||
name: Install
|
||||
command: npm ci
|
||||
- persist_to_workspace:
|
||||
root: .
|
||||
paths:
|
||||
- node_modules
|
||||
- save_cache:
|
||||
key: *npm-cache-key
|
||||
paths:
|
||||
- ~/.npm/_cacache
|
||||
|
||||
test_node_8:
|
||||
lint:
|
||||
docker:
|
||||
- image: circleci/node:8
|
||||
environment:
|
||||
- NODE_VERSION: 8
|
||||
<<: *base-build
|
||||
- image: *node-image
|
||||
steps:
|
||||
- checkout
|
||||
- attach_workspace:
|
||||
at: .
|
||||
- run:
|
||||
name: Lint
|
||||
command: npm run verify
|
||||
|
||||
test_node_6:
|
||||
test:
|
||||
docker:
|
||||
- image: circleci/node:6
|
||||
environment:
|
||||
- NODE_VERSION: 6
|
||||
<<: *base-build
|
||||
- image: *node-image
|
||||
steps:
|
||||
- checkout
|
||||
- attach_workspace:
|
||||
at: .
|
||||
- deploy:
|
||||
name: Test
|
||||
command: |
|
||||
npm run test
|
||||
|
||||
release_dry_run:
|
||||
docker:
|
||||
- image: *node-image
|
||||
steps:
|
||||
- checkout
|
||||
- attach_workspace:
|
||||
at: .
|
||||
- deploy:
|
||||
name: Dry Release
|
||||
command: |
|
||||
npm run semantic-release -- --dry-run
|
||||
|
||||
release:
|
||||
docker:
|
||||
- image: circleci/node:8
|
||||
environment:
|
||||
- NODE_VERSION: 8
|
||||
- image: *node-image
|
||||
steps:
|
||||
- checkout
|
||||
- <<: *create-cache-file
|
||||
- restore_cache:
|
||||
<<: *package-json-cache
|
||||
- attach_workspace:
|
||||
at: .
|
||||
- deploy:
|
||||
name: Semantic Release
|
||||
name: Release
|
||||
command: |
|
||||
npm run semantic-release
|
||||
|
||||
workflows:
|
||||
version: 2
|
||||
test_and_tag:
|
||||
publish:
|
||||
jobs:
|
||||
- test_node_10:
|
||||
filters:
|
||||
branches:
|
||||
only: master
|
||||
- test_node_8:
|
||||
filters:
|
||||
branches:
|
||||
only: master
|
||||
- test_node_6:
|
||||
- build
|
||||
- lint:
|
||||
requires:
|
||||
- build
|
||||
- test:
|
||||
requires:
|
||||
- build
|
||||
- release_dry_run:
|
||||
filters:
|
||||
branches:
|
||||
only: master
|
||||
requires:
|
||||
- test
|
||||
- lint
|
||||
- hold_release:
|
||||
type: approval
|
||||
requires:
|
||||
- release_dry_run
|
||||
- release:
|
||||
requires:
|
||||
- test_node_6
|
||||
- test_node_8
|
||||
- test_node_10
|
||||
|
||||
build_and_test:
|
||||
jobs:
|
||||
- test_node_10:
|
||||
filters:
|
||||
branches:
|
||||
ignore: master
|
||||
- test_node_8:
|
||||
filters:
|
||||
branches:
|
||||
ignore: master
|
||||
- test_node_6:
|
||||
filters:
|
||||
branches:
|
||||
ignore: master
|
||||
- hold_release
|
||||
|
||||
@@ -1,9 +1,2 @@
|
||||
# START_CONFIT_GENERATED_CONTENT
|
||||
# Common folders to ignore
|
||||
node_modules/*
|
||||
bower_components/*
|
||||
|
||||
# Config folder (optional - you might want to lint this...)
|
||||
config/*
|
||||
|
||||
# END_CONFIT_GENERATED_CONTENT
|
||||
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,7 +1,6 @@
|
||||
test_tmp/
|
||||
|
||||
node_modules/
|
||||
|
||||
dist/
|
||||
reports/
|
||||
npm-debug.log
|
||||
.nyc_output/
|
||||
test_tmp/
|
||||
@@ -16,5 +16,10 @@
|
||||
|
||||
- Any new fixes are features should include new or updated [tests](/test).
|
||||
- Commits follow the [AngularJS Git Commit Message Conventions](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit), please review and commit accordingly
|
||||
- Submit your pull requests to the `master` branch, these will normally be merged into a seperate branch for any finally changes before being merged into `master`.
|
||||
- Submit any bugs or requests to the issues page in Github.
|
||||
- Submit your pull requests to the `master` branch, these will normally be merged into a separate branch for any finally changes before being merged into `master`.
|
||||
- Submit any bugs or requests to the issues page in Github.
|
||||
|
||||
## Setup
|
||||
|
||||
- Clone the repository `git clone`
|
||||
- Install dependencies `npm install`
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Tyler Stewart
|
||||
Copyright (c) 2019 Tyler Stewart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
30
README.md
30
README.md
@@ -124,6 +124,10 @@ __Allowable values:__
|
||||
#### `log`
|
||||
A [bunyan logger](https://github.com/trentm/node-bunyan) instance. Created by default.
|
||||
|
||||
#### `timeout`
|
||||
Sets the timeout (in ms) after that an idle connection is closed by the server
|
||||
__Default:__ `0`
|
||||
|
||||
## CLI
|
||||
|
||||
`ftp-srv` also comes with a builtin CLI.
|
||||
@@ -242,7 +246,7 @@ connection.on('RNTO', (error, fileName) => { ... });
|
||||
|
||||
Occurs when a file is renamed.
|
||||
|
||||
`error` if successful, will be `null`
|
||||
`error` if successful, will be `null`
|
||||
`fileName` name of the file that was renamed
|
||||
|
||||
## Supported Commands
|
||||
@@ -321,32 +325,34 @@ __Used in:__ `SITE CHMOD`
|
||||
Returns a unique file name to write to
|
||||
__Used in:__ `STOU`
|
||||
|
||||
<!--[RM_CONTRIBUTING]-->
|
||||
## Contributing
|
||||
|
||||
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
||||
|
||||
<!--[]-->
|
||||
|
||||
## Contributors
|
||||
|
||||
- [OzairP](https://github.com/OzairP)
|
||||
- [qchar](https://github.com/qchar)
|
||||
- [jorinvo](https://github.com/jorinvo)
|
||||
- [voxsoftware](https://github.com/voxsoftware)
|
||||
- [pkeuter](https://github.com/pkeuter)
|
||||
- [TimLuq](https://github.com/TimLuq)
|
||||
- [edin-mg](https://github.com/edin-m)
|
||||
- [crabl](https://github.com/crabl)
|
||||
- [hirviid](https://github.com/hirviid)
|
||||
- [DiegoRBaquero](https://github.com/DiegoRBaquero)
|
||||
- [edin-m](https://github.com/edin-m)
|
||||
- [voxsoftware](https://github.com/voxsoftware)
|
||||
- [jorinvo](https://github.com/jorinvo)
|
||||
- [Johnnyrook777](https://github.com/Johnnyrook777)
|
||||
- [qchar](https://github.com/qchar)
|
||||
- [mikejestes](https://github.com/mikejestes)
|
||||
- [pkeuter](https://github.com/pkeuter)
|
||||
- [qiansc](https://github.com/qiansc)
|
||||
- [broofa](https://github.com/broofa)
|
||||
- [lafin](https://github.com/lafin)
|
||||
- [alancnet](https://github.com/alancnet)
|
||||
- [zgwit](https://github.com/zgwit)
|
||||
|
||||
<!--[RM_LICENSE]-->
|
||||
## License
|
||||
|
||||
This software is licensed under the MIT Licence. See [LICENSE](LICENSE).
|
||||
|
||||
<!--[]-->
|
||||
|
||||
## References
|
||||
|
||||
- [https://cr.yp.to/ftp.html](https://cr.yp.to/ftp.html)
|
||||
|
||||
10
bin/index.js
10
bin/index.js
@@ -113,11 +113,15 @@ function setupState(_args) {
|
||||
}
|
||||
|
||||
function startFtpServer(_state) {
|
||||
// Remove null/undefined options so they get set to defaults, below
|
||||
for (const key in _state) {
|
||||
if (_state[key] === undefined) delete _state[key];
|
||||
}
|
||||
|
||||
function checkLogin(data, resolve, reject) {
|
||||
const user = _state.credentials[data.username]
|
||||
if (_state.anonymous || (user && user.password === data.password)) {
|
||||
return resolve({root: (user && user.root) || _state.root});
|
||||
const user = _state.credentials[data.username];
|
||||
if (_state.anonymous || user && user.password === data.password) {
|
||||
return resolve({root: user && user.root || _state.root});
|
||||
}
|
||||
|
||||
return reject(new errors.GeneralError('Invalid username or password', 401));
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
|
||||
types: [
|
||||
{value: 'feat', name: 'feat: A new feature'},
|
||||
{value: 'fix', name: 'fix: A bug fix'},
|
||||
{value: 'docs', name: 'docs: Documentation only changes'},
|
||||
{value: 'style', name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)'},
|
||||
{value: 'refactor', name: 'refactor: A code change that neither fixes a bug nor adds a feature'},
|
||||
{value: 'perf', name: 'perf: A code change that improves performance'},
|
||||
{value: 'test', name: 'test: Adding missing tests'},
|
||||
{value: 'chore', name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation'},
|
||||
{value: 'revert', name: 'revert: Revert to a commit'},
|
||||
{value: 'WIP', name: 'WIP: Work in progress'}
|
||||
],
|
||||
|
||||
scopes: [],
|
||||
|
||||
// it needs to match the value for field type. Eg.: 'fix'
|
||||
/*
|
||||
scopeOverrides: {
|
||||
fix: [
|
||||
|
||||
{name: 'merge'},
|
||||
{name: 'style'},
|
||||
{name: 'e2eTest'},
|
||||
{name: 'unitTest'}
|
||||
]
|
||||
},
|
||||
*/
|
||||
|
||||
allowCustomScopes: true,
|
||||
allowBreakingChanges: ['feat', 'fix'],
|
||||
|
||||
// Appends the branch name to the footer of the commit. Useful for tracking commits after branches have been merged
|
||||
appendBranchNameToCommitMessage: false
|
||||
};
|
||||
@@ -1,5 +0,0 @@
|
||||
test/**/*.spec.js
|
||||
--reporter mocha-multi-reporters
|
||||
--reporter-options configFile=config/testUnit/reporters.json
|
||||
--ui bdd
|
||||
--bail
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"reporterEnabled": "spec",
|
||||
"mochaJunitReporterReporterOptions": {
|
||||
"mochaFile": "reports/junit.xml"
|
||||
}
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
{
|
||||
"extends": "eslint:recommended",
|
||||
"env": {
|
||||
"node": true,
|
||||
"mocha": true,
|
||||
"es6": true
|
||||
},
|
||||
"plugins": [
|
||||
"mocha",
|
||||
"node"
|
||||
],
|
||||
"rules": {
|
||||
"mocha/no-exclusive-tests": 2,
|
||||
"no-warning-comments": [
|
||||
1,
|
||||
{
|
||||
"terms": ["todo", "fixme", "xxx"],
|
||||
"location": "start"
|
||||
},
|
||||
],
|
||||
"object-curly-spacing": [
|
||||
2,
|
||||
"never"
|
||||
],
|
||||
"array-bracket-spacing": [
|
||||
2,
|
||||
"never"
|
||||
],
|
||||
"brace-style": [
|
||||
2,
|
||||
"1tbs"
|
||||
],
|
||||
"consistent-return": 0,
|
||||
"indent": [
|
||||
"error",
|
||||
2,
|
||||
{
|
||||
"SwitchCase": 1,
|
||||
"MemberExpression": "off"
|
||||
}
|
||||
],
|
||||
"no-multiple-empty-lines": [
|
||||
2,
|
||||
{
|
||||
"max": 2
|
||||
}
|
||||
],
|
||||
"no-use-before-define": [
|
||||
2,
|
||||
"nofunc"
|
||||
],
|
||||
"one-var": [
|
||||
2,
|
||||
"never"
|
||||
],
|
||||
"quote-props": [
|
||||
2,
|
||||
"as-needed"
|
||||
],
|
||||
"quotes": [
|
||||
2,
|
||||
"single"
|
||||
],
|
||||
"keyword-spacing": 2,
|
||||
"space-before-function-paren": [
|
||||
2,
|
||||
{
|
||||
"anonymous": "always",
|
||||
"named": "never"
|
||||
}
|
||||
],
|
||||
"space-in-parens": [
|
||||
2,
|
||||
"never"
|
||||
],
|
||||
"strict": [
|
||||
2,
|
||||
"global"
|
||||
],
|
||||
"curly": [
|
||||
2,
|
||||
"multi-line"
|
||||
],
|
||||
"eol-last": 2,
|
||||
"key-spacing": [
|
||||
2,
|
||||
{
|
||||
"beforeColon": false,
|
||||
"afterColon": true
|
||||
}
|
||||
],
|
||||
"no-eval": 2,
|
||||
"no-with": 2,
|
||||
"space-infix-ops": 2,
|
||||
"dot-notation": [
|
||||
2,
|
||||
{
|
||||
"allowKeywords": true
|
||||
}
|
||||
],
|
||||
"eqeqeq": 2,
|
||||
"no-alert": 2,
|
||||
"no-caller": 2,
|
||||
"no-extend-native": 2,
|
||||
"no-extra-bind": 2,
|
||||
"no-implied-eval": 2,
|
||||
"no-iterator": 2,
|
||||
"no-label-var": 2,
|
||||
"no-labels": 2,
|
||||
"no-lone-blocks": 2,
|
||||
"no-loop-func": 2,
|
||||
"no-multi-spaces": 2,
|
||||
"no-multi-str": 2,
|
||||
"no-native-reassign": 2,
|
||||
"no-new": 2,
|
||||
"no-new-func": 2,
|
||||
"no-new-wrappers": 2,
|
||||
"no-octal-escape": 2,
|
||||
"no-proto": 2,
|
||||
"no-return-assign": 2,
|
||||
"no-script-url": 2,
|
||||
"no-sequences": 2,
|
||||
"no-unused-expressions": 2,
|
||||
"yoda": 2,
|
||||
"no-shadow": 2,
|
||||
"no-shadow-restricted-names": 2,
|
||||
"no-undef-init": 2,
|
||||
"no-console": 1,
|
||||
"camelcase": [
|
||||
0,
|
||||
{
|
||||
"properties": "never"
|
||||
}
|
||||
],
|
||||
"comma-spacing": 2,
|
||||
"comma-dangle": 1,
|
||||
"new-cap": 2,
|
||||
"new-parens": 2,
|
||||
"arrow-parens": [2, "always"],
|
||||
"no-array-constructor": 2,
|
||||
"array-callback-return": 1,
|
||||
"no-extra-parens": 2,
|
||||
"no-new-object": 2,
|
||||
"no-spaced-func": 2,
|
||||
"no-trailing-spaces": 2,
|
||||
"no-underscore-dangle": 0,
|
||||
"no-fallthrough": 0,
|
||||
"semi": 2,
|
||||
"semi-spacing": [
|
||||
2,
|
||||
{
|
||||
"before": false,
|
||||
"after": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"parserOptions": {
|
||||
"emcaVersion": 6,
|
||||
"sourceType": "module",
|
||||
"impliedStrict": true
|
||||
}
|
||||
}
|
||||
3
ftp-srv.d.ts
vendored
3
ftp-srv.d.ts
vendored
@@ -69,7 +69,8 @@ export interface FtpServerOptions {
|
||||
blacklist?: Array<string>,
|
||||
whitelist?: Array<string>,
|
||||
file_format?: (stat: Stats) => string | Promise<string> | "ls" | "ep",
|
||||
log?: any,
|
||||
log?: any,
|
||||
timeout?: number
|
||||
}
|
||||
|
||||
export class FtpServer extends EventEmitter {
|
||||
|
||||
27
meta/contributors/contributors.js
Normal file
27
meta/contributors/contributors.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const {get} = require('https');
|
||||
|
||||
get('https://api.github.com/repos/trs/ftp-srv/contributors', {
|
||||
headers: {
|
||||
'User-Agent': 'Chrome'
|
||||
}
|
||||
}, (res) => {
|
||||
let response = '';
|
||||
res.on('data', (data) => {
|
||||
response += data;
|
||||
});
|
||||
res.on('end', () => {
|
||||
const contributors = JSON.parse(response)
|
||||
.filter((contributor) => contributor.type === 'User');
|
||||
|
||||
for (const contributor of contributors) {
|
||||
const url = contributor.html_url;
|
||||
const username = contributor.login;
|
||||
|
||||
const markdown = `- [${username}](${url})\n`;
|
||||
|
||||
process.stdout.write(markdown);
|
||||
}
|
||||
});
|
||||
}).on('error', (err) => {
|
||||
process.stderr.write(err);
|
||||
});
|
||||
6440
package-lock.json
generated
6440
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
73
package.json
73
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ftp-srv",
|
||||
"version": "0.0.0-development",
|
||||
"version": "0.0.0",
|
||||
"description": "Modern, extensible FTP Server",
|
||||
"keywords": [
|
||||
"ftp",
|
||||
@@ -26,26 +26,40 @@
|
||||
},
|
||||
"scripts": {
|
||||
"pre-release": "npm run verify",
|
||||
"commitmsg": "cz-customizable-ghooks",
|
||||
"dev": "cross-env NODE_ENV=development npm run verify:watch",
|
||||
"prepush": "npm run verify && npm run test:once --silent",
|
||||
"semantic-release": "semantic-release",
|
||||
"start": "npm run dev",
|
||||
"test": "cross-env NODE_ENV=test mocha --opts config/testUnit/mocha.opts -w",
|
||||
"test:once": "cross-env NODE_ENV=test mocha --opts config/testUnit/mocha.opts",
|
||||
"verify": "npm run verify:js --silent",
|
||||
"verify:js": "eslint -c config/verify/.eslintrc \"src/**/*.js\" \"test/**/*.js\" \"config/**/*.js\" && echo ✅ verify:js success",
|
||||
"verify:js:fix": "eslint --fix -c config/verify/.eslintrc \"src/**/*.js\" \"test/**/*.js\" \"config/**/*.js\" && echo ✅ verify:js:fix success"
|
||||
"test": "mocha test/**/*.spec.js test/*.spec.js --ui bdd",
|
||||
"verify": "eslint src/**/*.js test/**/*.js bin/**/*.js"
|
||||
},
|
||||
"release": {
|
||||
"verifyConditions": "condition-circle"
|
||||
},
|
||||
"config": {
|
||||
"commitizen": {
|
||||
"path": "node_modules/cz-customizable"
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "lint-staged",
|
||||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
||||
}
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.js": [
|
||||
"eslint --fix",
|
||||
"git add"
|
||||
]
|
||||
},
|
||||
"commitlint": {
|
||||
"extends": [
|
||||
"@commitlint/config-conventional"
|
||||
]
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "eslint:recommended",
|
||||
"env": {
|
||||
"node": true,
|
||||
"mocha": true,
|
||||
"es6": true
|
||||
},
|
||||
"cz-customizable": {
|
||||
"config": "config/release/commitMessageConfig.js"
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 6,
|
||||
"sourceType": "module"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -58,29 +72,20 @@
|
||||
"yargs": "^11.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^7.5.2",
|
||||
"@commitlint/config-conventional": "^7.5.0",
|
||||
"@icetee/ftp": "^1.0.2",
|
||||
"chai": "^4.0.2",
|
||||
"condition-circle": "^1.6.0",
|
||||
"cross-env": "3.1.4",
|
||||
"cz-customizable": "5.2.0",
|
||||
"cz-customizable-ghooks": "1.5.0",
|
||||
"dotenv": "^4.0.0",
|
||||
"eslint": "4.5.0",
|
||||
"eslint-config-google": "0.8.0",
|
||||
"eslint-friendly-formatter": "3.0.0",
|
||||
"eslint-plugin-mocha": "^4.11.0",
|
||||
"eslint-plugin-node": "5.1.1",
|
||||
"husky": "0.13.3",
|
||||
"istanbul": "0.4.5",
|
||||
"chai": "^4.2.0",
|
||||
"condition-circle": "^2.0.2",
|
||||
"eslint": "^5.14.1",
|
||||
"husky": "^1.3.1",
|
||||
"lint-staged": "^8.1.4",
|
||||
"mocha": "^5.2.0",
|
||||
"mocha-junit-reporter": "1.13.0",
|
||||
"mocha-multi-reporters": "1.1.5",
|
||||
"rimraf": "2.6.1",
|
||||
"semantic-release": "^15.10.6",
|
||||
"rimraf": "^2.6.1",
|
||||
"semantic-release": "^15.13.16",
|
||||
"sinon": "^2.3.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.x",
|
||||
"npm": ">=5.x"
|
||||
"node": ">=6.x"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,11 @@ const PassiveConnector = require('../../connector/passive');
|
||||
|
||||
module.exports = {
|
||||
directive: 'PASV',
|
||||
handler: function () {
|
||||
handler: function ({log} = {}) {
|
||||
if (!this.server.options.pasv_url) {
|
||||
return this.reply(502);
|
||||
}
|
||||
|
||||
this.connector = new PassiveConnector(this);
|
||||
return this.connector.setupServer()
|
||||
.then((server) => {
|
||||
@@ -13,6 +17,10 @@ module.exports = {
|
||||
const portByte2 = port % 256;
|
||||
|
||||
return this.reply(227, `PASV OK (${host},${portByte1},${portByte2})`);
|
||||
})
|
||||
.catch((err) => {
|
||||
log.error(err);
|
||||
return this.reply(425);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}}',
|
||||
|
||||
@@ -3,7 +3,7 @@ const ActiveConnector = require('../../connector/active');
|
||||
|
||||
module.exports = {
|
||||
directive: 'PORT',
|
||||
handler: function ({command} = {}) {
|
||||
handler: function ({log, command} = {}) {
|
||||
this.connector = new ActiveConnector(this);
|
||||
|
||||
const rawConnection = _.get(command, 'arg', '').split(',');
|
||||
@@ -14,7 +14,11 @@ module.exports = {
|
||||
const port = portBytes[0] * 256 + portBytes[1];
|
||||
|
||||
return this.connector.setupConnection(ip, port)
|
||||
.then(() => this.reply(200));
|
||||
.then(() => this.reply(200))
|
||||
.catch((err) => {
|
||||
log.error(err);
|
||||
return this.reply(425);
|
||||
});
|
||||
},
|
||||
syntax: '{{cmd}} <x>,<x>,<x>,<x>,<y>,<y>',
|
||||
description: 'Specifies an address and port to which the server should connect'
|
||||
|
||||
@@ -21,11 +21,14 @@ module.exports = {
|
||||
const serverPath = stream.path || fileName;
|
||||
|
||||
const destroyConnection = (connection, reject) => (err) => {
|
||||
if (connection) {
|
||||
if (connection.writable) connection.end();
|
||||
connection.destroy(err);
|
||||
try {
|
||||
if (connection) {
|
||||
if (connection.writable) connection.end();
|
||||
connection.destroy(err);
|
||||
}
|
||||
} finally {
|
||||
reject(err);
|
||||
}
|
||||
reject(err);
|
||||
};
|
||||
|
||||
const streamPromise = new Promise((resolve, reject) => {
|
||||
|
||||
@@ -30,8 +30,8 @@ class FtpConnection extends EventEmitter {
|
||||
this.server.emit('client-error', {connection: this, context: 'commandSocket', error: err});
|
||||
});
|
||||
this.commandSocket.on('data', this._handleData.bind(this));
|
||||
this.commandSocket.on('timeout', (err) => {
|
||||
this.log.trace(err, 'Client timeout');
|
||||
this.commandSocket.on('timeout', () => {
|
||||
this.log.trace('Client timeout');
|
||||
this.close().catch((e) => this.log.trace(e, 'Client close error'));
|
||||
});
|
||||
this.commandSocket.on('close', () => {
|
||||
|
||||
@@ -1,33 +1,58 @@
|
||||
const net = require('net');
|
||||
const Promise = require('bluebird');
|
||||
const errors = require('../errors');
|
||||
|
||||
function* portNumberGenerator(min, max) {
|
||||
const MAX_PORT = 65535;
|
||||
const MAX_PORT_CHECK_ATTEMPT = 5;
|
||||
|
||||
function* portNumberGenerator(min, max = MAX_PORT) {
|
||||
let current = min;
|
||||
while (true) {
|
||||
if (current > 65535 || current > max) {
|
||||
if (current > MAX_PORT || current > max) {
|
||||
current = min;
|
||||
}
|
||||
yield current++;
|
||||
}
|
||||
}
|
||||
|
||||
function getNextPortFactory(min, max = Infinity) {
|
||||
const nextPortNumber = portNumberGenerator(min, max);
|
||||
const portCheckServer = net.createServer();
|
||||
portCheckServer.maxConnections = 0;
|
||||
portCheckServer.on('error', () => {
|
||||
portCheckServer.listen(nextPortNumber.next().value);
|
||||
});
|
||||
function getNextPortFactory(host, portMin, portMax, maxAttempts = MAX_PORT_CHECK_ATTEMPT) {
|
||||
const nextPortNumber = portNumberGenerator(portMin, portMax);
|
||||
|
||||
return () => new Promise((resolve) => {
|
||||
portCheckServer.once('listening', () => {
|
||||
const {port} = portCheckServer.address();
|
||||
portCheckServer.close(() => resolve(port));
|
||||
});
|
||||
portCheckServer.listen(nextPortNumber.next().value);
|
||||
})
|
||||
.catch(RangeError, (err) => Promise.reject(new errors.ConnectorError(err.message)));
|
||||
return () => new Promise((resolve, reject) => {
|
||||
const portCheckServer = net.createServer();
|
||||
portCheckServer.maxConnections = 0;
|
||||
|
||||
let attemptCount = 0;
|
||||
const tryGetPort = () => {
|
||||
attemptCount++;
|
||||
if (attemptCount > maxAttempts) {
|
||||
reject(new errors.ConnectorError('Unable to find valid port'));
|
||||
return;
|
||||
}
|
||||
|
||||
const {value: port} = nextPortNumber.next();
|
||||
|
||||
portCheckServer.removeAllListeners();
|
||||
portCheckServer.once('error', (err) => {
|
||||
if (['EADDRINUSE'].includes(err.code)) {
|
||||
tryGetPort();
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
portCheckServer.once('listening', () => {
|
||||
portCheckServer.removeAllListeners();
|
||||
portCheckServer.close(() => resolve(port));
|
||||
});
|
||||
|
||||
try {
|
||||
portCheckServer.listen(port, host);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
|
||||
tryGetPort();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
const http = require('http');
|
||||
const Promise = require('bluebird');
|
||||
const errors = require('../errors');
|
||||
|
||||
const IP_WEBSITE = 'http://api.ipify.org/';
|
||||
|
||||
module.exports = function (hostname) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!hostname || hostname === '0.0.0.0') {
|
||||
let ip = '';
|
||||
http.get(IP_WEBSITE, (response) => {
|
||||
if (response.statusCode !== 200) {
|
||||
return reject(new errors.GeneralError('Unable to resolve hostname', response.statusCode));
|
||||
}
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', (chunk) => {
|
||||
ip += chunk;
|
||||
});
|
||||
response.on('end', () => {
|
||||
resolve(ip);
|
||||
});
|
||||
});
|
||||
} else resolve(hostname);
|
||||
});
|
||||
};
|
||||
38
src/index.js
38
src/index.js
@@ -7,7 +7,6 @@ const tls = require('tls');
|
||||
const EventEmitter = require('events');
|
||||
|
||||
const Connection = require('./connection');
|
||||
const resolveHost = require('./helpers/resolve-host');
|
||||
const {getNextPortFactory} = require('./helpers/find-port');
|
||||
|
||||
class FtpServer extends EventEmitter {
|
||||
@@ -24,7 +23,8 @@ class FtpServer extends EventEmitter {
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
greeting: null,
|
||||
tls: false
|
||||
tls: false,
|
||||
timeout: 0
|
||||
}, options);
|
||||
|
||||
this._greeting = this.setupGreeting(this.options.greeting);
|
||||
@@ -36,10 +36,15 @@ class FtpServer extends EventEmitter {
|
||||
this.log = this.options.log;
|
||||
this.url = nodeUrl.parse(this.options.url);
|
||||
this.getNextPasvPort = getNextPortFactory(
|
||||
_.get(this, 'url.hostname'),
|
||||
_.get(this, 'options.pasv_min'),
|
||||
_.get(this, 'options.pasv_max'));
|
||||
|
||||
const timeout = Number(this.options.timeout);
|
||||
this.options.timeout = isNaN(timeout) ? 0 : Number(timeout);
|
||||
|
||||
const serverConnectionHandler = (socket) => {
|
||||
socket.setTimeout(this.options.timeout);
|
||||
let connection = new Connection(this, {log: this.log, socket});
|
||||
this.connections[connection.id] = connection;
|
||||
|
||||
@@ -67,22 +72,21 @@ class FtpServer extends EventEmitter {
|
||||
}
|
||||
|
||||
listen() {
|
||||
return resolveHost(this.options.pasv_url || this.url.hostname)
|
||||
.then((pasvUrl) => {
|
||||
this.options.pasv_url = pasvUrl;
|
||||
if (!this.options.pasv_url) {
|
||||
this.log.warn('Passive URL not set. Passive connections not available.');
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.server.once('error', reject);
|
||||
this.server.listen(this.url.port, this.url.hostname, (err) => {
|
||||
this.server.removeListener('error', reject);
|
||||
if (err) return reject(err);
|
||||
this.log.info({
|
||||
protocol: this.url.protocol.replace(/\W/g, ''),
|
||||
ip: this.url.hostname,
|
||||
port: this.url.port
|
||||
}, 'Listening');
|
||||
resolve('Listening');
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
this.server.once('error', reject);
|
||||
this.server.listen(this.url.port, this.url.hostname, (err) => {
|
||||
this.server.removeListener('error', reject);
|
||||
if (err) return reject(err);
|
||||
this.log.info({
|
||||
protocol: this.url.protocol.replace(/\W/g, ''),
|
||||
ip: this.url.hostname,
|
||||
port: this.url.port
|
||||
}, 'Listening');
|
||||
resolve('Listening');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ const ActiveConnector = require('../../src/connector/active');
|
||||
const {getNextPortFactory} = require('../../src/helpers/find-port');
|
||||
|
||||
describe('Connector - Active //', function () {
|
||||
let getNextPort = getNextPortFactory(1024);
|
||||
const host = '127.0.0.1';
|
||||
let getNextPort = getNextPortFactory(host, 1024);
|
||||
let PORT;
|
||||
let active;
|
||||
let mockConnection = {};
|
||||
@@ -36,7 +37,7 @@ describe('Connector - Active //', function () {
|
||||
});
|
||||
|
||||
it('sets up a connection', function () {
|
||||
return active.setupConnection('127.0.0.1', PORT)
|
||||
return active.setupConnection(host, PORT)
|
||||
.then(() => {
|
||||
expect(active.dataSocket).to.exist;
|
||||
});
|
||||
@@ -45,7 +46,7 @@ describe('Connector - Active //', function () {
|
||||
it('destroys existing connection, then sets up a connection', function () {
|
||||
const destroyFnSpy = sandbox.spy(active.dataSocket, 'destroy');
|
||||
|
||||
return active.setupConnection('127.0.0.1', PORT)
|
||||
return active.setupConnection(host, PORT)
|
||||
.then(() => {
|
||||
expect(destroyFnSpy.callCount).to.equal(1);
|
||||
expect(active.dataSocket).to.exist;
|
||||
@@ -53,7 +54,7 @@ describe('Connector - Active //', function () {
|
||||
});
|
||||
|
||||
it('waits for connection', function () {
|
||||
return active.setupConnection('127.0.0.1', PORT)
|
||||
return active.setupConnection(host, PORT)
|
||||
.then(() => {
|
||||
expect(active.dataSocket).to.exist;
|
||||
return active.waitForConnection();
|
||||
@@ -73,7 +74,7 @@ describe('Connector - Active //', function () {
|
||||
}
|
||||
};
|
||||
|
||||
return active.setupConnection('127.0.0.1', PORT)
|
||||
return active.setupConnection(host, PORT)
|
||||
.then(() => {
|
||||
expect(active.dataSocket).to.exist;
|
||||
return active.waitForConnection();
|
||||
|
||||
@@ -10,6 +10,7 @@ const PassiveConnector = require('../../src/connector/passive');
|
||||
const {getNextPortFactory} = require('../../src/helpers/find-port');
|
||||
|
||||
describe('Connector - Passive //', function () {
|
||||
const host = '127.0.0.1';
|
||||
let mockConnection = {
|
||||
reply: () => Promise.resolve({}),
|
||||
close: () => Promise.resolve({}),
|
||||
@@ -20,7 +21,7 @@ describe('Connector - Passive //', function () {
|
||||
},
|
||||
server: {
|
||||
url: '',
|
||||
getNextPasvPort: getNextPortFactory(1024)
|
||||
getNextPasvPort: getNextPortFactory(host, 1024)
|
||||
}
|
||||
};
|
||||
let sandbox;
|
||||
@@ -48,7 +49,7 @@ describe('Connector - Passive //', function () {
|
||||
|
||||
describe('setup', function () {
|
||||
before(function () {
|
||||
sandbox.stub(mockConnection.server, 'getNextPasvPort').value(getNextPortFactory());
|
||||
sandbox.stub(mockConnection.server, 'getNextPasvPort').value(getNextPortFactory(host));
|
||||
});
|
||||
|
||||
it('no pasv range provided', function (done) {
|
||||
@@ -56,7 +57,7 @@ describe('Connector - Passive //', function () {
|
||||
passive.setupServer()
|
||||
.catch((err) => {
|
||||
try {
|
||||
expect(err.name).to.equal('ConnectorError');
|
||||
expect(err.name).to.contain('RangeError');
|
||||
done();
|
||||
} catch (ex) {
|
||||
done(ex);
|
||||
@@ -68,7 +69,7 @@ describe('Connector - Passive //', function () {
|
||||
describe('setup', function () {
|
||||
let connection;
|
||||
before(function () {
|
||||
sandbox.stub(mockConnection.server, 'getNextPasvPort').value(getNextPortFactory(-1, -1));
|
||||
sandbox.stub(mockConnection.server, 'getNextPasvPort').value(getNextPortFactory(host, -1, -1));
|
||||
|
||||
connection = new PassiveConnector(mockConnection);
|
||||
});
|
||||
@@ -76,7 +77,7 @@ describe('Connector - Passive //', function () {
|
||||
it('has invalid pasv range', function (done) {
|
||||
connection.setupServer()
|
||||
.catch((err) => {
|
||||
expect(err.name).to.equal('ConnectorError');
|
||||
expect(err.name).to.contain('RangeError');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,52 +1,55 @@
|
||||
/* eslint no-unused-expressions: 0 */
|
||||
const {expect} = require('chai');
|
||||
const net = require('net');
|
||||
const sinon = require('sinon');
|
||||
|
||||
const {getNextPortFactory} = require('../../src/helpers/find-port');
|
||||
const {getNextPortFactory, portNumberGenerator} = require('../../src/helpers/find-port');
|
||||
|
||||
describe('helpers // find-port', function () {
|
||||
let sandbox;
|
||||
let getNextPort;
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create().usingPromise(Promise);
|
||||
|
||||
getNextPort = getNextPortFactory(1, 2);
|
||||
});
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('finds a port', () => {
|
||||
sandbox.stub(net.Server.prototype, 'listen').callsFake(function (port) {
|
||||
this.address = () => ({port});
|
||||
setImmediate(() => this.emit('listening'));
|
||||
});
|
||||
|
||||
return getNextPort()
|
||||
.then((port) => {
|
||||
expect(port).to.equal(1);
|
||||
describe('getNextPortFactory', function () {
|
||||
describe('portNumberGenerator', () => {
|
||||
it('loops through given set of numbers', () => {
|
||||
const nextNumber = portNumberGenerator(1, 5);
|
||||
expect(nextNumber.next().value).to.equal(1);
|
||||
expect(nextNumber.next().value).to.equal(2);
|
||||
expect(nextNumber.next().value).to.equal(3);
|
||||
expect(nextNumber.next().value).to.equal(4);
|
||||
expect(nextNumber.next().value).to.equal(5);
|
||||
expect(nextNumber.next().value).to.equal(1);
|
||||
expect(nextNumber.next().value).to.equal(2);
|
||||
});
|
||||
});
|
||||
|
||||
it('restarts count', () => {
|
||||
sandbox.stub(net.Server.prototype, 'listen').callsFake(function (port) {
|
||||
this.address = () => ({port});
|
||||
setImmediate(() => this.emit('listening'));
|
||||
describe('keeps trying new ports', () => {
|
||||
let getNextPort;
|
||||
let serverAlreadyRunning;
|
||||
beforeEach((done) => {
|
||||
getNextPort = getNextPortFactory('::', 8821);
|
||||
|
||||
serverAlreadyRunning = net.createServer();
|
||||
serverAlreadyRunning.listen(8821, () => done());
|
||||
});
|
||||
|
||||
return getNextPort()
|
||||
.then((port) => {
|
||||
expect(port).to.equal(1);
|
||||
})
|
||||
.then(() => getNextPort())
|
||||
.then((port) => {
|
||||
expect(port).to.equal(2);
|
||||
})
|
||||
.then(() => getNextPort())
|
||||
.then((port) => {
|
||||
expect(port).to.equal(1);
|
||||
afterEach((done) => {
|
||||
serverAlreadyRunning.close(() => done());
|
||||
});
|
||||
|
||||
it('test', () => {
|
||||
return getNextPort()
|
||||
.then((port) => {
|
||||
expect(port).to.equal(8822);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('finds ports concurrently', () => {
|
||||
const portStart = 10000;
|
||||
const getCount = 100;
|
||||
|
||||
const getNextPort = getNextPortFactory('::', portStart);
|
||||
const portFinders = new Array(getCount).fill().map(() => getNextPort());
|
||||
return Promise.all(portFinders)
|
||||
.then((ports) => {
|
||||
expect(ports.length).to.equal(getCount);
|
||||
expect(ports).to.eql(new Array(getCount).fill().map((v, i) => i + portStart));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
const {expect} = require('chai');
|
||||
const sinon = require('sinon');
|
||||
const resolveHost = require('../../src/helpers/resolve-host');
|
||||
|
||||
describe('helpers //resolve-host', function () {
|
||||
this.timeout(4000);
|
||||
|
||||
let sandbox;
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.sandbox.create().usingPromise(Promise);
|
||||
});
|
||||
afterEach(() => sandbox.restore());
|
||||
|
||||
it('fetches ip address', () => {
|
||||
const hostname = '0.0.0.0';
|
||||
return resolveHost(hostname)
|
||||
.then((resolvedHostname) => {
|
||||
expect(resolvedHostname).to.match(/^\d+\.\d+\.\d+\.\d+$/);
|
||||
});
|
||||
});
|
||||
|
||||
it('fetches ip address', () => {
|
||||
const hostname = null;
|
||||
return resolveHost(hostname)
|
||||
.then((resolvedHostname) => {
|
||||
expect(resolvedHostname).to.match(/^\d+\.\d+\.\d+\.\d+$/);
|
||||
});
|
||||
});
|
||||
|
||||
it('does nothing', () => {
|
||||
const hostname = '127.0.0.1';
|
||||
return resolveHost(hostname)
|
||||
.then((resolvedHostname) => {
|
||||
expect(resolvedHostname).to.equal(hostname);
|
||||
});
|
||||
});
|
||||
|
||||
it('fails on getting hostname', () => {
|
||||
sandbox.stub(require('http'), 'get').callsFake(function (url, cb) {
|
||||
cb({
|
||||
statusCode: 420
|
||||
});
|
||||
});
|
||||
|
||||
return resolveHost(null)
|
||||
.then(() => expect(1).to.equal(2))
|
||||
.catch((err) => {
|
||||
expect(err.code).to.equal(420);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -48,6 +48,7 @@ describe('Integration', function () {
|
||||
function startServer(options = {}) {
|
||||
server = new FtpServer(_.assign({
|
||||
log,
|
||||
pasv_url: '127.0.0.1',
|
||||
pasv_min: 8881,
|
||||
greeting: ['hello', 'world'],
|
||||
anonymous: true
|
||||
@@ -178,7 +179,7 @@ describe('Integration', function () {
|
||||
const fsPath = `${clientDirectory}/${name}/fail.txt`;
|
||||
|
||||
sandbox.stub(connection.fs, 'write').callsFake(function () {
|
||||
const stream = fs.createWriteStream(fsPath, {flags: 'w+'});
|
||||
const stream = fs.createWriteStream(fsPath, {flags: 'w+', autoClose: false});
|
||||
stream.on('error', () => fs.existsSync(fsPath) && fs.unlinkSync(fsPath));
|
||||
stream.on('close', () => stream.end());
|
||||
setImmediate(() => stream.emit('error', new Error('STOR fail test')));
|
||||
@@ -186,9 +187,9 @@ describe('Integration', function () {
|
||||
});
|
||||
|
||||
client.put(buffer, 'fail.txt', (err) => {
|
||||
expect(err).to.exist;
|
||||
setImmediate(() => {
|
||||
const fileExists = fs.existsSync(fsPath);
|
||||
expect(err).to.exist;
|
||||
expect(fileExists).to.equal(false);
|
||||
done();
|
||||
});
|
||||
@@ -403,7 +404,7 @@ describe('Integration', function () {
|
||||
readFile(`${process.cwd()}/test/cert/server.csr`)
|
||||
]))
|
||||
.then(([key, cert, ca]) => startServer({
|
||||
url: 'ftp://127.0.0.1:8880',
|
||||
url: 'ftp://127.0.0.1:8881',
|
||||
tls: {key, cert, ca}
|
||||
}))
|
||||
.then(() => {
|
||||
@@ -431,7 +432,7 @@ describe('Integration', function () {
|
||||
readFile(`${process.cwd()}/test/cert/server.csr`)
|
||||
]))
|
||||
.then(([key, cert, ca]) => startServer({
|
||||
url: 'ftps://127.0.0.1:8880',
|
||||
url: 'ftps://127.0.0.1:8882',
|
||||
tls: {key, cert, ca}
|
||||
}))
|
||||
.then(() => {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
require('dotenv').load();
|
||||
const bunyan = require('bunyan');
|
||||
const fs = require('fs');
|
||||
const FtpServer = require('../src');
|
||||
@@ -6,6 +5,7 @@ const FtpServer = require('../src');
|
||||
const server = new FtpServer({
|
||||
log: bunyan.createLogger({name: 'test', level: 'trace'}),
|
||||
url: 'ftp://127.0.0.1:8880',
|
||||
pasv_url: '127.0.0.1',
|
||||
pasv_min: 8881,
|
||||
greeting: ['Welcome', 'to', 'the', 'jungle!'],
|
||||
tls: {
|
||||
|
||||
Reference in New Issue
Block a user