tunnel

package
v2.20.3-rc.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Normal = MessageCode(iota)

	// DialOK is sent when a Dialer successfully dialed its connection.
	//
	// A TCP client that receives a DialOK must transit from state SYN_RECEIVED to ESTABLISHED.
	DialOK

	// DialReject is sent when a Dialer fails to dial its connection.
	//
	// A TCP client that receives a DialReject must send an RST and transit from state SYN_RECEIVED to CLOSED.
	DialReject

	// Disconnect is sent when
	//
	// - A TCP client receives a RST, after it has changed its state to CLOSED
	//
	// - A Dialer or Listener Endpoint has been made unavailable for other reasons than a proper close or EOF.
	Disconnect

	KeepAlive
	Session
)
View Source
const Version = uint16(2)

Version

0 which didn't report versions and didn't do synchronization
1 used MuxTunnel instead of one tunnel per connection.

Variables

This section is empty.

Functions

func DialWaitLoop

func DialWaitLoop(
	ctx context.Context,
	tunnelProvider Provider,
	dialStream rpc.Manager_WatchDialClient,
	sessionID string,
) error

DialWaitLoop reads from the given dialStream. A new goroutine that creates a Tunnel to the manager and then attaches a dialer Endpoint to that tunnel is spawned for each request that arrives. The method blocks until the dialStream is closed.

func GetSession

func GetSession(m Message) string

func IsTimeout added in v2.6.8

func IsTimeout(err error) bool

IsTimeout returns true if the given error is a network timeout error.

func NewPipe added in v2.7.2

func NewPipe(id ConnID, sessionID string) (Stream, Stream)

NewPipe creates a pair of Streams connected using two channels.

func ReadLoop

func ReadLoop(ctx context.Context, s Stream, p *CounterProbe) (<-chan Message, <-chan error)

ReadLoop reads from the Stream and dispatches messages and error to the give channels. There will be max one error since the error also terminates the loop.

func UdpReader added in v2.6.8

func UdpReader(ctx context.Context, conn net.PacketConn, ch chan<- UdpReadResult)

UdpReader continuously reads from a net.PacketConn and writes the resulting payload and reply address to a channel. The loop is cancelled when the connection is closed or when the context is done, at which time the channel is closed.

func WithPool

func WithPool(ctx context.Context, pool *Pool) context.Context

WithPool returns a context with the given Pool.

func WriteLoop

func WriteLoop(
	ctx context.Context,
	s Stream, msgCh <-chan Message,
	wg *sync.WaitGroup,
	p *CounterProbe,
)

WriteLoop reads messages from the channel and writes them to the Stream. It will call CloseSend() on the stream when the channel is closed.

Types

type BidiPipeProbes added in v2.15.0

type BidiPipeProbes struct {
	BytesProbeA, BytesProbeB *CounterProbe
}

type Client added in v2.17.0

type Client interface {
	Send(*rpc.TunnelMessage) error
	Recv() (*rpc.TunnelMessage, error)
	grpc.ClientStream
}

type ClientStreamProvider added in v2.17.0

type ClientStreamProvider interface {
	CreateClientStream(ctx context.Context, clientSessionID string, id ConnID, roundTripLatency, dialTimeout time.Duration) (Stream, error)
	ReportMetrics(ctx context.Context, metrics *manager.TunnelMetrics)
}

type ConnID

type ConnID string

A ConnID is a compact and immutable representation of protocol, source IP, source port, destination IP and destination port which is suitable as a map key.

func ConnIDFromUDP added in v2.6.8

func ConnIDFromUDP(src, dst *net.UDPAddr) ConnID

func NewConnID

func NewConnID(proto int, src, dst net.IP, srcPort, dstPort uint16) ConnID

NewConnID returns a new ConnID for the given values.

func NewZeroID

func NewZeroID() ConnID

func (ConnID) Destination

func (id ConnID) Destination() net.IP

Destination returns the destination IP.

func (ConnID) DestinationAddr

func (id ConnID) DestinationAddr() net.Addr

DestinationAddr returns the *net.TCPAddr or *net.UDPAddr that corresponds to the destination IP and port of this instance.

func (ConnID) DestinationNetwork added in v2.18.0

func (id ConnID) DestinationNetwork() string

DestinationNetwork returns either "ip4" or "ip6".

func (ConnID) DestinationPort

func (id ConnID) DestinationPort() uint16

DestinationPort returns the destination port.

func (ConnID) DestinationProtocolString added in v2.18.0

func (id ConnID) DestinationProtocolString() (proto string)

DestinationProtocolString returns the protocol string for the source, e.g. "tcp4".

func (ConnID) IsDestinationIPv4 added in v2.18.0

func (id ConnID) IsDestinationIPv4() bool

IsDestinationIPv4 returns true if the destination of this ConnID is IPv4.

func (ConnID) IsSourceIPv4 added in v2.18.0

func (id ConnID) IsSourceIPv4() bool

IsSourceIPv4 returns true if the source of this ConnID is IPv4.

func (ConnID) Protocol

