protocol

package
v0.0.0-...-53e2619 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CloseCodeNormalClosure is Normal closure (WebSocket internal close code)
	CloseCodeNormalClosure = 1000
	// CloseCodeGoingAway is Going Away (WebSocket internal close code)
	CloseCodeGoingAway = 1001
	// CloseCodeSubprotocolError is Protocol Error (WebSocket internal close code)
	CloseCodeSubprotocolError = 1002
	// CloseCodePathFullError is Path Full
	CloseCodePathFullError = 3000
	// CloseCodeProtocolError is Protocol Error
	CloseCodeProtocolError = 3001
	// CloseCodeInternalError is Internal Error
	CloseCodeInternalError = 3002
	// CloseCodeHandover is Handover of the Signalling Channel
	CloseCodeHandover = 3003
	// CloseCodeDropByInitiator is  Dropped by Initiator
	CloseCodeDropByInitiator = 3004
	// CloseCodeInitiatorCouldNotDecrypt is Initiator Could Not Decrypt
	CloseCodeInitiatorCouldNotDecrypt = 3005
	// CloseCodeNoSharedTasks is No Shared Task Found
	CloseCodeNoSharedTasks = 3006
	// CloseCodeInvalidKey is Invalid Key
	CloseCodeInvalidKey = 3007
	// CloseCodeTimeout is Timeout
	CloseCodeTimeout = 3008
)
View Source
const (
	// KeyBitSize represents the size of a key in bits
	KeyBitSize = 256
	// KeyBytesSize represents the size of a key in bytes
	KeyBytesSize = 32
	// KeyStringLength represents the size of a key in string
	KeyStringLength = 64
	// PathLength is length of a path in string
	PathLength = KeyStringLength
)
View Source
const (
	// DataLengthMin minimum length of data (nonce + payload)
	DataLengthMin = 25
	// NonceLength is length of nonce in bytes
	NonceLength = 24
	// CookieLength is length of cookie in bytes
	CookieLength = 16
	// SourceLength is length of source client in bytes
	SourceLength = 1
	// SourceUpperBound is CookieLength + SourceLength
	SourceUpperBound = CookieLength + SourceLength
	// DestinationLength is length of destination client
	DestinationLength = 1
	// DestinationUpperBound is SourceUpperBound + DestinationLength
	DestinationUpperBound = SourceUpperBound + DestinationLength
	// CsnUpperBound is upper bound of combined sequence number
	CsnUpperBound = NonceLength
)
View Source
const (
	// HeaderSize is size of header
	HeaderSize = 24
)
View Source
const (
	// SubprotocolSaltyRTCv1 default protocol identifier: "v1.saltyrtc.org"
	SubprotocolSaltyRTCv1 = "v1.saltyrtc.org"
)

Variables

View Source
var (
	// ErrNotAllowedMessage occurs when you are trying to relay a message to invalid dest
	ErrNotAllowedMessage = errors.New("not allowed message")
	// ErrNotMatchedIdentities occurs when you identities dont match for two different source
	ErrNotMatchedIdentities = errors.New("identities dont match")
	// ErrNotAuthenticatedClient occurs when you are trying to encrypt message
	ErrNotAuthenticatedClient = errors.New("client is not authenticated")
	// ErrMessageTooShort occurs when the length of message less than expected
	ErrMessageTooShort = errors.New("message is too short")
	// ErrCantDecodePayload occurs when try to decode payload
	ErrCantDecodePayload = errors.New("cant decode payload")
	// ErrFieldNotExist occurs when a field should exist but it doesnt
	ErrFieldNotExist = errors.New("field doesnt exist")
	// ErrInvalidFieldValue occurs when fiel value is not valid
	ErrInvalidFieldValue = errors.New("invalid field value")
	// ErrCantDecryptPayload occurs when try to decrypt payload
	ErrCantDecryptPayload = errors.New("cant decrypt payload")
)
View Source
var (
	// ErrHeaderLengthUnexpected occurs when header does not match with expected length
	ErrHeaderLengthUnexpected = fmt.Errorf("header error: unexpected header length bytes")
)
View Source
var (
	// ErrSlotsFull occurs when no free slot on path
	ErrSlotsFull = errors.New("no free slot on path")
)

Functions

func DecodePayload

func DecodePayload(encodedPayload []byte, v interface{}) error

DecodePayload decodes encodedPayload into v

func DecryptPayload

