-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
126 lines (111 loc) · 2.83 KB
/
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
var ssh2 = require('ssh2'),
fs = require('fs'),
express = require('express'),
bodyParser = require('body-parser');
// Load config defaults from JSON file.
// Environment variables override defaults.
function loadConfig() {
var config = JSON.parse(fs.readFileSync(__dirname+ '/config.json', 'utf-8'));
for (var i in config) {
config[i] = process.env[i.toUpperCase()] || config[i];
}
console.log('Configuration');
console.log(config);
return config;
}
var config = loadConfig();
var app = express();
app.use(bodyParser());
// Convenience for allowing CORS on routes - GET only
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.post('/upload', function(req, res) {
console.log("/upload");
var conn = new ssh2();
conn.on(
'connect',
function () {
console.log("- connected");
}
);
conn.on(
'ready',
function () {
console.log("- ready");
conn.sftp(
function (err, sftp) {
if (err) {
console.log("Error, problem while starting SFTP: %s", err);
res.json({error: 'Unable to start SFTP'});
}
console.log("- SFTP started");
var writeStream = sftp.createWriteStream(req.body.path);
// what to do when transfer finishes
writeStream.on(
'close',
function () {
console.log("- file transferred");
sftp.end();
conn.end();
}
);
writeStream.on(
'error',
function (err) {
console.log('Error, problem while writing file "%s": %s', req.body.path, err);
res.json({error: 'Unable to write "' + req.body.path + '"'});
sftp.end();
conn.end();
}
);
// initiate transfer of file
var offset = 0;
while(offset < req.body.content.length) {
writeStream.write(req.body.content.slice(offset, offset+65536));
offset += 65536;
}
writeStream.end();
}
);
}
);
conn.on(
'error',
function (err) {
console.log("- connection error: %s", err);
if(err.level == "authentication") {
res.json({error: "Authentication failure"});
}
else if(err.code == "ENOTFOUND") {
res.json({error: "Host not found"});
}
else if(err.code == "ETIMEDOUT") {
res.json({error: "Connection timeout"});
}
else {
res.json({error: err});
}
}
);
conn.on(
'end',
function () {
console.log("- connection end");
res.json({});
}
);
conn.connect({
host: req.body.host,
port: req.body.port || 22,
username: req.body.username,
password: req.body.password
});
});
var port = process.env.PORT || config.port || 9999;
app.listen(port, null, function (err) {
console.log('Server started: http://localhost:' + port);
});