Skip to content
Browse files

connection: allow supplying of tls.connect() options

  • Loading branch information...
1 parent 71ca54f commit f7fed5a86786a9371f622d9318eb2919a3ec0010 @mscdex committed Oct 8, 2013
Showing with 38 additions and 20 deletions.
  1. +2 −0 README.md
  2. +36 −20 lib/connection.js
View
2 README.md
@@ -104,6 +104,8 @@ Methods
* secure - _mixed_ - Set to true for both control and data connection encryption, 'control' for control connection encryption only, or 'implicit' for implicitly encrypted control connection (this mode is deprecated in modern times, but usually uses port 990) **Default:** false
+ * secureOptions - _object_ - Additional options to be passed to `tls.connect()`. **Default:** (none)
+
* user - _string_ - Username for authentication. **Default:** 'anonymous'
* password - _string_ - Password for authentication. **Default:** 'anonymous@'
View
56 lib/connection.js
@@ -72,6 +72,7 @@ var FTP = module.exports = function() {
user: undefined,
password: undefined,
secure: false,
+ secureOptions: undefined,
connTimeout: undefined,
pasvTimeout: undefined,
aliveTimeout: undefined
@@ -90,15 +91,20 @@ FTP.prototype.connect = function(options) {
this.options.user = options.user || 'anonymous';
this.options.password = options.password || 'anonymous@';
this.options.secure = options.secure || false;
+ this.options.secureOptions = options.secureOptions;
this.options.connTimeout = options.connTimeout || 10000;
this.options.pasvTimeout = options.pasvTimeout || 10000;
this.options.aliveTimeout = options.keepalive || 10000;
if (typeof options.debug === 'function')
this._debug = options.debug;
- var debug = this._debug;
- var socket = this._socket = new Socket();
+ var secureOptions,
+ debug = this._debug,
+ socket = new Socket();
+
+ socket.setTimeout(0);
+ socket.setKeepAlive(true);
this._parser = new Parser({ debug: debug });
this._parser.on('response', function(code, text) {
@@ -124,17 +130,20 @@ FTP.prototype.connect = function(options) {
noopreq.cb();
});
- this._socket.setTimeout(0);
- if (this.options.secure === 'implicit')
- socket = tls.connect({ socket: this._socket }, onconnect);
- else
- this._socket.once('connect', onconnect);
+ if (this.options.secure) {
+ secureOptions = {};
+ for (var k in this.options.secureOptions)
+ secureOptions[k] = this.options.secureOptions[k];
+ secureOptions.socket = socket;
+ this.options.secureOptions = secureOptions;
+ }
- var timer = setTimeout(function() {
- self.emit('error', new Error('Timeout while connecting to server'));
- self._socket.destroy();
- self._reset();
- }, this.options.connTimeout);
+ if (this.options.secure === 'implicit')
+ this._socket = tls.connect(secureOptions, onconnect);
+ else {
+ socket.once('connect', onconnect);
+ this._socket = socket;
+ }
var noopreq = {
cmd: 'NOOP',
@@ -245,7 +254,9 @@ FTP.prototype.connect = function(options) {
socket._decoder = null;
self._curReq = null; // prevent queue from being processed during
// TLS/SSL negotiation
- socket = tls.connect({ socket: self._socket }, onconnect);
+ secureOptions.socket = self._socket;
+ secureOptions.session = undefined;
+ socket = tls.connect(secureOptions, onconnect);
socket.setEncoding('binary');
socket.on('data', ondata);
socket.once('end', onend);
@@ -266,18 +277,18 @@ FTP.prototype.connect = function(options) {
self.emit('error', err);
});
- var hasReset = false;
- this._socket.once('end', onend);
+ socket.once('end', onend);
function onend() {
ondone();
self.emit('end');
}
- this._socket.once('close', function(had_err) {
+ socket.once('close', function(had_err) {
ondone();
self.emit('close', had_err);
});
+ var hasReset = false;
function ondone() {
if (!hasReset) {
hasReset = true;
@@ -286,6 +297,12 @@ FTP.prototype.connect = function(options) {
}
}
+ var timer = setTimeout(function() {
+ self.emit('error', new Error('Timeout while connecting to server'));
+ self._socket.destroy();
+ self._reset();
+ }, this.options.connTimeout);
+
this._socket.connect(this.options.port, this.options.host);
};
@@ -829,10 +846,9 @@ FTP.prototype._pasvConnect = function(ip, port, cb) {
socket.once('connect', function() {
self._debug&&self._debug('[connection] PASV socket connected');
if (self.options.secure === true) {
- socket = tls.connect({
- socket: socket,
- session: self._socket.getSession() // re-use existing session
- });
+ self.options.secureOptions.socket = socket;
+ self.options.secureOptions.session = self._socket.getSession();
+ socket = tls.connect(self.options.secureOptions);
socket.setTimeout(0);
}
clearTimeout(timer);

0 comments on commit f7fed5a

Please sign in to comment.
Something went wrong with that request. Please try again.