relay

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2022 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package relay implements relay client/server interactive data structures, the Mesh Encrypted Routing Protocol.

Package relay is a generated GoMock package.

Index

Constants

View Source
const (
	MaxPacketSize = 64 << 10

	// ProtocolVersion is bumped whenever there's a wire-incompatible change.
	//   * version 1: received packets have src addrs in frameRecvPacket at beginning
	ProtocolVersion = 1
)

MaxPacketSize is the maximum size of a packet sent over MERP. (This only includes the data bytes visible to nicesock, not including its on-wire framing overhead)

Variables

This section is empty.

Functions

func NewClientTransporter

func NewClientTransporter(server protocol.RelayServer, credentials []byte, nodeDHKey noise.DHKey, srvPubKey security.DHPublic) *clientTransporterImpl

Types

type Client

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

Client represents the relay server client which is used to interactive with relay server.

func NewClient

func NewClient(transporter ClientTransporter) *Client

NewClient returns a new client instance.

func (*Client) Close

func (c *Client) Close() error

func (*Client) Handler

func (c *Client) Handler() ClientHandler

func (*Client) OnClosed

func (c *Client) OnClosed(cb func())

func (*Client) Send

func (c *Client) Send(typ message.PacketType, msg proto.Message) error

func (*Client) Serve

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

func (*Client) SetLatency

func (c *Client) SetLatency(lat time.Duration)

type ClientCallback

type ClientCallback func(s *Client, typ message.PacketType, msg proto.Message) error

type ClientHandler

type ClientHandler interface {
	On(typ message.PacketType, cb ClientCallback)
	Handle(s *Client, packet codec.RawPacket) error
}

func NewClientHandler

func NewClientHandler() ClientHandler

type ClientTransporter

type ClientTransporter interface {
	RelayServer() protocol.RelayServer
	State() ClientTransporterState
	SetState(s ClientTransporterState)
	Cipher() noise.Cipher
	SetCipher(cipher noise.Cipher)
	SetHeartbeatInterval(interval time.Duration)
	SetIsPrimary(is bool)
	HandshakeState() *noise.HandshakeState
	ReadQueue() <-chan codec.RawPacket
	WriteQueue() chan<- Packet
	Connect(ctx context.Context) error
	Read(ctx context.Context)
	Write(ctx context.Context)
	Close() error
}

ClientTransporter interface, together with clientTransporterImpl struct, is an abstraction of network IO so that the network behaviors could be mocked.

type ClientTransporterState

type ClientTransporterState byte
const (
	ClientTransporterStateInit       ClientTransporterState = 0
	ClientTransporterStateConnecting ClientTransporterState = 1
	ClientTransporterStateConnected  ClientTransporterState = 2
)

type Event

type Event struct {
	Type EventType
	Data interface{}
}

type EventClientClosed

type EventClientClosed struct {
	RelayServer protocol.RelayServer
	Client      *Client
}

type EventClientConnected

type EventClientConnected struct {
	RelayServer protocol.RelayServer
	Client      *Client
}

type EventType

type EventType byte
const (
	EventTypeClientClosed EventType = iota
	EventTypeClientConnected
)

type Manager

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

Manager maintains the relay clients and keep heartbeat with the relay server. When a peer cannot connect to the remote peer, the lowest latency relay server will be used to relay the traffic.

func NewManager

func NewManager(staticKey noise.DHKey, callback PacketCallback) *Manager

NewManager returns the relay manager

func (*Manager) AddServer

func (m *Manager) AddServer(relayServer protocol.RelayServer)

func (*Manager) Events

func (m *Manager) Events() <-chan Event

Events returns a channel which will track clients connected/closed event. Some events will be dropped if the channel is full.

func (*Manager) PrimaryRelayServerClient

func (m *Manager) PrimaryRelayServerClient() *Client

PrimaryRelayServerClient returns the primary relay server client of current node. A nil value will be returned if the primary relay server didn't connect.

func (*Manager) PrimaryServerID

func (m *Manager) PrimaryServerID() protocol.ServerID

PrimaryServerID returns the primary relay server id.

func (*Manager) RelayServerClient

func (m *Manager) RelayServerClient(id protocol.ServerID) *Client

RelayServerClient returns the relay server client corresponding server id. A nil value will be returned if the primary relay server didn't connect.

func (*Manager) SetCredential

func (m *Manager) SetCredential(credential []byte)

func (*Manager) SetPrimaryServerID

func (m *Manager) SetPrimaryServerID(id protocol.ServerID)

SetPrimaryServerID sets the primary relay server id.

func (*Manager) Stop

func (m *Manager) Stop()

Stop stops the relay manager and all the relay clients.

func (*Manager) Tick

