forked from nodeftpd/nodeftpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
81 lines (75 loc) · 2 KB
/
test.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
var ftpd = require('./');
var fs = require('fs');
var path = require('path');
var keyFile;
var certFile;
var server;
var options = {
host: process.env.IP || '127.0.0.1',
port: process.env.PORT || 7002,
tls: null,
};
if (process.env.KEY_FILE && process.env.CERT_FILE) {
console.log('Running as FTPS server');
if (process.env.KEY_FILE.charAt(0) !== '/') {
keyFile = path.join(__dirname, process.env.KEY_FILE);
}
if (process.env.CERT_FILE.charAt(0) !== '/') {
certFile = path.join(__dirname, process.env.CERT_FILE);
}
options.tls = {
key: fs.readFileSync(keyFile),
cert: fs.readFileSync(certFile),
ca: !process.env.CA_FILES ? null : process.env.CA_FILES
.split(':')
.map(function(f) {
return fs.readFileSync(f);
}),
};
} else {
console.log();
console.log('*** To run as FTPS server, ***');
console.log('*** set "KEY_FILE", "CERT_FILE" ***');
console.log('*** and (optionally) "CA_FILES" env vars. ***');
console.log();
}
server = new ftpd.FtpServer(options.host, {
getInitialCwd: function() {
return '/';
},
getRoot: function() {
return process.cwd();
},
pasvPortRangeStart: 1025,
pasvPortRangeEnd: 1050,
tlsOptions: options.tls,
allowUnauthorizedTls: true,
useWriteFile: false,
useReadFile: false,
uploadMaxSlurpSize: 7000, // N/A unless 'useWriteFile' is true.
});
server.on('error', function(error) {
console.log('FTP Server error:', error);
});
server.on('client:connected', function(connection) {
var username = null;
console.log('client connected: ' + connection.remoteAddress);
connection.on('command:user', function(user, success, failure) {
if (user) {
username = user;
success();
} else {
failure();
}
});
connection.on('command:pass', function(pass, success, failure) {
if (pass) {
success(username);
} else {
failure();
}
});
});
server.debugging = 4;
server.listen(options.port);
console.log('Listening on port ' + options.port);