-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtinymqtt.go
325 lines (259 loc) · 7.43 KB
/
tinymqtt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package tinymqtt
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strings"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/gogap/config"
"github.com/sirupsen/logrus"
"github.com/gogap/tinymqtt/store"
_ "github.com/gogap/tinymqtt/store/file"
_ "github.com/gogap/tinymqtt/store/memory"
)
type SubscribeOption struct {
Topic string
Qos byte
Handler mqtt.MessageHandler
}
type MQTTClient struct {
client mqtt.Client
broker string
username string
quiesce uint
subscribes []SubscribeOption
}
type LogrusLogger struct {
level logrus.Level
}
func (p *LogrusLogger) Println(v ...interface{}) {
switch p.level {
case logrus.DebugLevel:
{
logrus.Debugln(v...)
}
case logrus.ErrorLevel, logrus.PanicLevel:
{
logrus.Errorln(v...)
}
case logrus.WarnLevel:
{
logrus.Warnln(v...)
}
}
}
func (p *LogrusLogger) Printf(format string, v ...interface{}) {
switch p.level {
case logrus.DebugLevel:
{
logrus.Printf(format, v...)
}
case logrus.ErrorLevel, logrus.PanicLevel:
{
logrus.Printf(format, v...)
}
case logrus.WarnLevel:
{
logrus.Printf(format, v...)
}
}
}
func init() {
mqtt.DEBUG = &LogrusLogger{logrus.DebugLevel}
mqtt.CRITICAL = &LogrusLogger{logrus.PanicLevel}
mqtt.WARN = &LogrusLogger{logrus.WarnLevel}
mqtt.ERROR = &LogrusLogger{logrus.ErrorLevel}
}
func NewMQTTClient(conf config.Configuration, subscribes ...SubscribeOption) (ret *MQTTClient, err error) {
for i := 0; i < len(subscribes); i++ {
if len(subscribes[i].Topic) == 0 {
err = fmt.Errorf("Topic name could not be empty")
return
}
}
clientConf := conf.GetConfig("client")
clientID := clientConf.GetString("client-id")
brokerServer := clientConf.GetString("broker-server")
keepAlive := clientConf.GetTimeDuration("keep-alive")
pingTimeout := clientConf.GetTimeDuration("ping-timeout")
cleanSession := clientConf.GetBoolean("clean-session")
orderMatters := clientConf.GetBoolean("order_matters", true)
quiesce := uint(clientConf.GetInt32("quiesce"))
if quiesce == 0 {
quiesce = 250 //ms
}
logrus.WithField("broker", brokerServer).Debugln("init mqtt client")
credentialMode := clientConf.GetString("credential.mode", "normal")
credentialName := clientConf.GetString("credential.name")
if len(credentialName) == 0 {
err = fmt.Errorf("credential name is empty")
return
}
username := conf.GetString("credentials." + credentialName + ".username")
password := conf.GetString("credentials." + credentialName + ".password")
if credentialMode == "aliyun-signature" {
instanceID := clientConf.GetString("instance-id")
deviceID := clientConf.GetString("device-id")
groupID := clientConf.GetString("group-id")
if len(deviceID) == 0 {
if deviceIDEnv := clientConf.GetString("device-id-env"); len(deviceIDEnv) > 0 {
logrus.WithField("env", deviceIDEnv).Debugln("get device-id from env")
deviceID = os.Getenv(deviceIDEnv)
} else if deviceIDFile := clientConf.GetString("device-id-file"); len(deviceIDFile) > 0 {
logrus.WithField("file", deviceIDFile).Debugln("get device-id from file")
var deviceData []byte
deviceData, err = ioutil.ReadFile(deviceIDFile)
if err != nil {
err = fmt.Errorf("read device id file failure, path: %s, err: %w", deviceIDFile, err)
return
}
deviceID = strings.TrimSpace(string(deviceData))
}
}
if len(deviceID) == 0 {
err = fmt.Errorf("could not get device_id")
return
}
accessKeyID := username
accessKeySecret := password
clientID, username, password, err = calcAliyunSignature(accessKeyID, accessKeySecret, instanceID, groupID, deviceID)
if err != nil {
return
}
}
if len(clientID) == 0 {
err = fmt.Errorf("client-id could not be empty")
return
}
opts := mqtt.NewClientOptions()
storeConf := clientConf.GetConfig("store")
storeProvier := storeConf.GetString("provider")
if len(storeProvier) > 0 {
var mqttStore mqtt.Store
mqttStore, err = store.NewStore(storeProvier, storeConf)
if err != nil {
return
}
opts.SetStore(mqttStore)
}
autoReconnect := clientConf.GetBoolean("auto-reconnect", true)
opts.AddBroker(brokerServer)
opts.SetClientID(clientID)
opts.SetUsername(username)
opts.SetPassword(password)
opts.SetCleanSession(cleanSession)
opts.SetKeepAlive(keepAlive)
opts.SetPingTimeout(pingTimeout)
opts.SetAutoReconnect(autoReconnect)
opts.SetOrderMatters(orderMatters)
client := mqtt.NewClient(opts)
ret = &MQTTClient{
client: client,
broker: brokerServer,
username: username,
quiesce: quiesce,
subscribes: subscribes,
}
return
}
func hidePassword(pwd string, l int) string {
return pwd[0:l] + "*****" + pwd[len(pwd)-l:]
}
func calcAliyunSignature(accessKeyID, accessKeySecret, instanceID, groupID, deviceID string) (clientID, username, password string, err error) {
if len(accessKeyID) == 0 || len(accessKeySecret) == 0 {
err = fmt.Errorf("accessKeyID or accessKeySecret is empty")
return
}
if len(instanceID) == 0 {
err = fmt.Errorf("instanceID is empty")
return
}
if len(groupID) == 0 {
err = fmt.Errorf("groupID is empty")
return
}
if len(deviceID) == 0 {
err = fmt.Errorf("deviceID is empty")
return
}
logrus.WithFields(
logrus.Fields{
"access_key_id": accessKeyID,
"access_key_secret": hidePassword(accessKeySecret, 4),
"instance_id": instanceID,
"group_id": groupID,
"device_id": deviceID,
},
).Debugln("cacl aliyun signature")
username = "Signature" + "|" + accessKeyID + "|" + instanceID
clientID = groupID + "@@@" + deviceID
pwdHmac := hmac.New(sha1.New, []byte(accessKeySecret))
pwdHmac.Write([]byte(clientID))
pwdBytes := pwdHmac.Sum(nil)
password = base64.StdEncoding.EncodeToString(pwdBytes)
logrus.WithFields(
logrus.Fields{
"username": username,
"password": hidePassword(password, 4),
"client_id": clientID,
},
).Debugln("username and password calced")
return
}
func (p *MQTTClient) Start() (err error) {
logrus.Debugln("MQTTClient begin to connect to server")
if token := p.client.Connect(); token.Wait() && token.Error() != nil {
err = token.Error()
return
}
logrus.Debugln("MQTTClient client started()")
for i := 0; i < len(p.subscribes); i++ {
if token := p.client.Subscribe(p.subscribes[i].Topic, p.subscribes[i].Qos, p.subscribes[i].Handler); token.Wait() && token.Error() != nil {
err = token.Error()
return
}
}
logrus.Debugln("MQTTClient client subscribed successful")
return
}
func (p *MQTTClient) Stop() (err error) {
var topicList []string
for i := 0; i < len(p.subscribes); i++ {
topicList = append(topicList, p.subscribes[i].Topic)
}
if token := p.client.Unsubscribe(topicList...); token.Wait() && token.Error() != nil {
err = token.Error()
p.client.Disconnect(p.quiesce)
return
}
p.client.Disconnect(p.quiesce)
return
}
type MQSendResult struct {
Topic string `json:"topic"`
Broker string `json:"broker"`
Username string `json:"username"`
MsgID uint16 `json:"msg_id"`
Qos byte `json:"qos"`
Retained bool `json:"retained"`
}
func (p *MQTTClient) SendMessage(topic string, qos byte, retained bool, msg []byte) (ret MQSendResult, err error) {
token := p.client.Publish(topic, qos, retained, msg)
if token.Wait() && token.Error() != nil {
err = token.Error()
return
}
pubToken := token.(*mqtt.PublishToken)
ret = MQSendResult{
Topic: topic,
Broker: p.broker,
Username: p.username,
MsgID: pubToken.MessageID(),
Qos: qos,
Retained: retained,
}
return
}