Documentation ¶
Index ¶
- Constants
- func DialWaitLoop(ctx context.Context, manager rpc.ManagerClient, ...) error
- func GetSession(m Message) string
- func IsTimeout(err error) bool
- func NewPipe(id ConnID, sessionID string) (Stream, Stream)
- func ReadLoop(ctx context.Context, s Stream) (<-chan Message, <-chan error)
- func UdpReader(ctx context.Context, conn net.PacketConn, ch chan<- UdpReadResult)
- func WithPool(ctx context.Context, pool *Pool) context.Context
- func WriteLoop(ctx context.Context, s Stream, msgCh <-chan Message, wg *sync.WaitGroup)
- 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) SpanRecord(span trace.Span)
- func (id ConnID) String() string
- type Endpoint
- func NewBidiPipe(a, b Stream, name string, counter *int32) Endpoint
- func NewConnEndpoint(stream Stream, conn net.Conn, cancel context.CancelFunc) Endpoint
- func NewConnEndpointTTL(stream Stream, conn net.Conn, cancel context.CancelFunc, ttl time.Duration) Endpoint
- func NewDialer(stream Stream, cancel context.CancelFunc) Endpoint
- func NewDialerTTL(stream Stream, cancel context.CancelFunc, ttl time.Duration) Endpoint
- func NewUDPListener(conn *net.UDPConn, targetAddr *net.UDPAddr, ...) Endpoint
- type GRPCStream
- type GRPClientCStream
- type Handler
- type HandlerCreator
- type Message
- type MessageCode
- type Pool
- type Stream
- type StreamCreator
- type TimedHandler
- type UdpReadResult
Constants ¶
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 )
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 IsTimeout ¶ added in v2.6.8
IsTimeout returns true if the given error is a network timeout error.
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.
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.
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 (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.
func (ConnID) SpanRecord ¶ added in v2.7.2
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 NewConnEndpoint ¶
func NewConnEndpointTTL ¶ added in v2.8.0
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
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.
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.
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)
type StreamCreator ¶ added in v2.7.2
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)