uniserver

package
v0.0.0-...-05bc493 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NextPeerUID

func NextPeerUID() uint64

func RetryStartListenForTests

func RetryStartListenForTests(p *UnifiedServer, retryCount int)

Types

type AllTransportProvider

type AllTransportProvider interface {
	CreateSessionlessProvider(binding nwapi.Address, preference nwapi.Preference, maxUDPSize uint16) l1.SessionlessTransportProvider
	CreateSessionfulProvider(binding nwapi.Address, preference nwapi.Preference, tlsCfg *tls.Config) l1.SessionfulTransportProvider
}

func MapTransportProvider

func MapTransportProvider(
	provider AllTransportProvider,
	mappingLessFunc FuncMapSessionlessProvider,
	mappingFullFunc FuncMapSessionfullProvider,
) AllTransportProvider

type BlacklistManager

type BlacklistManager interface {
	IsBlacklisted(a nwapi.Address) bool
	ReportFraud(nwapi.Address, *PeerManager, error) bool
}

type ConnErrDetails

type ConnErrDetails struct {
	Local, Remote nwapi.Address
}

type DefaultTransportProvider

type DefaultTransportProvider struct{}

func (*DefaultTransportProvider) CreateSessionfulProvider

func (p *DefaultTransportProvider) CreateSessionfulProvider(binding nwapi.Address, preference nwapi.Preference, tlsCfg *tls.Config) l1.SessionfulTransportProvider

func (*DefaultTransportProvider) CreateSessionlessProvider

func (p *DefaultTransportProvider) CreateSessionlessProvider(binding nwapi.Address, preference nwapi.Preference, maxUDPSize uint16) l1.SessionlessTransportProvider

type Dispatcher

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

Dispatcher provides registration and management of protocols. Set of protocols can't be changed after Seal. But individual protocols can be enabled or disabled at any time with GetMode / SetMode functions.

func (*Dispatcher) GetDescriptors

func (p *Dispatcher) GetDescriptors() uniproto.Descriptors

func (*Dispatcher) GetMode

func (p *Dispatcher) GetMode() uniproto.ConnectionMode

func (*Dispatcher) GetReceiver

func (p *Dispatcher) GetReceiver(pt uniproto.ProtocolType) uniproto.Receiver

func (*Dispatcher) IsSealed

func (p *Dispatcher) IsSealed() bool

IsSealed returns true when set of protocols was sealed and can't be changed.

func (*Dispatcher) IsStarted

func (p *Dispatcher) IsStarted() bool

func (*Dispatcher) NextPulse

func (p *Dispatcher) NextPulse(pr pulse.Range)

func (*Dispatcher) RegisterProtocol

func (p *Dispatcher) RegisterProtocol(pt uniproto.ProtocolType, desc uniproto.Descriptor, ctl uniproto.Controller, rcv uniproto.Receiver)

func (*Dispatcher) RegisterProtocolByFunc

func (p *Dispatcher) RegisterProtocolByFunc(fn uniproto.RegisterProtocolFunc)

func (*Dispatcher) Seal

func (p *Dispatcher) Seal() uniproto.Descriptors

func (*Dispatcher) SetMode

func (p *Dispatcher) SetMode(mode uniproto.ConnectionMode)

SetMode changes a set of available protocols. When Dispatcher was started, changing a set of allowed protocols will result in starting/stopping of relevant protocols.

func (*Dispatcher) Start

func (p *Dispatcher) Start(manager uniproto.PeerManager)

func (*Dispatcher) Stop

func (p *Dispatcher) Stop()

type HTTPReceiver

type HTTPReceiver struct {
	PeerManager *PeerManager
	Relayer     Relayer
	Parser      uniproto.Parser
	VerifyFn    uniproto.VerifyHeaderFunc
	Format      TransportStreamFormat
}

type HTTPReceiverFunc

type HTTPReceiverFunc = func(*http.Request, *bufio.Writer, *HTTPReceiver) error

type MappedTransportProvider

type MappedTransportProvider struct {
	AllTransportProvider
	// contains filtered or unexported fields
}

func (*MappedTransportProvider) CreateSessionfulProvider

func (p *MappedTransportProvider) CreateSessionfulProvider(binding nwapi.Address, preference nwapi.Preference, tlsCfg *tls.Config) l1.SessionfulTransportProvider

func (*MappedTransportProvider) CreateSessionlessProvider

