forked from statusdashboard/statusdashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp.js
More file actions
97 lines (70 loc) · 2.38 KB
/
Copy pathftp.js
File metadata and controls
97 lines (70 loc) · 2.38 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
/**
TODO: Make it inherit TCP transport?
*/
var net = require('net');
var Ftp = function (service, stream, callback) {
this.service = service;
this.stream = stream;
/**
Actually required to get the SERVER READY out of the way
I am also assuming this might catch potential
on-connection ftp error
codes...
*/
this.command('', callback);
};
Ftp.prototype.command = function(command, callback) {
this.stream.write(command + '\r\n');
this.stream.once('data', function(data) {
var status = data.toString().match(/^\d\d\d/);
status = status[0] ? parseInt(status[0], 10) : 0;
/**
TODO?
4xx and 5xx should be treated differently. 4xx should be critical,
while 5xx should be (potentially) considered as down
Removed 600 from potential error responses
*/
if ((status > 399 && status < 600) || status > 999) {
return callback(status, data.toString());
}
return callback(null, data.toString());
});
};
Ftp.prototype.returnServiceStatus = function(service, status, statusCode, message, callback) {
service.status = status;
service.statusCode = statusCode;
service.message = message;
this.stream.end();
this.stream.destroy();
callback(service.status, service);
};
exports.check = function(serviceDefinition, service, callback) {
service.status = "unknown";
service.statusCode = 0;
service.message = 'FTP check in progress...';
var stream = net.createConnection(serviceDefinition.port, serviceDefinition.host);
stream.addListener('connect', function () {
var ftp = new Ftp(service, stream, function (err, data) {
if (err) {
return ftp.returnServiceStatus(service, 'down', err, data, callback);
}
ftp.command('USER ' + serviceDefinition.username, function(err, data) {
if (err) {
return ftp.returnServiceStatus(service, 'critical', err, data, callback);
}
ftp.command('PASS ' + serviceDefinition.password, function(err, data) {
if (err) {
return ftp.returnServiceStatus(service, 'critical', err, data, callback);
}
return ftp.returnServiceStatus(service, 'up', 0, '', callback);
});
});
});
});
stream.addListener('error', function (e) {
service.status = 'down';
service.statusCode = 0;
service.message = e.message;
return callback(service.status, service);
});
};