func (m *Manager) Tick(ctx context.Context)

Tick ticks the relay client manager to flush the pending connecting relay servers.

func (*Manager) Update

func (m *Manager) Update(ctx context.Context, relayServers []protocol.RelayServer)

Update updates the relay clients maintained by the relay manager.

type MockSessionTransporter

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

MockSessionTransporter is a mock of SessionTransporter interface.

func NewMockSessionTransporter

func NewMockSessionTransporter(ctrl *gomock.Controller) *MockSessionTransporter

NewMockSessionTransporter creates a new mock instance.

func (*MockSessionTransporter) Cipher

func (m *MockSessionTransporter) Cipher() noise.Cipher

Cipher mocks base method.

func (*MockSessionTransporter) Close

func (m *MockSessionTransporter) Close() error

Close mocks base method.

func (*MockSessionTransporter) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockSessionTransporter) PublicKey

func (m *MockSessionTransporter) PublicKey() []byte

PublicKey mocks base method.

func (*MockSessionTransporter) Read

func (m *MockSessionTransporter) Read(ctx context.Context)

Read mocks base method.

func (*MockSessionTransporter) ReadQueue

func (m *MockSessionTransporter) ReadQueue() <-chan codec.RawPacket

ReadQueue mocks base method.

func (*MockSessionTransporter) SetCipher

func (m *MockSessionTransporter) SetCipher(cipher noise.Cipher)

SetCipher mocks base method.

func (*MockSessionTransporter) SetPublicKey

func (m *MockSessionTransporter) SetPublicKey(pk []byte)

SetPublicKey mocks base method.

func (*MockSessionTransporter) Write

func (m *MockSessionTransporter) Write(ctx context.Context)

Write mocks base method.

func (*MockSessionTransporter) WriteQueue

func (m *MockSessionTransporter) WriteQueue() chan<- Packet

WriteQueue mocks base method.

type MockSessionTransporterMockRecorder

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

MockSessionTransporterMockRecorder is the mock recorder for MockSessionTransporter.

func (*MockSessionTransporterMockRecorder) Cipher

Cipher indicates an expected call of Cipher.

func (*MockSessionTransporterMockRecorder) Close

Close indicates an expected call of Close.

func (*MockSessionTransporterMockRecorder) PublicKey

PublicKey indicates an expected call of PublicKey.

func (*MockSessionTransporterMockRecorder) Read

func (mr *MockSessionTransporterMockRecorder) Read(ctx interface{}) *gomock.Call

Read indicates an expected call of Read.

func (*MockSessionTransporterMockRecorder) ReadQueue

ReadQueue indicates an expected call of ReadQueue.

func (*MockSessionTransporterMockRecorder) SetCipher

func (mr *MockSessionTransporterMockRecorder) SetCipher(cipher interface{}) *gomock.Call

SetCipher indicates an expected call of SetCipher.

func (*MockSessionTransporterMockRecorder) SetPublicKey

func (mr *MockSessionTransporterMockRecorder) SetPublicKey(pk interface{}) *gomock.Call

SetPublicKey indicates an expected call of SetPublicKey.

func (*MockSessionTransporterMockRecorder) Write

func (mr *MockSessionTransporterMockRecorder) Write(ctx interface{}) *gomock.Call

Write indicates an expected call of Write.

func (*MockSessionTransporterMockRecorder) WriteQueue

WriteQueue indicates an expected call of WriteQueue.

type Packet

type Packet struct {
	Type    message.PacketType
	Message proto.Message
}

type PacketCallback

type PacketCallback interface {
	// OnForward handles the messages forwarded by the relay server
	OnForward(s *Client, typ message.PacketType, msg proto.Message) error
	OnSyncPeer(s *Client, typ message.PacketType, msg proto.Message) error
	OnProbeResponse(s *Client, typ message.PacketType, msg proto.Message) error
}

type PeerRouter

type PeerRouter interface {
	AddMERPPeerRoute(protocol.PeerID, string, *Client)
	RemoveMERPPeerRoute(protocol.PeerID, string, *Client)
}

type ReadResult

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

ReadResult is the type sent by runMERPClient to receiveIPv4 when a MERP packet is available.

type Server

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

Server represents the relay server, which is used to accept connection incoming from pairmesh relay clients. The server only play the Session manager role: create/remove Session.

func NewServer

func NewServer(addr string, heartbeatInterval time.Duration, dhKey noise.DHKey, publicKey *rsa.PublicKey) *Server

NewServer returns a new Server instance according to the serve vaddress and heartbeat interval.

func (*Server) Close

func (s *Server) Close() error

func (*Server) DHKey

func (s *Server) DHKey() noise.DHKey

