Skip to content

Commit

Permalink
feat: add eprt and epsv for IPv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
trs committed Oct 30, 2017
1 parent e272802 commit 772fe5c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/commands/registration/eprt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const FAMILY = {
module.exports = {
directive: 'EPRT',
handler: function ({command} = {}) {
this.connector = new ActiveConnector(this);
const [protocol, ip, port] = _.compact(command.arg.split('|'));
const [, protocol, ip, port] = _.chain(command).get('arg', '').split('|').value();
const family = FAMILY[protocol];
if (!family) return this.reply(502, 'Unknown network protocol');
if (!family) return this.reply(504, 'Unknown network protocol');

this.connector = new ActiveConnector(this);
return this.connector.setupConnection(ip, port, family)
.then(() => this.reply(200));
},
Expand Down
4 changes: 3 additions & 1 deletion src/commands/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const commands = [
require('./registration/type'),
require('./registration/user'),
require('./registration/pbsz'),
require('./registration/prot')
require('./registration/prot'),
require('./registration/eprt'),
require('./registration/epsv')
];

const registry = commands.reduce((result, cmd) => {
Expand Down
60 changes: 60 additions & 0 deletions test/commands/registration/eprt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const when = require('when');
const {expect} = require('chai');
const sinon = require('sinon');

const ActiveConnector = require('../../../src/connector/active');

const CMD = 'EPRT';
describe(CMD, function () {
let sandbox;
const mockClient = {
reply: () => when.resolve()
};
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);

beforeEach(() => {
sandbox = sinon.sandbox.create();

sandbox.spy(mockClient, 'reply');
sandbox.stub(ActiveConnector.prototype, 'setupConnection').resolves();
});
afterEach(() => {
sandbox.restore();
});

it('// unsuccessful | no argument', () => {
return cmdFn()
.then(() => {
expect(mockClient.reply.args[0][0]).to.equal(504);
});
});

it('// unsuccessful | invalid argument', () => {
return cmdFn({command: {arg: 'blah'}})
.then(() => {
expect(mockClient.reply.args[0][0]).to.equal(504);
});
});

it('// successful IPv4', () => {
return cmdFn({command: {arg: '|1|192.168.0.100|35286|'}})
.then(() => {
const [ip, port, family] = ActiveConnector.prototype.setupConnection.args[0];
expect(mockClient.reply.args[0][0]).to.equal(200);
expect(ip).to.equal('192.168.0.100');
expect(port).to.equal('35286');
expect(family).to.equal(4);
});
});

it('// successful IPv6', () => {
return cmdFn({command: {arg: '|2|8536:933f:e7f3:3e91:6dc1:e8c6:8482:7b23|35286|'}})
.then(() => {
const [ip, port, family] = ActiveConnector.prototype.setupConnection.args[0];
expect(mockClient.reply.args[0][0]).to.equal(200);
expect(ip).to.equal('8536:933f:e7f3:3e91:6dc1:e8c6:8482:7b23');
expect(port).to.equal('35286');
expect(family).to.equal(6);
});
});
});
35 changes: 35 additions & 0 deletions test/commands/registration/epsv.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const when = require('when');
const {expect} = require('chai');
const sinon = require('sinon');

const PassiveConnector = require('../../../src/connector/passive');

const CMD = 'EPSV';
describe(CMD, function () {
let sandbox;
const mockClient = {
reply: () => when.resolve()
};
const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);

beforeEach(() => {
sandbox = sinon.sandbox.create();

sandbox.stub(mockClient, 'reply').resolves();
sandbox.stub(PassiveConnector.prototype, 'setupServer').resolves({
address: () => ({port: 12345})
});
});
afterEach(() => {
sandbox.restore();
});

it('// successful IPv4', () => {
return cmdFn()
.then(() => {
const [code, message] = mockClient.reply.args[0];
expect(code).to.equal(229);
expect(message).to.equal('EPSV OK (|||12345|)');
});
});
});

0 comments on commit 772fe5c

Please sign in to comment.