-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice.go
384 lines (322 loc) · 10.5 KB
/
device.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
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
// Copyright 2018 Dan Jacques. All rights reserved.
// Use of this source code is governed under the MIT License
// that can be found in the LICENSE file.
package proxy
import (
"fmt"
"net"
"sync"
"time"
"github.com/danjacques/gopushpixels/device"
"github.com/danjacques/gopushpixels/protocol"
"github.com/danjacques/gopushpixels/support/bufferpool"
"github.com/danjacques/gopushpixels/support/byteslicereader"
"github.com/danjacques/gopushpixels/support/fmtutil"
"github.com/danjacques/gopushpixels/support/logging"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
// Device is a device.D that proxies for another device. Data written to this
// device can be sent to the proxied base device and/or captured, enabling
// man-in-the-middle operations on devices.
//
// Device should not be instantiated directly; instead, it should be obtained
// through a Manager.
type Device struct {
// m is this device's proxy manager.
m *Manager
// logger is the resolved logger instance.
logger logging.L
// proxy is the local (proxy) device that the proxy endpoint is using.
proxy device.Local
// proxyHWAddr is the hardware address assigned to this proxy device. This is
// a fake address, generated by AddressRegistry.
proxyHWAddr net.HardwareAddr
// monitoring is the device monitoring state.
monitoring device.Monitoring
// base is the base device that Device is proxying for.
base device.D
// baseID is base's cached ID.
baseID string
// createdTime is the time when this device was created.
createdTime time.Time
// doneC is this Device's done channel. It will close when either the device
// has closed (explicit) or the device's base has closed (see
// closeWhenBaseCloses()).
doneC chan struct{}
shutdownOnce sync.Once
basePacketsC chan *packetData
listenerPacketsC chan *packetData
wg sync.WaitGroup
infoMu sync.Mutex
info device.Info
counterLabels prometheus.Labels
}
// makeProxyDevice allocates a new proxy device with the assigned address,
// hwAddr.
//
// The device will proxy received data to d, and leverage d's discovery headers
// when constructing its own.
//
// Any packets received will be sent to l, the set of registered listeners for
// this device.
//
// The proxy device will close and terminate once the supplied Context is
// closed.
func makeProxyDevice(m *Manager, d device.D, hwAddr net.HardwareAddr) (*Device, error) {
const chanSize = 1024
// Create a PacketReader for our base device.
dh := d.DiscoveryHeaders()
basePR, err := dh.PacketReader()
if err != nil {
return nil, errors.Wrap(err, "could not create PacketReader")
}
// Open a listening socket for this device.
conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: m.ProxyAddr})
if err != nil {
return nil, err
}
defer func() {
if conn != nil {
_ = conn.Close()
}
}()
// Create a monitored Sender.
s, err := d.Sender()
if err != nil {
return nil, err
}
pd := Device{
m: m,
logger: logging.Must(m.Logger),
proxy: device.Local{
DeviceID: hwAddr.String(),
UDPPacketPool: m.udpPacketPool,
Logger: m.Logger,
},
proxyHWAddr: hwAddr,
base: d,
baseID: d.ID(),
createdTime: time.Now(),
doneC: make(chan struct{}),
basePacketsC: make(chan *packetData, chanSize),
listenerPacketsC: make(chan *packetData, chanSize),
}
// Connect our callback and start our local listener.
pd.proxy.OnPacketData = pd.onLocalPacketData
pd.proxy.Start(conn)
conn = nil // Owned by pd.proxy.
// Generate/cache our metric labels for this device.
pd.counterLabels = prometheus.Labels{
"proxy_id": pd.proxy.DeviceID,
"base_id": pd.baseID,
}
// Watch base and close the proxy when it closes.
go pd.closeWhenBaseCloses()
// Process packets from the local Proxy connection and dispatch them to our
// base device.
//
// This passes ownership of pw to the goroutine.
pd.wg.Add(1)
go func() {
defer pd.wg.Done()
pd.forwardPacketsToBase(s)
}()
// Process packets from the local Proxy connection and dispatch them to our
// listeners.
//
// We do this independently from Router dispatching in order to buffer router
// throughput from immediate Listener hiccups.
pd.wg.Add(1)
go func() {
defer pd.wg.Done()
pd.forwardPacketsToListeners(basePR)
}()
// Update our device monitoring.
pd.monitoring.Update(&pd)
return &pd, nil
}
func (pd *Device) String() string {
return fmt.Sprintf("proxy.Device{%s proxying %s}", pd.proxy.DeviceID, pd.baseID)
}
// ID implements device.D.
func (pd *Device) ID() string { return pd.proxy.DeviceID }
// Ordinal implements device.D.
func (pd *Device) Ordinal() device.Ordinal {
o := pd.base.Ordinal()
o.Group += int(pd.m.GroupOffset)
return o
}
// Sender implements device.D.
func (pd *Device) Sender() (device.Sender, error) { return pd.base.Sender() }
// DiscoveryHeaders implements device.D.
func (pd *Device) DiscoveryHeaders() *protocol.DiscoveryHeaders {
proxyAddr := pd.proxy.Addr().(*net.UDPAddr)
// Permute the current base proxy headers.
dh := pd.base.DiscoveryHeaders().Clone()
dh.SetIP4Address(proxyAddr.IP)
dh.SetHardwareAddr(pd.proxyHWAddr)
if dh.PixelPusher != nil {
ordinal := pd.Ordinal()
dh.PixelPusher.MyPort = uint16(proxyAddr.Port)
dh.PixelPusher.GroupOrdinal = int32(ordinal.Group)
}
return dh
}
// DoneC implements device.D.
//
// A Proxy device is done when its base's DoneC has been closed.
func (pd *Device) DoneC() <-chan struct{} { return pd.doneC }
// Addr implements device.D.
func (pd *Device) Addr() net.Addr { return pd.proxy.Addr() }
// Info implements device.D.
func (pd *Device) Info() (i device.Info) {
pd.modInfo(func(di *device.Info) {
i = device.Info{
PacketsReceived: di.PacketsReceived,
BytesReceived: di.BytesReceived,
PacketsSent: di.PacketsSent,
BytesSent: di.BytesSent,
Created: pd.createdTime,
Observed: pd.createdTime,
}
})
return
}
// Proxied returns the base device that pd is proxying for.
func (pd *Device) Proxied() device.D { return pd.base }
// onLocalPacketData is called when pd.Local's callback receives data.
func (pd *Device) onLocalPacketData(buf *bufferpool.Buffer) {
// Update our received metrics.
pd.modInfo(func(di *device.Info) {
di.PacketsReceived++
di.BytesReceived += int64(buf.Len())
})
proxyReceivedPackets.With(pd.counterLabels).Inc()
proxyReceivedBytes.With(pd.counterLabels).Add(float64(buf.Len()))
// Dispatch the raw buffer to the proxied base, unless we're not forwarding.
pkt := packetData{
Buffer: buf,
forwarded: pd.m.Forwarding(),
}
if pkt.forwarded {
buf.Retain()
pd.basePacketsC <- &pkt
} else {
pd.logger.Debugf("NOT forwarding packet (size %d) to proxy device %v (forwarding is disabled).",
buf.Len(), pd.baseID)
}
// Send the packet to our listeners channel.
buf.Retain()
pd.listenerPacketsC <- &pkt
}
// forwardPacketsToBase is run in its own goroutine. Its job is to forward raw
// packet data from basePacketsC to the base device immediately.
//
// forwardPacketsToBase takes ownership of pw, and will close it on exit.
func (pd *Device) forwardPacketsToBase(s device.Sender) {
defer func() {
if err := s.Close(); err != nil {
pd.logger.Warnf("Failed to close %q Sender: %s", pd.baseID, err)
}
}()
for pkt := range pd.basePacketsC {
// Dispatch the packet to our underlying device.
//
// We do this in a separate scope so we can ensure that we release the
// buffer on completion.
func() {
defer pkt.Release()
if err := s.SendDatagram(pkt.Bytes()); err != nil {
pd.logger.Warnf("Failed to forward packet from proxy device %q for: %s", pd.proxy.DeviceID, err)
proxyForwardErrors.With(pd.counterLabels).Inc()
return
}
// Update our sent metrics.
pd.modInfo(func(di *device.Info) {
di.PacketsSent++
di.BytesSent += int64(pkt.Len())
})
proxySentPackets.With(pd.counterLabels).Inc()
proxySentBytes.With(pd.counterLabels).Add(float64(pkt.Len()))
}()
}
}
// forwardPacketsToListeners is run in its own goroutine. Its job is to forward
// packets to registered listeners.
func (pd *Device) forwardPacketsToListeners(pr *protocol.PacketReader) {
var parsed protocol.Packet
for pkt := range pd.listenerPacketsC {
// Do we have registered listeners? If not, ignore this packet.
//
// NOTE: There is a "race" here between this decision and actually sending
// the packet; however, losing the race just means dropping a packet, so
// this is fine.
if !pd.m.hasListeners() {
continue
}
// Parse the packet and send it to listeners.
//
// We do this in a separate scope so we can release the buffer on
// completion.
func() {
defer pkt.Release()
// Parse the Packet.
//
// NOTE the parsed packet may contain references to the underlying
// buffer. The buffer must not be released until handling is finished.
bsr := byteslicereader.R{Buffer: pkt.Bytes()}
if err := pr.ReadPacket(&bsr, &parsed); err != nil {
// Release ownership of the buffer, opening it up for reuse.
pd.logger.Warnf("Discarding unknown packet for %q: %s", pd.proxy.DeviceID, err)
pd.logger.Debugf("Discarded packet contents:\n%s", fmtutil.Hex(pkt.Bytes()))
return
}
pd.m.sendPacketToListeners(pd.base, &parsed, pkt.forwarded)
}()
}
}
func (pd *Device) closeWhenBaseCloses() {
// Wait for either this device's Context to close, or its base device to
// close.
select {
case <-pd.doneC:
// Shutdown externally.
case <-pd.base.DoneC():
// Our base device has closed.
pd.shutdown()
}
}
func (pd *Device) shutdown() {
pd.shutdownOnce.Do(func() {
// Close our local connection. This will stop packets from being forwarded
// to our callback.
if err := pd.proxy.Close(); err != nil {
pd.logger.Warnf("Failed to close proxy device %q: %s", pd.proxy.DeviceID, err)
}
// At this point, our proxy has been closed, so all of our packet callbacks
// have completed and we will no longer receive any additional callbacks.
//
// Shut down our processing goroutines.
close(pd.basePacketsC)
close(pd.listenerPacketsC)
// Mark that we are done to external viewers.
close(pd.doneC)
pd.logger.Infof("Proxy device %q has finished; unregistering from manager.", pd.proxy.DeviceID)
pd.m.removeDevice(pd)
})
// Wait for any outstanding processes to finish.
pd.wg.Wait()
}
func (pd *Device) modInfo(fn func(*device.Info)) {
pd.infoMu.Lock()
defer pd.infoMu.Unlock()
fn(&pd.info)
}
// packetData is data belonging to a packet.
type packetData struct {
// buf if the buffer containing the packet.
*bufferpool.Buffer
// forwarded is true if the packet was forwarded to the underlying device.
forwarded bool
}