Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.

Commit a86e844

Browse files
author
junkern
committed
src: Implement basic debug output.
1 parent a585d25 commit a86e844

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"license": "MIT",
77
"description": "Easy to use ssh client with port forwarding and bastion host support",
88
"dependencies": {
9+
"debug": "^4.1.1",
910
"ssh2": "^0.8.9"
1011
},
1112
"keywords": [

src/Connection.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as fs from 'fs'
2121
import * as os from 'os'
2222
import * as path from 'path'
2323
import * as readline from 'readline'
24+
import * as debug from 'debug'
2425

2526
interface Options {
2627
username?: string
@@ -40,10 +41,12 @@ interface ForwardingOptions {
4041

4142
class 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

Comments
 (0)