Documentation
¶
Index ¶
- Variables
- func GenerateRSAKeyPair() (*rsa.PrivateKey, *rsa.PublicKey)
- func Unwrap[P any](msg *Message) (P, map[string]any, error)
- type Message
- type MessageStream
- func (ms *MessageStream) Close()
- func (ms *MessageStream) ForwardMessage(msg *Message) error
- func (ms *MessageStream) GetConnection() net.Conn
- func (ms *MessageStream) GetRecipientPublicKey() *rsa.PublicKey
- func (ms *MessageStream) On(msgType MessageType, callback func(*Message))
- func (ms *MessageStream) OnOne(msgType MessageType, callback func(*Message))
- func (ms *MessageStream) Read(msgType MessageType) <-chan *Message
- func (ms *MessageStream) ReadOne(msgType MessageType) *Message
- func (ms *MessageStream) Receiver() <-chan *Message
- func (ms *MessageStream) SendMessage(t MessageType, metadata map[string]any, payload any, proxies ...string) error
- type MessageStreamListener
- type MessageStreamOptions
- type MessageType
Constants ¶
This section is empty.
Variables ¶
var ( ErrFailedToSendPublicKey = errors.New("failed to send public key during public key exchange") ErrFailedToReceivePublicKey = errors.New("failed to receive public key during public key exchange") ErrProxyAddressUnresolvable = errors.New("failed to resolve proxy address") ErrFailedToUnwrapProxyMessage = errors.New("failed to unwrap proxied Message") ErrMessageStreamClosed = errors.New("message Stream is closed") ErrMissingPublicKey = errors.New("missing public key") ErrMissingPrivateKey = errors.New("missing private key") ErrProxyingNotAllowed = errors.New("proxying is not allowed") ErrFailedToProxyMessage = errors.New("failed to proxy Message") ErrFailedToProxyReplyMessage = errors.New("failed to return Message from proxy") )
var ErrListenerClosed = errors.New("listener already closed")
Functions ¶
func GenerateRSAKeyPair ¶ added in v0.5.0
func GenerateRSAKeyPair() (*rsa.PrivateKey, *rsa.PublicKey)
Convenience method to generate an RSA Private and Public key pair with a bit size of 2048.
If the default options, such as 2048 bit key size is suitable, this method is ok to use. But it is recommended, for security, to create the RSA keys on your own and take ownership of how they are built.
func Unwrap ¶ added in v0.4.0
Unwraps the Message into it's parts, the payload and the metadata. Returns the payload as the type P. For example, if a struct called Payload is sent, then Unwrap[Payload](someMessage) will return an instance of the Payload struct that was sent. Attempts to deserialize into an incorrect type will result in an error.
Types ¶
type Message ¶
type Message struct {
Type MessageType
Metadata map[string]any
Payload []byte
Proxies []string
}
Represents a Message that can be sent on a Message Stream. Most likely, you want NewMessage() so you can encode the metadata and payload properly.
type MessageStream ¶
type MessageStream struct {
// contains filtered or unexported fields
}
Message Stream supporting Send and Receive operations.
func Dial ¶ added in v0.6.0
func Dial(addr string, opts *MessageStreamOptions) (*MessageStream, error)
Dials a Message Stream endpoint and creates a Message Stream from the connection using the options provided
func New ¶
func New(rw io.ReadWriter, opts *MessageStreamOptions) (*MessageStream, error)
Create a new Message Stream from anything that implements io.ReadWriter.
func NewFrom ¶
func NewFrom(sender io.Writer, receiver io.Reader, opts *MessageStreamOptions) (*MessageStream, error)
Create a new Message Stream from an individual io.Reader and io.Writer.
func (*MessageStream) Close ¶
func (ms *MessageStream) Close()
Terminates any internal channels preventing sending and receiving on this Message Stream.
func (*MessageStream) ForwardMessage ¶ added in v0.2.0
func (ms *MessageStream) ForwardMessage(msg *Message) error
Forward an existing Message. This is useful in a situation where multiple Message Streams are being used and a received Message needs to be passed to a different Message Stream.
Returns an error if it fails to write data to the underlying `io.Writer` or generate a nonce.
func (*MessageStream) GetConnection ¶ added in v0.6.1
func (ms *MessageStream) GetConnection() net.Conn
Returns the underlying `net.Conn` if the Message Stream was created from one. nil otherwise.
func (*MessageStream) GetRecipientPublicKey ¶ added in v0.5.1
func (ms *MessageStream) GetRecipientPublicKey() *rsa.PublicKey
Returns the RSA Public Key that was negotiate from the other end of the Message Stream if encryption was used. The key is nil if no Public Key was sent.
func (*MessageStream) On ¶ added in v0.7.0
func (ms *MessageStream) On(msgType MessageType, callback func(*Message))
Creates a callback to handle Messages of a specific Message Type. When a Message of this type is received the callback will be executed and pass the received Message. Messages that are to be consumed in this way will NOT end up at the Receiver.
func (*MessageStream) OnOne ¶ added in v0.7.0
func (ms *MessageStream) OnOne(msgType MessageType, callback func(*Message))
Creates a single-use callback to handle one Message of a specific Message Type. When a Message of this type is received the callback will be executed and pass the received Message. The callback will then be discarded from the call list.
func (*MessageStream) Read ¶ added in v0.7.0
func (ms *MessageStream) Read(msgType MessageType) <-chan *Message
Returns a channel that will only read the specified Message Type. This will prevent this Message Type being received at the Receiver, however, you can create multiple readers of this Message Type still.
func (*MessageStream) ReadOne ¶ added in v0.7.0
func (ms *MessageStream) ReadOne(msgType MessageType) *Message
Reads one specific Message of the given Message Type. This call blocks this goroutine until this Message Type is received. Other messages can still be received and processed in other goroutines.
func (*MessageStream) Receiver ¶
func (ms *MessageStream) Receiver() <-chan *Message
Returns a channel where incoming Messages can be received.
func (*MessageStream) SendMessage ¶
func (ms *MessageStream) SendMessage(t MessageType, metadata map[string]any, payload any, proxies ...string) error
Sends a Message on the io.Writer portion of the Message Stream.
Returns an error if it fails serialise the metadata or payload, write data to the underlying `io.Writer` or generate a nonce.
If proxying is enabled, the Message will be proxied through the designated addresses if they support Message Streams
type MessageStreamListener ¶ added in v0.6.0
type MessageStreamListener struct {
// contains filtered or unexported fields
}
Represents a server capable of accepting Message Stream connections
func Listen ¶ added in v0.6.0
func Listen(addr string, opts *MessageStreamOptions) (*MessageStreamListener, error)
Creates a new Message Stream Server. The server will accept Message Stream connections and use `opts` when treating new incoming connections
func (*MessageStreamListener) Accept ¶ added in v0.6.0
func (listener *MessageStreamListener) Accept() (*MessageStream, error)
Accepts a new Message Stream connection. This function blocks until a connection is accepted
func (*MessageStreamListener) Close ¶ added in v0.6.0
func (listener *MessageStreamListener) Close() error
Closes the underlying `net.Listener` and prevents accepting anymore Message Streams on this server
type MessageStreamOptions ¶ added in v0.6.0
type MessageStreamOptions struct {
// Enables the use of RSA keys to perform Message encryption
UseAsymmetricEncryption bool
// RSA Private Key used for signing Messages and decrypting incoming Messages
PrivateKey *rsa.PrivateKey
// RSA Public Key to be sent to the other side of the Stream to be used for encrypting Messages that only this side of the stream can decrypt
PublicKey *rsa.PublicKey
// An identifier for the Message Stream, intended to be used for Logging correlation. It is not use in the communications protocol
ID string
// A structured logger for logging the loggy things
Logger *slog.Logger
// A timeout for sending and receiving public keys during key exchange
KeyExchangeTimeout time.Duration
// A timeout for sending and receiving Messages post key exchange
MessageExchangeTimeout time.Duration
// Forwards the given logger into underlying libraries and structures to produce even more loggy things for additional logginess
DeepLogging bool
// Enable Message Proxying
AllowProxying bool
}
Configuration options for creating and running a Message Stream
func NewMessageStreamOptions ¶ added in v0.6.0
func NewMessageStreamOptions() *MessageStreamOptions
Creates a new configuration with default values for the Message Stream. This will be used if `nil` is passed during the construction of a new Message Stream