kolo docsbeta

kolo-moq

Publish, subscribe to and relay live tracks over Kolo connections, with MoQ Transport draft 22. A relay shares one upstream subscription among many subscribers, and the secobj package seals objects so a relay forwards them without reading them.

On this page

kolo-moq implements MoQ Transport, draft-ietf-moq-transport-22, on a connection that kolo-secure or kolo-relay already authenticated. A publisher writes tracks made of groups of objects. Subscribers receive a track live, or fetch cached groups, and a relay fans one track out to many subscribers. It knows nothing about media: any application data can be a track. The secobj package seals the objects of a track so that a relay forwards them without being able to read the payload, with a Kolo profile of draft-ietf-moq-secure-objects-01. The Go packages are kolomoq and secobj.

All steps

A relay shares one subscription

However many subscribers ask for a track, the publisher writes each object once. The relay reads the objects it forwards, unless they are sealed, and keeps the newest groups in its cache.

  1. The publisher dials the relay and announces a namespace with PUBLISH_NAMESPACE. Config.Authorize can refuse it.
  2. A subscriber sends SUBSCRIBE for a track under that namespace. The relay opens one upstream SUBSCRIBE to the publisher.
  3. More subscribers ask for the same track. They join the upstream subscription that is already open, so the publisher still writes each object once.
  4. The publisher writes objects to the relay, which forwards each one to every subscriber and keeps the newest groups of the track in its cache (16 groups and 32 MiB by default).
  5. A subscriber that joins late reads the cached groups from the relay, with JoinCurrentGroup or a FETCH. The upstream subscription ends when the last subscriber leaves.

Install

go get github.com/ciranda-dev/kolo-moq

To pin this release, run go get github.com/ciranda-dev/kolo-moq@v0.1.0. It is Go only, with no cgo and nothing to install, so it builds with CGO_ENABLED=0. It needs Go 1.26 or newer. Both packages are in the same module.

Quick start

A publisher and a subscriber meet through a relay, all on 127.0.0.1. The relay listens and serves every peer that connects. The publisher dials it and announces the namespace demo, the subscriber dials the same relay and subscribes to the track chat, and the publisher writes one group of three objects and ends the track.

main.go
// Command quickstart publishes a track to a relay and subscribes to it through the relay, all on 127.0.0.1.
package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"time"

	koloid "github.com/ciranda-dev/kolo-id"
	kolomoq "github.com/ciranda-dev/kolo-moq"
	kolosecure "github.com/ciranda-dev/kolo-secure"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	relayID, pubID, subID := must(koloid.Generate()), must(koloid.Generate()), must(koloid.Generate())

	// The relay listens and serves every peer that connects.
	relay := must(kolomoq.NewRelay(nil))
	ln := must(kolosecure.Listen(ctx, "127.0.0.1:0", relayID, nil))
	defer ln.Close()
	go func() {
		for {
			conn, err := ln.Accept(ctx)
			if err != nil {
				return
			}
			relay.NewServer(ctx, conn)
		}
	}()
	dial := func(id *koloid.Identity, cfg *kolomoq.Config) *kolomoq.Session {
		conn := must(kolosecure.Dial(ctx, ln.Addr().String(), id, relayID.PublicKey(), nil))
		return must(kolomoq.NewClient(ctx, conn, cfg))
	}

	// The publisher dials the relay and announces the namespace "demo".
	origin := must(kolomoq.NewOrigin(nil))
	pub := dial(pubID, &kolomoq.Config{Origin: origin})
	defer pub.Close()
	ns := kolomoq.Namespace{"demo"}
	must(pub.PublishNamespace(ctx, ns))
	track := must(origin.CreateTrack(ns, "chat", nil))

	// The subscriber dials the same relay and subscribes to the track.
	sess := dial(subID, nil)
	defer sess.Close()
	sub := must(sess.Subscribe(ctx, ns, "chat", nil))

	// The publisher writes one group of three objects, then ends the track.
	group := must(track.NewGroup())
	for i := range 3 {
		must(group.WriteObject([]byte(fmt.Sprintf("hello %d", i))))
	}
	track.Close()

	for obj, err := sub.ReadObject(ctx); err != io.EOF; obj, err = sub.ReadObject(ctx) {
		if err != nil {
			log.Fatal(err)
		}
		if obj.Status == kolomoq.StatusNormal {
			fmt.Printf("group %d object %d: %s\n", obj.Group, obj.ID, obj.Payload)
		}
	}
}

func must[T any](v T, err error) T {
	if err != nil {
		log.Fatal(err)
	}
	return v
}

It prints:

Text
group 0 object 0: hello 0
group 0 object 1: hello 1
group 0 object 2: hello 2

The repository also has examples/publisher and examples/subscriber, which run the two sides as separate programs and take the address and an identity key file as flags, and examples/basic, which connects the two directly without a relay.

API reference

The API reference is generated from the Go source and is in English.

Package kolomoq · github.com/ciranda-dev/kolo-moq · v0.1.0

Package kolomoq runs MoQ Transport (draft-ietf-moq-transport-22) sessions over authenticated Kolo connections: publish tracks of groups and objects, subscribe to them, fetch cached groups and relay tracks to many subscribers.

Each control stream starts with the version preface "moqt-22"; objects travel only on subgroup and fetch streams, never in datagrams.

Experimental: the API may change before v1.0.0.

Constants

const DefaultPriority = 128

DefaultPriority is the draft's default publisher and subscriber priority. Lower numbers are more urgent.

const KeyNamespaceField = "kolo-keys"

KeyNamespaceField is the reserved first namespace field of key tracks: PublishNamespace refuses it, namespace answers never list it, and a Relay refuses every request under it.

const NewGroupRequestInterval = time.Second

NewGroupRequestInterval is the least time between two requests for a new group on one subscription, and the most often a publisher signals one for one track, whatever the number of subscriptions.

Variables

var (
	// ErrInvalidConfig means a Config, OriginConfig or TrackOptions field is out of range.
	ErrInvalidConfig = errors.New("kolomoq: invalid config")
	// ErrInvalidName means a namespace or track name breaks the draft or configured limits.
	ErrInvalidName = errors.New("kolomoq: invalid namespace or track name")
	// ErrInvalidProperty means a property's Type parity does not match its value, or its bytes are too long.
	ErrInvalidProperty = errors.New("kolomoq: invalid property")
	// ErrClosed means this side closed the session, subscription, fetch, track or group.
	ErrClosed = errors.New("kolomoq: closed")
	// ErrGoingAway means a GOAWAY was received, so no new request is sent, or this side already sent one, so GoAway is not repeated.
	ErrGoingAway = errors.New("kolomoq: session is going away")
	// ErrVersion means the peer's control stream did not start with the moqt-22 preface.
	ErrVersion = errors.New("kolomoq: peer speaks another MoQ Transport version")
	// ErrProtocol means the peer broke the draft; the session is closed.
	ErrProtocol = errors.New("kolomoq: protocol violation")
	// ErrTimeout means the setup or a request took longer than its configured timeout.
	ErrTimeout = errors.New("kolomoq: timeout")
	// ErrNewGroupTooSoon means RequestNewGroup was called less than NewGroupRequestInterval after the last request on the subscription.
	ErrNewGroupTooSoon = errors.New("kolomoq: new group requested too soon")
	// ErrTrackExists means the origin already has a track with this name.
	ErrTrackExists = errors.New("kolomoq: track exists")
	// ErrTooManyTracks means the origin holds MaxTracks tracks.
	ErrTooManyTracks = errors.New("kolomoq: too many tracks")
	// ErrObjectTooLarge means an object is larger than MaxObjectSize.
	ErrObjectTooLarge = errors.New("kolomoq: object too large")
	// ErrMalformedTrack means the publisher sent objects that break the draft's track rules.
	ErrMalformedTrack = errors.New("kolomoq: malformed track")
	// ErrTooManyNamespaces means a namespace subscription announced more than MaxNamespaces namespaces.
	ErrTooManyNamespaces = errors.New("kolomoq: too many namespaces")
	// ErrRequestCanceled means the peer reset a request stream; the error wraps the stream code.
	ErrRequestCanceled = errors.New("kolomoq: request canceled by peer")
	// ErrUnsupportedProperty means a track carries a mandatory track property (types 0x4000 to 0x7FFF) this side does not understand.
	ErrUnsupportedProperty = errors.New("kolomoq: unsupported mandatory track property")
)

Types

type Config

