Debian Codegraph @

smoketest/mqtt/mqtt.go

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2023 Greenbone AG
2 //
3 // SPDX-License-Identifier: AGPL-3.0-or-later
4
5 package mqtt
6
7 import (
8 "context"
9 "encoding/json"
10 "fmt"
11 "net"
12
13 "github.com/eclipse/paho.golang/paho"
14 )
15
16 // TopicData is a tuple for Topic and Message.
17 type TopicData struct {
18 Topic string
19 Message []byte
20 }
21
22 // MQTT is connection type for
23 type MQTT struct {
24 client *paho.Client
25 connectProperties *paho.Connect
26 qos byte
27 incoming chan *TopicData // Is used to send respons messages of a handler downwards
28 }
29
30 func (m MQTT) Incoming() <-chan *TopicData {
31 return m.incoming
32 }
33
34 func (m MQTT) Close() error {
35 close(m.incoming)
36 return m.client.Disconnect(&paho.Disconnect{ReasonCode: 0})
37 }
38
39 func (m MQTT) register(topic string) error {
40
41 m.client.Router.RegisterHandler(topic, func(p *paho.Publish) {
42 m.incoming <- &TopicData{Topic: topic, Message: p.Payload}
43
44 })
45
46 _, err := m.client.Subscribe(context.Background(), &paho.Subscribe{
47 // we need NoLocal otherwise we would consum our own messages
48 // again and ack them.
49 Subscriptions: map[string]paho.SubscribeOptions{
50 topic: {QoS: m.qos, NoLocal: true},
51 },
52 },
53 )
54 return err
55 }
56
57 func (m MQTT) Subscribe(topics ...string) error {
58 for _, t := range topics {
59 if err := m.register(t); err != nil {
60 return err
61 }
62 }
63 return nil
64 }
65
66 func (m MQTT) Publish(topic string, message interface{}) error {
67 b, err := json.Marshal(message)
68 if err != nil {
69 return err
70 }
71 props := &paho.PublishProperties{}
72 pb := &paho.Publish{
73 Topic: topic,
74 QoS: m.qos,
75 Payload: b,
76 Properties: props,
77 }
78 _, err = m.client.Publish(context.Background(), pb)
79 return err
80 }
81
82 func (m MQTT) Connect() error {
83 ca, err := m.client.Connect(context.Background(), m.connectProperties)
84 if err != nil {
85 return err
86 }
87 if ca.ReasonCode != 0 {
88 return fmt.Errorf(
89 "failed to connect to %s : %d - %s",
90 m.client.Conn.RemoteAddr().String(),
91 ca.ReasonCode,
92 ca.Properties.ReasonString,
93 )
94 }
95 return nil
96 }
97
98 // Configuration holds information for MQTT
99 type Configuration struct {
100 ClientID string // The ID to be used when connecting to a broker
101 Username string // Username to be used as authentication; empty for anonymous
102 Password string // Password to be used as authentication with Username
103 CleanStart bool // CleanStart when false and SessionExpiry set to > 1 it will reuse a session
104 SessionExpiry uint64 // Amount of seconds a session is valid; WARNING when set to 0 it is effectively a cleanstart.
105 QOS byte
106 KeepAlive uint16
107 Inflight uint
108 }
109
110 func New(conn net.Conn,
111 cfg Configuration,
112 ) (*MQTT, error) {
113
114 c := paho.NewClient(paho.ClientConfig{
115 Router: paho.NewStandardRouter(),
116 Conn: conn,
117 })
118
119 cp := &paho.Connect{
120 KeepAlive: cfg.KeepAlive,
121 ClientID: cfg.ClientID,
122 CleanStart: cfg.CleanStart,
123 Username: cfg.Username,
124 Password: []byte(cfg.Password),
125 }
126 if cfg.Username != "" {
127 cp.UsernameFlag = true
128 }
129 if cfg.Password != "" {
130 cp.PasswordFlag = true
131 }
132
133 return &MQTT{
134 client: c,
135 connectProperties: cp,
136 qos: cfg.QOS,
137 incoming: make(chan *TopicData, cfg.Inflight),
138 }, nil
139 }