DHKey implements the handler.SessionManager interface

func (*Server) ForeachSession

func (s *Server) ForeachSession(fn func(*Session))

func (*Server) Handler

func (s *Server) Handler() SessionHandler

func (*Server) HeartbeatInterval

func (s *Server) HeartbeatInterval() time.Duration

HeartbeatInterval implements the SessionManager interface.

func (*Server) OnSessionClosed

func (s *Server) OnSessionClosed(ses *Session)

OnSessionClosed implements the SessionLifetimeHook interface

func (*Server) OnSessionHandshake

func (s *Server) OnSessionHandshake(ses *Session)

OnSessionHandshake implements the SessionLifetimeHook interface

func (*Server) RSAPublicKey

func (s *Server) RSAPublicKey() *rsa.PublicKey

RSAPublicKey implements the handler.SessionManager interface

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve starts to serve the server process.

func (*Server) Session

func (s *Server) Session(peerID protocol.PeerID) *Session

Session implements the handler.SessionManager interface

func (*Server) SetRSAPublicKey

func (s *Server) SetRSAPublicKey(key *rsa.PublicKey)

type Session

type Session struct {
	SessionTransporter
	// contains filtered or unexported fields
}

Session maintains the connection Session between relay server/client.

func (*Session) Close

func (s *Session) Close() error

Close closes the current session.

func (*Session) IsPrimary

func (s *Session) IsPrimary() bool

func (*Session) LifetimeHook

func (s *Session) LifetimeHook() SessionLifetimeHook

func (*Session) PeerID

func (s *Session) PeerID() protocol.PeerID

PeerID returns the peer id of the current session.

func (*Session) Send

func (s *Session) Send(typ message.PacketType, msg proto.Message) error

Send sends message to the pairmesh client-side via the client session.

func (*Session) Serve

func (s *Session) Serve(ctx context.Context, wg *sync.WaitGroup)

func (*Session) SetHeartbeatAt

func (s *Session) SetHeartbeatAt(t time.Time)

func (*Session) SetIsPrimary

func (s *Session) SetIsPrimary(is bool)

func (*Session) SetPeerID

func (s *Session) SetPeerID(peerID protocol.PeerID)

SetPeerID sets the session peerID

func (*Session) SetState

func (s *Session) SetState(state SessionState)

SetState sets the session state.

func (*Session) SetSyncAt

func (s *Session) SetSyncAt(t time.Time)

func (*Session) SetUserID

func (s *Session) SetUserID(userID protocol.UserID)

SetUserID sets the session userID

func (*Session) SetVAddress

func (s *Session) SetVAddress(addr net.IP)

SetVAddress sets the virtual address allocated by portal service. The virtual address is coming from the credential which is encoded in handshake message and signed by portal service. So it cannot be counterfeit.

func (*Session) State

func (s *Session) State() SessionState

State returns the current session state.

func (*Session) String

func (s *Session) String() string

String implements the fmt.Stringer interface.

func (*Session) SyncAt

func (s *Session) SyncAt() time.Time

func (*Session) UserID

func (s *Session) UserID() protocol.UserID

UserID returns the user id of the current session.

func (*Session) VAddress

func (s *Session) VAddress() net.IP

VAddress returns the virtual address allocated by portal service.

type SessionCallback

type SessionCallback func(s *Session, typ message.PacketType, msg proto.Message) error

type SessionHandler

type SessionHandler interface {
	On(typ message.PacketType, cb SessionCallback)
	Handle(s *Session, packet codec.RawPacket) error
}

func NewSessionHandler

func NewSessionHandler(sm SessionManager) SessionHandler

type SessionLifetimeHook

type SessionLifetimeHook interface {
	OnSessionHandshake(ses *Session)
	OnSessionClosed(ses *Session)
}

type SessionManager

type SessionManager interface {
	HeartbeatInterval() time.Duration
	DHKey() noise.DHKey
	RSAPublicKey() *rsa.PublicKey
	Session(peerID protocol.PeerID) *Session
}

type SessionState

type SessionState byte
const (
	SessionStateInit    SessionState = 0
	SessionStateRunning SessionState = 1
)

type SessionTransporter

type SessionTransporter interface {
	Cipher() noise.Cipher
	SetCipher(cipher noise.Cipher)
	ReadQueue() <-chan codec.RawPacket
	WriteQueue() chan<- Packet
	Read(ctx context.Context)
	Write(ctx context.Context)
	Close() error
}

SessionTransporter interface, together with sessionTransporterImpl struct, is an abstraction of network IO so that the network behaviors could be mocked, and therefore the session and relay server could be better tested.

type WriteRequest

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

Jump to

Keyboard shortcuts

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