type Config struct {
	// Origin holds the tracks this side publishes. Nil means requests for tracks are answered DOES_NOT_EXIST.
	Origin *Origin
	// Authorize decides each request from the peer: a non-nil error refuses it with UNAUTHORIZED.
	// Nil allows every request; the peer is already authenticated by its kolo-id key.
	Authorize func(peer ed25519.PublicKey, kind RequestKind, ns Namespace, track string) error

	// SetupTimeout bounds the exchange of version prefaces and SETUP. Default 10s, from 1s to 10m.
	SetupTimeout time.Duration
	// RequestTimeout bounds the wait for a response, for the first message of a request stream and for a data stream header, the wait for the data stream of a fetch after FETCH_OK, and the time a data stream the peer does not read may stay blocked before it is reset. Default 10s, from 1s to 10m.
	RequestTimeout time.Duration
	// MaxRequests caps the requests the peer may have open at once; more are reset. Default 1000, from 1 to 1000000.
	MaxRequests int
	// MaxSubscriptions caps the peer's open subscriptions; more are refused with EXCESSIVE_LOAD. Default 1000, from 1 to 1000000.
	MaxSubscriptions int
	// MaxObjectSize caps an object payload received from the peer, in bytes. Default 16 MiB, from 1 KiB to 1 GiB.
	MaxObjectSize int
	// MaxControlMessageSize caps a control message from the peer, in bytes. Default and maximum 65535 (the draft's 16-bit length), from 8192.
	MaxControlMessageSize int
	// MaxNamespaceFields caps the fields of a namespace in a request. Default and maximum 32 (the draft limit), from 1.
	MaxNamespaceFields int
	// MaxNameLength caps a namespace plus track name in a request, in bytes. Default and maximum 4096 (the draft limit), from 1.
	MaxNameLength int
	// MaxBufferedGroups caps the groups queued for one subscription, on either side. Default 64, from 1 to 1000000.
	MaxBufferedGroups int
	// MaxBufferedBytes caps the payload bytes queued for one subscription, on either side. Default 32 MiB or MaxObjectSize if larger, from MaxObjectSize to 64 GiB.
	MaxBufferedBytes int64
	// MaxNamespaces caps the namespaces one namespace subscription tracks, on either side. Default 1000, from 1 to 1000000.
	MaxNamespaces int
	// MaxFetchBytes caps the bytes of cached objects held at once for the peer's FETCHes and fills, and in a relay the objects it buffers for the peer's forwarded FETCHes. A FETCH beyond it is refused with EXCESSIVE_LOAD, a fill is skipped and a forwarded FETCH is reset. Default MaxBufferedBytes, from MaxObjectSize to 64 GiB.
	MaxFetchBytes int64
	// contains filtered or unexported fields
}

Config sets the behavior and limits of a Session. A zero field takes its default.

type Conn

type Conn interface {
	RemotePublicKey() ed25519.PublicKey
	OpenStream(ctx context.Context) (*kolotransport.Stream, error)
	AcceptStream(ctx context.Context) (*kolotransport.Stream, error)
	OpenUniStream(ctx context.Context) (*kolotransport.SendStream, error)
	AcceptUniStream(ctx context.Context) (*kolotransport.ReceiveStream, error)
	Context() context.Context
	Close() error
	CloseWithError(code uint64, msg string) error
}

Conn is an authenticated Kolo connection; *kolosecure.Conn and *kolorelay.Conn satisfy it. A Session owns its Conn: it is the only caller of AcceptStream and AcceptUniStream, and closes the Conn when the session ends.

type DoneCode

type DoneCode uint64

DoneCode is a PUBLISH_DONE status code (draft section 12.4).

const (
	DoneInternalError  DoneCode = 0x0
	DoneUnauthorized   DoneCode = 0x1
	DoneTrackEnded     DoneCode = 0x2
	DoneGoingAway      DoneCode = 0x4
	DoneTooFarBehind   DoneCode = 0x5
	DoneExpired        DoneCode = 0x6
	DoneUpdateFailed   DoneCode = 0x8
	DoneExcessiveLoad  DoneCode = 0x9
	DoneMalformedTrack DoneCode = 0x12
)

PUBLISH_DONE status codes.

func (DoneCode) String
func (c DoneCode) String() string

String returns the draft name of the code, or its hex value if unknown.

type Fetch

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

Fetch receives the cached objects of a track that a FETCH asked for.

func (Fetch) Close
func (f *Fetch) Close() error

Close cancels the fetch.

func (Fetch) End
func (f *Fetch) End() Location

End returns the last location the fetch covers, from FETCH_OK.

func (Fetch) EndOfTrack
func (f *Fetch) EndOfTrack() bool

EndOfTrack reports whether End is the last object of the track.

func (Fetch) Properties
func (f *Fetch) Properties() []Property

Properties returns a copy of the track properties from FETCH_OK.

func (Fetch) ReadObject
func (f *Fetch) ReadObject(ctx context.Context) (Object, error)

ReadObject returns the next object; io.EOF after the last one. Calls must not overlap.

type FetchOptions

type FetchOptions struct {
	// RecentGroups, when above zero, fetches the newest RecentGroups groups up to the Largest Object; 1 is the current group.
	RecentGroups uint64
	// Start is the first location of an absolute range, used when RecentGroups is zero.
	Start Location
	// End is the last location of the range, inclusive; nil means up to the Largest Object.
	End *Location
	// Priority is the subscriber priority, 0 (most urgent) to 255.
	Priority uint8
	// GroupOrder is the order groups are sent in. Default OrderAscending.
	GroupOrder GroupOrder
}

FetchOptions selects the objects a Fetch asks for. A nil *FetchOptions fetches the current group; in a non-nil one, Priority is used as is.

type GroupOrder

type GroupOrder uint8

GroupOrder is the order groups of a track are sent in.

const (
	OrderDefault    GroupOrder = 0
	OrderAscending  GroupOrder = 1
	OrderDescending GroupOrder = 2
)

Group orders; OrderDefault means the publisher's preference.

type GroupWriter

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

GroupWriter writes the objects of one group, numbered from 0.

func (GroupWriter) Close
func (g *GroupWriter) Close() error

Close ends the group with an End of Group object.

func (GroupWriter) ID
func (g *GroupWriter) ID() uint64

ID returns the group ID.

func (GroupWriter) Priority
func (g *GroupWriter) Priority() uint8

Priority returns the publisher priority of the group's track.

func (GroupWriter) WriteObject
func (g *GroupWriter) WriteObject(payload []byte, props ...Property) (uint64, error)

WriteObject publishes the next object of the group and returns its ID. The payload and properties are copied.

func (GroupWriter) WriteObjectFunc
func (g *GroupWriter) WriteObjectFunc(build func(id uint64) ([]byte, []Property, error)) (uint64, error)

WriteObjectFunc publishes the next object of the group with the payload and properties that build returns for its Object ID, holding the group for the whole call. If build or the checks of its result fail, no ID is used. build must not call methods of this track's writers.

type Join

type Join int

Join is where a subscription starts.

const (
	// JoinNextObject delivers objects published from now on.
	JoinNextObject Join = iota
	// JoinCurrentGroup also delivers the current group's earlier objects
	// from the publisher's cache, on a fill fetch stream.
	JoinCurrentGroup
	// JoinNextGroup starts at the next group.
	JoinNextGroup
)

Join points (draft section 3.5).

type Location

type Location struct {
	Group, Object uint64
}

Location is a Group and Object pair.

func (Location) Less
func (l Location) Less(m Location) bool

Less reports whether l comes before m.

type Namespace

type Namespace []string

Namespace is a Track Namespace: 0 to 32 non-empty fields.

func (Namespace) HasPrefix
func (n Namespace) HasPrefix(p Namespace) bool

HasPrefix reports whether p is a prefix of n, field by field.

type NamespaceEvent

type NamespaceEvent struct {
	Namespace Namespace
	Active    bool
}

NamespaceEvent reports a namespace the peer starts (Active) or stops publishing.

type NamespaceSubscription

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

NamespaceSubscription receives the namespaces the peer publishes under a prefix.

func (NamespaceSubscription) Close
func (n *NamespaceSubscription) Close() error

Close ends the namespace subscription.

func (NamespaceSubscription) Next
func (n *NamespaceSubscription) Next(ctx context.Context) (NamespaceEvent, error)

Next returns the next change. When the peer ends the subscription it returns io.EOF; every namespace reported active is then gone. Calls must not overlap.

type Object

type Object struct {
	Group      uint64
	Subgroup   uint64
	ID         uint64
	Priority   uint8
	Status     ObjectStatus
	Properties []Property
	Payload    []byte
}

Object is one object of a track.

type ObjectStatus

type ObjectStatus uint64

ObjectStatus tells a normal object from an end marker.

const (
	StatusNormal     ObjectStatus = wire.StatusNormal
	StatusEndOfGroup ObjectStatus = wire.StatusEndOfGroup
	StatusEndOfTrack ObjectStatus = wire.StatusEndOfTrack
)

Object statuses (draft section 11.1.1).

type Origin

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

Origin holds the tracks one side publishes. Many sessions can serve the same Origin: each subscriber gets its own queue, so a slow one never blocks the writer or the others. Its methods are safe for concurrent use.

func NewOrigin
func NewOrigin(cfg *OriginConfig) (*Origin, error)

NewOrigin returns an empty Origin.

func (Origin) CreateTrack
func (o *Origin) CreateTrack(ns Namespace, name string, opts *TrackOptions) (*TrackWriter, error)

CreateTrack adds a track to the origin and returns its writer.

type OriginConfig

type OriginConfig struct {
	// MaxTracks caps the open tracks. Default 1000, from 1 to 1000000.
	MaxTracks int
	// MaxObjectSize caps an object payload, in bytes. Default 16 MiB, from 1 KiB to 1 GiB.
	MaxObjectSize int
}