func (p *MappedTransportProvider) CreateSessionlessProvider(binding nwapi.Address, preference nwapi.Preference, maxUDPSize uint16) l1.SessionlessTransportProvider

type MiniLogger

type MiniLogger interface {
	LogError(error)
	LogTrace(interface{})
}

type PacketErrDetails

type PacketErrDetails struct {
	Header uniproto.Header
	Pulse  pulse.Number
}

type Peer

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

Peer represents a logical peer for communications. One Peer may have multiple addresses and identities. Initially, a Peer is created by connection's remote address, but a Peer can be merged with another Peer after identification. Also, Peer can have multiple connections of different types.

func (*Peer) GetDataEncrypter

func (p *Peer) GetDataEncrypter() (d cryptkit.Encrypter, err error)

GetDataVerifier returns Encrypter to encrypt data for this peer. Will return error when SignatureKey is not set.

func (*Peer) GetDataVerifier

func (p *Peer) GetDataVerifier() (cryptkit.DataSignatureVerifier, error)

GetDataVerifier returns DataSignatureVerifier to check data provided by this peer. Will return error when SignatureKey is not set.

func (*Peer) GetLocalUID

func (p *Peer) GetLocalUID() nwapi.Address

GetLocalUID returns a locally unique peer's address. This address can only be used locally and it will change after each disconnection of the peer.

func (*Peer) GetNodeID

func (p *Peer) GetNodeID() nwapi.ShortNodeID

GetNodeID returns ShortNodeID of the peer. Result can be zero. ShortNodeID is related to consensus operations.

func (*Peer) GetOrCreateProtoInfo

func (p *Peer) GetOrCreateProtoInfo(pt uniproto.ProtocolType, factoryFn func(uniproto.Peer) io.Closer) io.Closer

GetOrCreateProtoInfo performs "atomic" get or create of peer's projection for the given protocol.

func (*Peer) GetPrimary

func (p *Peer) GetPrimary() nwapi.Address

GetPrimary returns a peer's primary identity

func (*Peer) GetProtoInfo

func (p *Peer) GetProtoInfo(pt uniproto.ProtocolType) io.Closer

GetProtoInfo returns peer's projection for the given protocol. Result can be nil.

func (*Peer) GetSignatureKey

func (p *Peer) GetSignatureKey() cryptkit.SigningKey

GetSignatureKey returns peer's signature key. Key can be zero.

func (*Peer) HasState

func (p *Peer) HasState(state PeerState) bool

HasState returns true when the peer has same or higher state.

func (*Peer) SendPacket

func (p *Peer) SendPacket(tp uniproto.OutType, packet uniproto.PacketPreparer) error

SendPacket is a convenience handler to send a packet provided by uniproto.PacketPreparer. See SendPreparedPacket for details.

func (*Peer) SendPreparedPacket

func (p *Peer) SendPreparedPacket(tp uniproto.OutType, packet *uniproto.Packet, dataSize uint, fn uniproto.PayloadSerializerFunc, checkFn func() bool) error

SendPreparedPacket prepares uniproto packet and checks its eligibility for the given (uniproto.OutType). Can also choose a specific out type by properties of packet and value of (uniproto.OutType). See SendingPacket.NewTransportFunc for details about serialization.

func (*Peer) SetNodeID

func (p *Peer) SetNodeID(id nwapi.ShortNodeID)

SetNodeID sets peer's ShortNodeID. Will panic on zero.

func (*Peer) SetProtoInfo

func (p *Peer) SetProtoInfo(pt uniproto.ProtocolType, info io.Closer)

SetProtoInfo sets/updated per-protocol peer's projection (info). It will be closed when the peer is unregistered.

func (*Peer) SetSignatureKey

func (p *Peer) SetSignatureKey(pk cryptkit.SigningKey)

SetSignatureKey sets/updates peer's signature key. Key can be zero.

func (*Peer) Transport

func (p *Peer) Transport() uniproto.OutTransport

Transport returns a multiplexed transport over available peer's transports. This transport does internal connection management (retry, reconnect, etc).

func (*Peer) UpgradeState

func (p *Peer) UpgradeState(state PeerState)

UpgradeState rises state of this peer. Will ignore lower values.

type PeerCryptographyFactory

