rename to ForwardToListener

This commit is contained in:
Alan Donovan 2021-09-03 09:43:31 -04:00
parent 5bd0519ef3
commit e2552fbd2a
2 changed files with 11 additions and 10 deletions

View file

@ -26,23 +26,24 @@ func NewPortForwarder(session *Session, name string, remotePort int) *PortForwar
}
}
// ListenTCP calls listen on the chosen local TCP port. Zero picks an arbitrary port.
// It is provided for the convenience of callers of ForwardToLocalPort.
func Listen(port int) (net.Listener, error) {
// ListenTCP calls listen on the chosen local TCP port. Zero picks an
// arbitrary port. It is provided for the convenience of callers of
// ForwardToListener.
func ListenTCP(port int) (net.Listener, error) {
return net.Listen("tcp", fmt.Sprintf(":%d", port))
}
// ForwardToLocalPort forwards traffic between the container's remote
// port and a local TCP port, which must already be listening for
// ForwardToListener forwards traffic between the container's remote
// port and a local port, which must already be listening for
// connections. (Accepting a listener rather than a port number avoids
// races against other processes opening ports, and against a client
// connecting to the socket prematurely.)
//
// ForwardToLocalPort accepts and handles connections on the local
// port until it encounters the first error, which may include context
// ForwardToListener accepts and handles connections on the local port
// until it encounters the first error, which may include context
// cancellation. Its error result is always non-nil. The caller is
// responsible for closing the listening port.
func (fwd *PortForwarder) ForwardToLocalPort(ctx context.Context, listen net.Listener) (err error) {
func (fwd *PortForwarder) ForwardToListener(ctx context.Context, listen net.Listener) (err error) {
id, err := fwd.shareRemotePort(ctx)
if err != nil {
return err

View file

@ -46,7 +46,7 @@ func TestPortForwarderStart(t *testing.T) {
}
defer testServer.Close()
listen, err := Listen(8000) // local port
listen, err := ListenTCP(8000) // local port
if err != nil {
t.Fatal(err)
}
@ -58,7 +58,7 @@ func TestPortForwarderStart(t *testing.T) {
done := make(chan error)
go func() {
const name, remote = "ssh", 8000
done <- NewPortForwarder(session, name, remote).ForwardToLocalPort(ctx, listen)
done <- NewPortForwarder(session, name, remote).ForwardToListener(ctx, listen)
}()
go func() {