forked from javiergarmon/node-sftp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
82 lines (75 loc) · 2.56 KB
/
server.js
File metadata and controls
82 lines (75 loc) · 2.56 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
"use strict";
var fs = require('fs');
var SFTPServer = require("./node-sftp-server");
var srv = new SFTPServer();
srv.listen(8022);
console.log(process.env.USER);
console.log(process.env.PASSWORD);
srv.on("connect", function(auth){
console.warn("authentication attempted");
if(auth.method !== 'password' || auth.username !== process.env.USER || auth.password !== process.env.PASSWORD){
return auth.reject();
}
var username = auth.username;
var password = auth.password;
return auth.accept(function(session){
session.on("readdir", function(path, responder){
console.warn("Readdir request for path: " + path);
var files = fs.readdirSync(path);
var x = 0;
responder.on("dir", function(){
if(files[x]){
responder.file(files[x++]);
}
else {
return responder.end();
}
});
return responder.on("end", function(){
return console.warn("Directory is done");
});
});
session.on("realpath", function(path, callback){
callback("/Users/" + username + "/")
});
session.on("readfile", function(path, writestream){
console.log("File: " + path + " requested!");
var readStream = fs.createReadStream(path);
readStream.on('open', function(){
readStream.pipe(writestream)
});
readStream.on('error', function(){
console.log('readstream error');
});
});
session.on("writefile", function(path, writestream){
console.log(username, "attempted to write a file");
session.emit('error', "You do not have permissions to write files");
return 'error';
});
session.on("stat", function(path, statkind, statresponder) {
fs.stat(path, function(err, stats){
if (err){
return statresponder.nofile();
}
if(stats.isFile()){
statresponder.is_file();
}
else {
statresponder.is_directory();
}
return statresponder.file();
});
});
session.on("error", function(err,path){
console.log(err);
});
});
});
srv.on("error", function(err,path){
console.log(err);
console.log(path);
});
srv.on("end", function(){
return console.warn("User Disconnected");
});