Skip to content

Commit

Permalink
timeout connections after some configurable time (#3805)
Browse files Browse the repository at this point in the history
  • Loading branch information
johrstrom authored Sep 19, 2024
1 parent c32048e commit 5106608
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion apps/shell/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ if (process.env.OOD_SSHHOST_ALLOWLIST){
host_allowlist = Array.from(new Set(process.env.OOD_SSHHOST_ALLOWLIST.split(':')));
}

// default is 8 hours.
const wsTimeout = (process.env.OOD_SHELL_WS_TIMEOUT_MS || 28800000)

let hosts = helpers.definedHosts();
let default_sshhost = hosts['default'];
hosts['hosts'].forEach((host) => {
Expand Down Expand Up @@ -146,6 +149,7 @@ wss.on('connection', function connection (ws, req) {
cmd = process.env.OOD_SSH_WRAPPER || 'ssh';

ws.isAlive = true;
ws.startedAt = Date.now();

console.log('Connection established');

Expand Down Expand Up @@ -200,7 +204,10 @@ wss.on('connection', function connection (ws, req) {

const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
const timeUsed = Date.now() - ws.startedAt;
if (ws.isAlive === false || timeUsed > wsTimeout) {
return ws.terminate();
}

ws.isAlive = false;
ws.ping();
Expand Down

0 comments on commit 5106608

Please sign in to comment.