-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
544 lines (476 loc) · 11.3 KB
/
peer.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package fleet
import (
"bytes"
"context"
"crypto/tls"
"encoding/asn1"
"encoding/binary"
"encoding/gob"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"sync"
"sync/atomic"
"time"
"github.com/KarpelesLab/goupd"
)
type Peer struct {
c *tls.Conn
outStream [][]byte
id string
name string
division string
addr *net.TCPAddr
valid bool
annIdx uint64
numG uint32
cnx time.Time
annTime time.Time
aliveTime time.Time
timeOfft time.Duration
Ping time.Duration
a *Agent
mutex sync.RWMutex
unreg sync.Once
write sync.Mutex
alive chan struct{}
meta map[string]interface{}
metaLk sync.Mutex
}
func (a *Agent) newConn(c net.Conn) {
tc, ok := c.(*tls.Conn)
if !ok {
log.Printf("[fleet] non-tls connection recieved?")
c.Close()
return
}
// make sure handshake has completed
err := tc.Handshake()
if err != nil {
log.Printf("[fleet] handshake failed with peer %s: %s", tc.RemoteAddr(), err)
tc.Close()
return
}
switch tc.ConnectionState().NegotiatedProtocol {
case "fbin":
a.handleFleetConn(tc)
return
case "p2p":
a.handleServiceConn(tc)
return
default:
tc.Close()
log.Printf("[fleet] invalid protocol in connection handshake")
}
}
// TlsProtocols returns a list of TLS protocols managed by the fleet system
// that should be directed to the fleet agent listener
func TlsProtocols() []string {
return []string{"fbin", "p2p"}
}
func (a *Agent) handleFleetConn(tc *tls.Conn) {
// instanciate peer and fetch certificate
p := &Peer{
c: tc,
a: a,
cnx: time.Now(),
addr: tc.RemoteAddr().(*net.TCPAddr),
alive: make(chan struct{}),
aliveTime: time.Now(),
annTime: time.Now(),
valid: true,
}
err := p.fetchUuidFromCertificate()
if err != nil {
log.Printf("[fleet] failed to get peer id: %s", err)
p.c.Close()
return
}
if p.id == a.id {
log.Printf("[fleet] connected to self, closing")
p.c.Close()
return
}
log.Printf("[fleet] Connection with peer %s established", p.id)
go p.sendHandshake(context.Background()) // will disappear
go p.register()
go p.loop()
go p.writeLoop()
go p.monitor()
}
func (p *Peer) retryLater(t time.Duration) {
time.Sleep(t)
p.a.dialPeer(p.addr.IP.String(), p.name, p.id)
}
func (p *Peer) loop() {
defer p.unregister()
defer p.c.Close()
// read from peer
header := make([]byte, 6)
var pc uint16 // packet code
var ln uint32 // packet len
var buf []byte // buffer (kept if large enough)
for {
_, err := io.ReadFull(p.c, header)
if err == nil {
pc = binary.BigEndian.Uint16(header[:2])
ln = binary.BigEndian.Uint32(header[2:])
if ln > PacketMaxLen {
// too large
err = fmt.Errorf("rejected packet too large (%d bytes)", ln)
} else if ln == 0 {
if buf != nil {
// set buf length to 0 but do not drop buf
buf = buf[:0]
}
} else if int(ln) <= cap(buf) {
// can store this in the current buffer
buf = buf[:ln]
_, err = io.ReadFull(p.c, buf)
} else {
// allocate new buffer
buf = make([]byte, ln)
_, err = io.ReadFull(p.c, buf)
}
}
if err == nil {
err = p.handleBinary(pc, buf)
}
if err != nil {
if err == io.EOF {
log.Printf("[fleet] disconnected peer %s (received EOF)", p.id)
} else {
log.Printf("[fleet] failed to read from peer %s: %s", p.id, err)
}
if p.valid {
go p.retryLater(10 * time.Second)
}
return
}
}
}
func (p *Peer) handleBinary(pc uint16, data []byte) error {
switch pc {
case PacketLegacy:
return p.handleLegacy(data)
case PacketPing:
p.aliveTime = time.Now()
var t DbStamp
err := t.UnmarshalBinary(data)
if err != nil {
return err
}
p.timeOfft = p.aliveTime.Sub(time.Time(t))
return p.WritePacket(context.Background(), PacketPong, data)
case PacketPong:
var t DbStamp
err := t.UnmarshalBinary(data)
if err != nil {
return err
}
p.Ping = time.Since(time.Time(t))
return nil
case PacketLockReq:
return p.a.handleLockReq(p, data)
case PacketLockRes:
return p.a.handleLockRes(p, data)
case PacketLockConfirm:
return p.a.handleLockConfirm(p, data)
case PacketLockRelease:
return p.a.handleLockRelease(p, data)
case PacketClose:
log.Printf("[fleet] Closing peer connection because: %s", data)
return io.EOF
default:
if pc >= PacketCustom && pc <= PacketCustomMax {
// custom packet
return callCustomHandler(p, pc, data)
}
}
return nil
}
func (p *Peer) handleLegacy(data []byte) error {
dec := gob.NewDecoder(bytes.NewReader(data))
var pkt Packet
err := dec.Decode(&pkt)
if err != nil {
return err
}
return p.handlePacket(pkt)
}
func (p *Peer) monitor() {
defer p.unregister()
t := time.NewTicker(5 * time.Second)
for {
select {
case <-p.alive:
p.Close("alive channel closed")
return
case <-t.C:
if time.Since(p.aliveTime) > time.Minute {
p.Close("alive time timeout")
return
}
}
}
}
func (p *Peer) handlePacket(pktI Packet) error {
switch pkt := pktI.(type) {
case *PacketHandshake:
if pkt.Id != p.id {
return errors.New("invalid handshake")
}
p.name = pkt.Name
p.division = pkt.Division
goupd.SignalVersion(pkt.Git, pkt.Build)
// TODO calculate offset
return nil
case *PacketSeed:
return p.a.handleNewSeed(pkt.Seed, pkt.Time)
case *PacketAnnounce:
return p.a.handleAnnounce(pkt, p)
case *PacketRpc:
if pkt.TargetId != p.a.id {
// fw
return p.a.SendTo(context.Background(), pkt.TargetId, pkt)
}
// we don't really care about the source, just do the rpc thing
return p.a.handleRpc(pkt)
case *PacketRpcResponse:
return p.a.handleRpcResponse(pkt)
case *PacketDbRecord:
if pkt.TargetId != p.a.id {
// fw
return p.a.SendTo(context.Background(), pkt.TargetId, pkt)
}
// let the db handle that
return p.a.feedDbSet(pkt.Bucket, pkt.Key, pkt.Val, pkt.Stamp)
case *PacketDbRequest:
if pkt.TargetId != p.a.id {
// fw
return p.a.SendTo(context.Background(), pkt.TargetId, pkt)
}
// grab from db
return p.handleDbRequest(pkt)
case *PacketDbVersions:
for _, v := range pkt.Info {
if p.a.needDbEntry(v.Bucket, v.Key, v.Stamp) {
if err := p.Send(context.Background(), &PacketDbRequest{TargetId: p.id, SourceId: p.a.id, Bucket: v.Bucket, Key: v.Key}); err != nil {
return err
}
}
}
return nil
default:
return errors.New("unsupported packet")
}
}
func (p *Peer) processAnnounce(ann *PacketAnnounce, fromPeer *Peer) error {
p.mutex.Lock()
defer p.mutex.Unlock()
if ann.Idx <= p.annIdx {
// already seen this announce, ignore it
log.Printf("[agent] got announce %d but already seen %d", ann.Idx, p.annIdx)
return nil
}
p.annIdx = ann.Idx
p.annTime = ann.Now
atomic.StoreUint32(&p.numG, ann.NumG)
p.setMeta(ann.Meta)
// send response
//p.a.TrySendTo(ann.Id, &PacketPong{TargetId: ann.Id, SourceId: p.a.id, Now: ann.Now})
// broadcast
//p.a.doBroadcast(ann, fromPeer.id)
return nil
}
func (p *Peer) handleDbRequest(pkt *PacketDbRequest) error {
val, stamp, err := p.a.dbGetVersion([]byte(pkt.Bucket), []byte(pkt.Key))
if err != nil {
// ignore it
return nil
}
// send response
res := &PacketDbRecord{
TargetId: pkt.SourceId,
SourceId: p.a.id,
Stamp: stamp,
Bucket: pkt.Bucket,
Key: pkt.Key,
Val: val,
}
return p.Send(context.Background(), res)
}
func (p *Peer) fetchUuidFromCertificate() error {
// grab certificate
chains := p.c.ConnectionState().VerifiedChains
if len(chains) == 0 {
return errors.New("no peer certificate?")
}
if len(chains[0]) == 0 {
return errors.New("no peer certificate? (2)")
}
peer_cert := chains[0][0] // *x509.Certificate
// grab id
peer_subject := peer_cert.Subject
peer_id := ""
for _, name := range peer_subject.Names {
// oid(2.5.4.45) = UniqueIdentifier
if !name.Type.Equal(asn1.ObjectIdentifier{2, 5, 4, 45}) {
continue
}
peer_id = name.Value.(string)
break
}
if peer_id == "" {
return errors.New("failed to get peer id from cert")
}
p.id = peer_id
return nil
}
// Id returns the peer's internal ID, which is unique and can be used to send
// packets to this peer specifically in the future.
func (p *Peer) Id() string {
return p.id
}
// Name returns this peer's name
func (p *Peer) Name() string {
return p.name
}
// Division returns this peer's division string
func (p *Peer) Division() string {
return p.division
}
// Agent returns the Agent object associated with this peer
func (p *Peer) Agent() *Agent {
return p.a
}
func (p *Peer) Send(ctx context.Context, pkt Packet) error {
//log.Printf("[debug] sending packet %T to %s with context", pkt, p.id)
buf := &bytes.Buffer{}
enc := gob.NewEncoder(buf)
enc.Encode(&pkt) // & is important
return p.WritePacket(ctx, PacketLegacy, buf.Bytes())
}
func (p *Peer) writeLoop() {
defer p.c.Close()
t := time.NewTicker(5 * time.Second)
for {
select {
case <-p.alive:
// closed channel
return
case now := <-t.C:
// send new alive packet
v := DbStamp(now).Bytes()
err := p.WritePacket(context.Background(), PacketPing, v)
if err != nil {
log.Printf("[fleet] Write to peer failed: %s", err)
return
}
}
}
}
func (p *Peer) writev(ctx context.Context, buf ...[]byte) (n int, err error) {
p.write.Lock()
defer p.write.Unlock()
if deadline, ok := ctx.Deadline(); ok {
if time.Until(deadline) < 0 {
// write error but non closing
return 0, os.ErrDeadlineExceeded
}
p.c.SetWriteDeadline(deadline)
} else {
// reset deadline
p.c.SetWriteDeadline(time.Now().Add(30 * time.Second))
}
for _, b := range buf {
sn, serr := p.c.Write(b)
if sn > 0 {
n += sn
}
if serr != nil {
err = serr
if n > 0 {
p.c.Close() // close because that is a partial write
}
return
}
}
return
}
func (p *Peer) WritePacket(ctx context.Context, pc uint16, data []byte) error {
pcBin := []byte{byte(pc >> 8), byte(pc)}
ln := len(data)
lnBin := []byte{
byte(ln >> 24),
byte(ln >> 16),
byte(ln >> 8),
byte(ln),
}
_, err := p.writev(ctx, pcBin, lnBin, data)
return err
}
func (p *Peer) Close(reason string) error {
log.Printf("[fleet] Closing connection to %s: %s", p.id, reason)
p.c.SetWriteDeadline(time.Now().Add(1 * time.Second))
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err := p.WritePacket(ctx, PacketClose, []byte(reason))
if err != nil {
return err
}
return p.c.Close()
}
func (p *Peer) register() {
a := p.a
a.peersMutex.Lock()
defer a.peersMutex.Unlock()
old, ok := a.peers[p.id]
if ok && old != p {
go p.Close("already connected, dropping new connection")
return
}
a.peers[p.id] = p
}
func (p *Peer) unregister() {
p.unreg.Do(func() {
close(p.alive) // no more alive
a := p.a
a.peersMutex.Lock()
defer a.peersMutex.Unlock()
old, ok := a.peers[p.id]
if ok && old == p {
delete(a.peers, p.id)
}
})
}
func (p *Peer) sendHandshake(ctx context.Context) error {
pkt := &PacketHandshake{
Id: p.a.id,
Name: p.a.name,
Division: p.a.division,
Now: time.Now(),
Git: goupd.GIT_TAG,
Build: goupd.DATE_TAG,
}
err := p.Send(ctx, pkt)
if err != nil {
return err
}
p.WritePacket(ctx, PacketPing, DbStamp(time.Now()).Bytes())
p.Send(ctx, p.a.databasePacket())
return p.Send(ctx, p.a.seedPacket())
}
func (p *Peer) Meta() map[string]interface{} {
p.metaLk.Lock()
defer p.metaLk.Unlock()
return p.meta
}
func (p *Peer) setMeta(v map[string]interface{}) {
p.metaLk.Lock()
defer p.metaLk.Unlock()
p.meta = v
}