peer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ENCRYPT_PARTIALY = 0x00
	ENCRYPT_GLOBALLY = 0x01
)
View Source
const (
	INDEX_HEADER = iota
	INDEX_PAYLOAD
	INDEX_ENCRYPT_FLAG
	INDEX_PACKET_LENGTH
)
View Source
const (
	HandshakeNonceSize = 32
	HandshakeKeySize   = 32
)
View Source
const DefaultReceiveBufferSize = 8 << 20
View Source
const MaxClientIDLength = 128
View Source
const MinPreSharedKeySize = 32

Variables

View Source
var ErrReceiveBufferFull = errors.New("receive buffer limit exceeded")
View Source
var ErrUnknownClient = errors.New("unknown client")

Functions

func DeriveSessionKey

func DeriveSessionKey(
	masterKey, sharedSecret, salt []byte,
	roomID, clientID, serverID string,
	clientNonce, serverNonce, clientEphemeralKey, serverEphemeralKey []byte,
) ([]byte, error)

DeriveSessionKey creates a key scoped to one room, client, pinned server, and handshake. The salt is freshly generated for every successful handshake.

func ICECandidateSize

func ICECandidateSize(candidate *webrtc.ICECandidateInit) int

func ServerIDFromPublicKey

func ServerIDFromPublicKey(publicKey crypto.PublicKey) (string, error)

func SignServerHello

func SignServerHello(
	privateKey ed25519.PrivateKey,
	roomID, clientID, serverID string,
	clientNonce, serverNonce, clientEphemeralKey, serverEphemeralKey, sessionSalt []byte,
) ([]byte, error)

func ValidateClientCredentials

func ValidateClientCredentials(clientID string, key []byte) error

func ValidateClientID

func ValidateClientID(clientID string) error

func VerifyServerHello

func VerifyServerHello(
	publicKey ed25519.PublicKey,
	roomID, clientID, serverID string,
	clientNonce, serverNonce, clientEphemeralKey, serverEphemeralKey, sessionSalt, signature []byte,
) bool

Types

type Addr

type Addr struct {
	RoomID string
	PeerID string
	IP     string
	Port   int
}

Addr implements net.Addr for a room-scoped Telekit endpoint.

func AddrFromNet added in v0.0.2

func AddrFromNet(roomID, peerID string, address net.Addr) Addr

AddrFromNet returns a room-scoped address with the host and port extracted from a selected ICE transport address.

func (Addr) Network

func (Addr) Network() string

func (Addr) String

func (a Addr) String() string

type ByteBudget

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

ByteBudget is a concurrency-safe shared memory budget. It is used by a server to cap unread application data across all of its connections.

func NewByteBudget

func NewByteBudget(limit int64) *ByteBudget

func (*ByteBudget) Release

func (b *ByteBudget) Release(n int)

func (*ByteBudget) Reserve

func (b *ByteBudget) Reserve(n int) bool

func (*ByteBudget) Used

func (b *ByteBudget) Used() int64

type CallbackPool

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

CallbackPool bounds asynchronous application callback concurrency and backlog. Submit never blocks the WebRTC receive loop.

func NewCallbackPool

func NewCallbackPool(workers, queueSize int) *CallbackPool

func (*CallbackPool) Close

func (p *CallbackPool) Close()

func (*CallbackPool) Submit

func (p *CallbackPool) Submit(callback func()) bool

func (*CallbackPool) SubmitWithCancel

func (p *CallbackPool) SubmitWithCancel(callback, cancel func()) bool

type Codec

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

func NewCodec

func NewCodec(encryptionType string, secret, additionalData []byte, useCompress bool) (*Codec, error)

func (*Codec) DecodeMessage

func (e *Codec) DecodeMessage(data []byte) (*Message, error)

func (*Codec) DecodeMessageHeader

func (e *Codec) DecodeMessageHeader(data []byte) (*Header, error)

func (*Codec) DecodeWithDecryption

func (e *Codec) DecodeWithDecryption(data []byte) ([]byte, error)

func (*Codec) DecodeWithDecryptionLimit

func (e *Codec) DecodeWithDecryptionLimit(data []byte, maxDecodedSize int) ([]byte, error)

func (*Codec) EncodeMessage

func (e *Codec) EncodeMessage(m *Message) ([]byte, error)

func (*Codec) EncodeWithEncryption

func (e *Codec) EncodeWithEncryption(data []byte) ([]byte, error)

func (*Codec) GetSecret

