|
| 1 | +import { RandomUuid } from "../common"; |
| 2 | +import { AbstractStartedContainer } from "../generic-container/abstract-started-container"; |
| 3 | +import { GenericContainer } from "../generic-container/generic-container"; |
| 4 | +import { StartedTestContainer } from "../test-container"; |
| 5 | + |
| 6 | +export class SocatContainer extends GenericContainer { |
| 7 | + private targets: { [key in number]: string } = {}; |
| 8 | + |
| 9 | + constructor(image = "alpine/socat:1.7.4.3-r0") { |
| 10 | + super(image); |
| 11 | + this.withEntrypoint(["/bin/sh"]); |
| 12 | + this.withName(`testcontainers-socat-${new RandomUuid().nextUuid()}`); |
| 13 | + } |
| 14 | + |
| 15 | + public withTarget(exposePort: number, host: string): this; |
| 16 | + public withTarget(exposePort: number, host: string, internalPort: number): this; |
| 17 | + public withTarget(exposePort: number, host: string, internalPort?: number): this { |
| 18 | + this.withExposedPorts(exposePort); |
| 19 | + if (internalPort == null) { |
| 20 | + internalPort = exposePort; |
| 21 | + } |
| 22 | + this.targets[exposePort] = `${host}:${internalPort}`; |
| 23 | + return this; |
| 24 | + } |
| 25 | + |
| 26 | + public override async start(): Promise<StartedSocatContainer> { |
| 27 | + const command = Object.entries(this.targets) |
| 28 | + .map(([exposePort, target]) => `socat TCP-LISTEN:${exposePort},fork,reuseaddr TCP:${target}`) |
| 29 | + .join(" & "); |
| 30 | + |
| 31 | + this.withCommand(["-c", command]); |
| 32 | + return new StartedSocatContainer(await super.start()); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export class StartedSocatContainer extends AbstractStartedContainer { |
| 37 | + constructor(startedTestcontainers: StartedTestContainer) { |
| 38 | + super(startedTestcontainers); |
| 39 | + } |
| 40 | +} |
0 commit comments