Skip to content

Commit

Permalink
Add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
cgarnier committed Jun 16, 2017
1 parent 7cacec7 commit 18c8733
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ var Ftp = new JSFtp({
});
```

Through a proxy:

```javascript
var Ftp = new JSFtp({
host: proxy.host,
port: ftp.port,
user: `${ftp.user}@${ftp.host} ${proxy.user}`,
pass: ftp.pass,
cwd: '/82844',
root: '/82844',
debugMode: true,
acct: proxy.pass
})
```


jsftp gives you access to all the raw commands of the FTP protocol in form of
methods in the `Ftp` object. It also provides several convenience methods for
actions that require complex chains of commands (e.g. uploading and retrieving
Expand Down
19 changes: 13 additions & 6 deletions lib/jsftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var Ftp = module.exports = function(cfg) {
this.port = cfg.port || FTP_PORT;
this.user = cfg.user || 'anonymous';
this.pass = cfg.pass || '@anonymous';
this.acct = cfg.acct || ''
// 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
// more efficient to avoid the round-trip to the server.
Expand Down Expand Up @@ -234,7 +235,7 @@ Ftp.prototype.runCommand = function(action, callback) {

var self = this;
this.getFeatures(function() {
self.auth(self.user, self.pass, function() {
self.auth(self.user, self.pass, self.acct, function() {
self.commandQueue.push(cmd);
self.nextCmd();
});
Expand Down Expand Up @@ -316,9 +317,8 @@ Ftp.prototype.getFeatures = function(callback) {
* @param {String} pass Password
* @param {Function} callback Follow-up function.
*/
Ftp.prototype.auth = function(user, pass, callback) {
Ftp.prototype.auth = function(user, pass, acct, callback) {
var self = this;

if (this.authenticating === true) {
return callback(new Error('This client is already authenticating'));
}
Expand All @@ -329,6 +329,9 @@ Ftp.prototype.auth = function(user, pass, callback) {
if (!pass) {
pass = '@anonymous';
}
if (!acct) {
acct = ''
}

this.authenticating = true;
self.raw('user', user, function(err, res) {
Expand All @@ -338,19 +341,23 @@ Ftp.prototype.auth = function(user, pass, callback) {
return;
}
self.raw('pass', pass, function(err, res) {
self.authenticating = false;

if (err) {
self.authenticating = false;
callback(err);
} else if ([230, 202].indexOf(res.code) > -1) {
self.authenticating = false;
self.authenticated = true;
self.user = user;
self.pass = pass;
self.raw('type', 'I', function() {
callback(undefined, res);
});
} else if (res.code === 332) {
self.raw('acct', ''); // ACCT not really supported
self.raw('acct', acct, function (err, res) {
self.authenticating = false;
self.authenticated = true;
callback(undefined, res)
}); // ACCT not really supported
}
});
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"mocha": "^3.2.0",
"mocha-istanbul": "0.2.0",
"rimraf": "^2.2.8",
"sinon": "^1.17.7"
"sinon": "^1.17.7",
"socksv5": "0.0.6"
},
"main": "lib/jsftp.js",
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion test/jsftp_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('jsftp test suite', function() {
ftp.auth(
options.user,
options.pass + '_invalid',
null,
function(err, data) {
assert.equal(err.code, 530);
assert.equal(data, null);
Expand Down Expand Up @@ -554,7 +555,7 @@ describe('jsftp test suite', function() {

it('test attach event handlers: connect', function(_next) {
var clientOnConnect = function() {
client.auth(options.user, options.pass, next);
client.auth(options.user, options.pass, null, next);
};

var next = function(err) {
Expand Down

0 comments on commit 18c8733

Please sign in to comment.