diff --git a/tunnel-server/src/ssh/base-server.ts b/tunnel-server/src/ssh/base-server.ts index 598809ba..945fed86 100644 --- a/tunnel-server/src/ssh/base-server.ts +++ b/tunnel-server/src/ssh/base-server.ts @@ -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 { @@ -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) @@ -213,7 +213,7 @@ export const baseSshServer = ( log.debug('streamlocal-forward@openssh.com: 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) diff --git a/tunnel-server/src/ssh/index.ts b/tunnel-server/src/ssh/index.ts index f0dc6fc1..b6254b7b 100644 --- a/tunnel-server/src/ssh/index.ts +++ b/tunnel-server/src/ssh/index.ts @@ -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 })