p2p

package
v0.0.0-...-c97221a Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: GPL-3.0 Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TopicKeepAlive is keep alive endpoint.
	TopicKeepAlive = "p2p-keepalive"

	// TopicSessionCreate is a session create endpoint for p2p communication.
	TopicSessionCreate = "p2p-session-create"
	// TopicSessionAcknowledge is a session acknowledge endpoint for p2p communication.
	TopicSessionAcknowledge = "p2p-session-acknowledge"
	// TopicSessionStatus is a session status notification for p2p communication.
	TopicSessionStatus = "p2p-session-connectivity-status"
	// TopicSessionDestroy is a session destroy endpoint for p2p communication.
	TopicSessionDestroy = "p2p-session-destroy"

	// TopicPaymentMessage is a payment messages endpoint for p2p communication.
	TopicPaymentMessage = "p2p-payment-message"
	// TopicPaymentInvoice is a payment invoices endpoint for p2p communication.
	TopicPaymentInvoice = "p2p-payment-invoice"
)
View Source
const AppTopicSTUN = "STUN detection"

AppTopicSTUN represents the STUN detection topic.

View Source
const (
	// ContactTypeV1 is p2p contact type.
	ContactTypeV1 = "nats/p2p/v1"
)

Variables

View Source
var (

	// ErrSendTimeout indicates send timeout error.
	ErrSendTimeout = errors.New("p2p send timeout")

	// ErrHandlerNotFound indicates that peer is not registered handler yet.
	ErrHandlerNotFound = errors.New("p2p peer handler not found")
)
View Source
var ErrContactNotFound = errors.New("p2p contact not found")

ErrContactNotFound represents that no p2p contact is found.

Functions

func GenerateKey

func GenerateKey() (PublicKey, PrivateKey, error)

GenerateKey generates p2p public and private key pairs.

func RegisterContactUnserializer

func RegisterContactUnserializer()

RegisterContactUnserializer registers global proposal contact unserializer.

Types

type Channel

type Channel interface {
	ChannelSender
	ChannelHandler

	// Tracer returns tracer which tracks channel establishment
	Tracer() *trace.Tracer

	// ServiceConn returns UDP connection which can be used for services.
	ServiceConn() *net.UDPConn

	// Conn returns underlying channel's UDP connection.
	Conn() *net.UDPConn

	// Close closes p2p communication channel.
	Close() error

	// Unique ID
	ID() string
}

Channel represents p2p communication channel which can send and receive messages over encrypted and reliable UDP transport.

type ChannelHandler

type ChannelHandler interface {
	// Handle registers handler for given topic which handles peer request.
	Handle(topic string, handler HandlerFunc)
}

ChannelHandler is used to handle messages.

type ChannelSender

type ChannelSender interface {
	// Send sends message to given topic. Peer listening to topic will receive message.
	Send(ctx context.Context, topic string, msg *Message) (*Message, error)
}

ChannelSender is used to send messages.

type ContactDefinition

type ContactDefinition struct {
	BrokerAddresses []string `json:"broker_addresses"`
}

ContactDefinition represents p2p contact which contains NATS broker addresses for connection.

func ParseContact

func ParseContact(contacts market.ContactList) (ContactDefinition, error)

ParseContact tries to parse p2p contact from given contacts list.

type Context

type Context interface {
	// Request returns message with data bytes.
	Request() *Message

	// Error allows to return error which will be seen for peer.
	Error(err error) error

	// OkWithReply indicates that request was handled successfully and returns reply with given message.
	OkWithReply(msg *Message) error

	// Ok indicates that request was handled successfully
	OK() error

	// PeerID returns peer identity used to authenticate P2P channel
	PeerID() identity.Identity
}

Context represents request context.

type Dialer

type Dialer interface {
	// Dial exchanges p2p configuration via broker, performs NAT pinging if needed
	// and create p2p channel which is ready for communication.
	Dial(ctx context.Context, consumerID, providerID identity.Identity, serviceType string, contactDef ContactDefinition, tracer *trace.Tracer) (Channel, error)
}

Dialer knows how to exchange p2p keys and encrypted configuration and creates ready to use p2p channels.

func NewDialer

func NewDialer(broker brokerConnector, signer identity.SignerFactory, verifierFactory identity.VerifierFactory, ipResolver ip.Resolver, portPool port.ServicePortSupplier, eventBus eventbus.EventBus) Dialer

NewDialer creates new p2p communication dialer which is used on consumer side.

type HandlerFunc

type HandlerFunc func(c Context) error

HandlerFunc is channel request handler func signature.

type Listener

type Listener interface {
	// Listen listens for incoming peer connections to establish new p2p channels. Establishes p2p channel and passes it
	// to channelHandlers
	Listen(providerID identity.Identity, serviceType string, channelHandler func(ch Channel)) (func(), error)

	// GetContact returns contract which is later can be added to proposal contacts definition so consumer can
	// know how to connect to this p2p listener.
	GetContact() market.Contact
}

Listener knows how to exchange p2p keys and encrypted configuration and creates ready to use p2p channels.

func NewListener

func NewListener(brokerConn nats.Connection, signer identity.SignerFactory, verifier identity.Verifier, ipResolver ip.Resolver, eventBus eventbus.EventBus) Listener

NewListener creates new p2p communication listener which is used on provider side.

type Message

type Message struct {
	Data []byte
}

Message represent message with data bytes.

func ProtoMessage

func ProtoMessage(m proto.Message) *Message

ProtoMessage is convenient helper to return message with marshaled proto data bytes.

func (*Message) UnmarshalProto

func (m *Message) UnmarshalProto(to proto.Message) error

UnmarshalProto is convenient helper to unmarshal message data into strongly typed proto message.

type PrivateKey

type PrivateKey [32]byte

PrivateKey represents p2p private key.

func (*PrivateKey) Decrypt

func (k *PrivateKey) Decrypt(publicKey PublicKey, ciphertext []byte) ([]byte, error)

Decrypt decrypts data using nacl as underlying crypt system.

func (*PrivateKey) Encrypt

func (k *PrivateKey) Encrypt(publicKey PublicKey, src []byte) ([]byte, error)

Encrypt encrypts data using nacl as underlying crypt system.

type PublicKey

type PublicKey [keySize]byte

PublicKey represents p2p public key part.

func DecodePublicKey

func DecodePublicKey(keyHex string) (PublicKey, error)

DecodePublicKey converts hex string to PublicKey.

func (*PublicKey) Hex

func (k *PublicKey) Hex() string

Hex returns public encoded as in hex string.

type STUNDetectionStatus

type STUNDetectionStatus struct {
	Identity string
	NATType  string
}

STUNDetectionStatus represents information about detected NAT type using STUN servers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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