socat

package module
v0.40.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	// defaultImage {
	// DefaultImage is the default image used by the Socat container.
	DefaultImage = "alpine/socat:1.8.0.1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

Container represents the Socat container type used in the module. A socat container is used as a TCP proxy, enabling any TCP port of another container to be exposed publicly, even if that container does not make the port public itself.

func Run

Run creates an instance of the Socat container type

Example
ctx := context.Background()

nw, err := network.New(ctx)
if err != nil {
	log.Printf("failed to create network: %v", err)
	return
}
defer func() {
	if err := nw.Remove(ctx); err != nil {
		log.Printf("failed to remove network: %s", err)
	}
}()

ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0",
	testcontainers.WithExposedPorts("8080/tcp"),
	network.WithNetwork([]string{"helloworld"}, nw),
)
if err != nil {
	log.Printf("failed to create container: %v", err)
	return
}
defer func() {
	if err := testcontainers.TerminateContainer(ctr); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()

target := socat.NewTarget(8080, "helloworld")

socatContainer, err := socat.Run(
	ctx, socat.DefaultImage,
	socat.WithTarget(target),
	network.WithNetwork([]string{"socat"}, nw),
)
if err != nil {
	log.Printf("failed to create container: %v", err)
	return
}
defer func() {
	if err := testcontainers.TerminateContainer(socatContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()

// readFromSocat {
httpClient := http.DefaultClient

baseURI := socatContainer.TargetURL(target.ExposedPort())

resp, err := httpClient.Get(baseURI.String() + "/ping")
if err != nil {
	log.Printf("failed to get response: %v", err)
	return
}
defer resp.Body.Close()
// }

body, err := io.ReadAll(resp.Body)
if err != nil {
	log.Printf("failed to read body: %v", err)
	return
}

fmt.Printf("%d - %s", resp.StatusCode, string(body))
Output:

200 - PONG
Example (MultipleTargets)
ctx := context.Background()

// createNetwork {
nw, err := network.New(ctx)
if err != nil {
	log.Printf("failed to create network: %v", err)
	return
}
defer func() {
	if err := nw.Remove(ctx); err != nil {
		log.Printf("failed to remove network: %s", err)
	}
}()
// }

// createHelloWorldContainer {
ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0",
	network.WithNetwork([]string{"helloworld"}, nw),
	testcontainers.WithExposedPorts("8080/tcp"),
)
if err != nil {
	log.Printf("failed to create container: %v", err)
	return
}
defer func() {
	if err := testcontainers.TerminateContainer(ctr); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
// }

// createSocatContainer {
const (
	// The helloworld container is listening on both ports: 8080 and 8081
	port1 = 8080
	port2 = 8081
	// The helloworld container is not listening on these ports,
	// but the socat container will forward the traffic to the correct port
	port3 = 9080
	port4 = 9081
)

targets := []socat.Target{
	socat.NewTarget(port1, "helloworld"),                        // using a default port
	socat.NewTarget(port2, "helloworld"),                        // using a default port
	socat.NewTargetWithInternalPort(port3, port1, "helloworld"), // using a different port
	socat.NewTargetWithInternalPort(port4, port2, "helloworld"), // using a different port
}

socatContainer, err := socat.Run(
	ctx, socat.DefaultImage,
	socat.WithTarget(targets[0]),
	socat.WithTarget(targets[1]),
	socat.WithTarget(targets[2]),
	socat.WithTarget(targets[3]),
	network.WithNetwork([]string{"socat"}, nw),
)
if err != nil {
	log.Printf("failed to create container: %v", err)
	return
}
defer func() {
	if err := testcontainers.TerminateContainer(socatContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
// }

httpClient := http.DefaultClient

for _, target := range targets {
	baseURI := socatContainer.TargetURL(target.ExposedPort())

	resp, err := httpClient.Get(baseURI.String() + "/ping")
	if err != nil {
		log.Printf("failed to get response: %v", err)
		return
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Printf("failed to read body: %v", err)
		return
	}

	fmt.Printf("%d - %s\n", resp.StatusCode, string(body))
}
Output:

200 - PONG
200 - PONG
200 - PONG
200 - PONG

func (*Container) TargetURL

func (c *Container) TargetURL(exposedPort int) *url.URL

TargetURL returns the URL for the exposed port of a target, nil if the port is not mapped

type Option

type Option func(*options) error

Option is an option for the Socat container.

func WithTarget

func WithTarget(target Target) Option

WithTarget sets a single target for the socat container. The host of the target must be without the port, as it is internally mapped to the exposed port. Multiple calls to WithTarget will accumulate targets.

func (Option) Customize

Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.

type Target

type Target struct {
	// contains filtered or unexported fields
}

Target represents a target for the socat container. Create a new target with NewTarget or NewTargetWithInternalPort.

func NewTarget

func NewTarget(exposedPort int, host string) Target

NewTarget creates a new target for the socat container. The host of the target must be without the port, as it is internally mapped to the exposed port. The exposed port is exposed by the socat container.

func NewTargetWithInternalPort

func NewTargetWithInternalPort(exposedPort int, internalPort int, host string) Target

NewTargetWithInternalPort creates a new target for the socat container. The host of the target must be without the port, as it is internally mapped to the exposed port. The exposed port is the port of the socat container, and the internal port is the port of the target container.

func (Target) ExposedPort

func (t Target) ExposedPort() int

ExposedPort returns the exposed port of the target.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL