func (c *Controller) socatProcessOnPort(port int) (*process.Process, string, error) {
	processes, err := process.Processes()
	if err != nil {
		return nil, "", err
	}
	for _, p := range processes {
		cmdline, err := p.Cmdline()
		if err != nil {
			continue
		}
		if strings.HasPrefix(cmdline, fmt.Sprintf("socat TCP-LISTEN:%d,", port)) {
			return p, cmdline, nil
		}
	}
	return nil, "", nil
}
Returns the socat process listening on a port, plus its commandline.
package socat
import (
	"context"
	"fmt"
	"net"
	"os/exec"
	"strings"
	"time"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/client"
	"github.com/shirou/gopsutil/v3/process"
)
const serviceName = "ctlptl-portforward-service"
type ContainerClient interface {
	ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error)
	ContainerRemove(ctx context.Context, id string, options types.ContainerRemoveOptions) error
}
type Controller struct {
	client ContainerClient
}
func NewController(client ContainerClient) *Controller {
	return &Controller{client: client}
}
func DefaultController(ctx context.Context) (*Controller, error) {
	client, err := client.NewClientWithOpts(client.FromEnv)
	if err != nil {
		return nil, err
	}
	client.NegotiateAPIVersion(ctx)
	return NewController(client), nil
}
func (c *Controller) ConnectRemoteDockerPort(ctx context.Context, port int) error {
	err := c.StartRemotePortforwarder(ctx)
	if err != nil {
		return err
	}
	return c.StartLocalPortforwarder(ctx, port)
}
func (c *Controller) StartRemotePortforwarder(ctx context.Context) error {
	container, err := c.client.ContainerInspect(ctx, serviceName)
	if err == nil && (container.ContainerJSONBase != nil && container.State.Running) {
		return nil
	} else if err == nil {
		err := c.client.ContainerRemove(ctx, serviceName, types.ContainerRemoveOptions{Force: true})
		if err != nil {
			return fmt.Errorf("creating remote portforwarder: %v", err)
		}
	} else if !client.IsErrNotFound(err) {
		return fmt.Errorf("inspecting remote portforwarder: %v", err)
	}
	cmd := exec.Command("docker", "run", "-d", "-it",
		"--name", serviceName, "--net=host", "--restart=always",
		"--entrypoint", "/bin/sh", "alpine/socat", "-c", "while true; do sleep 1000; done")
	return cmd.Run()
}