OriginConfig sets the limits of an Origin. A zero field takes its default.

type Property

type Property struct {
	Type  uint64
	Value uint64
	Bytes []byte
}

Property is an object or track property: an even Type carries Value, an odd Type carries Bytes (at most 65535 of them).

type PublishDoneError

type PublishDoneError struct {
	Code   DoneCode
	Reason string
}

PublishDoneError means the publisher ended a subscription with a status other than TRACK_ENDED. Reason is sanitized peer text, as in RequestError.

func (PublishDoneError) Error
func (e *PublishDoneError) Error() string

Error names the status code and leaves out the peer text.

type PublishedNamespace

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

PublishedNamespace is a namespace this side announced with PUBLISH_NAMESPACE.

func (PublishedNamespace) Close
func (p *PublishedNamespace) Close() error

Close withdraws the namespace.

func (PublishedNamespace) Done
func (p *PublishedNamespace) Done() <-chan struct{}

Done is closed when the peer revoked the namespace or the session ended.

type Relay

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

Relay forwards tracks from publishers to many subscribers. Publishers announce namespaces with PUBLISH_NAMESPACE; a SUBSCRIBE for a track under such a namespace opens one upstream subscription to the publisher, shared by every subscriber of the track and cached for FETCH and late joiners. The upstream subscription ends when the last subscriber leaves; when the publisher ends the track or goes away, subscribers get PUBLISH_DONE.

func NewRelay
func NewRelay(cfg *RelayConfig) (*Relay, error)

NewRelay returns a Relay with no sessions.

func (Relay) NewClient
func (r *Relay) NewClient(ctx context.Context, conn Conn) (*Session, error)

NewClient starts a relay session as the MoQT client on a Conn this side dialed, as the package-level NewClient.

func (Relay) NewServer
func (r *Relay) NewServer(ctx context.Context, conn Conn) (*Session, error)

NewServer starts a relay session as the MoQT server on a Conn this side accepted, as the package-level NewServer.

type RelayConfig

type RelayConfig struct {
	// Session configures every session of the relay; its Origin must be nil.
	// Its Authorize hook decides PUBLISH_NAMESPACE from publishers and every
	// request from subscribers.
	Session Config
	// MaxTracks caps the tracks relayed at once. Default 1000, from 1 to 1000000.
	MaxTracks int
	// MaxSessionTracks caps the relayed tracks that one session's SUBSCRIBEs opened and that are still relayed; more are refused with EXCESSIVE_LOAD. Default MaxTracks / 10 (at least 1), from 1 to MaxTracks.
	MaxSessionTracks int
	// MaxSessionFetches caps the FETCHes of one session the relay forwards to publishers at once; more are refused with EXCESSIVE_LOAD. Default 16, from 1 to 1000000.
	MaxSessionFetches int
	// CacheGroups and CacheBytes bound the cache of each relayed track, as in TrackOptions. Defaults 16 groups and 32 MiB.
	CacheGroups int
	CacheBytes  int64
	// Resume lets a publisher come back after its connection is lost. When a session ends because the connection
	// failed or the peer called Suspend, not because a side closed it, its announcements are held, so subscribers do
	// not see the publisher leave. While held, no other peer may announce the namespace or one under it, and no
	// request under it goes to another peer. A new session of the same peer that announces the namespace takes over
	// and closes the older one if it still runs. A hold ends when Resume is over or the session closes cleanly. Zero,
	// the default, withdraws the announcements with the session. From 1s to 1h.
	Resume time.Duration
	// MaxResumeHeld caps the announcements held at once for sessions that ended. A session whose announcements do not
	// fit loses them with the session, as without Resume, so a flood of short sessions cannot push out the holds of
	// others. Default 10000, ten times the default MaxRequests, from 1 to 1000000. It counts only with Resume.
	MaxResumeHeld int
	// MaxPeerResumeHeld caps the announcements held for one peer key, so one key cannot fill MaxResumeHeld. Default
	// the Session's MaxRequests, the most announcements one session can have, or MaxResumeHeld if that is lower;
	// from 1 to MaxResumeHeld. It counts only with Resume.
	MaxPeerResumeHeld int
}

RelayConfig sets a Relay. A zero field takes its default.

type RequestCode

type RequestCode uint64

RequestCode is a REQUEST_ERROR code (draft section 12.3).

const (
	ReqInternalError        RequestCode = 0x0
	ReqUnauthorized         RequestCode = 0x1
	ReqTimeout              RequestCode = 0x2
	ReqNotSupported         RequestCode = 0x3
	ReqMalformedAuthToken   RequestCode = 0x4
	ReqExpiredAuthToken     RequestCode = 0x5
	ReqGoingAway            RequestCode = 0x6
	ReqExcessiveLoad        RequestCode = 0x9
	ReqDoesNotExist         RequestCode = 0x10
	ReqInvalidRange         RequestCode = 0x11
	ReqMalformedTrack       RequestCode = 0x12
	ReqUninterested         RequestCode = 0x20
	ReqPrefixOverlap        RequestCode = 0x30
	ReqNamespaceTooLarge    RequestCode = 0x31
	ReqUnsupportedExtension RequestCode = 0x33
	ReqRedirect             RequestCode = 0x34
	ReqConflictingFilters   RequestCode = 0x35
	ReqInvalidFilter        RequestCode = 0x36
)

REQUEST_ERROR codes.

func (RequestCode) String
func (c RequestCode) String() string

String returns the draft name of the code, or its hex value if unknown.

type RequestError

type RequestError struct {
	Code RequestCode
	// Retry reports whether the peer allows a retry, after RetryAfter.
	Retry      bool
	RetryAfter time.Duration
	Reason     string
}

RequestError is a REQUEST_ERROR from the peer. Reason is the peer's text (at most 1024 bytes on the wire) with bytes outside printable ASCII escaped; it is not part of Error().

func (RequestError) Error
func (e *RequestError) Error() string

Error names the refusal code and leaves out the peer text.

type RequestKind

type RequestKind int

RequestKind is the kind of request the Authorize hook decides on.

const (
	KindSubscribe RequestKind = iota + 1
	KindFetch
	KindTrackStatus
	KindPublishNamespace
	KindSubscribeNamespace
)

Request kinds.

func (RequestKind) String
func (k RequestKind) String() string

String returns the draft message name of the kind.

type RoomPolicy

type RoomPolicy struct {
	Room   Namespace
	Member func(peer ed25519.PublicKey) bool
}

RoomPolicy authorizes requests for a relay serving rooms: a peer publishes only Room+{hex(peer)} and, if Member allows it, reads under Room. Everything else is refused. A nil Member admits no reader.

func (RoomPolicy) Authorize
func (p RoomPolicy) Authorize(peer ed25519.PublicKey, kind RequestKind, ns Namespace, track string) error

Authorize decides one request as Config.Authorize does; a peer's field is the lowercase hex of its 32-byte key.

type Session

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

Session is a MoQ Transport session over one Conn. It owns the Conn: it is the only caller of AcceptStream and AcceptUniStream and closes the Conn when the session ends. Its methods are safe for concurrent use.

func NewClient
func NewClient(ctx context.Context, conn Conn, cfg *Config) (*Session, error)

NewClient starts a session as the MoQT client, on a Conn this side dialed. It returns once both SETUP messages are exchanged.

func NewServer
func NewServer(ctx context.Context, conn Conn, cfg *Config) (*Session, error)

NewServer starts a session as the MoQT server, on a Conn this side accepted. It returns once both SETUP messages are exchanged.

func (Session) Close
func (s *Session) Close() error

Close ends the session with NO_ERROR and waits for its goroutines. Calls in progress return ErrClosed.

func (Session) Done
func (s *Session) Done() <-chan struct{}

Done is closed when the session has ended.

func (Session) Err
func (s *Session) Err() error

Err returns why the session ended, or nil while it runs: ErrClosed after Close, a *SessionError for a termination code, or a wrapped kolo-transport error when the connection failed.

func (Session) Fetch
func (s *Session) Fetch(ctx context.Context, ns Namespace, track string, opts *FetchOptions) (*Fetch, error)

Fetch asks the peer for cached objects and returns once the peer accepted (FETCH_OK) or refused (*RequestError, INVALID_RANGE when nothing is cached in the range).

func (Session) GoAway
func (s *Session) GoAway(ctx context.Context, timeout time.Duration) error

GoAway sends GOAWAY, refuses new requests with GOING_AWAY, and closes the session with NO_ERROR once the peer has no request open, or with GOAWAY_TIMEOUT after timeout (at least 1ms). It returns nil once it has closed the session either way; Err tells which. A second call returns ErrGoingAway, and a session that had already ended returns its error.

func (Session) GoingAway
func (s *Session) GoingAway() <-chan struct{}

GoingAway is closed when the peer sent GOAWAY: open a new session and move the subscriptions there.

func (Session) PeerImplementation
func (s *Session) PeerImplementation() string

PeerImplementation returns the peer's MOQT_IMPLEMENTATION, cut to 1024 bytes, then with bytes outside printable ASCII escaped.

