-
Notifications
You must be signed in to change notification settings - Fork 302
/
mqtt.go
288 lines (261 loc) · 8.68 KB
/
mqtt.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package mqtt implements the MQTT frontend.
package mqtt
import (
"context"
"fmt"
"net"
"sync"
"github.com/TheThingsIndustries/mystique/pkg/auth"
mqttnet "github.com/TheThingsIndustries/mystique/pkg/net"
"github.com/TheThingsIndustries/mystique/pkg/packet"
"github.com/TheThingsIndustries/mystique/pkg/session"
"github.com/TheThingsIndustries/mystique/pkg/topic"
"go.thethings.network/lorawan-stack/v3/pkg/applicationserver/io"
ttsauth "go.thethings.network/lorawan-stack/v3/pkg/auth"
"go.thethings.network/lorawan-stack/v3/pkg/auth/rights"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/mqtt"
"go.thethings.network/lorawan-stack/v3/pkg/ratelimit"
"go.thethings.network/lorawan-stack/v3/pkg/task"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/unique"
"google.golang.org/grpc/metadata"
)
const qosUpstream byte = 0
// Serve serves the MQTT frontend.
func Serve(ctx context.Context, server io.Server, listener net.Listener, format Format, protocol string) error {
ctx = log.NewContextWithField(ctx, "namespace", "applicationserver/io/mqtt")
lis := mqttnet.NewListener(listener, protocol)
go func() {
<-ctx.Done()
lis.Close()
}()
return mqtt.RunListener(
ctx, lis, server,
ratelimit.ApplicationAcceptMQTTConnectionResource, server.RateLimiter(),
func(ctx context.Context, mqttConn mqttnet.Conn) error {
return setupConnection(ctx, mqttConn, format, server)
},
)
}
type connection struct {
format Format
server io.Server
io *io.Subscription
resource ratelimit.Resource
}
func setupConnection(ctx context.Context, mqttConn mqttnet.Conn, format Format, server io.Server) error {
c := &connection{
format: format,
server: server,
}
ctx = auth.NewContextWithInterface(ctx, c)
session := session.New(ctx, mqttConn, c.deliver)
if err := session.ReadConnect(); err != nil {
if c.io != nil {
c.io.Disconnect(err)
}
return err
}
ctx = c.io.Context()
wg := &sync.WaitGroup{}
wg.Add(1)
f := func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case up := <-c.io.Up():
logger := log.FromContext(ctx).WithField("device_uid", unique.ID(up.Context, up.EndDeviceIds))
topicParts := TopicParts(up, format)
if topicParts == nil {
continue
}
buf, err := format.FromUp(up.ApplicationUp)
if err != nil {
logger.WithError(err).Warn("Failed to marshal upstream message")
continue
}
topicName := topic.Join(topicParts)
logger.WithField("topic", topicName).Debug("Publish upstream message")
session.Publish(&packet.PublishPacket{
TopicName: topicName,
TopicParts: topicParts,
QoS: qosUpstream,
Message: buf,
})
}
}
}
server.StartTask(&task.Config{
Context: ctx,
ID: "mqtt_publish_uplinks",
Func: f,
Done: wg.Done,
Restart: task.RestartNever,
Backoff: task.DefaultBackoffConfig,
})
mqtt.RunSession(ctx, c.io.Disconnect, server, session, mqttConn, wg)
return nil
}
type topicAccess struct {
appUID string
reads [][]string
writes [][]string
}
func (c *connection) Connect(ctx context.Context, info *auth.Info) (_ context.Context, err error) {
ids := &ttnpb.ApplicationIdentifiers{
ApplicationId: info.Username,
}
if err := ids.ValidateContext(ctx); err != nil {
return nil, err
}
md := metadata.New(map[string]string{
"id": ids.ApplicationId,
"authorization": fmt.Sprintf("Bearer %s", info.Password),
})
if ctxMd, ok := metadata.FromIncomingContext(ctx); ok {
md = metadata.Join(ctxMd, md)
}
ctx = metadata.NewIncomingContext(ctx, md)
ctx = c.server.FillContext(ctx)
uid := unique.ID(ctx, ids)
ctx = log.NewContextWithField(ctx, "application_uid", uid)
defer func() {
if err != nil {
registerConnectFail(ctx, ids, err)
}
switch {
case errors.IsPermissionDenied(err):
err = packet.ConnectNotAuthorized
case errors.IsResourceExhausted(err):
err = packet.ConnectServerUnavailable
}
}()
if err := rights.RequireApplication(ctx, ids); err != nil {
return nil, err
}
c.io, err = c.server.Subscribe(ctx, "mqtt", ids, true)
if err != nil {
return nil, err
}
ctx = c.io.Context()
authTokenID := ""
if _, v, _, err := ttsauth.SplitToken(string(info.Password)); err == nil && v != "" {
authTokenID = v
}
c.resource = ratelimit.ApplicationMQTTDownResource(ctx, ids, authTokenID)
access := topicAccess{
appUID: uid,
}
if err := rights.RequireApplication(ctx, ids, ttnpb.Right_RIGHT_APPLICATION_TRAFFIC_READ); err == nil {
access.reads = append(access.reads,
c.format.UplinkMessageTopic(uid, topic.PartWildcard),
c.format.UplinkNormalizedTopic(uid, topic.PartWildcard),
c.format.JoinAcceptTopic(uid, topic.PartWildcard),
c.format.DownlinkAckTopic(uid, topic.PartWildcard),
c.format.DownlinkNackTopic(uid, topic.PartWildcard),
c.format.DownlinkSentTopic(uid, topic.PartWildcard),
c.format.DownlinkFailedTopic(uid, topic.PartWildcard),
c.format.DownlinkQueuedTopic(uid, topic.PartWildcard),
c.format.DownlinkQueueInvalidatedTopic(uid, topic.PartWildcard),
c.format.LocationSolvedTopic(uid, topic.PartWildcard),
c.format.ServiceDataTopic(uid, topic.PartWildcard),
)
}
if err := rights.RequireApplication(ctx, ids, ttnpb.Right_RIGHT_APPLICATION_TRAFFIC_DOWN_WRITE); err == nil {
access.writes = append(access.writes,
c.format.DownlinkPushTopic(uid, topic.PartWildcard),
c.format.DownlinkReplaceTopic(uid, topic.PartWildcard),
)
}
info.Metadata = access
info.Interface = c
return ctx, nil
}
var errNotAuthorized = errors.DefinePermissionDenied("not_authorized", "not authorized")
func (c *connection) Subscribe(info *auth.Info, requestedTopic string, requestedQoS byte) (acceptedTopic string, acceptedQoS byte, err error) {
access := info.Metadata.(topicAccess)
accepted, ok := c.format.AcceptedTopic(access.appUID, topic.Split(requestedTopic))
if !ok {
return "", 0, errNotAuthorized.New()
}
return topic.Join(accepted), requestedQoS, nil
}
func (c *connection) CanRead(info *auth.Info, topicParts ...string) bool {
access := info.Metadata.(topicAccess)
for _, reads := range access.reads {
if topic.MatchPath(topicParts, reads) {
return true
}
}
return false
}
func (c *connection) CanWrite(info *auth.Info, topicParts ...string) bool {
access := info.Metadata.(topicAccess)
for _, writes := range access.writes {
if topic.MatchPath(topicParts, writes) {
return true
}
}
return false
}
func (c *connection) deliver(pkt *packet.PublishPacket) {
logger := log.FromContext(c.io.Context()).WithField("topic", pkt.TopicName)
if err := ratelimit.Require(c.server.RateLimiter(), c.resource); err != nil {
logger.WithError(err).Warn("Terminate connection")
c.io.Disconnect(err)
return
}
var deviceID string
var op func(io.Server, context.Context, *ttnpb.EndDeviceIdentifiers, []*ttnpb.ApplicationDownlink) error
switch {
case c.format.IsDownlinkPushTopic(pkt.TopicParts):
deviceID = c.format.ParseDownlinkPushTopic(pkt.TopicParts)
op = io.Server.DownlinkQueuePush
case c.format.IsDownlinkReplaceTopic(pkt.TopicParts):
deviceID = c.format.ParseDownlinkReplaceTopic(pkt.TopicParts)
op = io.Server.DownlinkQueueReplace
default:
logger.Error("Invalid topic path")
return
}
items, err := c.format.ToDownlinks(pkt.Message)
if err != nil {
logger.WithError(err).Warn("Failed to decode downlink messages")
return
}
if err := items.ValidateFields(); err != nil {
logger.WithError(err).Warn("Failed to validate downlink messages")
return
}
ids := &ttnpb.EndDeviceIdentifiers{
ApplicationIds: c.io.ApplicationIDs(),
DeviceId: deviceID,
}
if err := ids.ValidateContext(c.io.Context()); err != nil {
logger.WithError(err).Warn("Failed to validate message identifiers")
return
}
logger.WithFields(log.Fields(
"device_uid", unique.ID(c.io.Context(), ids),
"count", len(items.Downlinks),
)).Debug("Handle downlink messages")
if err := op(c.server, c.io.Context(), ids, items.Downlinks); err != nil {
logger.WithError(err).Warn("Failed to handle downlink messages")
}
}