tunnel

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: Apache-2.0 Imports: 19 Imported by: 16

Documentation

Overview

Package tunnel defines the a TCP over gRPC transport client and server.

Index

Constants

This section is empty.

Variables

View Source
var (
	// RetryBaseDelay is the initial retry interval for re-connecting tunnel server/client.
	RetryBaseDelay = time.Second
	// RetryMaxDelay caps the retry interval for re-connecting attempts.
	RetryMaxDelay = time.Minute
	// RetryRandomization is the randomization factor applied to the retry
	// interval.
	RetryRandomization = 0.5
)

Functions

func DialTLSCredsOpts

func DialTLSCredsOpts(certFile string) ([]grpc.DialOption, error)

DialTLSCredsOpts returns a slice of DialOption with TLS credential obtained from certFile.

func DialmTLSCredsOpts

func DialmTLSCredsOpts(certFile, keyFile, caFile string) ([]grpc.DialOption, error)

DialmTLSCredsOpts returns a slice of DialOption with mTLS credential obtained from certFile, keyFile and caFile.

func Listen

func Listen(ctx context.Context, addr string, cert string, targets map[Target]struct{}) (net.Listener, error)

Listen create a tunnel client and returns a Listener.

func ServerTLSCredsOpts

func ServerTLSCredsOpts(certFile, keyFile string) ([]grpc.ServerOption, error)

ServerTLSCredsOpts returns a slice of ServerOption with TLS credential obtained from certFile and keyFile.

func ServermTLSCredsOpts

func ServermTLSCredsOpts(certFile, keyFile, caFile string) ([]grpc.ServerOption, error)

ServermTLSCredsOpts returns a slice of ServerOption with mTLS credential obtained from certFile, keyFile and caFile.

Types

type Client

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

Client implementation of an endpoint.

func NewClient

func NewClient(tc tpb.TunnelClient, cc ClientConfig, ts map[Target]struct{}) (*Client, error)

NewClient creates a new tunnel client.

func (*Client) DeleteTarget

func (c *Client) DeleteTarget(target Target) error

DeleteTarget sends a target deletion registration via regStream.

func (*Client) Error

func (c *Client) Error() error

Error returns the error collected from streamHandler.

func (*Client) NewSession

func (c *Client) NewSession(target Target) (_ io.ReadWriteCloser, err error)

NewSession requests a new stream identified on the server side by target.

func (*Client) NewTarget

func (c *Client) NewTarget(target Target) error

NewTarget sends a target addition registration via regStream.

func (*Client) PeerTargets

func (c *Client) PeerTargets(typ string) map[Target]struct{}

PeerTargets returns all the peer targets matching the given type. If type is an empty string, it will return all targets.

func (*Client) Register

func (c *Client) Register(ctx context.Context) (err error)

Register initializes the client register stream and determines the capabilities of the tunnel server.

func (*Client) Run

func (c *Client) Run(ctx context.Context) error

Run initializes the client register stream and determines the capabilities of the tunnel server. Once done, it starts the stream handler responsible for determining what to do with received requests.

func (*Client) Start

func (c *Client) Start(ctx context.Context)

Start handles received register stream requests.

func (*Client) Subscribe

func (c *Client) Subscribe(typ string) error

Subscribe send a subscription request to the server for targets updates.

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(typ string) error

Unsubscribe send a unsubscription request to the server for stopping receiving targets updates.

type ClientConfig

type ClientConfig struct {
	RegisterHandler ClientRegHandlerFunc
	Handler         ClientHandlerFunc
	PeerAddHandler  ClientPeerAddHandlerFunc
	PeerDelHandler  ClientPeerDelHandlerFunc
	Opts            []grpc.CallOption
	Subscriptions   []string
}

ClientConfig contains the config for the client.

type ClientHandlerFunc

type ClientHandlerFunc func(target Target, rwc io.ReadWriteCloser) error

ClientHandlerFunc handles sessions the client receives from the server.

type ClientPeerAddHandlerFunc

type ClientPeerAddHandlerFunc func(target Target) error

ClientPeerAddHandlerFunc is called when a peer target is registered.

type ClientPeerDelHandlerFunc

type ClientPeerDelHandlerFunc func(target Target) error

ClientPeerDelHandlerFunc is called when a peer target is deleted.