func DecryptPayload(clientKey [nacl.NaclKeyBytesSize]byte, serverSessionSk [nacl.NaclKeyBytesSize]byte, nonce []byte, data []byte) ([]byte, error)

DecryptPayload returns decrypted data in bytes

func EncodePayload

func EncodePayload(payload interface{}) ([]byte, error)

EncodePayload returns encoded of payload in bytes

func EncryptPayload

func EncryptPayload(clientKey [nacl.NaclKeyBytesSize]byte, serverSessionSk [nacl.NaclKeyBytesSize]byte, nonce []byte, encodedPayload []byte) ([]byte, error)

EncryptPayload encryptes payload as bytes

func ExtractNonce

func ExtractNonce(b []byte) (n []byte, err error)

ExtractNonce extracts nonce from b

func IsValidAddressID

func IsValidAddressID(id interface{}) bool

IsValidAddressID checks whether id is a valid address

func IsValidPingInterval

func IsValidPingInterval(pingInterval interface{}) bool

IsValidPingInterval checks if given pingInterval is valid. it must be type of int and higher than 0

func IsValidReasonCode

func IsValidReasonCode(reason interface{}) bool

IsValidReasonCode checks if given reason valid

func IsValidResponderAddressID

func IsValidResponderAddressID(id interface{}) bool

IsValidResponderAddressID returns true if id is a valid responder address

func IsValidResponderAddressType

func IsValidResponderAddressType(addr AddressType) bool

IsValidResponderAddressType checks if addr is valid address

func IsValidSubprotocols

func IsValidSubprotocols(subprotocols interface{}) bool

IsValidSubprotocols checks if given subprotocols is valid. it must be type of []string

func IsValidYourCookieBytes

func IsValidYourCookieBytes(pk interface{}) bool

IsValidYourCookieBytes checks if given pk is valid. length of pk must be equal to 16(CookieLength)

func IsValidYourKey

func IsValidYourKey(yourKey interface{}) bool

IsValidYourKey checks if given yourKey is valid. It must be a valid public key of nacl box

func MakeNonce

func MakeNonce(h Header) (bts []byte)

MakeNonce makes bytes of nonce from h

func ParsePingInterval

func ParsePingInterval(pingInterval interface{}) (int, error)

ParsePingInterval parses given pingInterval as type of int

func ParseReasonCode

func ParseReasonCode(reason interface{}) (int, error)

ParseReasonCode parses given reason as type of int

func ParseSubprotocols

func ParseSubprotocols(subprotocols interface{}) ([]string, error)

ParseSubprotocols parses given subprotocols to subprotocols as type of []string

func ParseYourCookie

func ParseYourCookie(pk interface{}) ([]byte, error)

ParseYourCookie parses given pk to your_cookie in bytes

func ParseYourKey

func ParseYourKey(yourKey interface{}) ([KeyBytesSize]byte, error)

ParseYourKey parses yourKey. It creates nacl box public key in bytes

func SignKeys

func SignKeys(clientKey [nacl.NaclKeyBytesSize]byte, serverSessionPk [nacl.NaclKeyBytesSize]byte, serverPermanentSk [nacl.NaclKeyBytesSize]byte, nonce []byte) []byte

SignKeys seals nonce with client and server keys to bytes

func UnmarshalMessage

func UnmarshalMessage(f Frame) (msg interface{}, err error)

UnmarshalMessage ..

func WriteFrame

func WriteFrame(w io.Writer, f Frame) error

WriteFrame writes frame binary representation into w.

func WriteHeader

func WriteHeader(w io.Writer, h Header) error

WriteHeader writes header binary representation into w.

Types

type AddressType

type AddressType = uint8

AddressType represents type of address

const (
	// Server represents address identifier for a Server
	Server AddressType = 0x00
	// Initiator represents address identifier for a Initiator
	Initiator AddressType = 0x01
	// Responder represents address identifier for a Responder
	Responder AddressType = 0xff
)

func GetAddressTypeFromAddr

func GetAddressTypeFromAddr(addr AddressType) AddressType

GetAddressTypeFromAddr parses type of addr

func ParseAddressID

func ParseAddressID(id interface{}) (AddressType, error)

ParseAddressID parses id to address of type

func ParseResponderAddressID

func ParseResponderAddressID(id interface{}) (AddressType, error)

ParseResponderAddressID parses id as address of type

type BaseMessage

type BaseMessage struct {
	Src  AddressType
	Dest AddressType
}

BaseMessage ..

type BasicEncodingOpts

