tsnet: expose ForwardTCPHandler from netstack

Expose the newly added netstack.Impl.ForwardTCPHandler in tsnet so that
callers don't have to implement it themselves when writing TCP forwarders.

Updates #13513

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali 2024-09-18 18:34:18 -07:00
parent f51c968b2a
commit bba4452220

View File

@ -162,6 +162,20 @@ type Server struct {
// over the TCP conn.
type FallbackTCPHandler func(src, dst netip.AddrPort) (handler func(net.Conn), intercept bool)
// ForwardTCPHandler returns a handler that forwards TCP connections to the
// provided destination address. The handler can be used to implement a
// [FallbackTCPHandler]. It returns an error if the destination cannot be
// reached.
func (s *Server) ForwardTCPHandler(dialCtx context.Context, dst string) (func(net.Conn), error) {
h, err := s.netstack.ForwardTCPHandler(dialCtx, dst)
if err != nil {
return nil, err
}
return func(c net.Conn) {
h(c)
}, nil
}
// Dial connects to the address on the tailnet.
// It will start the server if it has not been started yet.
func (s *Server) Dial(ctx context.Context, network, address string) (net.Conn, error) {