-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
205 lines (196 loc) · 8.51 KB
/
index.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
/*
* WebSSH2 - Web to SSH2 gateway
* Bill Church - https://github.com/billchurch - April 2016
*/
var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');
var server = require('http').Server(app);
var io = require('socket.io')(server, {
path: '/webssh/socket.io'
});
var path = require('path');
var fs = require('fs');
var basicAuth = require('basic-auth');
var ssh = require('ssh2');
var readConfig = require('read-config'),
config = readConfig(__dirname + '/config.json');
var myError = " - ";
var serverStatusFile = "/appsvctmp/status.txt";
function logErrors(err, req, res, next) {
console.error(err.stack);
next(err);
}
server.listen({
host: config.listen.ip,
port: config.listen.port
}).on('error', function (err) {
if (err.code === 'EADDRINUSE') {
config.listen.port++;
console.log('Address in use, retrying on port ' + config.listen.port);
setTimeout(function () {
server.listen(config.listen.port);
}, 250);
}
});
app.use(express.static(__dirname + '/webssh/public')).use(function (req, res, next) {
if (req.originalUrl != null && req.originalUrl.includes("webssh/host")) {
if (req.query.debugconsolereq != null) {
// Use the runtime User for Kudu, and the dynamically generated password during the startup
config.user.name = process.env.USER_NAME;
config.user.password = process.env.USER_PASSWORD;
config.ssh.host = "127.0.0.6";
config.ssh.port = 22;
config.ssh.kuduDebugReq = true;
} else {
// Default value of WEBSITE_SSH_USER=root, WEBSITE_SSH_PASSWORD=Docker!
config.user.name = process.env.WEBSITE_SSH_USER;
config.user.password = process.env.WEBSITE_SSH_PASSWORD;
config.ssh.port = 2222;
fs.readFile('/appsvctmp/ipaddr_' + process.env.WEBSITE_ROLE_INSTANCE_ID, 'utf8', function (err, data) {
if (err) {
fs.readFile('/home/site/ipaddr_' + process.env.WEBSITE_ROLE_INSTANCE_ID, 'utf8', function (err, data) {
if (err) {
config.ssh.host = 'Couldnt connect to main site container';
} else {
configureSshFromString(data);
}
});
} else {
configureSshFromString(data);
}
});
}
}
next();
}).use('/webssh/socket.io', express.static(__dirname + '/node_modules/socket.io-client/dist')).use('/webssh/client.js', express.static(__dirname + '/public/client.js')).use('/webssh/style', express.static(__dirname + '/public')).use('/webssh/src', express.static(__dirname + '/node_modules/xterm/dist')).use('/webssh/addons', express.static(__dirname + '/node_modules/xterm/dist/addons'))
.use(cookieParser()).get('/webssh/host/:host?', function (req, res) {
res.sendFile(path.join(__dirname + '/public/client.htm'));
console.log('Host: ' + config.ssh.host);
if (typeof req.query.port !== 'undefined' && req.query.port !== null) {
config.ssh.port = req.query.port;
}
if (typeof req.query.header !== 'undefined' && req.query.header !== null) {
config.header.text = req.query.header;
}
if (typeof req.query.headerBackground !== 'undefined' && req.query.headerBackground !== null) {
config.header.background = req.query.headerBackground;
}
console.log('webssh2 Login: user=' + config.user.name + ' from=' + req.ip + ' host=' + config.ssh.host + ' port=' + config.ssh.port + ' sessionID=' + req.headers['sessionid'] + ' allowreplay=' + req.headers['allowreplay']);
console.log('Headers: ' + JSON.stringify(req.headers));
config.options.allowreplay = req.headers['allowreplay'];
});
io.on('connection', function (socket) {
var conn = new ssh();
conn.on('banner', function (d) {
//need to convert to cr/lf for proper formatting
d = d.replace(/\r?\n/g, "\r\n");
socket.emit('data', d.toString('binary'));
}).on('ready', function () {
socket.emit('title', 'ssh://' + config.ssh.host);
socket.emit('headerBackground', config.header.background);
socket.emit('header', config.header.text);
socket.emit('footer', 'ssh://' + config.user.name + '@' + config.ssh.host + ':' + config.ssh.port);
socket.emit('status', 'SSH CONNECTION ESTABLISHED');
socket.emit('statusBackground', 'green');
socket.emit('allowreplay', config.options.allowreplay)
conn.shell(function (err, stream) {
if (err) {
console.log(err.message);
myError = myError + err.message
return socket.emit('status', 'SSH EXEC ERROR: ' + err.message).emit('statusBackground', 'red');
}
socket.on('data', function (data) {
stream.write(data);
});
socket.on('control', function (controlData) {
switch (controlData) {
case 'replayCredentials':
stream.write(config.user.password + '\n');
default:
console.log('controlData: ' + controlData);
};
});
stream.on('data', function (d) {
socket.emit('data', d.toString('binary'));
}).on('close', function (code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
}).stderr.on('data', function (data) {
console.log('STDERR: ' + data);
});
});
}).on('end', function () {
socket.emit('status', 'SSH CONNECTION CLOSED BY HOST' + myError);
socket.emit('statusBackground', 'red');
}).on('close', function () {
socket.emit('status', 'SSH CONNECTION CLOSE' + myError);
socket.emit('statusBackground', 'red');
}).on('error', function (err) {
myError = myError + err
socket.emit('status', 'SSH CONNECTION ERROR' + myError);
socket.emit('statusBackground', 'red');
console.log('on.error' + myError);
}).on('keyboard-interactive', function (name, instructions, instructionsLang, prompts, finish) {
console.log('Connection :: keyboard-interactive');
finish([config.user.password]);
}).connect({
host: config.ssh.host,
port: config.ssh.port,
username: config.user.name,
password: config.user.password,
tryKeyboard: true,
// some cisco routers need the these cipher strings
algorithms: {
'cipher': ['aes128-cbc', '3des-cbc', 'aes256-cbc', 'aes128-ctr', 'aes256-ctr', 'aes192-ctr'],
'hmac': ['hmac-sha1', 'hmac-sha1-96', 'hmac-md5-96']
}
});
});
// Monitors change in the server status file to refresh/reload WebSsh on client
io.sockets.on('connection', function (socket) {
if (!config.ssh.kuduDebugReq) {
fs.watchFile(serverStatusFile, {
persistent: true,
interval: 1000
}, function (data) {
fs.readFile(serverStatusFile, 'utf8', function (err, fileData) {
if (err) {
console.log('Status_WatchFile :: Error ' + err);
//pass
} else {
socket.emit('server', {
message: fileData
});
}
});
});
// Continually check if the Server Status file exists
var checkStatusFileContents = function () {
fs.access(serverStatusFile, (err) => {
if (err) {
socket.emit('server', {
message: 'LSiteNotStarted'
});
} else {
setTimeout(checkStatusFileContents, 1000);
}
});
}
checkStatusFileContents();
}
});
// Parse a string in IP or IP:PORT format and set the configs accordingly
function configureSshFromString(instr) {
// Check if port exists
var portloc = instr.indexOf(':');
if (portloc > -1) {
config.ssh.host = instr.substr(0, portloc);
config.ssh.port = instr.substr(portloc + 1, instr.length - config.ssh.host.length - 1);
console.log('Port from file: ' + config.ssh.port);
} else {
config.ssh.host = instr;
config.ssh.port = 2222;
}
console.log('Host from file: ' + config.ssh.host);
}