From 0a451c5876b4e0512c2e2aa50395841286821343 Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Wed, 11 Dec 2019 16:31:05 +0100 Subject: [PATCH] automatically reconnect without x11 forwarding if rejected (fixes #1880) --- terminus-ssh/src/api.ts | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/terminus-ssh/src/api.ts b/terminus-ssh/src/api.ts index 5b2606c3..5e8d9106 100644 --- a/terminus-ssh/src/api.ts +++ b/terminus-ssh/src/api.ts @@ -1,6 +1,6 @@ import { BaseSession } from 'terminus-terminal' import { Server, Socket, createServer, createConnection } from 'net' -import { Client, ClientChannel } from 'ssh2' +import { Client, ClientChannel, Channel } from 'ssh2' import { Logger } from 'terminus-core' import { Subject, Observable } from 'rxjs' @@ -84,19 +84,35 @@ export class SSHSession extends BaseSession { this.scripts = connection.scripts || [] } - async start () { - this.open = true - - this.shell = await new Promise((resolve, reject) => { - this.ssh.shell({ term: 'xterm-256color' }, { x11: true }, (err, shell) => { + private openShellChannel (options): Promise { + return new Promise((resolve, reject) => { + this.ssh.shell({ term: 'xterm-256color' }, options, (err, shell) => { if (err) { - this.emitServiceMessage(`Remote rejected opening a shell channel: ${err}`) reject(err) } else { resolve(shell) } }) }) + } + + async start () { + this.open = true + + try { + try { + this.shell = await this.openShellChannel({ x11: true }) + } catch (e) { + if (e.toString().includes('Unable to request X11')) { + this.logger.debug('X11 forwarding rejected, trying without') + this.shell = await this.openShellChannel({}) + } else { + throw e + } + } + } catch (err) { + this.emitServiceMessage(`Remote rejected opening a shell channel: ${err}`) + } this.shell.on('greeting', greeting => { this.emitServiceMessage(`Shell greeting: ${greeting}`)