func (e *Codec) GetSecret() (typ string, secret []byte)

func (*Codec) UpdateSecret

func (e *Codec) UpdateSecret(encryptionType string, secret []byte) error
type Header struct {
	Type     MessageType
	SourceId string
	TargetId string
	Sequence uint64
	Metadata map[string]any
}

func (*Header) Marshal

func (msg *Header) Marshal() []byte

func (*Header) Unmarshal

func (msg *Header) Unmarshal(data []byte) error

type KeyProvider

type KeyProvider interface {
	Key(clientID string) ([]byte, error)
}

KeyProvider resolves client identities without exposing a peer ID to the client-facing connection API.

type KeyProviderFunc

type KeyProviderFunc func(clientID string) ([]byte, error)

func (KeyProviderFunc) Key

func (f KeyProviderFunc) Key(clientID string) ([]byte, error)

type Message

type Message struct {
	Header  *Header
	Payload *Payload
	Encrypt bool
}

type MessageType

type MessageType string
const (
	MessageTypeClientHello     MessageType = "client_hello"
	MessageTypeServerHello     MessageType = "server_hello"
	MessageTypeOffer           MessageType = "offer"
	MessageTypeAnswer          MessageType = "answer"
	MessageTypeICE             MessageType = "ice"
	MessageTypeTransportSelect MessageType = "transport_select"
	MessageTypeICEOffer        MessageType = "ice_offer"
	MessageTypeICEAnswer       MessageType = "ice_answer"
	MessageTypeDisconnect      MessageType = "disconnect"
)

type Payload

type Payload struct {
	SDP                *webrtc.SessionDescription
	ICE                *webrtc.ICECandidateInit
	SessionSalt        []byte
	ClientNonce        []byte
	ServerNonce        []byte
	ClientEphemeralKey []byte
	ServerEphemeralKey []byte
	Signature          []byte
	HandshakeRoomID    string
	HandshakeClientID  string
	Timestamp          []byte
	// Transports is populated in ServerHello. Transport is the client choice.
	Transports    []string
	Transport     string
	ICEUsername   string
	ICEPassword   string
	ICECandidates []string
	Data          any
}

func (*Payload) Marshal

func (msg *Payload) Marshal() []byte

func (*Payload) Unmarshal

func (msg *Payload) Unmarshal(data []byte) error

type PreSharedKey

type PreSharedKey struct {
	ClientID string
	Key      []byte
	// ServerPublicKey pins the Ed25519 identity authorized to answer this
	// client's handshake.
	ServerPublicKey ed25519.PublicKey
}

PreSharedKey couples the public client identity used for routing with its secret authentication key and the pinned server identity. Key should contain at least 256 bits of random material; it is never sent over signaling.

func (PreSharedKey) Validate

func (p PreSharedKey) Validate() error

type RecvBuffer

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

RecvBuffer is a thread-safe, blocking read buffer that delivers complete reassembled messages in order. Read blocks until data is available or the buffer is closed. Write pushes data in and signals any blocked Read calls. Close signals EOF to all blocked Read calls.

func NewRecvBuffer

func NewRecvBuffer() *RecvBuffer

func NewRecvBufferWithLimit

func NewRecvBufferWithLimit(limit int, budget *ByteBudget) *RecvBuffer

func (*RecvBuffer) Close

func (rb *RecvBuffer) Close()

Close marks the buffer as closed and unblocks all pending Read calls. Unread data is discarded so shared server memory is released immediately. Safe to call multiple times.

func (*RecvBuffer) Read

func (rb *RecvBuffer) Read(p []byte) (int, error)

Read blocks until data is available or the buffer is closed. Returns io.EOF when the buffer has been closed and is empty.

func (*RecvBuffer) SetDeadline

func (rb *RecvBuffer) SetDeadline(deadline time.Time)

func (*RecvBuffer) Write

func (rb *RecvBuffer) Write(data []byte) error

Write appends data to the buffer and wakes a waiting Read.

type ReplayWindow

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

ReplayWindow accepts each non-zero sequence number once while allowing bounded out-of-order delivery. It is safe for concurrent signaling handlers.

func (*ReplayWindow) Accept

func (w *ReplayWindow) Accept(sequence uint64) bool

func (*ReplayWindow) Reset

func (w *ReplayWindow) Reset()

type StaticKeyring

type StaticKeyring map[string][]byte

StaticKeyring is a convenient KeyProvider for small deployments.

func (StaticKeyring) Key

func (k StaticKeyring) Key(clientID string) ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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