-
Notifications
You must be signed in to change notification settings - Fork 2
/
peer.go
141 lines (114 loc) · 3.55 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
// SPDX-License-Identifier: MPL-2.0
/*
* Copyright (C) 2024 The Noisy Sockets Authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package noisysockets
import (
"fmt"
"net/netip"
"slices"
"sync"
"github.com/noisysockets/noisysockets/internal/conn"
"github.com/noisysockets/noisysockets/internal/transport"
"github.com/noisysockets/noisysockets/types"
)
var (
ErrNoEndpoint = fmt.Errorf("no known endpoint for peer")
)
// Peer represents a wireguard peer in the network.
type Peer struct {
*transport.Peer
mu sync.RWMutex
name string
publicKey types.NoisePublicKey
addrs []netip.Addr
destinationForPrefixes []netip.Prefix
}
func newPeer(transportPeer *transport.Peer, name string, publicKey types.NoisePublicKey) *Peer {
return &Peer{
Peer: transportPeer,
name: name,
publicKey: publicKey,
}
}
// Name returns the human friendly name of the peer.
func (p *Peer) Name() string {
return p.name
}
// PublicKey returns the public key of the peer.
func (p *Peer) PublicKey() types.NoisePublicKey {
return p.publicKey
}
// Addresses returns the list of addresses of the peer.
func (p *Peer) Addresses() []netip.Addr {
p.mu.RLock()
defer p.mu.RUnlock()
return p.addrs
}
// AddAddress adds one or more addresses to the peer.
func (p *Peer) AddAddresses(addrs ...netip.Addr) {
p.mu.Lock()
defer p.mu.Unlock()
p.addrs = slices.CompactFunc(append(p.addrs, addrs...), func(a, b netip.Addr) bool { return a == b })
}
// RemoveAddress removes one or more addresses from the peer.
func (p *Peer) RemoveAddresses(addrs ...netip.Addr) {
p.mu.Lock()
defer p.mu.Unlock()
toRemove := make(map[netip.Addr]bool, len(addrs))
for _, a := range addrs {
toRemove[a] = true
}
for i, a := range p.addrs {
if toRemove[a] {
p.addrs = append(p.addrs[:i], p.addrs[i+1:]...)
}
}
}
// DestinationForPrefixes returns the list of prefixes the peer is the destination for.
func (p *Peer) DestinationForPrefixes() []netip.Prefix {
p.mu.RLock()
defer p.mu.RUnlock()
return p.destinationForPrefixes
}
// AddDestinationForPrefix adds one or more prefixes the peer is the destination for.
func (p *Peer) AddDestinationPrefixes(prefixes ...netip.Prefix) {
p.mu.Lock()
defer p.mu.Unlock()
p.destinationForPrefixes = slices.CompactFunc(append(p.destinationForPrefixes, prefixes...),
func(a, b netip.Prefix) bool { return a == b })
}
// RemoveDestinationForPrefix removes one or more prefixes the peer is the destination for.
func (p *Peer) RemoveDestinationPrefixes(prefixes ...netip.Prefix) {
p.mu.Lock()
defer p.mu.Unlock()
toRemove := make(map[netip.Prefix]bool, len(prefixes))
for _, prefix := range prefixes {
toRemove[prefix] = true
}
for i, prefix := range p.destinationForPrefixes {
if toRemove[prefix] {
p.destinationForPrefixes = append(p.destinationForPrefixes[:i], p.destinationForPrefixes[i+1:]...)
}
}
}
// GetEndpoint returns the endpoint (public address) of the peer.
func (p *Peer) GetEndpoint() (netip.AddrPort, error) {
p.mu.RLock()
defer p.mu.RUnlock()
endpoint := p.Peer.GetEndpoint()
if endpoint == nil {
return netip.AddrPort{}, ErrNoEndpoint
}
return netip.ParseAddrPort(endpoint.DstToString())
}
// SetEndpoint sets the endpoint (public address) of the peer.
func (p *Peer) SetEndpoint(endpoint netip.AddrPort) {
p.mu.Lock()
defer p.mu.Unlock()
p.Peer.SetEndpoint(&conn.StdNetEndpoint{AddrPort: endpoint})
}