-
Notifications
You must be signed in to change notification settings - Fork 61
/
node-sftp-server.js
470 lines (408 loc) · 14.4 KB
/
node-sftp-server.js
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
"use strict";
var ContextWrapper, DirectoryEmitter, EventEmitter, Readable, Responder, SFTP, SFTPFileStream, SFTPServer, SFTPSession, Statter, Transform, Writable, constants, fs, ssh2, ssh2_stream,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty,
slice = [].slice;
ssh2 = require('ssh2');
ssh2_stream = require('ssh2-streams');
SFTP = ssh2_stream.SFTPStream;
Readable = require('stream').Readable;
Writable = require('stream').Writable;
Transform = require('stream').Transform;
EventEmitter = require("events").EventEmitter;
fs = require('fs');
constants = require('constants');
Responder = (function(superClass) {
extend(Responder, superClass);
Responder.Statuses = {
"denied": "PERMISSION_DENIED",
"nofile": "NO_SUCH_FILE",
"end": "EOF",
"ok": "OK",
"fail": "FAILURE",
"bad_message": "BAD_MESSAGE",
"unsupported": "OP_UNSUPPORTED"
};
function Responder(req1) {
var fn, methodname, ref, symbol;
this.req = req1;
ref = this.constructor.Statuses;
fn = (function(_this) {
return function(symbol) {
return _this[methodname] = function() {
_this.done = true;
console.warn("Going to invoke " + symbol + " on behalf of req: " + _this.req + ". value: " + ssh2.SFTP_STATUS_CODE[symbol]);
return _this.sftpStream.status(_this.req, ssh2.SFTP_STATUS_CODE[symbol]);
};
};
})(this);
for (methodname in ref) {
symbol = ref[methodname];
fn(symbol);
}
}
return Responder;
})(EventEmitter);
DirectoryEmitter = (function(superClass) {
extend(DirectoryEmitter, superClass);
function DirectoryEmitter(sftpStream1, req1) {
this.sftpStream = sftpStream1;
this.req = req1 != null ? req1 : null;
this.stopped = false;
this.done = false;
DirectoryEmitter.__super__.constructor.call(this, this.req);
}
DirectoryEmitter.prototype.request_directory = function(req) {
this.req = req;
console.warn("Directory entry requested! " + req);
if (!this.done) {
return this.emit("dir");
} else {
return this.end();
}
};
DirectoryEmitter.prototype.file = function(name) {
console.warn("Returning a file: " + name + " for req: " + this.req);
this.stopped = this.sftpStream.name(this.req, {
filename: name.toString(),
longname: name.toString(),
attrs: {}
});
if (!this.stopped && !this.done) {
return this.emit("dir");
}
};
return DirectoryEmitter;
})(Responder);
ContextWrapper = (function() {
function ContextWrapper(ctx1, server) {
this.ctx = ctx1;
this.server = server;
this.method = this.ctx.method;
this.username = this.ctx.username;
this.password = this.ctx.password;
}
ContextWrapper.prototype.reject = function() {
return this.ctx.reject();
};
ContextWrapper.prototype.accept = function(callback) {
if (callback == null) {
callback = function() {};
}
console.warn("Accepting callback!!!!!");
this.ctx.accept();
return this.server._session_start_callback = callback;
};
return ContextWrapper;
})();
module.exports = SFTPServer = (function(superClass) {
extend(SFTPServer, superClass);
function SFTPServer() {
this.server = new ssh2.Server({
privateKey: fs.readFileSync('ssh_host_rsa_key')
}, (function(_this) {
return function(client, info) {
client.on('authentication', function(ctx) {
console.warn("Authentication!");
_this.auth_wrapper = new ContextWrapper(ctx, _this);
return _this.emit("connect", _this.auth_wrapper);
});
client.on('end', function() {
console.warn("Disconnection!");
return _this.emit("end");
});
return client.on('ready', function(channel) {
client._sshstream.debug = function(msg) {
return "CLIENT ssh stream debug: " + msg;
};
console.warn("Uhm, I guess we authenticated OK?");
return client.on('session', function(accept, reject) {
var session;
session = accept();
return session.on('sftp', function(accept, reject) {
var sftpStream;
console.log('Client SFTP session?!?!!?!?!?');
sftpStream = accept();
session = new SFTPSession(sftpStream);
return _this._session_start_callback(session);
});
});
});
};
})(this));
}
SFTPServer.prototype.listen = function(port) {
return this.server.listen(port);
};
return SFTPServer;
})(EventEmitter);
Statter = (function() {
function Statter(sftpStream1, reqid1) {
this.sftpStream = sftpStream1;
this.reqid = reqid1;
}
Statter.prototype.is_file = function() {
return this.type = constants.S_IFREG;
};
Statter.prototype.is_directory = function() {
return this.type = constants.S_IFDIR;
};
Statter.prototype.file = function(attrs) {
if (attrs == null) {
attrs = {};
}
return this.sftpStream.attrs(this.reqid, this._get_statblock());
};
Statter.prototype.nofile = function() {
return this.sftpStream.status(this.reqid, ssh2.SFTP_STATUS_CODE.NO_SUCH_FILE);
};
Statter.prototype._get_mode = function() {
return this.type | this.permissions;
};
Statter.prototype._get_statblock = function() {
return {
mode: this._get_mode(),
uid: this.uid,
gid: this.gid,
size: this.size,
atime: this.atime,
mtime: this.mtime
};
};
return Statter;
})();
SFTPFileStream = (function(superClass) {
extend(SFTPFileStream, superClass);
function SFTPFileStream() {
return SFTPFileStream.__super__.constructor.apply(this, arguments);
}
SFTPFileStream.prototype._read = function(size) {};
return SFTPFileStream;
})(Readable);
SFTPSession = (function(superClass) {
extend(SFTPSession, superClass);
SFTPSession.Events = ["REALPATH", "STAT", "LSTAT", "OPENDIR", "CLOSE", "REMOVE", "READDIR", "OPEN", "READ", "WRITE"];
function SFTPSession(sftpStream1) {
var event, fn, i, len, ref;
this.sftpStream = sftpStream1;
this.max_filehandle = 0;
this.handles = {};
ref = this.constructor.Events;
fn = (function(_this) {
return function(event) {
console.warn("Now looking at event: " + event);
return _this.sftpStream.on(event, function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
console.warn("UNIVERSAL EVENT DETECTED: " + event + " - reqid: " + args[0]);
return _this[event].apply(_this, args);
});
};
})(this);
for (i = 0, len = ref.length; i < len; i++) {
event = ref[i];
fn(event);
}
}
SFTPSession.prototype.fetchhandle = function() {
var prevhandle;
prevhandle = this.max_filehandle;
this.max_filehandle++;
return new Buffer(prevhandle.toString());
};
SFTPSession.prototype.REALPATH = function(reqid, path) {
var callback;
console.warn("REALPATH METHOD CALLED via reqid: " + reqid + " for path: " + path);
if (EventEmitter.listenerCount(this, "realpath")) {
callback = (function(_this) {
return function(name) {
return _this.sftpStream.name(reqid, {
filename: name,
longname: "-rwxrwxrwx 1 foo foo 3 Dec 8 2009 " + name,
attrs: {}
});
};
})(this);
return this.emit("realpath", path, callback);
} else {
return this.sftpStream.name(reqid, {
filename: path,
longname: path,
attrs: {}
});
}
};
SFTPSession.prototype.do_stat = function(reqid, path, kind) {
if (EventEmitter.listenerCount(this, "stat")) {
return this.emit("stat", path, kind, new Statter(this.sftpStream, reqid));
} else {
console.warn("WARNING: No stat function for " + kind + ", all files exist!");
return this.sftpStream.attrs(reqid, {
filename: path,
longname: path,
attrs: {}
});
}
};
SFTPSession.prototype.STAT = function(reqid, path) {
return this.do_stat(reqid, path, 'STAT');
};
SFTPSession.prototype.LSTAT = function(reqid, path) {
return this.do_stat(reqid, path, 'LSTAT');
};
SFTPSession.prototype.OPENDIR = function(reqid, path) {
var diremit;
diremit = new DirectoryEmitter(this.sftpStream, reqid);
diremit.on("newListener", (function(_this) {
return function(event, listener) {
var handle;
console.warn("New Listener detected!!!!! FREAK OUT!!!! " + event);
if (event !== "dir") {
return;
}
handle = _this.fetchhandle();
_this.handles[handle] = {
mode: "OPENDIR",
path: path,
loc: 0,
responder: diremit
};
return _this.sftpStream.handle(reqid, handle);
};
})(this));
return this.emit("readdir", path, diremit);
};
SFTPSession.prototype.READDIR = function(reqid, handle) {
var ref;
if (((ref = this.handles[handle]) != null ? ref.mode : void 0) !== "OPENDIR") {
console.warn("handle: " + handle + " is not an open directory!");
return this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.NO_SUCH_FILE);
}
return this.handles[handle].responder.request_directory(reqid);
};
SFTPSession.prototype.OPEN = function(reqid, pathname, flags, attrs) {
var handle, rs, started, stringflags, ts;
stringflags = SFTP.flagsToString(flags);
switch (stringflags) {
case "r":
ts = new Transform();
ts._transform = function(data, encoding, callback) {
this.push(data);
return callback();
};
ts._flush = function(cb) {
ts.eof = true;
return cb();
};
handle = this.fetchhandle();
this.handles[handle] = {
mode: "READ",
path: pathname,
stream: ts
};
this.emit("readfile", pathname, ts);
return this.sftpStream.handle(reqid, handle);
case "w":
rs = new Readable();
started = false;
rs._read = (function(_this) {
return function(bytes) {
if (started) {
return;
}
handle = _this.fetchhandle();
console.warn("INTERNAL _read METHOD INVOKED, DELAYED HANDLE IS BEING RETURNED: " + handle);
_this.handles[handle] = {
mode: "WRITE",
path: pathname,
stream: rs
};
_this.sftpStream.handle(reqid, handle);
return started = true;
};
})(this);
return this.emit("writefile", pathname, rs);
default:
return this.emit("error", new Error("Unknown open flags: " + stringflags));
}
};
SFTPSession.prototype.READ = function(reqid, handle, offset, length) {
var badchunk, chunk, goodchunk;
console.warn("READ REQUEST FIRED - all we're doing is...asking for reqid: " + reqid + ", offset: " + offset + ", length: " + length);
chunk = this.handles[handle].stream.read();
if (chunk) {
console.warn("INSTA-CHUNK AVAIL!!!!");
if ((chunk != null ? chunk.length : void 0) > length) {
console.warn("CHUNK IS TOOOOOOOOOOO BIIIIIIGGGGGGGG - you should split, return one, and 'unshift' the other?");
badchunk = chunk.slice(length);
goodchunk = chunk.slice(0, length);
chunk = goodchunk;
this.handles[handle].stream.unshift(badchunk);
}
return this.sftpStream.data(reqid, chunk);
} else {
if (this.handles[handle].stream.eof) {
return this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.EOF);
}
return this.handles[handle].stream.once("readable", (function(_this) {
return function() {
console.warn("READABLE FIRED?!");
chunk = _this.handles[handle].stream.read();
if ((chunk != null ? chunk.length : void 0) > length) {
console.warn("CHUNK IS TOOOOOOOOOOO BIIIIIIGGGGGGGG - you should split, return one, and 'unshift' the other?");
badchunk = chunk.slice(length);
goodchunk = chunk.slice(0, length);
chunk = goodchunk;
_this.handles[handle].stream.unshift(badchunk);
}
console.warn("Read request gave us " + (chunk != null ? chunk.length : void 0) + " bytes!");
if (chunk) {
_this.sftpStream.data(reqid, chunk);
return _this.handles[handle].stream.read(0);
} else {
if (_this.handles[handle].stream.finished) {
return _this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.EOF);
} else {
console.warn("RETURNING EMPTY STREAM!");
_this.sftpStream.data(reqid, new Buffer(""));
return _this.handles[handle].stream.read(0);
}
}
};
})(this));
}
};
SFTPSession.prototype.WRITE = function(reqid, handle, offset, data) {
console.warn("WRITE DETECTED: handle: " + handle + ", offset: " + offset + ", datalength: " + data.length);
this.handles[handle].stream.push(data);
return this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.OK);
};
SFTPSession.prototype.CLOSE = function(reqid, handle) {
return this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.OK);
if (this.handles[handle]) {
switch (this.handles[handle].mode) {
case "OPENDIR":
console.warn("Closing directory for handle: " + handle);
this.handles[handle].responder.emit("end");
delete this.handles[handle];
return this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.OK);
case "READ":
delete this.handles[handle];
return this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.OK);
case "WRITE":
console.warn("CLOSE-WRITE");
this.handles[handle].stream.push(null);
delete this.handles[handle];
return this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.OK);
default:
console.warn("Handle: " + handle + " has data:");
console.dir(this.handles[handle]);
return this.sftpStream.status(reqid, ssh2.SFTP_STATUS_CODE.FAILURE);
}
}
};
SFTPSession.prototype.REMOVE = function(reqid, handle) {
return this.emit("delete", new Responder(reqid));
};
return SFTPSession;
})(EventEmitter);