type PeerCryptographyFactory interface {
	// TODO for some reason linter can't handle multiple declarations of the same method while it is valid for 1.14
	// cryptkit.DataSignatureVerifierFactory
	// cryptkit.DataSignerFactory
	CreateDataSignatureVerifier(cryptkit.SigningKey) cryptkit.DataSignatureVerifier
	CreateDataSigner(cryptkit.SigningKey) cryptkit.DataSigner
	IsSignatureKeySupported(cryptkit.SigningKey) bool
	CreateDataDecrypter(cryptkit.SigningKey) cryptkit.Decrypter
	CreateDataEncrypter(cryptkit.SigningKey) cryptkit.Encrypter
	GetMaxSignatureSize() int
}

type PeerManager

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

PeerManager runs a map of peers. See PeerMap

func NewPeerManager

func NewPeerManager(factory PeerTransportFactory, local nwapi.Address, localFn func(*Peer)) *PeerManager

func (*PeerManager) AddHostID

func (p *PeerManager) AddHostID(to nwapi.Address, id nwapi.HostID) (bool, error)

AddHostID adds an alias with protocol specific HostID. When HostID is NodeID then will also call SetNodeID

func (*PeerManager) Close

func (p *PeerManager) Close() error

Close removes all peers with relevant finalization of them.

func (*PeerManager) GetLocalDataDecrypter

func (p *PeerManager) GetLocalDataDecrypter() (cryptkit.Decrypter, error)

GetLocalDataDecrypter returns Decrypter for the local peer.

func (*PeerManager) GetPrimary

func (p *PeerManager) GetPrimary(a nwapi.Address) nwapi.Address

GetPrimary returns primary address of a peer registered with the given address or alias. Will return zero value when not found.

func (*PeerManager) HasAddress

func (p *PeerManager) HasAddress(a nwapi.Address) bool

HasAddress return true when there is a peer registered with the given address or alias.

func (*PeerManager) Local

func (p *PeerManager) Local() *Peer

Local returns a local peer - peer that represents a local node.

func (*PeerManager) Manager

func (p *PeerManager) Manager() uniproto.PeerManager

func (*PeerManager) MustAddAliases

func (p *PeerManager) MustAddAliases(to nwapi.Address, aliases ...nwapi.Address)

MustAddAliases adds aliases for a peer identified by (to). Panics on an unknown peer or on collisions.

func (*PeerManager) MustAddPeer

func (p *PeerManager) MustAddPeer(primary nwapi.Address, aliases ...nwapi.Address)

MustAddPeer creates a peer for the given primary address and aliases. Panics on collisions.

func (*PeerManager) RemoveAlias

func (p *PeerManager) RemoveAlias(a nwapi.Address) bool

RemoveAlias removes/unregisters the given alias. Will return false when the alias is unknown or can't be unregistered (is primary).

func (*PeerManager) RemovePeer

func (p *PeerManager) RemovePeer(a nwapi.Address) bool

RemovePeer removes/unregisters a peer with the given address or alias.

func (*PeerManager) SetIdentityClassifier

func (p *PeerManager) SetIdentityClassifier(idWithPortFn func(nwapi.Address) bool)

SetIdentityClassifier can only be used for initialization

func (*PeerManager) SetPeerConnectionLimit

func (p *PeerManager) SetPeerConnectionLimit(n uint8)

SetPeerConnectionLimit sets a max number of sessionful connections established to/from peer.

func (*PeerManager) SetPeerMapper

func (p *PeerManager) SetPeerMapper(fn PeerMapperFunc)

func (*PeerManager) SetQuotaFactory

func (p *PeerManager) SetQuotaFactory(quotaFn PeerQuotaFactoryFunc)

SetQuotaFactory sets factory to allocate traffic quota when a peer is added.

func (*PeerManager) SetSignatureFactory

func (p *PeerManager) SetSignatureFactory(f PeerCryptographyFactory)

SetSignatureFactory sets a provider of cryptographic tools. Changing this factory will not unset already created cryptographic tools.

type PeerMap

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

type PeerMapperFunc

type PeerMapperFunc = func(*Peer) (remapTo nwapi.Address, err error)

PeerMapperFunc is invoked after a new peer is registered to check if this peek has to be merged with another one.

type PeerQuotaFactoryFunc

type PeerQuotaFactoryFunc = func([]nwapi.Address) ratelimiter.RWRateQuota

PeerQuotaFactoryFunc is invoked to create a transfer rate quota for a peer. Quota applies to all connections of the peer. Can return nil.

