Documentation ¶
Index ¶
- Constants
- func DialWaitLoop(ctx context.Context, manager rpc.ManagerClient, ...)
- func GetSession(m Message) string
- func IPProto(network string) int
- func ReadLoop(ctx context.Context, s Stream) (<-chan Message, <-chan error)
- func WithPool(ctx context.Context, pool *Pool) context.Context
- func WriteLoop(ctx context.Context, s Stream, msgCh <-chan Message)
- type ConnID
- func (id ConnID) Destination() net.IP
- func (id ConnID) DestinationAddr() net.Addr
- func (id ConnID) DestinationPort() uint16
- func (id ConnID) IsIPv4() bool
- func (id ConnID) Network() string
- func (id ConnID) Protocol() int
- func (id ConnID) ProtocolString() (proto string)
- func (id ConnID) Reply() ConnID
- func (id ConnID) ReplyString() string
- func (id ConnID) Source() net.IP
- func (id ConnID) SourceAddr() net.Addr
- func (id ConnID) SourcePort() uint16
- func (id ConnID) String() string
- type Endpoint
- type GRPCStream
- type GRPClientCStream
- type Handler
- type HandlerCreator
- type Message
- type MessageCode
- type Pool
- func (p *Pool) CloseAll(ctx context.Context)
- func (p *Pool) Get(id ConnID) Handler
- func (p *Pool) GetOrCreate(ctx context.Context, id ConnID, createHandler HandlerCreator) (Handler, bool, error)
- func (p *Pool) GetOrCreateTCP(ctx context.Context, id ConnID, createHandler HandlerCreator, ...) (Handler, bool, error)
- type RecursionBlocker
- type Stream
Constants ¶
const ( Normal = MessageCode(iota) DialOK DialReject Disconnect KeepAlive Session )
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)
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 IPProto ¶
IPProto returns the IP protocol for the given network. Currently only supports TCP, UDP, and ICMP
func ReadLoop ¶
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.
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 (ConnID) Destination ¶
Destination returns the destination IP
func (ConnID) DestinationAddr ¶
DestinationAddr returns the *net.TCPAddr or *net.UDPAddr that corresponds to the destination IP and port of this instance.
func (ConnID) DestinationPort ¶
DestinationPort returns the destination port
func (ConnID) ProtocolString ¶
ProtocolString returns the protocol string, e.g. "tcp4"
func (ConnID) Reply ¶
Reply returns a copy of this ConnID with swapped source and destination properties
func (ConnID) ReplyString ¶
ReplyString returns a formatted string suitable for logging showing the destination:destinationPort -> source:sourcePort
func (ConnID) SourceAddr ¶
SourceAddr returns the *net.TCPAddr or *net.UDPAddr that corresponds to the source IP and port of this instance.
type Endpoint ¶
Endpoint is an endpoint for a Stream such as a Dialer or a bidirectional pipe.
func NewBidiPipe ¶
NewBidiPipe creates a bidirectional pipe between the two given streams
func NewDialer ¶
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 HandlerCreator ¶
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 StreamInfoMessage ¶
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 (*Pool) Get ¶
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.
func (*Pool) GetOrCreateTCP ¶ added in v2.4.7
func (p *Pool) GetOrCreateTCP(ctx context.Context, id ConnID, createHandler HandlerCreator, initialPacket ip.Packet) (Handler, bool, error)
GetOrCreateTCP is like GetOrCreate but with the addition that it detects and delays attempts to create handlers for the same destination IP and port until the first attempt has either succeeded or failed. If it fails, then attempts made during between the start and end of that attempt will fail too.
type RecursionBlocker ¶ added in v2.4.7
type RecursionBlocker interface { InitDone() <-chan struct{} Proceed() bool Reset(context.Context, ip.Packet) error Discard(ip.Packet) bool }
RecursionBlocker is implemented by handlers that may experience recursive calls back into the TUN device for IP addresses that have not been forwarded by the cluster. This typically happens when running a cluster in a docker container on the local host and making attempts to connect to an existing IP on a port that no service is listening to.
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).
- Read from local connection fails (typically EOF)
- Write to local connection fails (connection peer closed)
- Idle timer timed out.
- Context is cancelled.
- closeSend request received from Tunnel peer.
- 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 NewServerStream ¶
func NewServerStream(ctx context.Context, grpcStream GRPCStream) (Stream, error)