-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbootstrap.go
200 lines (174 loc) · 5.27 KB
/
bootstrap.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
package iot
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/golang/glog"
"sync"
"time"
)
const (
bsServer = "tls://iot-bs.cn-north-4.myhuaweicloud.com:8883"
bsServerCa = "-----BEGIN CERTIFICATE-----\n" +
"MIIETjCCAzagAwIBAgINAe5fIh38YjvUMzqFVzANBgkqhkiG9w0BAQsFADBMMSAw\n" +
"HgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMzETMBEGA1UEChMKR2xvYmFs\n" +
"U2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjAeFw0xODExMjEwMDAwMDBaFw0yODEx\n" +
"MjEwMDAwMDBaMFAxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52\n" +
"LXNhMSYwJAYDVQQDEx1HbG9iYWxTaWduIFJTQSBPViBTU0wgQ0EgMjAxODCCASIw\n" +
"DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKdaydUMGCEAI9WXD+uu3Vxoa2uP\n" +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SR+Y0fFukAj6KYXwidlNalFMz\n" +
"hriSqHKvoflShx8xpfywgVcvzfTO3PYkz6fiNJBonf6q8amaEsybwMbDqKWwIX7eSPY=\n" +
"-----END CERTIFICATE-----"
)
type BootstrapClient interface {
Boot() string
Close()
}
func NewBootstrapClient(id, password string) (BootstrapClient, error) {
client := &bsClient{
id: id,
password: password,
iotdaServer: newResult(),
}
res, err := client.init()
if res {
return client, nil
}
return nil, err
}
type bsClient struct {
id string
password string
client mqtt.Client // 使用的MQTT客户端
iotdaServer *Result // 设备接入平台地址
}
func (bs *bsClient) init() (bool, error) {
options := mqtt.NewClientOptions()
options.AddBroker(bsServer)
options.SetClientID(CreateMqttClientId(bs.id))
options.SetUsername(bs.id)
options.SetPassword(hmacSha256(bs.password, timeStamp()))
options.SetKeepAlive(250 * time.Second)
options.SetAutoReconnect(true)
options.SetConnectRetry(true)
options.SetConnectTimeout(2 * time.Second)
ca := []byte(bsServerCa)
serverCaPool := x509.NewCertPool()
serverCaPool.AppendCertsFromPEM(ca)
tlsConfig := &tls.Config{
RootCAs: serverCaPool,
InsecureSkipVerify: true,
MaxVersion: tls.VersionTLS12,
MinVersion: tls.VersionTLS12,
}
options.SetTLSConfig(tlsConfig)
bs.client = mqtt.NewClient(options)
if token := bs.client.Connect(); token.Wait() && token.Error() != nil {
glog.Warningf("device %s create bootstrap client failed,error = %v", bs.id, token.Error())
return false, token.Error()
}
downTopic := fmt.Sprintf("$oc/devices/%s/sys/bootstrap/down", bs.id)
subRes := bs.client.Subscribe(downTopic, 0, func(client mqtt.Client, message mqtt.Message) {
go func() {
fmt.Println("get message from bs server")
serverResponse := &serverResponse{}
err := json.Unmarshal(message.Payload(), serverResponse)
if err != nil {
fmt.Println(err)
bs.iotdaServer.CompleteError(err)
} else {
bs.iotdaServer.Complete(serverResponse.Address)
}
}()
})
if subRes.Wait() && subRes.Error() != nil {
fmt.Printf("sub topic %s failed,error is %s\n", downTopic, subRes.Error())
return false, subRes.Error()
} else {
fmt.Printf("sub topic %s success\n", downTopic)
}
return true, nil
}
func (bs *bsClient) Boot() string {
upTopic := fmt.Sprintf("$oc/devices/%s/sys/bootstrap/up", bs.id)
pubRes := bs.client.Publish(upTopic, 0, false, "")
if pubRes.Wait() && pubRes.Error() != nil {
fmt.Println(pubRes.Error())
return ""
}
bs.iotdaServer.Wait()
return "tls://" + bs.iotdaServer.Value()
}
func (bs *bsClient) Close() {
bs.client.Disconnect(1000)
}
type serverResponse struct {
Address string `json:"address"`
}
type Result struct {
Flag chan int
err error
mErr sync.RWMutex
res string
mRes sync.RWMutex
}
func (b *Result) Value() string {
b.mRes.RLock()
defer b.mRes.RUnlock()
return b.res
}
func (b *Result) Wait() bool {
<-b.Flag
return true
}
func (b *Result) WaitTimeout(d time.Duration) bool {
timer := time.NewTimer(d)
select {
case <-b.Flag:
if !timer.Stop() {
<-timer.C
}
return true
case <-timer.C:
}
return false
}
func (b *Result) Complete(res string) {
b.mRes.Lock()
defer b.mRes.Unlock()
b.res = res
b.Flag <- 1
}
func (b *Result) CompleteError(err error) {
b.mErr.Lock()
defer b.mErr.Unlock()
b.err = err
b.Flag <- 1
}
func (b *Result) Error() error {
b.mErr.RLock()
defer b.mErr.RUnlock()
return b.err
}
func newResult() *Result {
return &Result{
Flag: make(chan int),
}
}