Skip to content

Commit

Permalink
tunnel server: cleanup on error
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Sep 10, 2023
1 parent ac9a32f commit a2af125
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 6 additions & 6 deletions tunnel-server/src/ssh/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ const parseForwardRequestFromSocketBindInfo = (
}

export interface ClientForward extends EventEmitter {
localSocketPath: string
on: (event: 'close', listener: () => void) => this
on: (
(event: 'close', listener: () => void) => this
) & (
(event: 'error', listener: (err: Error) => void) => this
)
}

export interface BaseSshClient extends EventEmitter {
Expand Down Expand Up @@ -196,9 +199,6 @@ export const baseSshServer = (
if (err) {
log.error('error forwarding request %j: %s', request, inspect(err))
socket.end()
socketServer.close(closeErr => {
log.error('error closing socket server for request %j: %j', request, inspect(closeErr))
})
return
}
upstream.pipe(socket).pipe(upstream)
Expand All @@ -213,7 +213,7 @@ export const baseSshServer = (
log.debug('[email protected]: request %j calling accept: %j', request, accept)
accept?.()
socketServers.set(request, socketServer)
resolveForward(Object.assign(socketServer, { localSocketPath: socketPath }))
resolveForward(socketServer)
})
.on('error', (err: unknown) => {
log.error('socketServer request %j error: %j', request, err)
Expand Down
12 changes: 9 additions & 3 deletions tunnel-server/src/ssh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ export const createSshServer = ({
return undefined
}
tunnels.set(requestId, tunnelUrl(clientId, tunnelPath))
forward.on('close', () => {
log.info('deleting tunnel %s', key)
const onForwardClose = (event: 'close' | 'error') => (err?: Error) => {
if (err) {
log.info('%s: deleting tunnel %s due to forward server error: %j', event, key, inspect(err))
} else {
log.info('%s: deleting tunnel %s', event, key)
}
tunnels.delete(requestId)
void activeTunnelStore.delete(key, setTx)
tunnelsGauge.dec({ clientId })
})
}
forward.on('close', onForwardClose('close'))
forward.on('error', onForwardClose('error'))
tunnelsGauge.inc({ clientId })
return undefined
})
Expand Down

0 comments on commit a2af125

Please sign in to comment.