|
@@ -1,6 +1,7 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "crypto/tls"
|
|
|
"encoding/hex"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
@@ -12,7 +13,6 @@ import (
|
|
|
"sparrow/pkg/coap"
|
|
|
"sparrow/pkg/klink"
|
|
|
"sparrow/pkg/protocol"
|
|
|
- "sparrow/pkg/server"
|
|
|
"sparrow/pkg/tlv"
|
|
|
"time"
|
|
|
|
|
@@ -402,20 +402,26 @@ func (d *Device) doMQTTAccess() error {
|
|
|
|
|
|
//create a ClientOptions struct setting the broker address, clientid, turn
|
|
|
//off trace output and set the default message handler
|
|
|
- opts := MQTT.NewClientOptions().AddBroker("tcp://" + d.access)
|
|
|
+ opts := MQTT.NewClientOptions().AddBroker("ssl://" + d.access)
|
|
|
clientid := fmt.Sprintf("%x", d.id)
|
|
|
opts.SetClientID(clientid)
|
|
|
opts.SetUsername(clientid) // clientid as username
|
|
|
opts.SetPassword(hex.EncodeToString(d.token))
|
|
|
opts.SetKeepAlive(30 * time.Second)
|
|
|
+ // process key files
|
|
|
+ cert, err := tls.LoadX509KeyPair(*confCAFile, *confKeyFile)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
opts.SetDefaultPublishHandler(d.messageHandler)
|
|
|
- //opts.SetTLSConfig(&tls.Config{Certificates: nil, InsecureSkipVerify: true})
|
|
|
+ opts.SetTLSConfig(&tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true})
|
|
|
|
|
|
//create and start a client using the above ClientOptions
|
|
|
c := MQTT.NewClient(opts)
|
|
|
go func() {
|
|
|
if token := c.Connect(); token.Wait() && token.Error() != nil {
|
|
|
- server.Log.Error(token.Error())
|
|
|
+ fmt.Println(err.Error())
|
|
|
return
|
|
|
}
|
|
|
}()
|