-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (46 loc) · 1.47 KB
/
main.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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
"net/url"
"os"
"strconv"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
hub := "sbox-iot"
deviceId := "wsl2"
hostname := fmt.Sprintf("%s.azure-devices.net", hub)
sas := generateSasToken(hostname, deviceId, os.Getenv("SIGNIN_KEY"), time.Now().Unix()+60*60)
opts := mqtt.NewClientOptions()
opts.SetProtocolVersion(4) // 4 - MQTT 3.1.1
opts.AddBroker(fmt.Sprintf("ssl://%s:8883", hostname))
opts.SetClientID(deviceId)
opts.SetUsername(fmt.Sprintf("%s/%s/api-version=2016-11-14", hostname, deviceId))
opts.SetPassword(sas)
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
log.Fatalf("Mqtt error: %s", token.Error())
}
token := c.Publish(fmt.Sprintf("devices/%s/messages/events/", deviceId), 0, false, "{\"v\":\"gopher\"}")
token.Wait()
c.Disconnect(250)
fmt.Println("Complete publish")
}
func generateSasToken(hostname string, deviceId string, key string, expires int64) string {
uri := url.QueryEscape(hostname + "/devices/" + deviceId)
toSign := uri + "\n" + strconv.FormatInt(expires, 10)
bkey, err := base64.StdEncoding.DecodeString(key)
if err != nil {
panic(err)
}
mac := hmac.New(sha256.New, bkey)
mac.Write([]byte(toSign))
sig := mac.Sum(nil)
hash := url.QueryEscape(base64.StdEncoding.EncodeToString(sig))
return "SharedAccessSignature sr=" + uri + "&sig=" + hash + "&se=" + strconv.FormatInt(expires, 10)
}