-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
369 lines (316 loc) · 8.99 KB
/
Copy pathproxy.go
File metadata and controls
369 lines (316 loc) · 8.99 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
package main
// TODO: The big ticket items missing are:
// - Persist data somehow?
// - Expiry of messages with an expiry attribute
import (
"encoding/json"
"errors"
"flag"
"io"
"io/ioutil"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/sirupsen/logrus"
"nhooyr.io/websocket"
)
const (
url1 = "https://warnung.bund.de/bbk.mowas/gefahrendurchsagen.json"
url2 = "https://warnung.bund.de/bbk.biwapp/warnmeldungen.json"
url3 = "https://warnung.bund.de/bbk.dwd/unwetter.json"
// TODO: Handle these as well, need to see them in action
url4 = "https://warnung.bund.de/bbk.lhp/hochwassermeldungen.json"
)
var (
_updateDelay time.Duration
_socketPath string
_socketAddr string
_logLevel string
_logCallers bool
)
func init() {
flag.DurationVar(&_updateDelay, "updateDelay", 30*time.Second, "Intervall between polling for new data")
flag.StringVar(&_socketPath, "socketPath", "/coords", "Path to websocket")
flag.StringVar(&_socketAddr, "socketAddr", ":8080", "Address to listen on for websocket connections")
flag.StringVar(&_logLevel, "logLevel", "info", "Log level to use")
flag.BoolVar(&_logCallers, "logCallers", false, "Whether to log callers")
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
}
// Client wraps a proxy reference and a logger for convenient access in the client's main loop
type Client struct {
p *Proxy
log atomic.Value
}
func (c *Client) Log() *logrus.Entry {
return c.log.Load().(*logrus.Entry)
}
func (c *Client) SetLog(l *logrus.Entry) {
c.log.Store(l)
}
// getMatchingAlerts returns all alerts that have areas affecting the provided coordinate
// This function is really fucking ugly.
func (cl *Client) getMatchingAlerts(c Coordinate) []alertMessage {
p := cl.p
p.Lock()
defer p.Unlock()
messageIDs := make(map[MessageID]bool)
cl.Log().WithField("numareas", len(p.areas)).Debug("checking area collections")
for id, areas := range p.areas {
cl.Log().WithField("numareas", len(areas)).Println("checking area(s)")
for _, area := range areas {
if area.Contains(c) {
messageIDs[id] = true
}
}
}
cl.Log().WithFields(logrus.Fields{
"count": len(messageIDs),
"ids": messageIDs,
}).Debug("got matching message IDs")
// Create empty list. This doesn't use the `nil` pattern for new slices because those encode to `null` values in JSON.
matchingAlerts := make([]alertMessage, 0)
for id := range messageIDs {
// Gather all messages
for _, alerts := range p.activeAlerts {
if alert, ok := alerts[id]; ok {
matchingAlerts = append(matchingAlerts, alert)
}
}
}
return matchingAlerts
}
type Proxy struct {
sync.Mutex
// Maps URLs to active messages, keyed by message ID
activeAlerts map[URL]map[MessageID]alertMessage
updateChans map[chan bool]bool
areas map[MessageID][]Area
}
func newProxy() Proxy {
return Proxy{
activeAlerts: make(map[URL]map[MessageID]alertMessage),
updateChans: make(map[chan bool]bool),
areas: make(map[MessageID][]Area),
}
}
func (p *Proxy) registerUpdateChan(ch chan bool) {
p.Lock()
defer p.Unlock()
p.updateChans[ch] = true
}
func (p *Proxy) unregisterUpdateChan(ch chan bool) {
p.Lock()
defer p.Unlock()
delete(p.updateChans, ch)
}
// socketHandler runs a client connection
func (p *Proxy) socketHandler(w http.ResponseWriter, r *http.Request) {
client := Client{
p: p,
}
client.SetLog(logrus.WithFields(logrus.Fields{
"component": "client",
"remote": r.RemoteAddr,
}))
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{InsecureSkipVerify: true})
if err != nil {
client.Log().Error("Failed to set up websocket:", err)
return
}
defer conn.Close(websocket.StatusInternalError, "internal server error")
// When a user sends a Lat/Lon pair, check active alerts on p
// Otherwise, watch the active alerts for new things
quit := make(chan interface{})
done := make(chan interface{})
coords := make(chan Coordinate)
updateChan := make(chan bool)
p.registerUpdateChan(updateChan)
defer p.unregisterUpdateChan(updateChan)
// First part, run a goroutine to await changes on the active alerts
go func() {
var currentCoords Coordinate
coordsSet := false // True if coordinates have been received
running := true
client.Log().Info("Waiting for changes in active alerts or coordinates")
for running {
writer, err := conn.Writer(r.Context(), websocket.MessageText)
if err != nil {
client.Log().Error("can't set up writer:", err)
return
}
enc := json.NewEncoder(writer)
select {
case c := <-coords:
currentCoords = c
coordsSet = true
client.SetLog(client.Log().WithField("coordinate", c))
client.Log().Info("Received new coordinate")
alerts := client.getMatchingAlerts(c)
enc.Encode(&alerts)
case <-updateChan:
if coordsSet {
alerts := client.getMatchingAlerts(currentCoords)
enc.Encode(&alerts)
} else {
client.Log().Info("not checking update, no coords set")
}
case <-quit:
running = false
}
writer.Close()
}
client.Log().Info("Active alert watcher quitting")
close(done)
}()
// Consume from the websocket tow gather new lat/lon pairs, exit on first error
for {
mt, reader, err := conn.Reader(r.Context())
if err != nil {
client.Log().Error("Failed to read from websocket:", err)
break
}
if mt != websocket.MessageText {
// Consume all non-text message and drop them
client.Log().Debug("Non-text message received")
for {
buf := make([]byte, 32)
_, err := reader.Read(buf)
if err != nil {
client.Log().Error("Failed to read from websocket:", err)
break
}
client.Log().WithField("part", buf).Debug("message part consumed")
}
continue
}
for {
dec := json.NewDecoder(reader)
var coord Coordinate
err = dec.Decode(&coord)
if err != nil {
if errors.Is(err, io.EOF) {
client.Log().Error("Error while decoding:", err)
}
break
}
// Update coordinates and re-check active alerts
coords <- coord
}
}
// Signal goroutine that it's time to go
close(quit)
// ... and wait for it to exit
<-done
conn.Close(websocket.StatusNormalClosure, "")
}
// updateData requests new data from url and updates the stored alert messages. It returns true if an update was performed, and
// false if no new data arrived
//
// It requires p to be locked.
func (p *Proxy) updateData(url URL) (bool, error) {
if _, ok := p.activeAlerts[url]; !ok {
p.activeAlerts[url] = make(map[MessageID]alertMessage)
}
resp, err := http.Get(string(url))
if err != nil {
return false, err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
var alerts []alertMessage
err = json.Unmarshal(content, &alerts)
if err != nil {
return false, err
}
// Track new alerts
newAlerts := 0
for _, alert := range alerts {
// Update messages for this url
if _, ok := p.activeAlerts[url][alert.Identifier]; !ok {
// This message is new, track it.
newAlerts++
}
}
// Set new active message map
p.activeAlerts[url] = make(map[MessageID]alertMessage)
for _, message := range alerts {
p.activeAlerts[url][message.Identifier] = message
// Collect all areas for this message
var areas []Area
for _, info := range message.Info {
for _, area := range info.Area {
for _, poly := range area.Polygon {
a, err := NewAreaFromString(poly)
if err != nil {
return false, err
}
areas = append(areas, a)
}
}
}
p.areas[message.Identifier] = areas
}
return newAlerts != 0, nil
}
// updateLoop polls all URLs and updates the proxy state. On update, it checks subscribed customers for area containment and notify
// them.
func (p *Proxy) updateLoop() {
log := logrus.WithField("component", "updater")
urls := []URL{url1, url2, url3, url4}
ticker := time.NewTicker(_updateDelay)
for {
needUpdate := false
p.Lock()
p.areas = make(map[MessageID][]Area)
for _, url := range urls {
newData, err := p.updateData(url)
if err != nil {
log.WithFields(logrus.Fields{
"url": url,
"err": err}).Error("update failed")
continue
}
log.WithField("url", url).Debug("data refreshed")
needUpdate = needUpdate || newData
}
if needUpdate {
log.Info("Notifying connected clients of updates")
// Non-blocking notify to make sure slow clients don't block us
for ch := range p.updateChans {
select {
case ch <- true:
default:
}
}
}
p.Unlock()
log.WithField("delay", _updateDelay).Debug("waiting for next update")
<-ticker.C
}
}
func main() {
flag.Parse()
lvl, err := logrus.ParseLevel(_logLevel)
if err != nil {
logrus.Fatalln("Can't parse log level:", err)
}
logrus.SetReportCaller(_logCallers)
logrus.SetLevel(lvl)
logrus.Info("Starting up")
proxy := newProxy()
go proxy.updateLoop()
http.HandleFunc(_socketPath, proxy.socketHandler)
http.Handle("/", http.FileServer(http.Dir("static")))
logrus.Info("Handlers configured, app started")
err = http.ListenAndServe(_socketAddr, nil)
if err != nil {
logrus.Fatalln("failed to start web socket server:", err)
}
}