tunnel

package
v2.10.3-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: Apache-2.0 Imports: 20 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, manager rpc.ManagerClient, 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 IPProto

func IPProto(network string) int

IPProto returns the IP protocol for the given network. Currently only supports TCP, UDP, and ICMP.

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) (<-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)

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 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) DestinationPort

func (id ConnID) DestinationPort() uint16

DestinationPort returns the destination port.

func (ConnID) IsIPv4

func (id ConnID) IsIPv4() bool

IsIPv4 returns true if the source and destination of this ConnID are IPv4.

func (ConnID) Network

func (id ConnID) Network() string

Network returns either "ip4" or "ip6".

func (ConnID) Protocol

func (id ConnID) Protocol() int

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

func (ConnID) ProtocolString

func (id ConnID) ProtocolString() (proto string)

ProtocolString returns the protocol string, e.g. "tcp4".

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) SourcePort

func (id ConnID) SourcePort() uint16

SourcePort returns the source port.

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 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) Endpoint

NewBidiPipe creates a bidirectional pipe between the two given streams.

func NewConnEndpoint

func NewConnEndpoint(stream Stream, conn net.Conn, cancel context.CancelFunc) Endpoint

func NewConnEndpointTTL added in v2.8.0

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

func NewDialer

func NewDialer(stream Stream, cancel context.CancelFunc) 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) 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 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 GRPClientCStream

type GRPClientCStream interface {
	GRPCStream
	CloseSend() error
}

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 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 GRPClientCStream, 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 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 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