type BasicEncodingOpts struct {
	ClientKey       [32]byte
	ServerSessionSk [32]byte
	Nonce           []byte
}

BasicEncodingOpts is options for encoding with basic fields

type ClientAuthMessage

type ClientAuthMessage struct {
	BaseMessage
	ServerCookie []byte
	Subprotocols []string
	PingInterval uint32
	ServerKey    [KeyBytesSize]byte

	EncodingOpts BasicEncodingOpts
}

ClientAuthMessage ..

func NewClientAuthMessage

func NewClientAuthMessage(src AddressType, dest AddressType, serverCookie []byte,
	subprotocols []string, pingInterval uint32, serverKey [KeyBytesSize]byte) *ClientAuthMessage

NewClientAuthMessage ..

func (*ClientAuthMessage) MarshalPayload

func (m *ClientAuthMessage) MarshalPayload() ([]byte, error)

MarshalPayload ..

type ClientHelloMessage

type ClientHelloMessage struct {
	BaseMessage
	ClientPublicKey []byte
}

ClientHelloMessage ..

func NewClientHelloMessage

func NewClientHelloMessage(src AddressType, dest AddressType, clientPublicKey []byte) *ClientHelloMessage

NewClientHelloMessage ..

func (*ClientHelloMessage) MarshalPayload

func (m *ClientHelloMessage) MarshalPayload() ([]byte, error)

MarshalPayload ..

type DisconnectedMessage

type DisconnectedMessage struct {
	BaseMessage

	EncodingOpts BasicEncodingOpts
	// contains filtered or unexported fields
}

DisconnectedMessage ..

func NewDisconnectedMessage

func NewDisconnectedMessage(src AddressType, dest AddressType, clientID AddressType) *DisconnectedMessage

NewDisconnectedMessage ..

func (*DisconnectedMessage) MarshalPayload

func (m *DisconnectedMessage) MarshalPayload() ([]byte, error)

MarshalPayload ..

type DropResponderMessage

type DropResponderMessage struct {
	BaseMessage
	ResponderID AddressType
	Reason      int

	EncodingOpts BasicEncodingOpts
}

DropResponderMessage ..

func NewDropResponderMessage

func NewDropResponderMessage(src AddressType, dest AddressType, responderID AddressType) *DropResponderMessage

NewDropResponderMessage ..

func NewDropResponderMessageWithReason

func NewDropResponderMessageWithReason(src AddressType, dest AddressType, responderID AddressType, reason int) *DropResponderMessage

NewDropResponderMessageWithReason ..

func (*DropResponderMessage) MarshalPayload

func (m *DropResponderMessage) MarshalPayload() ([]byte, error)

MarshalPayload returns the bytes encoding of m

type Frame

type Frame struct {
	Header  Header
	Payload []byte
}

Frame represents combination of header and payload

func ParseFrame

func ParseFrame(b []byte) (f Frame, err error)

ParseFrame parses b into f.

func ReadFrame

func ReadFrame(r io.Reader) (f Frame, err error)

ReadFrame reads a frame from r.

func ReadFrameWithSize

func ReadFrameWithSize(r io.Reader, n int) (f Frame, err error)

ReadFrameWithSize reads a fixed size frame from r.

type Header struct {
	Cookie []byte // 16 byte
	Csn    []byte // 2+4 byte
	Src    uint8  // 1 byte
	Dest   uint8  // 1 byte
}

Header represents fields in header of a frame

func ParseHeader

func ParseHeader(b []byte) (h Header, err error)

ParseHeader parses b into h.

func ReadHeader

func ReadHeader(r io.Reader) (h Header, err error)

ReadHeader reads a frame header from r.

type MessageFlowError

type MessageFlowError struct {
	Msg string
	Err error
}

MessageFlowError occurs when a received message violates flow

func NewMessageFlowError

func NewMessageFlowError(message string, err error) *MessageFlowError

NewMessageFlowError ..

func (*MessageFlowError) Error

func (e *MessageFlowError) Error() string

type MessageType

type MessageType = string

MessageType is used to represent type of a message

const (
	// ServerHello ..
	ServerHello MessageType = "server-hello"
	// ClientHello ..
	ClientHello MessageType = "client-hello"
	// ClientAuth ..
	ClientAuth MessageType = "client-auth"
	// ServerAuth ..
	ServerAuth MessageType = "server-auth"
	// NewResponder ..
	NewResponder MessageType = "new-responder"
	// NewInitiator ..
	NewInitiator MessageType = "new-initiator"
	// DropResponder ..
	DropResponder MessageType = "drop-responder"
	// SendError ..
	SendError MessageType = "send-error"
	// Disconnected ..
	Disconnected MessageType = "disconnected"
)

