tunnel

package
v2.6.1-rc.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Normal = MessageCode(iota)

	DialOK
	DialReject
	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 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 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)

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

func NewDialer

func NewDialer(stream Stream) Endpoint

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

The handler remains active until it's been idle for idleDuration, 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.

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 {
	// Close closes the handle
	Close(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)

Jump to

Keyboard shortcuts

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