forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws.go
More file actions
433 lines (392 loc) · 12.6 KB
/
ws.go
File metadata and controls
433 lines (392 loc) · 12.6 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// 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 semtechws implements a common interface for Semtech web socket frontends.
package semtechws
import (
"context"
"encoding/hex"
"fmt"
"net"
"net/http"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"go.thethings.network/lorawan-stack/v3/pkg/auth/rights"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/frequencyplans"
"go.thethings.network/lorawan-stack/v3/pkg/gatewayserver/io"
"go.thethings.network/lorawan-stack/v3/pkg/gatewayserver/scheduling"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/random"
"go.thethings.network/lorawan-stack/v3/pkg/ratelimit"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/types"
"go.thethings.network/lorawan-stack/v3/pkg/unique"
"go.thethings.network/lorawan-stack/v3/pkg/web"
"go.thethings.network/lorawan-stack/v3/pkg/webhandlers"
"google.golang.org/grpc/metadata"
)
const pingIntervalJitter = 0.1
var (
errGatewayID = errors.DefineInvalidArgument("invalid_gateway_id", "invalid gateway ID `{id}`")
errNoAuthProvided = errors.DefineUnauthenticated("no_auth_provided", "no auth provided `{uid}`")
errMissedTooManyPongs = errors.Define("missed_too_many_pongs", "gateway missed too many pongs")
)
type srv struct {
ctx context.Context
server io.Server
upgrader *websocket.Upgrader
cfg Config
formatter Formatter
}
func (s *srv) Protocol() string { return "semtechws/" + s.formatter.ID() }
func (*srv) SupportsDownlinkClaim() bool { return false }
func (*srv) DutyCycleStyle() scheduling.DutyCycleStyle {
return scheduling.DutyCycleStyleBlockingWindow
}
// New creates a new WebSocket frontend.
func New(ctx context.Context, server io.Server, formatter Formatter, cfg Config) (*web.Server, error) {
ctx = log.NewContextWithField(ctx, "namespace", "gatewayserver/io/semtechws")
s := &srv{
ctx: ctx,
server: server,
upgrader: &websocket.Upgrader{
HandshakeTimeout: 120 * time.Second,
WriteBufferPool: &sync.Pool{},
Error: func(w http.ResponseWriter, r *http.Request, _ int, err error) {
webhandlers.Error(w, r, err)
},
},
formatter: formatter,
cfg: cfg,
}
w, err := web.New(
ctx,
web.WithDisableWarnings(true),
web.WithTrustedProxies(server.GetBaseConfig(ctx).HTTP.TrustedProxies...),
)
if err != nil {
return nil, err
}
router := w.RootRouter()
router.Use(
ratelimit.HTTPMiddleware(server.RateLimiter(), "gs:accept:"+s.Protocol()),
)
eps := s.formatter.Endpoints()
router.HandleFunc(eps.ConnectionInfo, s.handleConnectionInfo).Methods(http.MethodGet)
router.HandleFunc(eps.Traffic, func(w http.ResponseWriter, r *http.Request) {
if err := s.handleTraffic(w, r); err != nil {
webhandlers.Error(w, r, err)
}
}).Methods(http.MethodGet)
return w, nil
}
func (s *srv) handleConnectionInfo(w http.ResponseWriter, r *http.Request) {
eps := s.formatter.Endpoints()
ctx := log.NewContextWithFields(r.Context(), log.Fields(
"endpoint", eps.ConnectionInfo,
"remote_addr", r.RemoteAddr,
))
logger := log.FromContext(ctx)
assertAuth := func(ctx context.Context, ids *ttnpb.GatewayIdentifiers) error {
ctx, hasAuth := withForwardedAuth(ctx, ids, r.Header.Get("Authorization"))
if !hasAuth {
if !s.cfg.AllowUnauthenticated {
return errNoAuthProvided.WithAttributes("uid", unique.ID(ctx, ids))
}
return nil
}
return s.server.AssertGatewayRights(ctx, ids, ttnpb.Right_RIGHT_GATEWAY_LINK)
}
ws, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
logger.WithError(err).Debug("Failed to upgrade request to websocket connection")
return
}
defer ws.Close()
_, data, err := ws.ReadMessage()
if err != nil {
logger.WithError(err).Debug("Failed to read message")
return
}
var (
scheme = "ws"
port = "80"
)
if r.TLS != nil || s.cfg.UseTrafficTLSAddress {
scheme = "wss"
port = "443"
}
// If port is retrievable from the host, use it.
host, p, err := net.SplitHostPort(r.Host)
if err == nil {
// Both `host` and `p` are valid, since we have no error.
port = p
} else {
// `host` and `p` are unknown/empty. Reset `host` to `r.Host` since it does not contain the port.
host = r.Host
}
info := ServerInfo{
Scheme: scheme,
Address: net.JoinHostPort(host, port),
}
resp := s.formatter.HandleConnectionInfo(ctx, data, s.server, info, assertAuth)
if err := ws.WriteMessage(websocket.TextMessage, resp); err != nil {
logger.WithError(err).Warn("Failed to write connection info response message")
return
}
logger.Debug("Sent connection info response message")
}
var euiHexPattern = regexp.MustCompile("^eui-([a-f0-9A-F]{16})$")
func (s *srv) handleTraffic(w http.ResponseWriter, r *http.Request) (err error) {
var (
id = mux.Vars(r)["id"]
auth = r.Header.Get("Authorization")
ctx = r.Context()
eps = s.formatter.Endpoints()
missingPongs = int64(0)
pongCount = int64(0)
pongCh = make(chan []byte, 1)
downstreamCh = make(chan []byte, 1)
)
ctx = log.NewContextWithFields(ctx, log.Fields(
"endpoint", eps.Traffic,
"remote_addr", r.RemoteAddr,
))
ctx = NewContextWithSession(ctx, &Session{})
// Convert the ID to EUI.
str := euiHexPattern.FindStringSubmatch(id)
if len(str) != 2 {
return errGatewayID.WithAttributes("id", id)
}
hexValue, err := hex.DecodeString(str[1])
if err != nil {
return errGatewayID.WithAttributes("id", id)
}
var eui types.EUI64
eui.UnmarshalBinary(hexValue)
ctx, ids, err := s.server.FillGatewayContext(ctx, &ttnpb.GatewayIdentifiers{Eui: eui.Bytes()})
if err != nil {
return err
}
uid := unique.ID(ctx, ids)
ctx = log.NewContextWithField(ctx, "gateway_uid", uid)
// If a fallback frequency is defined in the server context, inject it into local the context.
if fallback, ok := frequencyplans.FallbackIDFromContext(s.ctx); ok {
ctx = frequencyplans.WithFallbackID(ctx, fallback)
}
ctx, hasAuth := withForwardedAuth(ctx, ids, auth)
if !hasAuth {
if !s.cfg.AllowUnauthenticated {
// We error here directly as there is no auth.
return errNoAuthProvided.WithAttributes("uid", uid)
}
// If the server allows unauthenticated connections (for local testing), we provide the link rights ourselves.
ctx = rights.NewContext(ctx, &rights.Rights{
GatewayRights: *rights.NewMap(map[string]*ttnpb.Rights{
uid: {
Rights: []ttnpb.Right{ttnpb.Right_RIGHT_GATEWAY_LINK},
},
}),
})
}
logger := log.FromContext(ctx)
addr, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return err
}
if xRealIP := r.Header[http.CanonicalHeaderKey("X-Real-IP")]; len(xRealIP) == 1 {
addr = xRealIP[0]
}
conn, err := s.server.Connect(ctx, s, ids, &ttnpb.GatewayRemoteAddress{
Ip: addr,
})
if err != nil {
logger.WithError(err).Warn("Failed to connect")
return err
}
defer func() {
conn.Disconnect(err)
err = nil // Errors are sent over the websocket connection that is established by this point.
}()
ws, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
logger.WithError(err).Debug("Failed to upgrade request to websocket connection")
return err
}
defer ws.Close()
var pingTickerC <-chan time.Time
if s.cfg.MissedPongThreshold > 0 && random.CanJitter(s.cfg.WSPingInterval, pingIntervalJitter) {
pingTicker := time.NewTicker(random.Jitter(s.cfg.WSPingInterval, pingIntervalJitter))
pingTickerC = pingTicker.C
defer pingTicker.Stop()
}
ws.SetPingHandler(func(data string) error {
logger.Debug("Received client ping")
select {
case <-ctx.Done():
return ctx.Err()
case pongCh <- []byte(data):
}
return nil
})
// Not all gateways support pongs to the server's pings.
ws.SetPongHandler(func(data string) error {
logger.Debug("Received client pong")
for n := atomic.LoadInt64(&missingPongs); ; n = atomic.LoadInt64(&missingPongs) {
if n == 0 {
logger.Warn("Unsolicited client pong")
return nil
}
if atomic.CompareAndSwapInt64(&missingPongs, n, n-1) {
break
}
}
atomic.AddInt64(&pongCount, 1)
return nil
})
var timeSyncTickerC <-chan time.Time
if s.cfg.TimeSyncInterval > 0 {
ticker := time.NewTicker(random.Jitter(s.cfg.TimeSyncInterval, 0.1))
timeSyncTickerC = ticker.C
defer ticker.Stop()
}
// Store per-gateway downlink tokens to correlate with Tx acknowledgement messages.
downlinkTokens := &io.DownlinkTokens{}
go func() (err error) {
defer ws.Close()
defer func() {
if err != nil {
conn.Disconnect(err)
}
}()
for {
select {
case <-conn.Context().Done():
return
case <-pingTickerC:
if atomic.AddInt64(&missingPongs, 1) > int64(s.cfg.MissedPongThreshold) &&
atomic.LoadInt64(&pongCount) > 0 {
err := errMissedTooManyPongs.New()
logger.WithError(err).Warn("Gateway missed too many pings")
return err
}
if err := ws.WriteControl(websocket.PingMessage, nil, time.Time{}); err != nil {
logger.WithError(err).Warn("Failed to send ping message")
return err
}
logger.Debug("Server ping sent")
case data := <-pongCh:
if err := ws.WriteControl(websocket.PongMessage, data, time.Time{}); err != nil {
logger.WithError(err).Warn("Failed to send pong")
return err
}
logger.Debug("Server pong sent")
case <-timeSyncTickerC:
// Send LNS-initiated time transfer using xtime+gpstime from recent uplinks.
// References https://github.com/TheThingsNetwork/lorawan-stack/issues/4852
var gpsTime *time.Time
var concentratorTime *scheduling.ConcentratorTime
if xtime, gpstime, rxAt, ok := GetLastUplink(ctx); ok {
if time.Since(rxAt) < 10*time.Second {
elapsed := time.Since(rxAt)
extrapolatedXTime := xtime + int64(elapsed/time.Microsecond)
ct := ConcentratorTimeFromXTime(extrapolatedXTime)
concentratorTime = &ct
if gpstime != 0 {
gt := TimeFromGPSTime(gpstime).Add(elapsed)
gpsTime = >
}
}
}
b, err := s.formatter.TransferTime(ctx, time.Now(), gpsTime, concentratorTime)
if err != nil {
logger.WithError(err).Warn("Failed to generate time transfer")
return err
}
if b == nil {
continue
}
if err := ws.WriteMessage(websocket.TextMessage, b); err != nil {
logger.WithError(err).Warn("Failed to transfer time")
return err
}
case down := <-conn.Down():
dnmsg, err := s.formatter.FromDownlink(ctx, down, conn.BandID(), time.Now(), downlinkTokens)
if err != nil {
logger.WithError(err).Warn("Failed to marshal downlink message")
continue
}
if err = ws.WriteMessage(websocket.TextMessage, dnmsg); err != nil {
logger.WithError(err).Warn("Failed to send downlink message")
return err
}
case downstream := <-downstreamCh:
if err := ws.WriteMessage(websocket.TextMessage, downstream); err != nil {
logger.WithError(err).Warn("Failed to send message downstream")
return err
}
}
}
}()
resource := ratelimit.GatewayUpResource(ctx, ids)
for {
if err := ratelimit.Require(s.server.RateLimiter(), resource); err != nil {
logger.WithError(err).Warn("Terminate connection")
return err
}
_, data, err := ws.ReadMessage()
if err != nil {
logger.WithError(err).Debug("Failed to read message")
return err
}
downstream, err := s.formatter.HandleUp(ctx, data, ids, conn, time.Now(), downlinkTokens)
if err != nil {
return err
}
if downstream == nil {
continue
}
logger.Info("Send downstream message")
select {
case <-ctx.Done():
return ctx.Err()
case downstreamCh <- downstream:
}
}
}
func withForwardedAuth(ctx context.Context, ids *ttnpb.GatewayIdentifiers, auth string) (context.Context, bool) {
var md metadata.MD
var hasAuth bool
if auth != "" {
if !strings.HasPrefix(auth, "Bearer ") {
auth = fmt.Sprintf("Bearer %s", auth)
}
m := map[string]string{"authorization": auth}
if ids != nil {
m["id"] = ids.GatewayId
}
md = metadata.New(m)
if ctxMd, ok := metadata.FromIncomingContext(ctx); ok {
md = metadata.Join(ctxMd, md)
}
ctx = metadata.NewIncomingContext(ctx, md)
hasAuth = true
}
return ctx, hasAuth
}