type ClientRegHandlerFunc

type ClientRegHandlerFunc func(target Target) error

ClientRegHandlerFunc defines the targets that the handler function can accept.

type Conn

type Conn struct {
	io.ReadWriteCloser
}

Conn is a wraper as a net.Conn interface.

func ClientConn

func ClientConn(ctx context.Context, tc *Client, target *Target) (*Conn, error)

ClientConn returns a tunnel connection.

func ServerConn

func ServerConn(ctx context.Context, ts *Server, target *Target) (*Conn, error)

ServerConn returns a tunnel connection.

func (*Conn) LocalAddr

func (tc *Conn) LocalAddr() net.Addr

LocalAddr is trivial implementation, in order to match interface net.Conn.

func (*Conn) RemoteAddr

func (tc *Conn) RemoteAddr() net.Addr

RemoteAddr is trivial implementation, in order to match interface net.Conn.

func (*Conn) SetDeadline

func (tc *Conn) SetDeadline(t time.Time) error

SetDeadline is trivial implementation, in order to match interface net.Conn.

func (*Conn) SetReadDeadline

func (tc *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline is trivial implementation, in order to match interface net.Conn.

func (*Conn) SetWriteDeadline

func (tc *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline is trivial implementation, in order to match interface net.Conn.

type Listener

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

Listener wraps a tunnel connection.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept waits and returns a tunnel connection.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr is a trivial implementation.

func (*Listener) Close

func (l *Listener) Close() error

Close close the embedded connection. Will need more implementation to handle multiple connections.

type Server

type Server struct {
	tpb.UnimplementedTunnelServer
	// contains filtered or unexported fields
}

Server is the server implementation of an endpoint.

func NewServer

func NewServer(sc ServerConfig) (*Server, error)

NewServer creates a new tunnel server.

func (*Server) ErrorChan

func (s *Server) ErrorChan() <-chan error

ErrorChan returns a channel that sends errors from the operations such as Register.

func (*Server) NewSession

func (s *Server) NewSession(ctx context.Context, ss ServerSession) (io.ReadWriteCloser, error)

NewSession requests a new stream identified on the client side by uniqueID.

func (*Server) Register

func (s *Server) Register(stream tpb.Tunnel_RegisterServer) error

Register handles the gRPC register stream(s). The receive direction calls the client-installed handle function. The send direction is used by NewSession to get new tunnel sessions.

func (*Server) Tunnel

func (s *Server) Tunnel(stream tpb.Tunnel_TunnelServer) error

Tunnel accepts tunnel connections from the client. When it accepts a stream, it checks for the stream in the connections map. If it's there, it forwards the stream over the IOStream channel.

type ServerAddTargHandlerFunc

type ServerAddTargHandlerFunc func(t Target) error

ServerAddTargHandlerFunc is called for each target registered by a client. It will be called for target additions.

type ServerConfig

type ServerConfig struct {
	AddTargetHandler    ServerAddTargHandlerFunc
	DeleteTargetHandler ServerDeleteTargHandlerFunc
	RegisterHandler     ServerRegHandlerFunc
	Handler             ServerHandlerFunc
	LocalTargets        []Target
}

ServerConfig contains the config for the server.

type ServerDeleteTargHandlerFunc

type ServerDeleteTargHandlerFunc func(t Target) error

ServerDeleteTargHandlerFunc is called for each target registered by a client. It will be called for target additions.

type ServerHandlerFunc

type ServerHandlerFunc func(ss ServerSession, rwc io.ReadWriteCloser) error

ServerHandlerFunc handles sessions the server receives from the client.

type ServerRegHandlerFunc

type ServerRegHandlerFunc func(ss ServerSession) error

ServerRegHandlerFunc defines the targets that the handler function can accept. It is only called when the server accepts new session from the client.

type ServerSession

type ServerSession struct {
	Addr   net.Addr
	Target Target
}

ServerSession is used by NewSession and the register handler. In the register handler it is used by the client to indicate what it's trying to connect to. In NewSession, ServerSession will indicate what target to connect to, as well as potentially specifying the client to connect to.

type Target

type Target struct {
	ID   string
	Type string
}

Target consists id and type, used to represent a tunnel target.

Jump to

Keyboard shortcuts

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