func (Session) PeerNamespaces
func (s *Session) PeerNamespaces() []Namespace

PeerNamespaces returns the namespaces the peer has published to this side with PUBLISH_NAMESPACE and not withdrawn.

func (Session) PublishNamespace
func (s *Session) PublishNamespace(ctx context.Context, ns Namespace) (*PublishedNamespace, error)

PublishNamespace tells the peer this side has tracks under ns, and returns once the peer accepted it or refused it (*RequestError).

func (Session) RemotePublicKey
func (s *Session) RemotePublicKey() ed25519.PublicKey

RemotePublicKey returns a copy of the peer's kolo-id public key.

func (Session) Stats
func (s *Session) Stats() SessionStats

Stats returns counters of groups this side stopped sending, how far behind its sending is, and the transport's numbers of the connection.

func (Session) Subscribe
func (s *Session) Subscribe(ctx context.Context, ns Namespace, track string, opts *SubscribeOptions) (*Subscription, error)

Subscribe asks the peer for a track and returns once the peer accepted (SUBSCRIBE_OK) or refused (*RequestError).

func (Session) SubscribeNamespace
func (s *Session) SubscribeNamespace(ctx context.Context, prefix Namespace) (*NamespaceSubscription, error)

SubscribeNamespace asks the peer for the namespaces under prefix and their changes, and returns once the peer accepted or refused (*RequestError).

func (Session) Suspend
func (s *Session) Suspend() error

Suspend ends the session and closes its connection with CodeSuspend, to tell a Relay with Resume that this peer means to come back: the relay keeps the peer's announcements as it does for a lost connection. Close, which ends with CodeNoError, withdraws them at once.

func (Session) TrackStatus
func (s *Session) TrackStatus(ctx context.Context, ns Namespace, track string) (*TrackStatus, error)

TrackStatus asks the peer for the Largest Object and properties of a track without subscribing.

type SessionCode

type SessionCode uint64

SessionCode is a session termination code (draft section 12.2).

const (
	CodeNoError                 SessionCode = 0x0
	CodeInternalError           SessionCode = 0x1
	CodeUnauthorized            SessionCode = 0x2
	CodeProtocolViolation       SessionCode = 0x3
	CodeInvalidRequestID        SessionCode = 0x4
	CodeDuplicateTrackAlias     SessionCode = 0x5
	CodeKeyValueFormatting      SessionCode = 0x6
	CodeInvalidPath             SessionCode = 0x8
	CodeMalformedPath           SessionCode = 0x9
	CodeGoAwayTimeout           SessionCode = 0x10
	CodeControlMessageTimeout   SessionCode = 0x11
	CodeDataStreamTimeout       SessionCode = 0x12
	CodeAuthTokenCacheOverflow  SessionCode = 0x13
	CodeDuplicateAuthTokenAlias SessionCode = 0x14
	CodeMalformedAuthToken      SessionCode = 0x16
	CodeUnknownAuthTokenAlias   SessionCode = 0x17
	CodeExpiredAuthToken        SessionCode = 0x18
	CodeInvalidAuthority        SessionCode = 0x19
	CodeMalformedAuthority      SessionCode = 0x1A
	CodeTooManyRequestUpdates   SessionCode = 0x1B
	// CodeSuspend is kolo-moq's own code, outside the draft's: Suspend closes a session with it.
	CodeSuspend SessionCode = 0x100
)

Session termination codes.

func (SessionCode) String
func (c SessionCode) String() string

String returns the draft name of the code, or its hex value if unknown.

type SessionError

type SessionError struct {
	Code   SessionCode
	Remote bool
	// contains filtered or unexported fields
}

SessionError means the session ended with a MoQT termination code. Remote reports whether the peer chose the code.

func (SessionError) Error
func (e *SessionError) Error() string

Error says who closed the session and with which code.

func (SessionError) Unwrap
func (e *SessionError) Unwrap() error

Unwrap returns the sentinel behind a local close (ErrProtocol, ErrVersion, ErrTimeout), if any.

type SessionStats

type SessionStats struct {
	// SentBytes counts the bytes of data streams handed to the transport, which takes them as fast as the path lets
	// it: while QueueDelay is above zero, its change over time is the rate the path carries.
	SentBytes uint64
	// QueuedBytes is the payload waiting in subscribers' queues, not yet handed to the transport.
	QueuedBytes int64
	// QueueDelay is how long the oldest object still being sent or waiting has been held, across the subscribers of
	// this session. It is zero when nothing waits; it grows while the path carries less than is published.
	QueueDelay time.Duration
	// ResetGroups counts subgroup streams reset because a subscriber's queue exceeded MaxBufferedGroups or MaxBufferedBytes, or because the subscriber stopped reading the stream for RequestTimeout.
	ResetGroups uint64
	// ExpiredGroups counts subgroup streams reset with DELIVERY_TIMEOUT.
	ExpiredGroups uint64
	// Transport is the transport's statistics of the connection this session runs on: round-trip times, losses, the
	// congestion window, the congestion controller and the path (see kolotransport.ConnStats). It is the zero value
	// when the session's Conn has no Stats method, as a *kolorelay.Conn has none, and once the connection has ended.
	Transport kolotransport.ConnStats
}

SessionStats counts what this side sent and the groups it stopped sending, and says how far behind its sending is.

type SubscribeOptions

type SubscribeOptions struct {
	// Priority is the subscriber priority, 0 (most urgent) to 255; nil options give DefaultPriority.
	Priority uint8
	// GroupOrder overrides the publisher's group order.
	GroupOrder GroupOrder
	// Join is where the subscription starts.
	Join Join
	// Paused subscribes without receiving objects until Update resumes.
	Paused bool
	// SubgroupDeliveryTimeout and ObjectDeliveryTimeout ask the publisher to drop late groups; the smaller of its and these values applies. Zero means none.
	SubgroupDeliveryTimeout time.Duration
	ObjectDeliveryTimeout   time.Duration
}

SubscribeOptions sets a subscription. A nil *SubscribeOptions takes every default; in a non-nil one, Priority is used as is.

type Subscription

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

Subscription receives the objects of one track. ReadObject is safe for concurrent use with the other methods.

func (Subscription) Close
func (sub *Subscription) Close() error

Close unsubscribes; queued objects are dropped and ReadObject returns ErrClosed.

func (Subscription) Dropped
func (sub *Subscription) Dropped() uint64

Dropped returns how many groups this side dropped because the reader fell more than MaxBufferedGroups or MaxBufferedBytes behind.

func (Subscription) Largest
func (sub *Subscription) Largest() (Location, bool)

Largest returns the Largest Object the publisher last reported.

func (Subscription) Properties
func (sub *Subscription) Properties() []Property

Properties returns a copy of the track properties from SUBSCRIBE_OK.

func (Subscription) ReadObject
func (sub *Subscription) ReadObject(ctx context.Context) (Object, error)

ReadObject returns the next object, in arrival order. After the publisher ends the track it returns io.EOF; after another PUBLISH_DONE, a *PublishDoneError; after Close, ErrClosed.

func (Subscription) RequestNewGroup
func (sub *Subscription) RequestNewGroup(ctx context.Context) error

RequestNewGroup asks the publisher to start a new group, so a subscriber that lost a group or just joined does not wait for the next one: for a video track the publisher sends a key frame. The publisher ignores the request when a newer group exists. At most one request goes out every NewGroupRequestInterval; a sooner call returns ErrNewGroupTooSoon and sends nothing. A refused update ends the subscription, as with Update.

func (Subscription) Update
func (sub *Subscription) Update(ctx context.Context, u SubscriptionUpdate) error

Update changes the subscriber priority and pauses or resumes delivery, waiting for the publisher's answer. A refused update ends the subscription.

type SubscriptionUpdate

type SubscriptionUpdate struct {
	Priority uint8
	Paused   bool
}

SubscriptionUpdate is the new state Update asks for.

type TrackOptions

type TrackOptions struct {
	// Priority is the publisher priority, 0 (most urgent) to 255; nil options give DefaultPriority.
	Priority uint8
	// GroupOrder is the publisher's preferred group order. Default OrderAscending.
	GroupOrder GroupOrder
	// SubgroupDeliveryTimeout resets a group's stream that is not fully sent this long after the group ended. Zero means none; millisecond precision, rounded up.
	SubgroupDeliveryTimeout time.Duration
	// ObjectDeliveryTimeout resets a group's stream when an object waited this long to be sent. Zero means none; millisecond precision, rounded up.
	ObjectDeliveryTimeout time.Duration
	// CacheGroups caps the newest groups kept for FETCH and late joiners. Default 16, from 1 to 1000000.
	CacheGroups int
	// CacheBytes caps the payload bytes kept, in bytes. Default 32 MiB, from 1 KiB to 64 GiB.
	CacheBytes int64
	// Properties are extra track properties sent to subscribers; types 0x02, 0x06, 0x0E and 0x22 are set from the fields above.
	Properties []Property
	// Readers, if non-nil, are the only peers that may SUBSCRIBE to, FETCH or ask TRACK_STATUS of the track, whatever Config.Authorize decides; others get DOES_NOT_EXIST, also through a relay. An empty non-nil slice admits nobody. Each key must be 32 bytes.
	Readers []ed25519.PublicKey
}