type PeerReceiver

type PeerReceiver struct {
	Ctx         context.Context
	PeerManager *PeerManager
	Relayer     Relayer
	HTTP        HTTPReceiverFunc
	Parser      uniproto.Parser
}

func (PeerReceiver) ReceiveDatagram

func (p PeerReceiver) ReceiveDatagram(remote nwapi.Address, b []byte) (err error)

func (PeerReceiver) ReceiveStream

func (p PeerReceiver) ReceiveStream(remote nwapi.Address, conn io.ReadWriteCloser, wOut l1.OneWayTransport) (runFn func() error, err error)

type PeerState

type PeerState uint8
const (

	// Established
	Connected PeerState
	Verified
	Trusted
)

type PeerTransport

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

func (*PeerTransport) CanUseSessionless

func (p *PeerTransport) CanUseSessionless(size int64) bool

func (*PeerTransport) EnsureConnect

func (p *PeerTransport) EnsureConnect() error

func (*PeerTransport) UseSessionful

func (p *PeerTransport) UseSessionful(size int64, applyFn uniproto.OutFunc) error

func (*PeerTransport) UseSessionless

func (p *PeerTransport) UseSessionless(applyFn uniproto.OutFunc) error

type PeerTransportCentral

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

type PeerTransportFactory

type PeerTransportFactory interface {
	// LOCK: WARNING! This method is called under PeerTransport.mutex
	SessionlessConnectTo(to nwapi.Address) (l1.OneWayTransport, error)
	// LOCK: WARNING! This method is called under PeerTransport.mutex
	SessionfulConnectTo(to nwapi.Address) (l1.OneWayTransport, error)
	IsActive() bool
	MaxSessionlessSize() uint16
	IsSessionlessAllowed(int) bool
}

type ReceiveBuffer

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

func NewReceiveBuffer

func NewReceiveBuffer(regularLimit, priorityLimit, largeLimit int, dispatcher uniproto.Dispatcher) ReceiveBuffer

func (ReceiveBuffer) Close

func (p ReceiveBuffer) Close()

func (ReceiveBuffer) GetMode

func (ReceiveBuffer) GetReceiver

func (ReceiveBuffer) NextPulse

func (p ReceiveBuffer) NextPulse(pr pulse.Range)

func (ReceiveBuffer) ReceiveLargePacket

func (p ReceiveBuffer) ReceiveLargePacket(rp *uniproto.ReceivedPacket, preRead []byte, r io.LimitedReader) error

func (ReceiveBuffer) ReceiveSmallPacket

func (p ReceiveBuffer) ReceiveSmallPacket(rp *uniproto.ReceivedPacket, b []byte)

func (*ReceiveBuffer) RunWorkers

func (p *ReceiveBuffer) RunWorkers(count int, priorityOnly bool)

For initialization only

func (ReceiveBuffer) Seal

func (*ReceiveBuffer) SetDiscardHandler

func (p *ReceiveBuffer) SetDiscardHandler(fn func(nwapi.Address, uniproto.ProtocolType))

For initialization only

func (*ReceiveBuffer) SetLargePacketQueueTimeout

func (p *ReceiveBuffer) SetLargePacketQueueTimeout(d time.Duration)

For initialization only

func (*ReceiveBuffer) SetOutOfBand

func (p *ReceiveBuffer) SetOutOfBand(pt uniproto.ProtocolType, val bool)

For initialization only

func (*ReceiveBuffer) SetOutOfBandProtocols

func (p *ReceiveBuffer) SetOutOfBandProtocols(oob uniproto.ProtocolSet)

For initialization only

func (*ReceiveBuffer) SetPriority

func (p *ReceiveBuffer) SetPriority(pt uniproto.ProtocolType, pk uint8, val bool)

For initialization only

func (*ReceiveBuffer) SetPriorityByProtocol

func (p *ReceiveBuffer) SetPriorityByProtocol(pt uniproto.ProtocolType, val bool)

For initialization only

func (*ReceiveBuffer) SetPriorityPackets

func (p *ReceiveBuffer) SetPriorityPackets(pp [uniproto.ProtocolTypeCount]uniproto.PacketSet)

For initialization only

func (ReceiveBuffer) Start

func (p ReceiveBuffer) Start(manager uniproto.PeerManager)

func (ReceiveBuffer) Stop

func (p ReceiveBuffer) Stop()

