refactor: clean up shutdown and move sockets
This commit is contained in:
parent
3f22c8b8e0
commit
e0742db22f
2 changed files with 101 additions and 113 deletions
|
@ -17,23 +17,23 @@ const server = require('http').Server(app);
|
||||||
const favicon = require('serve-favicon');
|
const favicon = require('serve-favicon');
|
||||||
const io = require('socket.io')(server, config.socketio);
|
const io = require('socket.io')(server, config.socketio);
|
||||||
const session = require('express-session')(config.express);
|
const session = require('express-session')(config.express);
|
||||||
|
|
||||||
const appSocket = require('./socket');
|
const appSocket = require('./socket');
|
||||||
const myutil = require('./util');
|
const { setDefaultCredentials, basicAuth } = require('./util');
|
||||||
|
const { webssh2debug, auditLog, logError } = require('./logging');
|
||||||
const { reauth, connect, notfound, handleErrors } = require('./routes');
|
const { reauth, connect, notfound, handleErrors } = require('./routes');
|
||||||
|
|
||||||
myutil.setDefaultCredentials(config);
|
setDefaultCredentials(config);
|
||||||
|
|
||||||
// safe shutdown
|
// safe shutdown
|
||||||
|
let remainingSeconds = config.safeShutdownDuration;
|
||||||
let shutdownMode = false;
|
let shutdownMode = false;
|
||||||
let shutdownInterval = 0;
|
let shutdownInterval;
|
||||||
let connectionCount = 0;
|
let connectionCount = 0;
|
||||||
// eslint-disable-next-line consistent-return
|
// eslint-disable-next-line consistent-return
|
||||||
function safeShutdownGuard(req, res, next) {
|
function safeShutdownGuard(req, res, next) {
|
||||||
if (shutdownMode) {
|
if (!shutdownMode) return next();
|
||||||
res.status(503).end('Service unavailable: Server shutting down');
|
res.status(503).end('Service unavailable: Server shutting down');
|
||||||
} else {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// express
|
// express
|
||||||
app.use(safeShutdownGuard);
|
app.use(safeShutdownGuard);
|
||||||
|
@ -42,7 +42,7 @@ if (config.accesslog) app.use(logger('common'));
|
||||||
app.disable('x-powered-by');
|
app.disable('x-powered-by');
|
||||||
app.use(favicon(path.join(publicPath, 'favicon.ico')));
|
app.use(favicon(path.join(publicPath, 'favicon.ico')));
|
||||||
app.use('/ssh', express.static(publicPath, config.express.ssh));
|
app.use('/ssh', express.static(publicPath, config.express.ssh));
|
||||||
app.use(myutil.basicAuth);
|
app.use(basicAuth);
|
||||||
app.get('/ssh/reauth', reauth);
|
app.get('/ssh/reauth', reauth);
|
||||||
app.get('/ssh/host/:host?', connect);
|
app.get('/ssh/host/:host?', connect);
|
||||||
app.use(notfound);
|
app.use(notfound);
|
||||||
|
@ -52,7 +52,7 @@ app.use(handleErrors);
|
||||||
function stopApp(reason) {
|
function stopApp(reason) {
|
||||||
shutdownMode = false;
|
shutdownMode = false;
|
||||||
if (reason) console.info(`Stopping: ${reason}`);
|
if (reason) console.info(`Stopping: ${reason}`);
|
||||||
if (shutdownInterval) clearInterval(shutdownInterval);
|
clearInterval(shutdownInterval);
|
||||||
io.close();
|
io.close();
|
||||||
server.close();
|
server.close();
|
||||||
}
|
}
|
||||||
|
@ -66,39 +66,41 @@ io.use((socket, next) => {
|
||||||
socket.request.res ? session(socket.request, socket.request.res, next) : next(next); // eslint disable-line
|
socket.request.res ? session(socket.request, socket.request.res, next) : next(next); // eslint disable-line
|
||||||
});
|
});
|
||||||
|
|
||||||
io.on('connection', (socket) => {
|
function countdownTimer() {
|
||||||
connectionCount += 1;
|
if (!shutdownMode) clearInterval(shutdownInterval);
|
||||||
|
remainingSeconds -= 1;
|
||||||
|
if (remainingSeconds <= 0) {
|
||||||
|
stopApp('Countdown is over');
|
||||||
|
} else io.emit('shutdownCountdownUpdate', remainingSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
const signals = ['SIGTERM', 'SIGINT'];
|
||||||
|
signals.forEach((signal) =>
|
||||||
|
process.on(signal, () => {
|
||||||
|
if (shutdownMode) stopApp('Safe shutdown aborted, force quitting');
|
||||||
|
if (!connectionCount > 0) stopApp('All connections ended');
|
||||||
|
shutdownMode = true;
|
||||||
|
console.error(
|
||||||
|
`\r\n${connectionCount} client(s) are still connected.\r\nStarting a ${remainingSeconds} seconds countdown.\r\nPress Ctrl+C again to force quit`
|
||||||
|
);
|
||||||
|
if (!shutdownInterval) shutdownInterval = setInterval(countdownTimer, 1000);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
module.exports = { server, config };
|
||||||
|
|
||||||
|
const onConnection = (socket) => {
|
||||||
|
connectionCount += 1;
|
||||||
socket.on('disconnect', () => {
|
socket.on('disconnect', () => {
|
||||||
connectionCount -= 1;
|
connectionCount -= 1;
|
||||||
if (connectionCount <= 0 && shutdownMode) {
|
if (connectionCount <= 0 && shutdownMode) {
|
||||||
stopApp('All clients disconnected');
|
stopApp('All clients disconnected');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
socket.on('geometry', (cols, rows) => {
|
||||||
|
socket.request.session.ssh.terminfo = { cols, rows };
|
||||||
|
webssh2debug(socket, `SOCKET GEOMETRY: termCols = ${cols}, termRows = ${rows}`);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const signals = ['SIGTERM', 'SIGINT'];
|
io.on('connection', onConnection);
|
||||||
signals.forEach((signal) =>
|
|
||||||
process.on(signal, () => {
|
|
||||||
if (shutdownMode) stopApp('Safe shutdown aborted, force quitting');
|
|
||||||
else if (connectionCount > 0) {
|
|
||||||
let remainingSeconds = config.safeShutdownDuration;
|
|
||||||
shutdownMode = true;
|
|
||||||
const message =
|
|
||||||
connectionCount === 1 ? ' client is still connected' : ' clients are still connected';
|
|
||||||
console.error(connectionCount + message);
|
|
||||||
console.error(`Starting a ${remainingSeconds} seconds countdown`);
|
|
||||||
console.error('Press Ctrl+C again to force quit');
|
|
||||||
|
|
||||||
shutdownInterval = setInterval(() => {
|
|
||||||
remainingSeconds -= 1;
|
|
||||||
if (remainingSeconds <= 0) {
|
|
||||||
stopApp('Countdown is over');
|
|
||||||
} else {
|
|
||||||
io.sockets.emit('shutdownCountdownUpdate', remainingSeconds);
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
} else stopApp();
|
|
||||||
})
|
|
||||||
);
|
|
||||||
module.exports = { server, config };
|
|
||||||
|
|
|
@ -13,9 +13,6 @@ const dnsPromises = require('dns').promises;
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const { webssh2debug, auditLog, logError } = require('./logging');
|
const { webssh2debug, auditLog, logError } = require('./logging');
|
||||||
|
|
||||||
let termCols;
|
|
||||||
let termRows;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* parse conn errors
|
* parse conn errors
|
||||||
* @param {object} socket Socket object
|
* @param {object} socket Socket object
|
||||||
|
@ -75,11 +72,6 @@ async function checkSubnet(socket) {
|
||||||
// public
|
// public
|
||||||
module.exports = function appSocket(socket) {
|
module.exports = function appSocket(socket) {
|
||||||
let login = false;
|
let login = false;
|
||||||
socket.once('geometry', (cols, rows) => {
|
|
||||||
termCols = cols;
|
|
||||||
termRows = rows;
|
|
||||||
webssh2debug(socket, `SOCKET GEOMETRY: termCols = ${cols}, termRows = ${rows}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.once('disconnecting', (reason) => {
|
socket.once('disconnecting', (reason) => {
|
||||||
webssh2debug(socket, `SOCKET DISCONNECTING: ${reason}`);
|
webssh2debug(socket, `SOCKET DISCONNECTING: ${reason}`);
|
||||||
|
@ -138,13 +130,8 @@ module.exports = function appSocket(socket) {
|
||||||
socket.emit('status', 'SSH CONNECTION ESTABLISHED');
|
socket.emit('status', 'SSH CONNECTION ESTABLISHED');
|
||||||
socket.emit('statusBackground', 'green');
|
socket.emit('statusBackground', 'green');
|
||||||
socket.emit('allowreplay', socket.request.session.ssh.allowreplay);
|
socket.emit('allowreplay', socket.request.session.ssh.allowreplay);
|
||||||
conn.shell(
|
const { term, cols, rows } = socket.request.session.ssh;
|
||||||
{
|
conn.shell({ term, cols, rows }, (err, stream) => {
|
||||||
term: socket.request.session.ssh.term,
|
|
||||||
cols: termCols,
|
|
||||||
rows: termRows,
|
|
||||||
},
|
|
||||||
(err, stream) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
logError(socket, `EXEC ERROR`, err);
|
logError(socket, `EXEC ERROR`, err);
|
||||||
conn.end();
|
conn.end();
|
||||||
|
@ -204,8 +191,7 @@ module.exports = function appSocket(socket) {
|
||||||
stream.stderr.on('data', (data) => {
|
stream.stderr.on('data', (data) => {
|
||||||
console.error(`STDERR: ${data}`);
|
console.error(`STDERR: ${data}`);
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
conn.on('end', (err) => {
|
conn.on('end', (err) => {
|
||||||
|
|
Loading…
Reference in a new issue