From 764cfd3cf0933106c97d5da75945500fbaaaede2 Mon Sep 17 00:00:00 2001 From: Martin Wentzel Date: Fri, 1 Mar 2019 15:54:41 +0100 Subject: [PATCH 1/3] Implements custom end host port. --- src/Connection.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Connection.ts b/src/Connection.ts index 1624d1b..5154a9b 100644 --- a/src/Connection.ts +++ b/src/Connection.ts @@ -24,10 +24,11 @@ import * as readline from 'readline' interface Options { username?: string - privateKey?: string | Buffer, - agentForward? : boolean, - bastionHost?: string, + privateKey?: string | Buffer + agentForward? : boolean + bastionHost?: string passphrase?: string + endPort?: number endHost: string } @@ -46,6 +47,9 @@ class SSHConnection { if (!options.username) { this.options.username = process.env['SSH_USERNAME'] || process.env['USER'] } + if (!options.endPort) { + this.options.endPort = 22 + } if (!options.privateKey) { this.options.privateKey = fs.readFileSync(`${os.homedir()}${path.sep}.ssh${path.sep}id_rsa`) } @@ -114,7 +118,7 @@ class SSHConnection { private async connectViaBastion(bastionHost: string) { const connectionToBastion = await this.connect(bastionHost) return new Promise((resolve, reject) => { - connectionToBastion.exec(`nc ${this.options.endHost} 22`, async (err, stream) => { + connectionToBastion.exec(`nc ${this.options.endHost} ${this.options.endPort}`, async (err, stream) => { if (err) { return reject(err) } From b64dae113b9f222d7a7568cca161305e4a9a0be6 Mon Sep 17 00:00:00 2001 From: Martin Wentzel Date: Fri, 1 Mar 2019 15:54:55 +0100 Subject: [PATCH 2/3] Adds test for custom end host port. --- test/server/Dockerfile | 4 ++-- test/test.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test/server/Dockerfile b/test/server/Dockerfile index 475bef5..a2dc778 100644 --- a/test/server/Dockerfile +++ b/test/server/Dockerfile @@ -12,5 +12,5 @@ COPY ./server/sshd_config /etc/ssh/sshd_config COPY ./keys/authorized_keys /root/.ssh/authorized_keys -EXPOSE 22 -CMD /usr/sbin/sshd -D \ No newline at end of file +EXPOSE 22 23 +CMD /usr/sbin/sshd -D -p 22 -p 23 \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index edac0be..8d83336 100644 --- a/test/test.ts +++ b/test/test.ts @@ -35,5 +35,13 @@ describe('node-ssh-forward', async () => { }) await ssh.executeCommand('uptime') }) + it('with a custom end port', async () => { + const ssh = new SSHConnection({ + username: 'root', + endHost: 'server', + endPort: 23 + }) + await ssh.executeCommand('uptime') + }) }) }) From 0161c20048a752248c122cbf2cdb962053b52743 Mon Sep 17 00:00:00 2001 From: Martin Wentzel Date: Fri, 1 Mar 2019 17:27:31 +0100 Subject: [PATCH 3/3] Adds documentation for endPort option. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ebc1c05..6f8266f 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Options are an object with following properties: * `privateKey` (optional): Can be a `string` or `Buffer` that contains a private key. If not set, it fallbacks to `~/.ssh/id_rsa` * `agentForward` (optional): Is a `boolean` which uses the `ssh-agent` for connection (defaults to `false`. * `endHost` (required): The host you want to end up on (connect to) +* `endPort` (optional): Port number of the server. Needed in case the server runs on a custom port (defaults to `22`) * `bastionHost` (optional): You can specify a bastion host if you want * `passphrase` (optional): You can specify the passphrase when you have an encrypted private key. If you don't specify the passphrase and you use an encrypted private key, you get prompted in the command line.