TrackOptions sets how a track is published. A nil *TrackOptions takes every default; in a non-nil one, Priority is used as is.

type TrackStatus

type TrackStatus struct {
	Largest    Location
	HasLargest bool
	Properties []Property
}

TrackStatus is the publisher's view of a track (TRACK_STATUS_OK).

type TrackWriter

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

TrackWriter publishes the groups of one track. Its methods are safe for concurrent use.

func (TrackWriter) Close
func (w *TrackWriter) Close() error

Close ends the track: the open group ends with an End of Track object, subscribers get PUBLISH_DONE TRACK_ENDED once their queues are sent, and the track leaves the origin.

func (TrackWriter) NewGroup
func (w *TrackWriter) NewGroup() (*GroupWriter, error)

NewGroup ends the open group, if any, and starts the next one; group IDs start at 0 and increase by one.

func (TrackWriter) NewGroupRequests
func (w *TrackWriter) NewGroupRequests() <-chan struct{}

NewGroupRequests returns a channel that receives when a subscriber asks for a new group with RequestNewGroup and no newer group exists. The application then calls NewGroup and writes a group that a late subscriber can start from: for video, one that begins with a key frame. Requests merge while one is pending, and the track signals at most once every NewGroupRequestInterval, however many subscriptions ask.

func (TrackWriter) Subscribers
func (w *TrackWriter) Subscribers() int

Subscribers returns the number of subscriptions the track has now, over all sessions.

Package secobj

github.com/ciranda-dev/kolo-moq/secobj

Package secobj protects MoQ objects end to end through relays with the Kolo profile of MoQ Secure Objects (draft-ietf-moq-secure-objects-01).

One cipher suite is used: AES_128_GCM_SHA256_128 (0x0004). The key and salt of a track come from HKDF-SHA256 over a 16-byte base key, the Key ID and the serialized full track name. The nonce is the salt XOR the 64-bit Group ID and 32-bit Object ID, so an Object ID is below 2^32. The AAD is the Group ID, the Object ID, the publisher priority and the Immutable Properties bytes.

A protected object carries one visible property: Immutable Properties (0x0B) holding one Key ID property of type PropertyKeyID (0x78). Its payload is the AEAD output of the payload length, the payload and, when there are private properties, the marker 00 0A, a length and the properties. Every encoding is draft-ietf-moq-transport-22's, so objects do not interoperate with other Secure Objects implementations.

Relays still see namespaces, track names, Group and Object IDs, priority, Key IDs, sizes and timing, object status and track properties. Status and track properties are not authenticated. Go's AES-GCM is constant time only in its assembly for amd64 and arm64 CPUs with AES instructions, ppc64x and s390x; on 386, arm, riscv64, loong64, mips, wasm, and amd64 or arm64 CPUs without AES instructions, it is generic code that is not constant time.

A Sender gives its key for one media namespace to each member over a key track that only that member may read; a Keyring receives keys only from the peer that KeyringConfig.Authorize names as the namespace owner; each reader of a track opens objects through its own Opener, which keeps its own replay window.

Keys are never written to disk. Base keys and salts are overwritten when replaced, removed or closed; AES key schedules and key messages in key track caches and transport buffers are only released, and Go does not guarantee that no copy remains.

Experimental: the API may change before v1.0.0.

Constants

const (
	// PropertyKeyID is the type of the Key ID property inside Immutable Properties.
	PropertyKeyID = 0x78
	// PropertyEncryptedList is the type that marks the Encrypted Properties List inside the plaintext.
	PropertyEncryptedList = 0x0A
)

Property types of the profile.

Variables

var (
	// ErrNoKey means no key is held for the object's Key ID.
	ErrNoKey = errors.New("kolomoq: secobj: no key for this key ID")
	// ErrKeyRetired means the object's key was erased.
	ErrKeyRetired = errors.New("kolomoq: secobj: key retired")
	// ErrAuthentication means the object failed AEAD authentication.
	ErrAuthentication = errors.New("kolomoq: secobj: authentication failed")
	// ErrMalformed means a Key ID property or a decrypted plaintext breaks the profile's encoding.
	ErrMalformed = errors.New("kolomoq: secobj: malformed object")
	// ErrNotProtected means the object carries no Immutable Properties, so it was not sealed.
	ErrNotProtected = errors.New("kolomoq: secobj: object not protected")
	// ErrReplay means the object was opened before or falls outside the replay window.
	ErrReplay = errors.New("kolomoq: secobj: replayed object")
	// ErrWrongSession means a session that neither owns a protected namespace nor is a listed relay carries it.
	ErrWrongSession = errors.New("kolomoq: secobj: session may not carry this namespace")
	// ErrObjectID means an Object ID of 2^32 or more, or a (Group ID, Object ID) not after the last one sealed under the key.
	ErrObjectID = errors.New("kolomoq: secobj: object ID not allowed")
	// ErrKeyMessage means a key message breaks the profile's encoding.
	ErrKeyMessage = errors.New("kolomoq: secobj: malformed key message")
	// ErrUnauthorized means a key message came from a peer that may not give keys for its namespace.
	ErrUnauthorized = errors.New("kolomoq: secobj: unauthorized key message")
	// ErrInvalidConfig means a configuration field is out of range.
	ErrInvalidConfig = errors.New("kolomoq: secobj: invalid config")
	// ErrTooManySenders means a Keyring already holds keys for MaxSenders namespaces.
	ErrTooManySenders = errors.New("kolomoq: secobj: too many senders")
)

Functions

func KeyNamespace
func KeyNamespace(ns kolomoq.Namespace) kolomoq.Namespace

KeyNamespace returns the namespace of the key tracks of the media namespace ns.

func KeyTrack
func KeyTrack(member ed25519.PublicKey) string

KeyTrack returns the name of the key track for member: the lowercase hex of its public key.

Types

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

Keyring holds the keys of the senders a participant receives, per media namespace: the current key and the one it replaced. Objects are opened through an Opener per reader, each with its own replay window. Make one with NewKeyring. Its methods are safe for concurrent use; it prints redacted with fmt.

func NewKeyring
func NewKeyring(cfg KeyringConfig) (*Keyring, error)

NewKeyring returns an empty Keyring.

func (Keyring) Changed
func (k *Keyring) Changed() <-chan struct{}

Changed returns a channel that is closed the next time a key is installed.

func (Keyring) Format
func (k *Keyring) Format(f fmt.State, _ rune)

Format prints no key material.

func (Keyring) MayCarry
func (k *Keyring) MayCarry(s *kolomoq.Session, ns kolomoq.Namespace) error

MayCarry reports whether s may carry the protected namespace ns: its peer owns ns or is a listed relay. Otherwise it returns ErrWrongSession.

func (Keyring) Opener
func (k *Keyring) Opener(s *kolomoq.Session, ns kolomoq.Namespace, track string) (*Opener, error)

Opener returns an Opener for track under ns read on s. It fails with ErrWrongSession unless MayCarry(s, ns) is nil, so a member cannot serve another member's namespace on its own session.

func (Keyring) Receive
func (k *Keyring) Receive(ctx context.Context, s *kolomoq.Session, self ed25519.PublicKey, ns kolomoq.Namespace) error

Receive installs the keys that the peer of s gives for ns on the key track of self, until ctx ends, s ends, the peer ends the track (nil) or Remove(ns) is called. The peer is s.RemotePublicKey(); Receive fails with ErrUnauthorized unless Authorize says it owns ns. Until the peer makes self a member, the subscription is refused and Receive returns that error. Key messages that break the rules are counted in Stats and skipped.

func (Keyring) Remove
func (k *Keyring) Remove(ns kolomoq.Namespace)

Remove erases the keys of ns and ends its Receive calls.

func (Keyring) Stats
func (k *Keyring) Stats() KeyringStats

Stats returns the key message counters.

type KeyringConfig
type KeyringConfig struct {
	// Authorize reports whether peer owns ns and may give its key. Required.
	Authorize func(peer ed25519.PublicKey, ns kolomoq.Namespace) error
	// Relays are the relay identities a protected watch may use besides a namespace's owner.
	Relays []ed25519.PublicKey
	// Retain is how long a replaced key still opens objects after the first object under its successor. Default 450ms, at most 1m.
	Retain time.Duration
	// RetainMax is how long a replaced key opens objects at most after its successor arrived, even if no object under the successor opens. Default Retain plus 400ms, from Retain to 2m.
	RetainMax time.Duration
	// MaxSenders caps the namespaces with keys. Default 64, from 1 to 1024.
	MaxSenders int
	// MaxKeyRate caps key messages per namespace per second; more are dropped. Default 64, from 1 to 1024.
	MaxKeyRate int
	// ReplayGroups and ReplayObjects bound the replay window per track. Defaults 64 and 8192, from 1 to 1000000.
	ReplayGroups  int
	ReplayObjects int
}

KeyringConfig sets a Keyring. A zero field takes its default.

type KeyringStats
type KeyringStats struct {
	// Rejected counts malformed key messages, ones naming another namespace and ones that change the key of a Key ID.
	Rejected uint64
	// Dropped counts key messages over MaxKeyRate.
	Dropped uint64
}

