Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ API and examples
port: 3333, // Port number for the current FTP server (defaults to 21).
user: 'user', // Username
pass: 'pass', // Password
ssl: true, //[optional] whether using SFTP
sslOptions: {} //[optional] config object for ssl connection (see below for details)
}
```
- `options.sslOptions` is a config object for an SFTP or FTP with TLS/SSL implicit or explicit encryption
- applicable properties are as required for `options` argument in [tls.connect(port, host, options)](https://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback)

Creates a new Ftp instance.

Expand Down
33 changes: 30 additions & 3 deletions lib/jsftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var util = require('util');
var fs = require('fs');
var once = require('once');
var unorm = require('unorm');
var tls = require('tls');

var debug = require('debug')('jsftp:general');
var dbgCommand = require('debug')('jsftp:command');
Expand Down Expand Up @@ -75,6 +76,8 @@ var Ftp = module.exports = function (cfg) {
this.port = cfg.port || FTP_PORT;
this.user = cfg.user || 'anonymous';
this.pass = cfg.pass || '@anonymous';
this.ssl = cfg.ssl || false;
this.sslOptions = cfg.sslOptions || null;

// True if the server doesn't support the `stat` command. Since listing a
// directory or retrieving file properties is quite a common operation, it is
Expand Down Expand Up @@ -117,8 +120,18 @@ Ftp.prototype._createSocket = function (port, host, firstAction) {
this.resParser = new ResponseParser();

this.authenticated = false;
this.socket = Net.createConnection(port, host, firstAction || NOOP);
this.socket.on('connect', this.reemit('connect'));

if (this.ssl) {
this.socket = tls.connect(port, host, this.sslOptions);
this.socket.on('secureConnect', function() {
self.runCommand('prot p', function() {
self.emit('connect');
});
});
} else {
this.socket = Net.createConnection(port, host, firstAction || NOOP);
this.socket.on('connect', this.reemit('connect'));
}
this.socket.on('timeout', this.reemit('timeout'));

this.pipeline = es.pipeline(this.socket, this.resParser);
Expand Down Expand Up @@ -618,7 +631,21 @@ Ftp.prototype.getPasvSocket = function (callback) {
return callback(new Error('Bad passive host/port combination'));
}

var socket = self._pasvSocket = Net.createConnection(options);
var socket;
if (self.ssl) {
socket = tls.connect(options.port, options.host, self.sslOptions);
socket.once('secureConnect', function() {
self.runCommand('prot p', function() {
self._pasvSocket = socket;
});
});
} else {
socket = self._pasvSocket = Net.createConnection(options);
socket.once('connect', function() {
self._pasvSocket = socket;
});
}

socket.setTimeout(self.timeout || TIMEOUT);
socket.once('close', function () {
self._pasvSocket = undefined;
Expand Down