func (id ConnID) Protocol() int

Protocol returns the protocol, e.g. ipproto.TCP.

func (ConnID) Reply

func (id ConnID) Reply() ConnID

Reply returns a copy of this ConnID with swapped source and destination properties.

func (ConnID) ReplyString

func (id ConnID) ReplyString() string

ReplyString returns a formatted string suitable for logging showing the destination:destinationPort -> source:sourcePort.

func (ConnID) Source

func (id ConnID) Source() net.IP

Source returns the source IP.

func (ConnID) SourceAddr

func (id ConnID) SourceAddr() net.Addr

SourceAddr returns the *net.TCPAddr or *net.UDPAddr that corresponds to the source IP and port of this instance.

func (ConnID) SourceNetwork added in v2.18.0

func (id ConnID) SourceNetwork() string

SourceNetwork returns either "ip4" or "ip6".

func (ConnID) SourcePort

func (id ConnID) SourcePort() uint16

SourcePort returns the source port.

func (ConnID) SourceProtocolString added in v2.18.0

func (id ConnID) SourceProtocolString() (proto string)

SourceProtocolString returns the protocol string for the source, e.g. "tcp4".

func (ConnID) SpanRecord added in v2.7.2

func (id ConnID) SpanRecord(span trace.Span)

func (ConnID) String

func (id ConnID) String() string

String returns a formatted string suitable for logging showing the source:sourcePort -> destination:destinationPort.

type CounterProbe added in v2.15.0

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

func NewCounterProbe added in v2.15.0

func NewCounterProbe(name string) *CounterProbe

func (*CounterProbe) GetName added in v2.15.0

func (p *CounterProbe) GetName() string

func (*CounterProbe) GetValue added in v2.15.0

func (p *CounterProbe) GetValue() uint64

func (*CounterProbe) Increment added in v2.15.0

func (p *CounterProbe) Increment(v uint64)

type Endpoint

type Endpoint interface {
	Start(ctx context.Context)
	Done() <-chan struct{}
}

Endpoint is an endpoint for a Stream such as a Dialer or a bidirectional pipe.

func NewBidiPipe

func NewBidiPipe(a, b Stream, name string, counter *int32, probes *BidiPipeProbes) Endpoint

NewBidiPipe creates a bidirectional pipe between the two given streams.

func NewConnEndpoint

func NewConnEndpoint(stream Stream, conn net.Conn, cancel context.CancelFunc, ingressBytesProbe, egressBytesProbe *CounterProbe) Endpoint

func NewConnEndpointTTL added in v2.8.0

func NewConnEndpointTTL(
	stream Stream,
	conn net.Conn,
	cancel context.CancelFunc,
	ttl time.Duration,
	ingressBytesProbe, egressBytesProbe *CounterProbe,
) Endpoint

func NewDialer

func NewDialer(
	stream Stream,
	cancel context.CancelFunc,
	ingressBytesProbe, egressBytesProbe *CounterProbe,
) Endpoint

NewDialer creates a new handler that dispatches messages in both directions between the given gRPC stream and the given connection.

func NewDialerTTL added in v2.8.0

func NewDialerTTL(stream Stream, cancel context.CancelFunc, ttl time.Duration, ingressBytesProbe, egressBytesProbe *CounterProbe) Endpoint

NewDialerTTL creates a new handler that dispatches messages in both directions between the given gRPC stream and the given connection. The TTL decides how long the connection can be idle before it's closed.

The handler remains active until it's been idle for the ttl duration, at which time it will automatically close and call the release function it got from the tunnel.Pool to ensure that it gets properly released.

func NewUDPListener added in v2.6.8

func NewUDPListener(conn *net.UDPConn, targetAddr *net.UDPAddr, creator func(context.Context, ConnID) (Stream, error)) Endpoint

type GRPCClientStream added in v2.17.0

type GRPCClientStream interface {
	GRPCStream
	CloseSend() error
}

type GRPCStream

type GRPCStream interface {
	Recv() (*rpc.TunnelMessage, error)
	Send(*rpc.TunnelMessage) error
}

GRPCStream is the bare minimum needed for reading and writing TunnelMessages on a Manager_TunnelServer or Manager_TunnelClient.

type Handler

type Handler interface {
	// Stop closes the handle
	Stop(context.Context)

	Start(ctx context.Context)
}

type HandlerCreator

type HandlerCreator func(ctx context.Context, release func()) (Handler, error)

HandlerCreator describes the function signature for the function that creates a handler.

type Message

type Message interface {
	Code() MessageCode
	Payload() []byte
	TunnelMessage() *manager.TunnelMessage
}

func NewMessage

func NewMessage(code MessageCode, payload []byte) Message

func SessionMessage

func SessionMessage(sessionID string) Message

func StreamInfoMessage

func StreamInfoMessage(id ConnID, sessionID string, callDelay, dialTimeout time.Duration) Message

func StreamOKMessage

func StreamOKMessage() Message

type MessageCode

type MessageCode byte

func (MessageCode) String