KeyringStats counts the key messages a Keyring did not install.

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

Opener opens the objects of one track of a protected namespace for one reader, such as one subscription, with the reader's own replay window: each Opener opens an object at most once, whatever other Openers of the same Keyring opened. Its methods are safe for concurrent use.

func (Opener) Open
func (o *Opener) Open(obj kolomoq.Object) ([]byte, []kolomoq.Property, error)

Open authenticates and decrypts an object with its sender's key and records it in the replay window. It fails with ErrNoKey for a Key ID newer than the keys held, ErrKeyRetired for an older one, and ErrReplay for an object this Opener opened before or one outside its window.

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

Sender holds one sender's key for one media namespace and gives it to each member on the member's key track. Make one with NewSender. Its methods are safe for concurrent use; it prints redacted with fmt.

func NewSender
func NewSender(origin *kolomoq.Origin, ns kolomoq.Namespace, cfg *SenderConfig) (*Sender, error)

NewSender returns a Sender for the media namespace ns with a random instance ID and a random key of Key ID 0, and no members. Its key tracks are published on origin.

func (Sender) Changed
func (s *Sender) Changed() <-chan struct{}

Changed returns a channel that is closed the next time the key changes.

func (Sender) Close
func (s *Sender) Close() error

Close closes the key tracks and erases the keys. Later calls fail with kolomoq.ErrClosed.

func (Sender) Format
func (s *Sender) Format(f fmt.State, _ rune)

Format prints no key material.

func (Sender) KeyID
func (s *Sender) KeyID() uint64

KeyID returns the Key ID of the current key.

func (Sender) Members
func (s *Sender) Members() []ed25519.PublicKey

Members returns the members that receive the key, in ascending order.

func (Sender) Namespace
func (s *Sender) Namespace() kolomoq.Namespace

Namespace returns the media namespace.

func (Sender) Rotate
func (s *Sender) Rotate() error

Rotate draws a new key, gives it to every member, then makes it current.

func (Sender) Seal
func (s *Sender) Seal(track string, group, object uint64, priority uint8, payload []byte, private []kolomoq.Property) ([]byte, []kolomoq.Property, error)

Seal protects one object of track under the current key and returns its payload and properties. A (group, object) not after the last one sealed for the track is refused. The Sender rotates first when the track is one more than MaxTracks or its key reached the usage limit.

func (Sender) SetMembers
func (s *Sender) SetMembers(members []ed25519.PublicKey) error

SetMembers sets who receives the key. When the set changes, the Sender rotates: the new key goes to every member's key track before it is used, and a removed member's track is closed. On error the members and the key do not change.

func (Sender) Write
func (s *Sender) Write(g *kolomoq.GroupWriter, track string, payload []byte, private ...kolomoq.Property) (uint64, error)

Write seals the next object of g, a group of track, under the track's priority inside GroupWriter.WriteObjectFunc and publishes it. Objects of one track must be written in increasing (Group ID, Object ID) order: one that is not means the track was re-created, so the Sender rotates before sealing, and a rotation per object soon goes over the receivers' MaxKeyRate.

type SenderConfig
type SenderConfig struct {
	// MaxTracks caps the tracks sealed under one key; one more rotates. Default 1024, from 1 to 1000000.
	MaxTracks int
}

SenderConfig sets a Sender. A zero field takes its default.

type Suite
type Suite uint16

Suite is an SFrame cipher suite identifier (RFC 9605 section 8.1).

const AES128GCMSHA256 Suite = 0x0004

AES128GCMSHA256 is AES_128_GCM_SHA256_128, the only suite of the profile.

Good to know

Sessions

  • NewClient(ctx, conn, cfg) and NewServer(ctx, conn, cfg) exchange the version preface and SETUP within Config.SetupTimeout and return a *Session. conn is any kolomoq.Conn; *kolosecure.Conn and *kolorelay.Conn satisfy it. The side that dialed the connection runs NewClient, the side that accepted it NewServer.
  • The session owns the connection: it is the only caller of AcceptStream and AcceptUniStream, and it closes the connection when it ends. Close ends the session with NO_ERROR and waits for its goroutines. Done and Err report the end: ErrClosed, a *SessionError with the termination code, or a wrapped kolo-transport error.
  • Each subscription holds one bidirectional stream and one unidirectional stream per group in flight. Raise kolo-transport's MaxIncomingStreams (default 16) to match the subscriptions you expect.
  • Config.Origin holds the tracks this side publishes. Without it, track requests are answered DOES_NOT_EXIST.
  • Config.Authorize(peer, kind, namespace, track) decides every request from the peer (SUBSCRIBE, FETCH, TRACK_STATUS, PUBLISH_NAMESPACE, SUBSCRIBE_NAMESPACE) by the peer's kolo-id key. An error refuses it with UNAUTHORIZED. A SUBSCRIBE that also asks for cached objects needs KindFetch too. Nil allows everything, since the peer is already authenticated.
  • GoAway(ctx, timeout) sends GOAWAY, refuses new requests with GOING_AWAY, and closes the session once the peer has no request open, or with GOAWAY_TIMEOUT after timeout. A second call returns ErrGoingAway. GoingAway() is closed when the peer sent GOAWAY.
  • Stats counts the groups this side reset for slow subscribers or for streams the peer stopped reading (ResetGroups), and the groups that hit a delivery timeout (ExpiredGroups).
  • SessionStats also says how far behind sending is: SentBytes handed to the transport, QueuedBytes waiting in subscribers' queues and QueueDelay, the age of the oldest unsent object. A publisher whose QueueDelay grows publishes more than its path carries, and lowering its bitrate then keeps its groups from expiring. SessionStats.Transport is the kolo-transport ConnStats of the session's connection (round-trip times, losses, CongestionWindow, BytesInFlight, the congestion controller and path changes), so with a growing QueueDelay it tells whether the path is slow (SmoothedRTT far above MinRTT, PacketsLost rising) or the sender is not limited by the network (BytesInFlight far below CongestionWindow). It comes from an optional Stats() kolotransport.ConnStats method of the Conn, which *kolosecure.Conn has, and is the zero value for a Conn without one, such as a *kolorelay.Conn, and after the connection ends.
  • Session.Suspend ends the session with CodeSuspend, a code of kolo-moq's own, so a relay that was given RelayConfig.Resume keeps the peer's announcements (see Relay).
  • Session.RemotePublicKey returns a copy of the peer's kolo-id key, so a caller cannot change the identity that the Readers checks of the session see.

Publishing

  • NewOrigin(cfg) holds tracks and can serve many sessions. CreateTrack(ns, name, opts) returns a *TrackWriter. TrackOptions sets the publisher priority, group order, delivery timeouts, extra track properties and the cache.
  • TrackWriter.NewGroup ends the open group and starts the next, with IDs from 0. GroupWriter.WriteObject(payload, props...) publishes the next object and returns its ID. GroupWriter.Close ends the group, and TrackWriter.Close ends the track: subscribers get PUBLISH_DONE TRACK_ENDED, and the track leaves the origin. Subscribers counts the current subscriptions.
  • Writes never block. Each subscriber has its own queue. When it holds more than MaxBufferedGroups open subgroups or MaxBufferedBytes bytes, the oldest group's stream is reset with EXCESSIVE_LOAD for that subscriber only.
  • A TrackWriter writes one subgroup per group, subgroup 0. Each group ends with an End of Group object and the track with End of Track.
  • GroupWriter.WriteObjectFunc(build) publishes the next object with the payload and properties that build returns for its Object ID, while the group is held, so a payload that depends on the ID, such as a sealed one, cannot pair with another object's ID. GroupWriter.Priority returns the publisher priority of the group's track.
  • TrackOptions.Readers limits a track to the listed kolo-id public keys: a SUBSCRIBE, FETCH or TRACK_STATUS from any other peer gets DOES_NOT_EXIST, whatever Config.Authorize decides, also when it is nil. A relay subscribes to a publisher as its own peer, so a relay that is not listed cannot read the track.
  • Delivery timeouts are read from an object's properties both at the top level and, as the draft requires, inside its Immutable Properties.
  • TrackWriter.NewGroupRequests is a channel of capacity 1 that never blocks the session. It fires when a subscriber asks for a new group (see Subscribing): the application then calls NewGroup and starts the group with what a joiner needs, for video a key frame. A request whose value is not above the track's largest group is answered and ignored, requests merge while one is pending, and the track signals at most once every NewGroupRequestInterval (1 s), however many subscriptions ask: a request inside the interval is dropped. A subscriber that repeats the request, or opens many subscriptions, cannot make the publisher cut groups more often, so an application that answers each signal with a key frame sends at most one a second for the track.

Priorities

Objects are handed to the transport one at a time per session, most urgent first: subscriber priority, then publisher priority, then group order, then subgroup. Lower numbers are more urgent, and the default is 128 (DefaultPriority). Subscription.Update changes the subscriber priority while the subscription runs. Each subscription or fetch has its own send queue, and queues of equal priority take turns, so a subscription at high group IDs cannot starve another one.