type NewInitiatorMessage

type NewInitiatorMessage struct {
	BaseMessage

	EncodingOpts BasicEncodingOpts
	// contains filtered or unexported fields
}

NewInitiatorMessage ..

func NewNewInitiatorMessage

func NewNewInitiatorMessage(src AddressType, dest AddressType) *NewInitiatorMessage

NewNewInitiatorMessage ..

func (*NewInitiatorMessage) MarshalPayload

func (m *NewInitiatorMessage) MarshalPayload() ([]byte, error)

MarshalPayload ..

type NewResponderMessage

type NewResponderMessage struct {
	BaseMessage

	EncodingOpts BasicEncodingOpts
	// contains filtered or unexported fields
}

NewResponderMessage ..

func NewNewResponderMessage

func NewNewResponderMessage(src AddressType, dest AddressType, responderID AddressType) *NewResponderMessage

NewNewResponderMessage ..

func (*NewResponderMessage) MarshalPayload

func (m *NewResponderMessage) MarshalPayload() ([]byte, error)

MarshalPayload ..

type PayloadFieldError

type PayloadFieldError struct {
	Type  string
	Field string
	Err   error
}

PayloadFieldError represents an error correlated to a particular field

func NewPayloadFieldError

func NewPayloadFieldError(payloadType string, field string, err error) *PayloadFieldError

NewPayloadFieldError creates PayloadFieldError instance

func (*PayloadFieldError) Error

func (e *PayloadFieldError) Error() string

Error ..

type PayloadMarshaler

type PayloadMarshaler interface {
	MarshalPayload() ([]byte, error)
}

PayloadMarshaler ..

type PayloadUnmarshaler

type PayloadUnmarshaler interface {
	UnmarshalPayload([]byte) error
}

PayloadUnmarshaler ..

type PayloadWriter

type PayloadWriter interface {
	WritePayload(w io.Writer) error
}

PayloadWriter writes payload into w

type RawMessage

type RawMessage struct {
	BaseMessage
	Data []byte
}

RawMessage ..

func NewRawMessage

func NewRawMessage(src AddressType, dest AddressType, data []byte) *RawMessage

NewRawMessage ..

func (*RawMessage) MarshalPayload

func (m *RawMessage) MarshalPayload() ([]byte, error)

MarshalPayload ..

type ServerAuthEncodingOpts

type ServerAuthEncodingOpts struct {
	ServerPermanentSk [nacl.NaclKeyBytesSize]byte
	ClientKey         [nacl.NaclKeyBytesSize]byte
	ServerSessionSk   [nacl.NaclKeyBytesSize]byte
	ServerSessionPk   [nacl.NaclKeyBytesSize]byte
	Nonce             []byte
}

ServerAuthEncodingOpts is options for encoding of server auth messsage

type ServerAuthMessage

type ServerAuthMessage struct {
	BaseMessage

	EncodingOpts ServerAuthEncodingOpts
	// contains filtered or unexported fields
}

ServerAuthMessage ..

func NewServerAuthMessageForInitiator

func NewServerAuthMessageForInitiator(src AddressType, dest AddressType, clientCookie []byte,
	signKeys bool, responderIds []AddressType) *ServerAuthMessage

NewServerAuthMessageForInitiator ..

func NewServerAuthMessageForResponder

func NewServerAuthMessageForResponder(src AddressType, dest AddressType, clientCookie []byte,
	signKeys bool, initiatorConnected bool) *ServerAuthMessage

NewServerAuthMessageForResponder ..

func (ServerAuthMessage) MarshalPayload

func (m ServerAuthMessage) MarshalPayload() ([]byte, error)

MarshalPayload ...

type ServerHelloMessage

type ServerHelloMessage struct {
	BaseMessage
	// contains filtered or unexported fields
}

ServerHelloMessage ..

func NewServerHelloMessage

func NewServerHelloMessage(src AddressType, dest AddressType, serverPublicKey []byte) *ServerHelloMessage

NewServerHelloMessage ..

func (*ServerHelloMessage) MarshalPayload

func (m *ServerHelloMessage) MarshalPayload() ([]byte, error)

MarshalPayload ..

Jump to

Keyboard shortcuts

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