-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshclient.js
More file actions
259 lines (232 loc) · 6.55 KB
/
sshclient.js
File metadata and controls
259 lines (232 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2013 Hsiaoming Yang
// https://raw.github.com/lepture/node-scp2/master/lib/client.js
var fs = require('fs');
var path = require('path');
var util = require('util');
var async = require('async');
var EventEmitter = require('events').EventEmitter;
var Connection = require('ssh2');
var _ = require('lodash');
function Client(options) {
this._options = options || {};
this.remote = {};
}
util.inherits(Client, EventEmitter);
Client.prototype.defaults = function(options) {
if (options) {
this._options = options || {};
} else {
return this._options;
}
};
Client.prototype.parse = function(remote) {
if (_.isString(remote)) {
// username:password@host:/path/to
var regex = /^([a-zA-Z0-9\-\.]+)(\:.*)?@([^:]+)(\:.*)?$/;
var m = remote.match(regex);
if (!m) return {};
var ret = {
username: m[1],
host: m[3]
};
if (m[2]) {
ret.password = m[2].slice(1);
}
if (m[4]) {
ret.path = m[4].slice(1);
}
this.remote = ret;
return ret;
}
this.remote = remote;
return remote;
};
Client.prototype.sftp = function(callback) {
if (this.__sftp) {
callback(null, this.__sftp);
return;
}
var remote = _.defaults(this.remote, this._options);
if (this.__ssh) {
this.__ssh.connect(remote);
return;
}
var self = this;
var ssh = new Connection();
ssh.on('connect', function() {
self.emit('connect');
});
ssh.on('ready', function() {
self.emit('ready');
ssh.sftp(function(err, sftp) {
if (err) throw err;
// save for reuse
self.__sftp = sftp;
callback(err, sftp);
});
});
ssh.on('error', function(err) {
self.emit('error', err);
});
ssh.on('end', function() {
self.emit('end');
});
ssh.on('close', function() {
self.emit('close');
});
ssh.connect(remote);
this.__ssh = ssh;
};
Client.prototype.close = function() {
if (this.__sftp) {
this.__sftp.end();
}
if (this.__ssh) {
this.__ssh.end();
}
};
Client.prototype.mkdir = function(dir, attrs, callback) {
if (_.isFunction(attrs)) {
callback = attrs;
attrs = undefined;
}
var self = this;
var dirs = [];
var exists = false;
this.sftp(function(err, sftp) {
// for record log
var mkdir = function(dir, callback) {
self.emit('mkdir', dir);
sftp.mkdir(dir, attrs, callback);
};
async.until(function() {
return exists;
}, function(done) {
// detect if the directory exists
sftp.stat(dir, function(err, attr) {
if (err) {
dirs.push(dir);
dir = path.dirname(dir);
} else {
exists = true;
}
done();
});
}, function(err) {
if (err) {
callback(err);
} else {
// just like mkdir -p
async.eachSeries(dirs.reverse(), mkdir, callback);
}
});
});
};
Client.prototype.write = function(options, callback) {
var destination = options.destination;
destination = unixy(destination);
var attrs = options.attrs;
var content = options.content;
if (!Buffer.isBuffer(content)) {
content = new Buffer(content, options.encoding);
}
var self = this;
this.sftp(function(err, sftp) {
var _write = function(handle) {
self.emit('write', options);
sftp.write(handle, content, 0, content.length, 0, function(err) {
var writeErr = err;
sftp.close(handle, function(err) {
callback(err || writeErr);
});
});
};
sftp.open(destination, 'w', attrs, function(err, handle) {
if (err) {
// destination is directory
destination = path.join(
destination, path.basename(options.source)
);
destination = unixy(destination);
// for emit write event
options.destination = destination;
sftp.open(destination, 'w', attrs, function(err, handle) {
_write(handle);
});
} else {
_write(handle);
}
});
});
};
Client.prototype.upload = function(src, dest, callback) {
dest = unixy(dest);
var self = this, _attrs, _buffer;
async.waterfall([
function(callback) {
fs.stat(src, callback);
},
function(stats, callback) {
_attrs = util.inspect(stats);
fs.readFile(src, callback);
},
function(buffer, callback) {
_buffer = buffer;
if (/\/$/.test(dest)) {
self.mkdir(dest, _attrs, callback);
} else {
self.mkdir(path.dirname(dest), _attrs, callback);
}
},
function(callback) {
self.write({
source: src,
destination: dest,
content: _buffer,
attrs: _attrs
}, callback)
}
], function(err) {
callback(err);
});
};
Client.prototype.download = function(src, dest, callback) {
var self = this;
var _sftp, _handle, _buffer;
async.waterfall([
function(callback) {
self.sftp(function(err, sftp) {
_sftp = sftp;
callback(err, sftp);
});
},
function(sftp, callback) {
sftp.open(src, 'r', callback);
},
function(handle, callback) {
_handle = handle;
_sftp.fstat(handle, callback);
},
function(attrs, callback) {
_buffer = new Buffer(attrs.size);
self.emit('read', src);
_sftp.read(_handle, _buffer, 0, attrs.size, 0, callback);
},
function(bytesRead, buffer, position, callback) {
callback(null, buffer);
},
function(buffer, callback) {
fs.writeFile(dest, buffer, callback);
}
], function(err) {
callback(err);
});
};
exports = module.exports = new Client();
exports.Client = Client;
function unixy(filepath) {
if (process.platform === 'win32') {
filepath = filepath.replace(/\\/g, '/');
}
return filepath;
}