Subscribing

  • Session.Subscribe(ctx, ns, track, opts) returns a *Subscription once the peer accepted, or a *RequestError. SubscribeOptions sets the subscriber priority, group order, Join, Paused and delivery timeouts. Join is JoinNextObject, JoinCurrentGroup, which receives the current group's earlier objects from the publisher's cache, or JoinNextGroup.
  • ReadObject(ctx) returns objects in arrival order with their group, subgroup and object IDs, priority, status and properties. It returns io.EOF after the publisher ended the track, and a *PublishDoneError after any other PUBLISH_DONE. Groups travel on separate streams, so a later group can arrive before an earlier one. A reader that falls more than MaxBufferedGroups groups or MaxBufferedBytes behind loses the oldest groups.
  • Update(ctx, SubscriptionUpdate{Priority, Paused}) changes the priority and pauses or resumes. Close unsubscribes.
  • After PUBLISH_DONE, a subscription that has no stream open but fewer streams than the publisher announced waits for the missing ones only as long as its larger delivery timeout, when one is set, instead of at least RequestTimeout.
  • Subscription.RequestNewGroup(ctx) asks the publisher for a new group, for a subscriber that lost a group or just joined a video track and does not want to wait for the next one. At most one request goes out every NewGroupRequestInterval (1 s) per subscription, and a sooner call returns ErrNewGroupTooSoon.

Cache and fetch

Each track keeps the newest CacheGroups groups within CacheBytes. A late joiner reads them with JoinCurrentGroup, and Session.Fetch(ctx, ns, track, opts) asks for them directly: the newest RecentGroups groups, or an absolute range. Fetch.ReadObject returns them in order and io.EOF at the end. Session.TrackStatus returns the Largest Object and the track properties without subscribing. A FETCH over objects the cache evicted gets an End of Unknown Range marker instead of a false "does not exist".

Namespaces

Session.PublishNamespace(ctx, ns) announces a namespace, and PublishedNamespace.Close withdraws it. Session.PeerNamespaces lists what the peer announced. Session.SubscribeNamespace(ctx, prefix) returns a *NamespaceSubscription whose Next reports the namespaces the peer starts or stops publishing under the prefix. The first namespace field kolo-keys (KeyNamespaceField) is reserved for key tracks: PublishNamespace refuses it with ErrInvalidName, a peer's PUBLISH_NAMESPACE under it gets UNAUTHORIZED, and a SUBSCRIBE_NAMESPACE answer never lists it.

Relay

  • NewRelay(cfg) returns a *Relay, and Relay.NewServer and Relay.NewClient start its sessions. Publishers announce namespaces with PUBLISH_NAMESPACE, decided by Config.Authorize. A peer cannot announce a namespace equal to or under one that another peer announced.
  • A SUBSCRIBE for a track under an announced namespace opens one upstream subscription to the publisher with the longest matching namespace. All the subscribers share it, and its objects are cached (CacheGroups, CacheBytes) for late joiners and FETCH. The upstream subscription ends when the last subscriber leaves.
  • When the publisher ends the track, subscribers get its PUBLISH_DONE status. When the publisher's session ends, they get GOING_AWAY only if a GOAWAY was sent or received on that session, and INTERNAL_ERROR if it was closed, lost or broken without one. MALFORMED_TRACK means the relay found the track malformed. A FETCH or TRACK_STATUS for a track the relay does not hold is forwarded to the publisher.
  • RelayConfig.MaxTracks caps the tracks relayed at once and MaxSessionTracks the tracks one session opened, so one subscriber cannot use every slot. A SUBSCRIBE beyond either gets EXCESSIVE_LOAD. MaxSessionFetches caps the FETCHes of one session forwarded to publishers at once.
  • A relay checks the malformed-track rules its cache can see: a repeated object that differs, another priority in a subgroup, and objects after the end of a subgroup, group or track. It does not cache the object that breaks a rule, ends the downstream subscriptions with MALFORMED_TRACK, and cancels the upstream subscription.
  • RoomPolicy{Room, Member}.Authorize is an Authorize for a relay of rooms. Under Room, a peer may announce only Room plus the lowercase hex of its own public key as one more field, and SUBSCRIBE, FETCH, TRACK_STATUS and SUBSCRIBE_NAMESPACE are allowed only if Member returns true. A nil Member admits nobody. Everything else, every request outside Room included, is refused with UNAUTHORIZED.
  • A relay refuses every request under kolo-keys with UNAUTHORIZED, so key tracks never pass through it.
  • A relayed track leaves the relay as soon as its End of Track object or its PUBLISH_DONE arrives. The next SUBSCRIBE, FETCH or TRACK_STATUS for its name goes to the publisher, so a track that the publisher ended and created again under the same name is served as the new one, while the subscribers of the ended track still get its last objects and its end. An ended track counts against MaxTracks and MaxSessionTracks until its upstream subscription ends or its last subscriber leaves. A SUBSCRIBE that reaches the relay before either end signal still joins the ended track and gets its end, because a track name does not tell two tracks apart: a subscriber that knows of a newer one subscribes again.
  • When the upstream subscription of a track ends before the relay handed the track to the subscribers waiting for it, they get the track, its objects and its end, instead of INTERNAL_ERROR.
  • RelayConfig.Resume (off by default, 1 s to 1 h) lets a publisher come back after it lost its connection. A session that ends because the connection timed out or failed, or because the peer called Session.Suspend, keeps its announcements for Resume, so the subscribers of the namespace do not see the publisher leave. A session closed by either side withdraws them at once. When a new session of the same peer announces the same namespace, it takes over: the older session of that peer, if it still runs, is closed, and its relayed tracks end, so subscribers subscribe again and reach the new session. A held announcement is reserved for its peer: no other peer can announce its namespace or one under it (UNAUTHORIZED), and a request for a track under it is never routed to another peer, even one that announced a wider namespace, so it gets DOES_NOT_EXIST until the publisher is back or Resume is over. The hold ends when the peer announces the namespace again, when that later session closes cleanly, or when Resume is over. The relay keeps only the namespace and the peer key of a held announcement, not the ended session, and holds at most MaxResumeHeld announcements, MaxPeerResumeHeld for one peer key; one that does not fit is withdrawn with its session.
  • A relay passes a new-group request for a relayed track on to the publisher on its upstream subscription, at most one at a time and one every second.

Protected objects

The secobj package is the Kolo profile of MoQ Secure Objects, draft-ietf-moq-secure-objects-01. It seals the objects of a track so that a relay forwards them without being able to read the payload. Its objects use the encodings of draft-ietf-moq-transport-22, so they do not interoperate with other Secure Objects implementations.

All steps

Objects sealed before the relay

The key goes to each member on its own session, never through the relay. The relay carries and caches sealed objects. Without the key for an object's Key ID, a member cannot open it.

  1. The sender gives its key to each member on a key track that only that member can read, over their own session. A relay refuses the kolo-keys namespace, so the key never goes through it.
  2. Sender.Write seals each object. The payload and the private properties are encrypted, and the Group ID, Object ID, priority and Key ID are authenticated.
  3. The relay receives the sealed object. It sees the namespace, the track name, the IDs, the priority, the Key ID, the size and the timing, not the payload.
  4. The relay forwards the object and caches it, as it does any other.
  5. The member's Opener opens the object with the key for its Key ID. With no key it returns ErrNoKey, and an object that fails authentication returns ErrAuthentication.

What is protected:

  • The payload and the private properties of each object, with AES-128-GCM (AES128GCMSHA256, the only suite). The key and the salt of a track come from HKDF-SHA256 over a 16-byte base key, the Key ID and the full track name.
  • The Group ID, the Object ID, the publisher priority and the Immutable Properties are authenticated. The one visible property is the Key ID, inside Immutable Properties (PropertyKeyID, 0x78). The nonce holds a 32-bit Object ID, so an Object ID must be below 2^32 (ErrObjectID).

What a relay still sees and can do:

  • It sees the namespace, the track name, the Group and Object IDs, the priority, the Key ID, the sizes and the timing, the object status and the track properties. The object status and the track properties are not authenticated.
  • It can drop, delay, reorder or withhold objects and end tracks. An object whose payload, IDs, priority or Key ID it changes fails authentication at the receiver.

What a participant without the keys gets:

  • Nothing it can open. Opener.Open returns ErrNoKey for a Key ID newer than the keys held, ErrKeyRetired for an older one, ErrAuthentication for an object that fails authentication, ErrNotProtected for an object with no Immutable Properties and ErrReplay for one it opened before.
  • Keyring.Receive is refused until the owner of the namespace makes the participant a member, and it returns that error.
  • Keyring.Opener(session, ns, track) fails with ErrWrongSession unless the session's peer owns ns or is a relay listed in KeyringConfig.Relays, so a member cannot serve another member's tracks on its own session.

