@@ -21,6 +21,7 @@ import * as fs from 'fs'
2121import * as os from 'os'
2222import * as path from 'path'
2323import * as readline from 'readline'
24+ import * as debug from 'debug'
2425
2526interface Options {
2627 username ?: string
@@ -40,10 +41,12 @@ interface ForwardingOptions {
4041
4142class SSHConnection {
4243
44+ private debug
4345 private server
4446 private connections : Client [ ] = [ ]
4547
4648 constructor ( private options : Options ) {
49+ this . debug = debug ( 'ssh' )
4750 if ( ! options . username ) {
4851 this . options . username = process . env [ 'SSH_USERNAME' ] || process . env [ 'USER' ]
4952 }
@@ -56,6 +59,7 @@ class SSHConnection {
5659 }
5760
5861 public async shutdown ( ) {
62+ this . debug ( "Shutdown connections" )
5963 for ( const connection of this . connections ) {
6064 connection . removeAllListeners ( )
6165 connection . end ( )
@@ -70,11 +74,13 @@ class SSHConnection {
7074
7175 public async tty ( ) {
7276 const connection = await this . establish ( )
77+ this . debug ( "Opening tty" )
7378 await this . shell ( connection )
7479 }
7580
7681 public async executeCommand ( command ) {
7782 const connection = await this . establish ( )
83+ this . debug ( 'Executing command "%s"' , command )
7884 await this . shell ( connection , command )
7985 }
8086
@@ -116,6 +122,7 @@ class SSHConnection {
116122 }
117123
118124 private async connectViaBastion ( bastionHost : string ) {
125+ this . debug ( 'Connecting to bastion host "%s"' , bastionHost )
119126 const connectionToBastion = await this . connect ( bastionHost )
120127 return new Promise < Client > ( ( resolve , reject ) => {
121128 connectionToBastion . exec ( `nc ${ this . options . endHost } ${ this . options . endPort } ` , async ( err , stream ) => {
@@ -129,6 +136,7 @@ class SSHConnection {
129136 }
130137
131138 private async connect ( host : string , stream ?: NodeJS . ReadableStream ) : Promise < Client > {
139+ this . debug ( 'Connecting to "%s"' , host )
132140 const connection = new Client ( )
133141 return new Promise < Client > ( async ( resolve ) => {
134142 const options = {
@@ -175,6 +183,7 @@ class SSHConnection {
175183 const connection = await this . establish ( )
176184 return new Promise ( ( resolve , reject ) => {
177185 this . server = net . createServer ( ( socket ) => {
186+ this . debug ( 'Forwarding connection from "localhost:%d" to "%s:%d"' , options . fromPort , options . toHost , options . toPort )
178187 connection . forwardOut ( 'localhost' , options . fromPort , options . toHost || 'localhost' , options . toPort , ( error , stream ) => {
179188 if ( error ) {
180189 return reject ( error )
0 commit comments