Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ssh-remote-port-forward lib #165

Merged
merged 1 commit into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@
},
"dependencies": {
"@types/dockerode": "^2.5.34",
"@types/ssh2": "^0.5.45",
"byline": "^5.0.0",
"debug": "^4.1.1",
"docker-compose": "^0.23.5",
"dockerode": "^3.2.1",
"get-port": "^5.1.1",
"glob": "^7.1.6",
"slash": "^3.0.0",
"ssh2": "^0.8.9",
"ssh-remote-port-forward": "^1.0.3",
"stream-to-array": "^2.3.0",
"tar-fs": "^2.1.0"
},
Expand Down
50 changes: 5 additions & 45 deletions src/port-forwarder.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import { Client } from "ssh2";
import { createSshConnection, SshConnection } from "ssh-remote-port-forward";
import { DockerClient } from "./docker-client";
import { log } from "./logger";
import { GenericContainer } from "./generic-container";
import { Port } from "./port";
import { StartedTestContainer } from "./test-container";
import net from "net";
import { Host } from "./docker-client-instance";
import { RandomUuid } from "./uuid";

export class PortForwarder {
constructor(private readonly sshConnection: Client, private readonly container: StartedTestContainer) {}
constructor(private readonly sshConnection: SshConnection, private readonly container: StartedTestContainer) {}

public async exposeHostPort(port: Port): Promise<void> {
log.info(`Exposing host port ${port}`);
await new Promise((resolve, reject) => {
this.sshConnection.forwardIn("127.0.0.1", port, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
await this.sshConnection.remoteForward("localhost", port);
}

public getNetworkId(): string {
Expand Down Expand Up @@ -75,40 +66,9 @@ export class PortForwarderInstance {
const port = container.getMappedPort(22);

log.debug(`Connecting to Port Forwarder on ${host}:${port}`);
const connection = await this.createSshConnection(host, port, username, password);
const connection = await createSshConnection({ host, port, username, password });
connection.unref();

return new PortForwarder(connection, container);
}

private static async createSshConnection(
host: Host,
port: Port,
username: string,
password: string
): Promise<Client> {
return await new Promise((resolve) => {
const connection = new Client();
connection
.on("ready", () => {
log.debug("SSH connection established");
// @ts-ignore
connection._sock.unref();
resolve(connection);
})
.on("tcp connection", (info, accept) => {
const stream = accept();
stream
.on("end", () => log.debug("SSH connection ended"))
.on("error", (err: unknown) => log.warn(`SSH error: ${err}`))
.pause();

const socket = net.connect(info.destPort, info.destIP, () => {
stream.pipe(socket);
socket.pipe(stream);
stream.resume();
});
})
.connect({ host, port, username, password });
});
}
}