Key handling, as the package provides it:

  • NewSender(origin, ns, cfg) makes a Sender for the media namespace ns, with a random instance ID, a random 16-byte key of Key ID 0 and no members. SetMembers(members) gives the key to each member on its own key track, KeyNamespace(ns) and KeyTrack(member) (the lowercase hex of its key), readable only by that member, and closes the track of a removed member. When the set changes the Sender rotates: the new key goes to every member's key track before it is used. Your application decides who the members are.
  • The Sender also rotates for a track created again, beyond SenderConfig.MaxTracks and at 2^24 units of 16 KiB of plaintext per track key. Rotate forces one, Changed is closed at each new key, and KeyID returns the current Key ID.
  • Write(g, track, payload, private...) seals the next object of a group under the priority of its track, inside WriteObjectFunc, and publishes it. Seal returns the sealed payload and properties without publishing. Write the objects of each track in increasing (Group ID, Object ID) order: an object out of order means a track created again, so the Sender rotates, and a rotation per object soon goes over the receivers' MaxKeyRate.
  • Keys travel only on key tracks, readable only by their member, over the session between the sender and that member. A relay refuses the reserved namespace field kolo-keys, so they never pass through it.
  • NewKeyring(cfg) makes the receiving side. KeyringConfig.Authorize(peer, ns) is required and says which peer owns which namespace. Receive(ctx, session, self, ns) takes the keys that the peer of the session gives for ns on the key track of self, and only if Authorize says that peer owns ns.
  • A Keyring keeps the current key and the one it replaced. The replaced key opens objects for Retain (450 ms) after the first object under its successor, and for RetainMax at most after its successor arrived, so a removed member who kept it cannot use it for long even if a relay withholds the newer objects.
  • Each reader of a track, such as one subscription, opens objects through its own Opener, with its own replay window, so several readers can share one Keyring. MayCarry, Changed, Remove(ns), which erases the keys of a namespace, and Stats, which counts Rejected and Dropped key messages, complete it.
  • Keys are never written to disk, and they print redacted. Base keys and salts are overwritten when replaced, removed or closed. The AES key schedules and the key messages held in key track caches and transport buffers are released, not overwritten, and Go does not guarantee that no copy remains in memory.

What the package does not provide:

  • A list of members. The application gives SetMembers the participants it authorizes.
  • A signature per sender. Every member holds every sender's key, so a member who controls the relay, or colludes with its operator, can make objects that seem to come from another member. Through an honest relay with RoomPolicy, and in a direct session, a member cannot.
  • Forward secrecy within a key generation, and recovery after a compromise. Anyone who holds a sender's key reads that sender's objects.
  • Constant-time AES-GCM on every CPU. Go's AES-GCM is constant time only in its assembly for amd64 and arm64 CPUs with AES instructions, ppc64x and s390x. On 386, arm, riscv64, loong64, mips, wasm, and amd64 or arm64 CPUs without AES instructions, it is generic code that is not constant time.

kolo-media uses secobj for protected broadcasts and has a complete program.

Limits

A zero field takes the default, and a value outside the range returns ErrInvalidConfig. The full tables are in Limits.

  • SetupTimeout and RequestTimeout, 10 s each, from 1 s to 10 min.
  • MaxRequests and MaxSubscriptions, 1000 each. More are refused with EXCESSIVE_LOAD.
  • MaxObjectSize, 16 MiB, from 1 KiB to 1 GiB, for a payload received from the peer. OriginConfig.MaxObjectSize is the same bound for a payload written to the origin.
  • MaxBufferedGroups, 64, and MaxBufferedBytes, 32 MiB or MaxObjectSize if larger, per subscription on both sides.
  • MaxFetchBytes, as MaxBufferedBytes: the cached objects held at once for the peer's FETCHes and fills.
  • TrackOptions.CacheGroups, 16, and CacheBytes, 32 MiB, per track. RelayConfig has the same two for each relayed track.
  • RelayConfig.MaxTracks, 1000, MaxSessionTracks, a tenth of that, and MaxSessionFetches, 16.
  • RelayConfig.Resume, off by default, from 1 s to 1 h. RelayConfig.MaxResumeHeld, 10000, from 1 to 1000000: announcements held for ended sessions at once. RelayConfig.MaxPeerResumeHeld, the session's MaxRequests (1000) or MaxResumeHeld if lower: announcements held for one peer key.
  • A track signals TrackWriter.NewGroupRequests at most once every NewGroupRequestInterval (1 s), whatever the number of subscriptions.
  • Each object counts its payload, 48 bytes per property and 64 bytes toward MaxBufferedBytes and the cache, so empty objects are not free. A payload over 64 KiB is read as it arrives, so a peer that announces a large object and sends nothing reserves no memory.
  • secobj: KeyringConfig.Retain, 450 ms, and RetainMax, Retain plus 400 ms; MaxSenders, 64; MaxKeyRate, 64 key messages per second per namespace; ReplayGroups, 64, and ReplayObjects, 8192, per Opener; and SenderConfig.MaxTracks, 1024. A track key seals at most 2^24 units of 16 KiB of plaintext, then the Sender rotates. The ranges are in Limits.

Security

  • A reason phrase from a peer is kept in Reason, at most 1024 bytes and escaped, and is never put in Error(). PeerImplementation is cut to 1024 bytes and escaped.
  • Every decoder has a fuzz target. Control messages over 65535 bytes are refused, not cut.
  • A stream that makes no progress for RequestTimeout is reset, so a peer that stops reading cannot hold the others up. A relay reads a forwarded FETCH at once into a bounded buffer and resets it when the subscriber falls too far behind.
  • The security and Go reviews found problems that are fixed with tests.

Standards

kolo-moq implements draft-ietf-moq-transport-22 on streams only, since Kolo connections are QUIC with ALPN kolo/0 and have no datagrams. Each control stream starts with the preface moqt-22. A peer whose control stream starts otherwise is closed with PROTOCOL_VIOLATION, and the session returns ErrVersion.

  • Implemented: SETUP, GOAWAY, SUBSCRIBE, REQUEST_UPDATE, FETCH, TRACK_STATUS, PUBLISH_DONE, PUBLISH_NAMESPACE, SUBSCRIBE_NAMESPACE, subscriber and publisher priorities, group order, delivery timeouts, and the section 12.1 rules a relay cache can check.
  • Answered NOT_SUPPORTED: PUBLISH, SUBSCRIBE_TRACKS and namespace-prefix updates. Range filters get INVALID_FILTER.
  • Not done: the relay subscribes to one publisher per track, not to every matching publisher as section 7.6 asks, and RENDEZVOUS_TIMEOUT is not honored. NEW_GROUP_REQUEST is honored in REQUEST_UPDATE: Subscription.RequestNewGroup sends it, and TrackWriter.NewGroupRequests tells the publisher. A subscriber that is not a relay checks only the malformed-track rules that one object shows.
  • Draft 22 is a moving draft, so a later draft will be a breaking change.

Status

  • Done: sessions, publish, subscribe, fetch, namespaces, relay, priorities and delivery timeouts, and interoperability with a second draft-22 implementation in both directions.
  • Done: the secobj package for objects protected end to end through relays, key tracks, TrackOptions.Readers and RoomPolicy.
  • Tested on macOS (arm64): go test, go test -race and the interop suite pass. On Linux and Windows go vet passes and the tests were not run.
  • Experimental. The API may change before v1.0.0.

Errors

ErrorMessage
ErrClosedkolomoq: closed
ErrGoingAwaykolomoq: session is going away
ErrInvalidConfigkolomoq: invalid config
ErrInvalidNamekolomoq: invalid namespace or track name
ErrInvalidPropertykolomoq: invalid property
ErrMalformedTrackkolomoq: malformed track
ErrNewGroupTooSoonkolomoq: new group requested too soon
ErrObjectTooLargekolomoq: object too large
ErrProtocolkolomoq: protocol violation
ErrRequestCanceledkolomoq: request canceled by peer
ErrTimeoutkolomoq: timeout
ErrTooManyNamespaceskolomoq: too many namespaces
ErrTooManyTrackskolomoq: too many tracks
ErrTrackExistskolomoq: track exists
ErrUnsupportedPropertykolomoq: unsupported mandatory track property
ErrVersionkolomoq: peer speaks another MoQ Transport version
secobj.ErrAuthenticationkolomoq: secobj: authentication failed
secobj.ErrInvalidConfigkolomoq: secobj: invalid config
secobj.ErrKeyMessagekolomoq: secobj: malformed key message
secobj.ErrKeyRetiredkolomoq: secobj: key retired
secobj.ErrMalformedkolomoq: secobj: malformed object
secobj.ErrNoKeykolomoq: secobj: no key for this key ID
secobj.ErrNotProtectedkolomoq: secobj: object not protected
secobj.ErrObjectIDkolomoq: secobj: object ID not allowed
secobj.ErrReplaykolomoq: secobj: replayed object
secobj.ErrTooManySenderskolomoq: secobj: too many senders
secobj.ErrUnauthorizedkolomoq: secobj: unauthorized key message
secobj.ErrWrongSessionkolomoq: secobj: session may not carry this namespace

Test sentinel errors with errors.Is. *SessionError, *RequestError and *PublishDoneError carry the draft's code (SessionCode, RequestCode, DoneCode), and errors.As reads them. Errors from requests, Fetch.ReadObject, Update and GoAway start with kolomoq: and wrap their cause. Errors of secobj start with kolomoq: secobj:.

References