mqtt.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mqtt
  15. import (
  16. "crypto/tls"
  17. "fmt"
  18. "strings"
  19. "time"
  20. MQTT "github.com/eclipse/paho.mqtt.golang"
  21. "github.com/google/uuid"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. "github.com/lf-edge/ekuiper/internal/pkg/cert"
  24. "github.com/lf-edge/ekuiper/pkg/cast"
  25. "github.com/lf-edge/ekuiper/pkg/errorx"
  26. )
  27. type MQTTConnectionConfig struct {
  28. Server string `json:"server"`
  29. PVersion string `json:"protocolVersion"`
  30. ClientId string `json:"clientid"`
  31. Uname string `json:"username"`
  32. Password string `json:"password"`
  33. Certification string `json:"certificationPath"`
  34. PrivateKPath string `json:"privateKeyPath"`
  35. RootCaPath string `json:"rootCaPath"`
  36. InsecureSkipVerify bool `json:"insecureSkipVerify"`
  37. }
  38. type MQTTClient struct {
  39. srv string
  40. clientid string
  41. pVersion uint
  42. uName string
  43. password string
  44. tls *tls.Config
  45. conn MQTT.Client
  46. }
  47. func (ms *MQTTClient) CfgValidate(props map[string]interface{}) error {
  48. cfg := MQTTConnectionConfig{}
  49. err := cast.MapToStruct(props, &cfg)
  50. if err != nil {
  51. return fmt.Errorf("failed to get config, the error is %s", err)
  52. }
  53. if cfg.Server != "" {
  54. ms.srv = cfg.Server
  55. } else {
  56. return fmt.Errorf("missing server property")
  57. }
  58. if cfg.ClientId == "" {
  59. if newUUID, err := uuid.NewUUID(); err != nil {
  60. return fmt.Errorf("failed to get uuid, the error is %s", err)
  61. } else {
  62. ms.clientid = newUUID.String()
  63. }
  64. } else {
  65. ms.clientid = cfg.ClientId
  66. }
  67. // Default to MQTT 3.1.1 or NanoMQ cannot connect
  68. ms.pVersion = 4
  69. if cfg.PVersion == "3.1" {
  70. ms.pVersion = 3
  71. }
  72. tlsOpts := cert.TlsConfigurationOptions{
  73. SkipCertVerify: cfg.InsecureSkipVerify,
  74. CertFile: cfg.Certification,
  75. KeyFile: cfg.PrivateKPath,
  76. CaFile: cfg.RootCaPath,
  77. }
  78. conf.Log.Infof("Connect MQTT broker %s with TLS configs: %v.", ms.srv, tlsOpts)
  79. tlscfg, err := cert.GenerateTLSForClient(tlsOpts)
  80. if err != nil {
  81. return err
  82. }
  83. ms.tls = tlscfg
  84. ms.uName = cfg.Uname
  85. ms.password = strings.Trim(cfg.Password, " ")
  86. return nil
  87. }
  88. func (ms *MQTTClient) Connect(connHandler MQTT.OnConnectHandler, lostHandler MQTT.ConnectionLostHandler) error {
  89. if conf.Config.Basic.Debug {
  90. MQTT.DEBUG = conf.Log
  91. MQTT.ERROR = conf.Log
  92. }
  93. opts := MQTT.NewClientOptions().AddBroker(ms.srv).SetProtocolVersion(4)
  94. opts = opts.SetTLSConfig(ms.tls)
  95. if ms.uName != "" {
  96. opts = opts.SetUsername(ms.uName)
  97. }
  98. if ms.password != "" {
  99. opts = opts.SetPassword(ms.password)
  100. }
  101. opts = opts.SetClientID(ms.clientid)
  102. opts = opts.SetAutoReconnect(true)
  103. opts.OnConnect = connHandler
  104. opts.OnConnectionLost = lostHandler
  105. opts.OnReconnecting = func(MQTT.Client, *MQTT.ClientOptions) {
  106. conf.Log.Infof("Reconnecting to mqtt broker %s client id %s", ms.srv, ms.clientid)
  107. }
  108. c := MQTT.NewClient(opts)
  109. token := c.Connect()
  110. err := handleToken(token)
  111. if err != nil {
  112. conf.Log.Errorf("The connection to mqtt broker %s failed: %s", ms.srv, err)
  113. return fmt.Errorf("found error when connecting for %s: %s", ms.srv, err)
  114. }
  115. conf.Log.Infof("The connection to mqtt broker is established successfully for %s.", ms.srv)
  116. ms.conn = c
  117. return nil
  118. }
  119. func (ms *MQTTClient) Subscribe(topic string, qos byte, handler MQTT.MessageHandler) error {
  120. token := ms.conn.Subscribe(topic, qos, handler)
  121. err := handleToken(token)
  122. if err != nil {
  123. return fmt.Errorf("found error when subscribing to %s of topic %s: %s", ms.srv, topic, err)
  124. }
  125. return nil
  126. }
  127. func (ms *MQTTClient) Publish(topic string, qos byte, retained bool, message []byte) error {
  128. token := ms.conn.Publish(topic, qos, retained, message)
  129. err := handleToken(token)
  130. if err != nil {
  131. return fmt.Errorf("found error when publishing to %s of topic %s: %s", ms.srv, topic, err)
  132. }
  133. return nil
  134. }
  135. func handleToken(token MQTT.Token) error {
  136. if !token.WaitTimeout(5 * time.Second) {
  137. return fmt.Errorf("%s: timeout", errorx.IOErr)
  138. } else if token.Error() != nil {
  139. return fmt.Errorf("%s: %s", errorx.IOErr, token.Error())
  140. }
  141. return nil
  142. }
  143. func (ms *MQTTClient) Disconnect() error {
  144. conf.Log.Infof("Closing the connection to mqtt broker for %s", ms.srv)
  145. if ms.conn != nil && ms.conn.IsConnected() {
  146. ms.conn.Disconnect(5000)
  147. }
  148. return nil
  149. }