fixed #6263, fixed #7460 - ssh: handle MaxSessions

This commit is contained in:
Eugene Pankov 2022-11-17 20:26:17 +01:00
parent c7eb19386d
commit 0f0f61f432
No known key found for this signature in database
GPG Key ID: 5896FCBBDD1CF4F4
3 changed files with 25 additions and 21 deletions

View File

@ -84,9 +84,9 @@ export class SSHTabComponent extends BaseTerminalTabComponent {
super.ngOnInit() super.ngOnInit()
} }
async setupOneSession (injector: Injector, profile: SSHProfile): Promise<SSHSession> { async setupOneSession (injector: Injector, profile: SSHProfile, multiplex = true): Promise<SSHSession> {
let session = await this.sshMultiplexer.getSession(profile) let session = await this.sshMultiplexer.getSession(profile)
if (!session || !profile.options.reuseSession) { if (!multiplex || !session || !profile.options.reuseSession) {
session = new SSHSession(injector, profile) session = new SSHSession(injector, profile)
if (profile.options.jumpHost) { if (profile.options.jumpHost) {
@ -146,11 +146,8 @@ export class SSHTabComponent extends BaseTerminalTabComponent {
try { try {
await session.start() await session.start()
} finally {
this.stopSpinner() this.stopSpinner()
} catch (e) {
this.stopSpinner()
this.write(colors.black.bgRed(' X ') + ' ' + colors.red(e.message) + '\r\n')
return session
} }
this.sshMultiplexer.addSession(session) this.sshMultiplexer.addSession(session)
@ -186,21 +183,14 @@ export class SSHTabComponent extends BaseTerminalTabComponent {
super.attachSessionHandlers() super.attachSessionHandlers()
} }
async initializeSession (): Promise<void> { private async initializeSessionMaybeMultiplex (multiplex = true): Promise<void> {
this.reconnectOffered = false
if (!this.profile) { if (!this.profile) {
this.logger.error('No SSH connection info supplied') throw new Error('No SSH connection info supplied')
return
}
try {
this.sshSession = await this.setupOneSession(this.injector, this.profile)
} catch (e) {
this.write(colors.black.bgRed(' X ') + ' ' + colors.red(e.message) + '\r\n')
return
} }
this.sshSession = await this.setupOneSession(this.injector, this.profile, multiplex)
const session = new SSHShellSession(this.injector, this.sshSession, this.profile) const session = new SSHShellSession(this.injector, this.sshSession, this.profile)
this.setSession(session) this.setSession(session)
this.attachSessionHandler(session.serviceMessage$, msg => { this.attachSessionHandler(session.serviceMessage$, msg => {
msg = msg.replace(/\n/g, '\r\n ') msg = msg.replace(/\n/g, '\r\n ')
@ -212,6 +202,20 @@ export class SSHTabComponent extends BaseTerminalTabComponent {
this.session?.resize(this.size.columns, this.size.rows) this.session?.resize(this.size.columns, this.size.rows)
} }
async initializeSession (): Promise<void> {
this.reconnectOffered = false
try {
await this.initializeSessionMaybeMultiplex(true)
} catch {
try {
await this.initializeSessionMaybeMultiplex(false)
} catch (e) {
this.write(colors.black.bgRed(' X ') + ' ' + colors.red(e.message) + '\r\n')
return
}
}
}
async getRecoveryToken (options?: GetRecoveryTokenOptions): Promise<RecoveryToken> { async getRecoveryToken (options?: GetRecoveryTokenOptions): Promise<RecoveryToken> {
return { return {
type: 'app:ssh-tab', type: 'app:ssh-tab',

View File

@ -15,7 +15,9 @@ export class SSHMultiplexerService {
const key = await this.getMultiplexerKey(session.profile) const key = await this.getMultiplexerKey(session.profile)
this.sessions.set(key, session) this.sessions.set(key, session)
session.willDestroy$.subscribe(() => { session.willDestroy$.subscribe(() => {
this.sessions.delete(key) if (this.sessions.get(key) === session) {
this.sessions.delete(key)
}
}) })
} }

View File

@ -1,5 +1,4 @@
import { Observable, Subject } from 'rxjs' import { Observable, Subject } from 'rxjs'
import colors from 'ansi-colors'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
import { ClientChannel } from 'ssh2' import { ClientChannel } from 'ssh2'
import { Injector } from '@angular/core' import { Injector } from '@angular/core'
@ -41,11 +40,10 @@ export class SSHShellSession extends BaseSession {
try { try {
this.shell = await this.ssh.openShellChannel({ x11: this.profile.options.x11 ?? false }) this.shell = await this.ssh.openShellChannel({ x11: this.profile.options.x11 ?? false })
} catch (err) { } catch (err) {
this.emitServiceMessage(colors.bgRed.black(' X ') + ` Remote rejected opening a shell channel: ${err}`)
if (err.toString().includes('Unable to request X11')) { if (err.toString().includes('Unable to request X11')) {
this.emitServiceMessage(' Make sure `xauth` is installed on the remote side') this.emitServiceMessage(' Make sure `xauth` is installed on the remote side')
} }
return throw new Error(`Remote rejected opening a shell channel: ${err}`)
} }
this.open = true this.open = true