type Relayer

type Relayer interface {
	// RelaySessionlessPacket should relay the given sessionless packet.
	RelaySessionlessPacket(*PeerManager, *uniproto.Packet, []byte) error
	// RelaySmallPacket should relay the given small packet.
	RelaySmallPacket(*PeerManager, *uniproto.Packet, []byte) error
	// RelayLargePacket should relay the given large packet.
	RelayLargePacket(*PeerManager, *uniproto.Packet, []byte, io.LimitedReader) error
}

Relayer is a helper to handle packets which were requested and authorized for relay to another peer

type ServerConfig

type ServerConfig struct {
	BindingAddress string
	PublicAddress  string
	NetPreference  nwapi.Preference
	TLSConfig      *tls.Config
	// UDPMaxSize sets max size for UDP packets. Zero will disable UDP.
	UDPMaxSize     int
	UDPParallelism int
	PeerLimit      int

	// For sessionful connections only
	RetryLimit         uint8 // +1 attempt
	RetryDelayInc      time.Duration
	RetryDelayVariance time.Duration
	RetryDelayMax      time.Duration
}

type Stats

type Stats struct {
	ReceivedSessionless TransportStats

	ReceivedSessionfulSmallPackets TransportStats
	ReceivedSessionfulLargePackets TransportStats
}

TODO

type TransportStats

type TransportStats struct {
	Count      atomickit.Uint64
	Size       atomickit.Uint64
	ConnectIn  atomickit.Uint32
	ConnectOut atomickit.Uint32
	ForcedOut  atomickit.Uint32
}

type TransportStreamFormat

type TransportStreamFormat uint8
const (
	DetectByFirstPacket TransportStreamFormat // considered as Unrestricted
	BinaryLimitedLength
	BinaryUnlimitedLength
	HTTPLimitedLength
	HTTPUnlimitedLength
)

func (TransportStreamFormat) IsBinary

func (v TransportStreamFormat) IsBinary() bool

func (TransportStreamFormat) IsDefined

func (v TransportStreamFormat) IsDefined() bool

func (TransportStreamFormat) IsDefinedLimited

func (v TransportStreamFormat) IsDefinedLimited() bool

func (TransportStreamFormat) IsHTTP

func (v TransportStreamFormat) IsHTTP() bool

func (TransportStreamFormat) IsUnlimited

func (v TransportStreamFormat) IsUnlimited() bool

type UnifiedServer

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

func NewUnifiedServer

func NewUnifiedServer(dispatcher uniproto.Dispatcher, logger MiniLogger) *UnifiedServer

func (*UnifiedServer) PeerManager

func (p *UnifiedServer) PeerManager() *PeerManager

func (*UnifiedServer) SetBlacklistManager

func (p *UnifiedServer) SetBlacklistManager(blacklist BlacklistManager)

func (*UnifiedServer) SetConfig

func (p *UnifiedServer) SetConfig(config ServerConfig)

func (*UnifiedServer) SetHTTPReceiver

func (p *UnifiedServer) SetHTTPReceiver(fn HTTPReceiverFunc)

func (*UnifiedServer) SetIdentityClassifier

func (p *UnifiedServer) SetIdentityClassifier(idWithPortFn func(nwapi.Address) bool)

SetIdentityClassifier can only be used for initialization

func (*UnifiedServer) SetPeerFactory

func (p *UnifiedServer) SetPeerFactory(fn PeerMapperFunc)

func (*UnifiedServer) SetQuotaFactory

func (p *UnifiedServer) SetQuotaFactory(quotaFn PeerQuotaFactoryFunc)

func (*UnifiedServer) SetRelayer

func (p *UnifiedServer) SetRelayer(r Relayer)

func (*UnifiedServer) SetSignatureFactory

func (p *UnifiedServer) SetSignatureFactory(f PeerCryptographyFactory)

func (*UnifiedServer) SetTransportProvider

func (p *UnifiedServer) SetTransportProvider(provider AllTransportProvider)

func (*UnifiedServer) StartListen

func (p *UnifiedServer) StartListen()

func (*UnifiedServer) StartNoListen

func (p *UnifiedServer) StartNoListen()

func (*UnifiedServer) Stop

func (p *UnifiedServer) Stop()

func (*UnifiedServer) TryStartListen

func (p *UnifiedServer) TryStartListen() error

Jump to

Keyboard shortcuts

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