mqtts

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 31, 2022 License: MIT Imports: 20 Imported by: 0

README

MQTTSecure

Description

MQTTSecure is a MQTT wrapper that adds end-to-end encryption to the protocol. It Ciphertext Policy Attribute Based Encryption (CP-ABE) or Symmetric Encryption (AESGCM, CHACHA20) and it is compatible with MQTTv3 and MQTTv5.

Installation

  • Install the package in your Go environment
go get "github.com/sacca97/unitn-mqtts"

Usage

  • Import the package
import mqtt "github.com/sacca97/unitn-mqtts"
  • Create a MQTTConfig variable with custom settings
	config := Config{
		Brokers:              []string{"localhost:1883"},
		ClientID:             "publisherNero",
		Username:             "",
		Password:             "",
		Topics:               []string{"test/simola"},
		QoS:                  0,
		Retained:             false,
		AutoReconnect:        true,
		MaxReconnectInterval: 5,
		PersistentSession:    false,
		KeepAlive:            15,
		TLSCA:                "",
		TLSCert:              "",
		TLSKey:               "",
		Version:              V3,
		Publisher:            true,
		EncryptionKey:        "public.key",
	}
  • Connect to a MQTT broker (or more)
client, err := config.CreateConnection()
if err != nil {
    log.Fatal(err)
}

Authors and acknowledgment

This Secure MQTT extension heavily relies on the following works:

  • github.com/alihanyalcin/mqtt-wrapper
  • github.com/flynn/noise
  • github.com/fentec-project/gofe

Documentation

Index

Constants

View Source
const (
	// Disconnected : no connection to broker
	Disconnected int8 = 0
	// Connected : connection established to broker
	Connected int8 = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.0.4

type Config struct {
	Brokers              []string      // MQTT Broker address. Format: scheme://host:port
	ClientID             string        // Client ID
	Username             string        // Username to connect the broker(s)
	Password             string        // Password to connect the broker(s)
	Topics               []string      // Topics for subscription
	QoS                  int           // QoS
	Retained             bool          // Retain Message
	AutoReconnect        bool          // Reconnect if connection is lost
	MaxReconnectInterval time.Duration // Maximum time that will be waited between reconnection attempts
	PersistentSession    bool          // Set persistent(clean start for v5) of session
	KeepAlive            uint16        // Keep Alive time in sec
	TLSCA                string        // CA file path
	TLSCert              string        // Cert file path
	TLSKey               string        // Key file path
	Version              Version       // MQTT Version of client
	Crypto               string        // Crypto algorithm
	EncryptionKey        string        // Encryption key file path
	Publisher            bool          // Publisher
}

Config contains configurable options for connecting to broker(s).

func (*Config) CreateConnection added in v0.0.4

func (m *Config) CreateConnection() (MQTT, error)

CreateConnection will automatically create connection to broker(s) with MQTTConfig parameters.

type ConnectionState added in v0.0.4

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

func (*ConnectionState) GetStatus added in v0.0.4

func (c *ConnectionState) GetStatus() int8

func (*ConnectionState) MarshalJSON added in v0.0.4

func (cs *ConnectionState) MarshalJSON() ([]byte, error)

func (*ConnectionState) ResetCounter added in v0.0.4

func (c *ConnectionState) ResetCounter()

type MQTT added in v0.0.4

type MQTT interface {
	// Handle handles new messages to subscribed topics.
	Handle(handler)
	// Publish sends a message to broker with a specific topic.
	Publish(string, string, any) error
	// Request sends a message to broker and waits for the response.
	Request(string, any, time.Duration, handler) error
	// RequestWith sends a message to broker with specific response topic,
	// and waits for the response.
	RequestWith(string, string, any, time.Duration, handler) error
	// SubscribeResponse creates new subscription for response topic.
	SubscribeResponse(string) error
	// Respond sends message to response topic with correlation id (use inside HandleRequest).
	Respond(string, any, []byte) error
	// HandleRequest handles imcoming request.
	HandleRequest(responseHandler)
	// GetConnectionStatus returns the connection status: Connected or Disconnected
	GetConnectionStatus() int8
	// Disconnect will close the connection to broker.
	Disconnect()
}

MQTT is an interface for mqttv3 and mqttv5 structs.

type Version added in v0.0.4

type Version int

Version of the client

const (
	V3 Version = iota
	V5
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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