func (c MessageCode) String() string

type Pool

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

func GetPool

func GetPool(ctx context.Context) *Pool

func NewPool

func NewPool() *Pool

func (*Pool) CloseAll

func (p *Pool) CloseAll(ctx context.Context)

func (*Pool) Get

func (p *Pool) Get(id ConnID) Handler

Get finds a handler for the given id from the pool and returns it. Nil is returned if no such handler exists.

func (*Pool) GetOrCreate

func (p *Pool) GetOrCreate(ctx context.Context, id ConnID, createHandler HandlerCreator) (Handler, bool, error)

GetOrCreate finds a handler for the given id from the pool, or creates a new handler using the given createHandler func when no handler was found. The handler is returned together with a boolean flag which is set to true if the handler was found or false if it was created.

type Provider added in v2.17.0

type Provider interface {
	Tunnel(ctx context.Context, opts ...grpc.CallOption) (Client, error)
}

func AgentProvider added in v2.17.0

func AgentProvider(a agent.AgentClient) Provider

func ManagerProvider added in v2.17.0

func ManagerProvider(m rpc.ManagerClient) Provider

func ManagerProxyProvider added in v2.18.0

func ManagerProxyProvider(m connector.ManagerProxyClient) Provider

type Stream

type Stream interface {
	Tag() string
	ID() ConnID
	Receive(context.Context) (Message, error)
	Send(context.Context, Message) error
	CloseSend(ctx context.Context) error
	PeerVersion() uint16
	SessionID() string
	DialTimeout() time.Duration
	RoundtripLatency() time.Duration
}

The Stream interface represents a bidirectional, synchronized connection Tunnel that sends TCP or UDP traffic over gRPC using manager.TunnelMessage messages.

A Stream is closed by one of six things happening at either end (or at both ends).

  1. Read from local connection fails (typically EOF)
  2. Write to local connection fails (connection peer closed)
  3. Idle timer timed out.
  4. Context is cancelled.
  5. closeSend request received from Tunnel peer.
  6. Disconnect received from Tunnel peer.

When #1 or #2 happens, the Stream will either call CloseSend() (if it's a client Stream) or send a closeSend request (if it's a StreamServer) to its Stream peer, shorten the Idle timer, and then continue to serve incoming data from the Stream peer until it's closed or a Disconnect is received. Once that happens, it's guaranteed that the Tunnel peer will send no more messages and the Stream is closed.

When #3, #4, or #5 happens, the Tunnel will send a Disconnect to its Stream peer and close.

When #6 happens, the Stream will simply close.

func NewClientStream

func NewClientStream(ctx context.Context, grpcStream GRPCClientStream, id ConnID, sessionID string, callDelay, dialTimeout time.Duration) (Stream, error)

func NewServerStream

func NewServerStream(ctx context.Context, grpcStream GRPCStream) (Stream, error)

type StreamCreator added in v2.7.2

type StreamCreator func(context.Context, ConnID) (Stream, error)

StreamCreator is a function that creats a Stream.

type StreamMetrics added in v2.17.0

type StreamMetrics struct {
	ClientSessionID string
	IngressBytes    *CounterProbe
	EgressBytes     *CounterProbe
}

type StreamProvider added in v2.17.0

type StreamProvider interface {
	CreateClientStream(ctx context.Context, clientSessionID string, id ConnID, roundTripLatency, dialTimeout time.Duration) (Stream, error)
}

type TimedHandler added in v2.6.8

type TimedHandler struct {
	ID ConnID
	// contains filtered or unexported fields
}

func NewTimedHandler added in v2.6.8

func NewTimedHandler(id ConnID, ttl time.Duration, remove func()) TimedHandler

func (*TimedHandler) GetTTL added in v2.6.8

func (h *TimedHandler) GetTTL() time.Duration

func (*TimedHandler) Idle added in v2.6.8

func (h *TimedHandler) Idle() <-chan time.Time

func (*TimedHandler) ResetIdle added in v2.6.8

func (h *TimedHandler) ResetIdle() bool

func (*TimedHandler) SetTTL added in v2.6.8

func (h *TimedHandler) SetTTL(ttl time.Duration)

func (*TimedHandler) Start added in v2.6.8

func (h *TimedHandler) Start(_ context.Context)

func (*TimedHandler) Stop added in v2.6.8

func (h *TimedHandler) Stop(_ context.Context)

type TrafficManagerStreamProvider added in v2.17.0

type TrafficManagerStreamProvider struct {
	Manager        manager.ManagerClient
	AgentSessionID string
}

func (*TrafficManagerStreamProvider) CreateClientStream added in v2.17.0

func (sp *TrafficManagerStreamProvider) CreateClientStream(
	ctx context.Context,
	clientSessionID string,
	id ConnID,
	roundTripLatency,
	dialTimeout time.Duration,
) (Stream, error)

type UdpReadResult added in v2.6.8

type UdpReadResult struct {
	Payload []byte
	Addr    *net.UDPAddr
}

Jump to

Keyboard shortcuts

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