campfire

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Package campfire implements the "camp fire" protocol.

Index

Constants

View Source
const PSKSize = 32

PSKSize is the size of the PSK in bytes.

View Source
const Protocol = "/webmesh/campfire/1.0.0"

Protocol is the protocol name.

Variables

View Source
var (
	// ErrClosed is returned when the camp fire is closed.
	ErrClosed = net.ErrClosed
)
View Source
var Now = time.Now

Now is the current time. It is a variable so it can be mocked out in tests.

Functions

func GetDefaultTURNServers

func GetDefaultTURNServers() []string

GetDefaultTURNServers returns the default list of TURN servers.

func Join

func Join(ctx context.Context, opts Options) (io.ReadWriteCloser, error)

Join will attempt to join the peer waiting at the given location.

func JoinICE

func JoinICE(ctx context.Context, opts Options) (io.ReadWriteCloser, error)

func JoinTURN added in v0.1.5

func JoinTURN(ctx context.Context, opts Options) (io.ReadWriteCloser, error)

JoinTURN will attempt to join the peer waiting at the given location.

Types

type CampFire

type CampFire interface {
	// Accept returns a connection to a peer.
	Accept() (io.ReadWriteCloser, error)
	// Close closes the camp fire.
	Close() error
	// Errors returns a channel of errors.
	Errors() <-chan error
	// Ready returns a channel that is closed when the camp fire is ready.
	Ready() <-chan struct{}
}

CampFire is a connection to one or more peers sharing the same pre-shared key.

func JoinViaRoom added in v0.1.5

func JoinViaRoom(ctx context.Context, room WaitingRoom) CampFire

JoinViaRoom joins a camp fire using the given waiting room.

func Wait added in v0.1.5

func Wait(ctx context.Context, opts Options) (CampFire, error)

Wait waits for a peer to join the camp fire and dispatches a connections and errors to the appropriate channels.

func WaitICE added in v0.1.5

func WaitICE(ctx context.Context, opts Options) (CampFire, error)

func WaitTURN added in v0.1.5

func WaitTURN(ctx context.Context, opts Options) (CampFire, error)

WaitTURN will wait for peers to join at the given location.

type Location

type Location struct {
	// PSK is the pre-shared key.
	PSK []byte
	// LocalSecret is the computed local secret from the PSK.
	LocalSecret string
	// RemoteSecret is the computed remote secret from the PSK.
	RemoteSecret string
	// TURNServer is the selected TURN server.
	TURNServer string
	// ExpiresAt is the time at which the campfire expires.
	ExpiresAt time.Time
}

Location is the secret and location of a campfire.

func Find

func Find(psk []byte, turnServers []string) (*Location, error)

Find finds a campfire using the given PSK and TURN servers. If turnServers is empty, a default list will be fetched from always-online-stun.

func (*Location) Expired

func (l *Location) Expired() <-chan struct{}

Expired returns a channel that is closed when the campfire expires.

func (*Location) LocalPwd added in v0.1.5

func (l *Location) LocalPwd() string

LocalPwd returns the local pwd.

func (*Location) LocalUfrag added in v0.1.5

func (l *Location) LocalUfrag() string

LocalUfrag returns the local ufrag.

func (*Location) RemotePwd added in v0.1.5

func (l *Location) RemotePwd() string

RemotePwd returns the remote pwd.

func (*Location) RemoteUfrag added in v0.1.5

func (l *Location) RemoteUfrag() string

RemoteUfrag returns the remote ufrag.

func (*Location) SessionID added in v0.1.5

func (l *Location) SessionID() int

SessionID returns the session ID.

func (*Location) TURNSessionID added in v0.1.5

func (l *Location) TURNSessionID() string

TURNSessionID returns the TURN session ID.

type Message

type Message struct {
	// Type is the type of message.
	Type MessageType
	// Candidate is an ICE candidate.
	Candidate string
	// SDP is an SDP offer or answer.
	SDP string
}

Message is a campfire message.

func NewCandidateMessage

func NewCandidateMessage(candidate string) Message

NewCandidateMessage creates a new candidate message.

func NewSDPMessage

func NewSDPMessage(sdp webrtc.SessionDescription) (Message, error)

NewSDPMessage creates a new SDP message.

func (Message) UnmarshalSDP

func (m Message) UnmarshalSDP() (webrtc.SessionDescription, error)

UnmarshalSDP unmarshals the SDP into a SessionDescription.

type MessageType

type MessageType int

MessageType indicates the type of message sent to a peer in a waiting room.

const (
	// CandidateMessageType is a message containing an ICE candidate.
	CandidateMessageType MessageType = iota
	// SDPMessageType is a message containing an SDP offer or answer.
	SDPMessageType
)

type Options

type Options struct {
	// PSK is the pre-shared key.
	PSK []byte
	// TURNServers is an optional list of turn servers to use.
	TURNServers []string
}

Options are options for creating or joining a new camp fire.

type Stream

type Stream interface {
	// PeerID returns the peer ID of the remote peer.
	PeerID() string
	// SendCandidate sends an ICE candidate on the stream.
	// This is a convenience method for sending a Candidate message.
	SendCandidate(candidate string) error
	// SendOffer sends an SDP offer on the stream.
	// This is a convenience method for sending an SDP message.
	SendOffer(offer webrtc.SessionDescription) error
	// Receive receives a message from the stream.
	Receive() (Message, error)
	// Close closes the stream.
	Close() error
}

Stream is a campfire stream.

type WaitingRoom

type WaitingRoom interface {
	// Connetions returns a channel that receives new incoming connections.
	Connections() <-chan Stream
	// Peers returns a channel that receives new peers that have joined the
	// campfire. A new stream to the peer is opened for each peer and sent
	// on the channel.
	Peers() <-chan Stream
	// Location returns the location of the campfire.
	Location() *Location
	// Errors returns a channel that receives errors.
	Errors() <-chan error
	// Close closes the waiting room.
	Close() error
}

WaitingRoom is an interface for a waiting for others to join the campfire.

func NewWebmeshWaitingRoom

func NewWebmeshWaitingRoom(ctx context.Context, campfireServer string, opts Options) (WaitingRoom, error)

Jump to

Keyboard shortcuts

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