2023-01-12 14:16:36 -07:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2021-08-18 15:30:55 +02:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"io"
|
|
|
|
|
"net"
|
|
|
|
|
"sync"
|
|
|
|
|
|
2022-05-09 12:45:10 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/daemon/worker/proxy"
|
2022-12-06 12:44:00 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2021-08-18 15:30:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
2023-01-09 13:43:02 -05:00
|
|
|
err := proxy.RegisterHandler(proxy.TcpHandlerName, handleProxy)
|
2021-08-18 15:30:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 13:43:02 -05:00
|
|
|
// handleProxy creates a tcp proxy between the incoming conn and the
|
|
|
|
|
// connection created by the ProxyDialer.
|
2021-08-18 15:30:55 +02:00
|
|
|
//
|
2023-01-09 13:43:02 -05:00
|
|
|
// handleProxy returns a ProxyConnFn which starts the copy between the
|
|
|
|
|
// connections and blocks until an error (EOF on happy path) is received on
|
|
|
|
|
// either connection.
|
2023-05-30 10:23:23 -07:00
|
|
|
func handleProxy(controlCtx context.Context, _ context.Context, _ proxy.DecryptFn, conn net.Conn, out *proxy.ProxyDialer, connId string, _ *anypb.Any, _ proxy.RecordingManager) (proxy.ProxyConnFn, error) {
|
2022-12-06 12:44:00 -07:00
|
|
|
const op = "tcp.HandleProxy"
|
|
|
|
|
switch {
|
|
|
|
|
case conn == nil:
|
2023-05-30 10:23:23 -07:00
|
|
|
return nil, errors.New(controlCtx, errors.InvalidParameter, op, "conn is nil")
|
2022-12-06 12:44:00 -07:00
|
|
|
case out == nil:
|
2023-05-30 10:23:23 -07:00
|
|
|
return nil, errors.New(controlCtx, errors.InvalidParameter, op, "proxy dialer is nil")
|
2022-12-06 12:44:00 -07:00
|
|
|
case len(connId) == 0:
|
2023-05-30 10:23:23 -07:00
|
|
|
return nil, errors.New(controlCtx, errors.InvalidParameter, op, "connection id is empty")
|
2021-08-18 15:30:55 +02:00
|
|
|
}
|
2023-05-30 10:23:23 -07:00
|
|
|
remoteConn, err := out.Dial(controlCtx)
|
2021-08-18 15:30:55 +02:00
|
|
|
if err != nil {
|
2022-12-06 12:44:00 -07:00
|
|
|
return nil, err
|
2021-08-18 15:30:55 +02:00
|
|
|
}
|
2021-09-17 11:48:44 -07:00
|
|
|
|
2023-05-30 10:23:23 -07:00
|
|
|
return func() {
|
2022-12-06 12:44:00 -07:00
|
|
|
connWg := new(sync.WaitGroup)
|
|
|
|
|
connWg.Add(2)
|
|
|
|
|
go func() {
|
|
|
|
|
defer connWg.Done()
|
|
|
|
|
_, _ = io.Copy(conn, remoteConn)
|
|
|
|
|
_ = conn.Close()
|
|
|
|
|
_ = remoteConn.Close()
|
|
|
|
|
}()
|
|
|
|
|
go func() {
|
|
|
|
|
defer connWg.Done()
|
|
|
|
|
_, _ = io.Copy(remoteConn, conn)
|
|
|
|
|
_ = remoteConn.Close()
|
|
|
|
|
_ = conn.Close()
|
|
|
|
|
}()
|
|
|
|
|
connWg.Wait()
|
|
|
|
|
}, nil
|
2021-08-18 15:30:55 +02:00
|
|
|
}
|