@@ -9,7 +9,7 @@ import { LocalSSHServiceImpl, startLocalSSHService } from './ipc/localssh';
99import { SupervisorSSHTunnel } from './sshTunnel' ;
1010import { ILogService } from '../services/logService' ;
1111import { SshServer , SshClient } from '@microsoft/dev-tunnels-ssh-tcp' ;
12- import { NodeStream , SshClientCredentials , SshClientSession , SshSessionConfiguration } from '@microsoft/dev-tunnels-ssh' ;
12+ import { NodeStream , SshClientCredentials , SshClientSession , SshDisconnectReason , SshSessionConfiguration } from '@microsoft/dev-tunnels-ssh' ;
1313import { importKeyBytes } from '@microsoft/dev-tunnels-ssh-keys' ;
1414import { parsePrivateKey } from 'sshpk' ;
1515import { PipeExtensions } from './patch/pipeExtension' ;
@@ -23,14 +23,15 @@ export class LocalSSHGatewayServer {
2323 private localsshService ! : LocalSSHServiceImpl ;
2424 private localsshServiceServer ?: GrpcServer ;
2525 private server ?: SshServer ;
26+ private clientCount = 0 ;
2627
2728 constructor (
2829 private readonly logger : ILogService ,
2930 private readonly port : number ,
3031 private readonly ipcPort : number ,
3132 ) { }
3233
33- async authenticateClient ( clientUsername : string ) {
34+ private async authenticateClient ( clientUsername : string ) {
3435 const workspaceInfo = await this . localsshService . getWorkspaceAuthInfo ( clientUsername ) . catch ( e => {
3536 this . logger . error ( e , 'failed to get workspace auth info' ) ;
3637 /*
@@ -69,22 +70,32 @@ export class LocalSSHGatewayServer {
6970
7071 server . onSessionOpened ( ( session ) => {
7172 let pipeSession : SshClientSession ;
73+ this . clientCount += 1 ;
7274 session . onAuthenticating ( ( e ) => {
73- e . authenticationPromise = new Promise ( ( resolve , reject ) => {
74- this . authenticateClient ( e . username ! ) . then ( async s => {
75+ e . authenticationPromise = this . authenticateClient ( e . username ! ) . then ( s => {
7576 this . logger . info ( 'authenticate with ' + e . username ) ;
7677 pipeSession = s ;
77- resolve ( new Object ( ) ) ;
78+ return { } ;
7879 } ) . catch ( e => {
7980 this . logger . error ( e , 'failed to authenticate client' ) ;
8081 // TODO not sure how to get gitpod host here
8182 // this.localsshService.sendErrorReport(e.username, undefined, e, 'failed to authenticate client');
82- reject ( null ) ;
83+ session . close ( SshDisconnectReason . hostNotAllowedToConnect , 'auth failed or workspace is not running' ) ;
84+ return null ;
8385 } ) ;
84- } ) ;
8586 } ) ;
86- session . onClientAuthenticated ( ( ) => {
87- PipeExtensions . pipeSession ( session , pipeSession ) ;
87+ session . onClientAuthenticated ( async ( ) => {
88+ try {
89+ await PipeExtensions . pipeSession ( session , pipeSession ) ;
90+ } catch ( e ) {
91+ this . logger . error ( e , 'pipe session ended with error' ) ;
92+ } finally {
93+ session . close ( SshDisconnectReason . connectionLost , 'pipe session ended' ) ;
94+ }
95+ } ) ;
96+ session . onClosed ( ( ) => {
97+ this . clientCount -= 1 ;
98+ this . logger . debug ( 'current connecting client count: ' + this . clientCount ) ;
8899 } ) ;
89100 } ) ;
90101 await server . acceptSessions ( this . port , '127.0.0.1' ) ;
@@ -180,4 +191,3 @@ export class LocalSSHGatewayServer {
180191 this . localsshServiceServer ?. shutdown ( ) ;
181192 }
182193}
183-
0 commit comments