diff --git a/LICENSE b/LICENSE index cfb8ed3f..e9e512ec 100644 --- a/LICENSE +++ b/LICENSE @@ -1,9 +1,21 @@ -ftp-srv Copyright (c) 2017 Tyler Stewart - MIT License -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Copyright (c) 2017 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 +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/package-lock.json b/package-lock.json index 6164671e..6567ad09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3722,9 +3722,9 @@ "dev": true }, "sinon": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/sinon/-/sinon-2.3.2.tgz", - "integrity": "sha1-xDqcVw8yuqwRWVBc/u0ZEIhV34k=", + "version": "2.3.5", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-2.3.5.tgz", + "integrity": "sha1-mi/A/41SbacW8wlTqixl1RiRf2w=", "dev": true }, "slash": { @@ -4210,9 +4210,9 @@ "dev": true }, "uuid": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.0.1.tgz", - "integrity": "sha1-ZUS7ot/ajBzxfmKaOjBeK7H+5sE=" + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.1.0.tgz", + "integrity": "sha512-DIWtzUkw04M4k3bf1IcpS2tngXEL26YUD2M0tMDUpnUrz2hgzUBlD55a4FjdLGPvfHxS6uluGWvaVEqgBcVa+g==" }, "validate-npm-package-license": { "version": "3.0.1", diff --git a/package.json b/package.json index 47885df4..05d55719 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "bunyan": "^1.8.10", "lodash": "^4.17.4", "moment": "^2.18.1", - "uuid": "^3.0.1", + "uuid": "^3.1.0", "when": "^3.7.8" }, "devDependencies": { @@ -76,7 +76,7 @@ "npm-run-all": "4.0.2", "rimraf": "2.6.1", "semantic-release": "^6.3.6", - "sinon": "^2.3.2" + "sinon": "^2.3.5" }, "engines": { "node": ">=6.x", diff --git a/src/commands/registration/feat.js b/src/commands/registration/feat.js index 8f3c24d3..365610f5 100644 --- a/src/commands/registration/feat.js +++ b/src/commands/registration/feat.js @@ -9,7 +9,7 @@ module.exports = { const feat = _.get(registry[cmd], 'flags.feat', null); if (feat) return _.concat(feats, feat); return feats; - }, []) + }, ['UTF8']) .map(feat => ` ${feat}`); return features.length ? this.reply(211, 'Extensions supported', ...features, 'End') diff --git a/src/commands/registration/retr.js b/src/commands/registration/retr.js index f8e101d9..0ab8a708 100644 --- a/src/commands/registration/retr.js +++ b/src/commands/registration/retr.js @@ -17,7 +17,7 @@ module.exports = { return when.promise((resolve, reject) => { dataSocket.on('error', err => stream.emit('error', err)); - stream.on('data', data => dataSocket.write(data, this.encoding)); + stream.on('data', data => dataSocket.write(data, this.transferType)); stream.on('end', () => resolve(this.reply(226))); stream.on('error', err => reject(err)); this.reply(150).then(() => dataSocket.resume()); diff --git a/src/commands/registration/stor.js b/src/commands/registration/stor.js index 529d8de2..2f665bb9 100644 --- a/src/commands/registration/stor.js +++ b/src/commands/registration/stor.js @@ -25,7 +25,7 @@ module.exports = { // It is assumed that the `close` handler will call the end() method dataSocket.once('end', () => stream.listenerCount('close') ? stream.emit('close') : stream.end()); dataSocket.once('error', err => reject(err)); - dataSocket.on('data', data => stream.write(data, this.encoding)); + dataSocket.on('data', data => stream.write(data, this.transferType)); this.reply(150).then(() => dataSocket.resume()); }) diff --git a/src/commands/registration/type.js b/src/commands/registration/type.js index e6f442f7..5968875f 100644 --- a/src/commands/registration/type.js +++ b/src/commands/registration/type.js @@ -1,20 +1,20 @@ -const _ = require('lodash'); - -const ENCODING_TYPES = { - A: 'utf8', - I: 'binary', - L: 'binary' -}; module.exports = { directive: 'TYPE', handler: function ({command} = {}) { - const encoding = _.upperCase(command.arg); - if (!ENCODING_TYPES.hasOwnProperty(encoding)) return this.reply(501); - this.encoding = ENCODING_TYPES[encoding]; - return this.reply(200); + if (/^A[0-9]?$/i.test(command.arg)) { + this.transferType = 'ascii'; + } else if (/^L[0-9]?$/i.test(command.arg) || /^I$/i.test(command.arg)) { + this.transferType = 'binary'; + } else { + return this.reply(501); + } + return this.reply(200, `Switch to "${this.transferType}" transfer mode.`); }, syntax: '{{cmd}} ', - description: 'Set the transfer mode, binary (I) or utf8 (A)' + description: 'Set the transfer mode, binary (I) or ascii (A)', + flags: { + feat: 'TYPE A,I,L' + } }; diff --git a/src/connection.js b/src/connection.js index 3a994d12..29a65e6d 100644 --- a/src/connection.js +++ b/src/connection.js @@ -15,7 +15,7 @@ class FtpConnection { this.id = uuid.v4(); this.log = options.log.child({id: this.id, ip: this.ip}); this.commands = new Commands(this); - this.encoding = 'utf8'; + this.transferType = 'binary'; this.bufferSize = false; this.connector = new BaseConnector(this); @@ -84,7 +84,7 @@ class FtpConnection { if (!letter.socket) letter.socket = options.socket ? options.socket : this.commandSocket; if (!letter.message) letter.message = DEFAULT_MESSAGE[options.code] || 'No information'; - if (!letter.encoding) letter.encoding = this.encoding; + if (!letter.encoding) letter.encoding = 'utf8'; return when(letter.message) // allow passing in a promise as a message .then(message => { letter.message = message; @@ -102,7 +102,7 @@ class FtpConnection { const packet = !letter.raw ? _.compact([letter.code || options.code, letter.message]).join(seperator) : letter.message; if (letter.socket && letter.socket.writable) { - this.log.trace({port: letter.socket.address().port, packet}, 'Reply'); + this.log.trace({port: letter.socket.address().port, encoding: letter.encoding, packet}, 'Reply'); letter.socket.write(packet + '\r\n', letter.encoding, err => { if (err) { this.log.error(err); diff --git a/src/connector/active.js b/src/connector/active.js index 4e31fcb5..1de092db 100644 --- a/src/connector/active.js +++ b/src/connector/active.js @@ -26,7 +26,7 @@ class Active extends Connector { return closeExistingServer() .then(() => { this.dataSocket = new Socket(); - this.dataSocket.setEncoding(this.encoding); + this.dataSocket.setEncoding(this.connection.transferType); 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(); diff --git a/src/connector/passive.js b/src/connector/passive.js index f191f21a..1f0ac8f7 100644 --- a/src/connector/passive.js +++ b/src/connector/passive.js @@ -54,7 +54,7 @@ class Passive extends Connector { this.dataSocket = socket; } this.dataSocket.connected = true; - this.dataSocket.setEncoding(this.connection.encoding); + this.dataSocket.setEncoding(this.connection.transferType); 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'); diff --git a/src/index.js b/src/index.js index f1751546..fc13a4a8 100644 --- a/src/index.js +++ b/src/index.js @@ -94,7 +94,7 @@ class FtpServer { } setupTLS(_tls) { - if (!tls) return false; + if (!_tls) return false; return _.assign({}, _tls, { cert: _tls.cert ? fs.readFileSync(_tls.cert) : undefined, key: _tls.key ? fs.readFileSync(_tls.key) : undefined, diff --git a/test/commands/registration/type.spec.js b/test/commands/registration/type.spec.js index 0f5541d2..db964d41 100644 --- a/test/commands/registration/type.spec.js +++ b/test/commands/registration/type.spec.js @@ -13,7 +13,7 @@ describe(CMD, function () { beforeEach(() => { sandbox = sinon.sandbox.create(); - mockClient.encoding = null; + mockClient.transferType = null; sandbox.spy(mockClient, 'reply'); }); afterEach(() => { @@ -24,7 +24,7 @@ describe(CMD, function () { return cmdFn({ command: { arg: 'A' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); - expect(mockClient.encoding).to.equal('utf8'); + expect(mockClient.transferType).to.equal('ascii'); }); }); @@ -32,7 +32,7 @@ describe(CMD, function () { return cmdFn({ command: { arg: 'I' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); - expect(mockClient.encoding).to.equal('binary'); + expect(mockClient.transferType).to.equal('binary'); }); }); @@ -40,7 +40,7 @@ describe(CMD, function () { return cmdFn({ command: { arg: 'L' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); - expect(mockClient.encoding).to.equal('binary'); + expect(mockClient.transferType).to.equal('binary'); }); }); @@ -48,7 +48,7 @@ describe(CMD, function () { return cmdFn({ command: { arg: 'X' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(501); - expect(mockClient.encoding).to.equal(null); + expect(mockClient.transferType).to.equal(null); }); }); }); diff --git a/test/index.spec.js b/test/index.spec.js index 53c5320f..416a75f2 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -109,12 +109,12 @@ describe('FtpServer', function () { }); }); - it('STOR test.txt', done => { + it('STOR tést.txt', done => { const buffer = Buffer.from('test text file'); - client.put(buffer, 'test.txt', err => { + client.put(buffer, 'tést.txt', err => { expect(err).to.not.exist; - expect(fs.existsSync('./test/test.txt')).to.equal(true); - fs.readFile('./test/test.txt', (fserr, data) => { + expect(fs.existsSync('./test/tést.txt')).to.equal(true); + fs.readFile('./test/tést.txt', (fserr, data) => { expect(fserr).to.not.exist; expect(data.toString()).to.equal('test text file'); done(); @@ -122,11 +122,11 @@ describe('FtpServer', function () { }); }); - it('APPE test.txt', done => { + it('APPE tést.txt', done => { const buffer = Buffer.from(', awesome!'); - client.append(buffer, 'test.txt', err => { + client.append(buffer, 'tést.txt', err => { expect(err).to.not.exist; - fs.readFile('./test/test.txt', (fserr, data) => { + fs.readFile('./test/tést.txt', (fserr, data) => { expect(fserr).to.not.exist; expect(data.toString()).to.equal('test text file, awesome!'); done(); @@ -134,8 +134,8 @@ describe('FtpServer', function () { }); }); - it('RETR test.txt', done => { - client.get('test.txt', (err, stream) => { + it('RETR tést.txt', done => { + client.get('tést.txt', (err, stream) => { expect(err).to.not.exist; let text = ''; stream.on('data', data => { @@ -148,10 +148,10 @@ describe('FtpServer', function () { }); }); - it('RNFR test.txt, RNTO awesome.txt', done => { - client.rename('test.txt', 'awesome.txt', err => { + it('RNFR tést.txt, RNTO awesome.txt', done => { + client.rename('tést.txt', 'awesome.txt', err => { expect(err).to.not.exist; - expect(fs.existsSync('./test/test.txt')).to.equal(false); + expect(fs.existsSync('./test/tést.txt')).to.equal(false); expect(fs.existsSync('./test/awesome.txt')).to.equal(true); fs.readFile('./test/awesome.txt', (fserr, data) => { expect(fserr).to.not.exist; @@ -196,19 +196,19 @@ describe('FtpServer', function () { }); }); - it('MKD tmp', done => { - if (fs.existsSync('./test/tmp')) { - fs.rmdirSync('./test/tmp'); + it('MKD témp', done => { + if (fs.existsSync('./test/témp')) { + fs.rmdirSync('./test/témp'); } - client.mkdir('tmp', err => { + client.mkdir('témp', err => { expect(err).to.not.exist; - expect(fs.existsSync('./test/tmp')).to.equal(true); + expect(fs.existsSync('./test/témp')).to.equal(true); done(); }); }); - it('CWD tmp', done => { - client.cwd('tmp', (err, data) => { + it('CWD témp', done => { + client.cwd('témp', (err, data) => { expect(err).to.not.exist; expect(data).to.be.a('string'); done(); @@ -222,10 +222,10 @@ describe('FtpServer', function () { }); }); - it('RMD tmp', done => { - client.rmdir('tmp', err => { + it('RMD témp', done => { + client.rmdir('témp', err => { expect(err).to.not.exist; - expect(fs.existsSync('./test/tmp')).to.equal(false); + expect(fs.existsSync('./test/témp')).to.equal(false); done(); }); });