-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestination.go
241 lines (200 loc) · 6.69 KB
/
destination.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
package edgelink
import (
"context"
"fmt"
"net"
"net/netip"
keyx "github.com/portainer/edgelink/keyx/api/v1"
"github.com/portainer/edgelink/wireguard"
"google.golang.org/grpc"
)
// destination holds the configuration for a destination node in the
// edgelink system. It includes settings for key exchange and WireGuard
// operations.
type destination struct {
// keyxPort is the port used for key exchange operations.
// It is the port that the destination node listens on for incoming
// key exchange requests from the source node.
keyxPort int
// wgPort is the port used for WireGuard operations.
// It is the port that the destination node listens on for incoming
// WireGuard connections from the source node.
wgPort int
// wgKeepAlive is the interval for sending keepalive packets in WireGuard.
wgKeepAlive int
// wgVerboseLogging enables verbose logging for the underlying WireGuard
// library.
wgVerboseLogging bool
// localIP is the local IP address assigned to the destination node.
localIP string
// sourceIP is the IP address of the source node. It is used to build
// the allowed IP for the destination node.
sourceIP string
}
// DestinationOption is a function type used to modify the destination
// configuration during its setup.
type DestinationOption func(*destination)
// WithDestinationKeyxPort sets the key exchange port for the destination.
func WithDestinationKeyxPort(port int) DestinationOption {
return func(dest *destination) {
dest.keyxPort = port
}
}
// WithDestinationLocalIP sets the local IP address for the destination node.
func WithDestinationLocalIP(ip string) DestinationOption {
return func(dest *destination) {
dest.localIP = ip
}
}
// WithDestinationOriginIP sets the IP address of the source node. It is used
// to build the allowed IP for the destination node.
func WithDestinationOriginIP(ip string) DestinationOption {
return func(dest *destination) {
dest.sourceIP = ip
}
}
// WithDestinationWGKeepalive sets the keepalive interval for WireGuard
// operations. The destination node will send keepalive packets to the source
// node to maintain the connection using this specified interval.
func WithDestinationWGKeepalive(keepalive int) DestinationOption {
return func(dest *destination) {
dest.wgKeepAlive = keepalive
}
}
// WithDestinationWGPort sets the WireGuard port for the destination.
func WithDestinationWGPort(port int) DestinationOption {
return func(dest *destination) {
dest.wgPort = port
}
}
// WithDestinationWGVerboseLogging enables or disables verbose logging
// for the underlying WireGuard library.
func WithDestinationWGVerboseLogging(verbose bool) DestinationOption {
return func(dest *destination) {
dest.wgVerboseLogging = verbose
}
}
// SetupAsDestination configures a Node as a destination node using the
// provided options. It initializes and validates the destination configuration.
// A destination node will setup a key exchange server if no peer public key is
// provided.
//
// Parameters:
// - options: A variadic list of DestinationOption functions to customize the destination.
//
// Returns:
// - error: An error object if the setup fails.
func (n *Node) SetupAsDestination(options ...DestinationOption) error {
d := &destination{
keyxPort: keyxDefaultPort,
wgPort: wgDefaultPort,
wgKeepAlive: wgDefaultKeepalive,
wgVerboseLogging: false,
localIP: wgDefaultDestinationLocalIP,
sourceIP: wgDefaultSourceLocalIP,
}
for _, option := range options {
option(d)
}
err := validateDestination(d)
if err != nil {
return fmt.Errorf("invalid destination configuration: %w", err)
}
n.destination = d
return nil
}
func validateDestination(d *destination) error {
_, err := netip.ParseAddr(d.localIP)
if err != nil {
return fmt.Errorf("invalid local IP address: %w", err)
}
_, err = netip.ParseAddr(d.sourceIP)
if err != nil {
return fmt.Errorf("invalid source IP address: %w", err)
}
return nil
}
func (n *Node) acceptLink() error {
if n.config.peerPublicKey == "" {
if err := n.startKeyExchangeServer(); err != nil {
return err
}
}
if err := n.createDestinationVirtualNetwork(); err != nil {
return fmt.Errorf("destination node failed to create virtual network: %w", err)
}
return nil
}
func (n *Node) startKeyExchangeServer() error {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", n.destination.keyxPort))
if err != nil {
return fmt.Errorf("unable to listen on port %d: %w", n.destination.keyxPort, err)
}
defer lis.Close()
keyExchange := make(chan string)
defer close(keyExchange)
grpcServer := grpc.NewServer()
srv := &keyxServer{publicKey: n.config.publicKey, keyExchange: keyExchange}
keyx.RegisterKeyExchangeServer(grpcServer, srv)
errChan := make(chan error, 1)
go func() {
errChan <- grpcServer.Serve(lis)
close(errChan)
}()
select {
case n.config.peerPublicKey = <-srv.keyExchange:
n.logf("Received public key from client: %s", n.config.peerPublicKey)
err = n.persistConfigChanges()
if err != nil {
return fmt.Errorf("failed to write node configuration changes on disk: %w", err)
}
case err := <-errChan:
if err != nil {
return fmt.Errorf("failed to serve gRPC server: %w", err)
}
}
grpcServer.GracefulStop()
return nil
}
func (n *Node) createDestinationWireGuardConfig() (wireguard.WireGuardConfig, error) {
ip, err := netip.ParseAddr(n.destination.localIP)
if err != nil {
return wireguard.WireGuardConfig{}, fmt.Errorf("invalid local IP address: %w", err)
}
nameservers, err := parseDNSAddresses(n.config.dns)
if err != nil {
return wireguard.WireGuardConfig{}, fmt.Errorf("invalid DNS address: %w", err)
}
return wireguard.WireGuardConfig{
PrivateKey: n.config.privateKey,
PeerPublicKey: n.config.peerPublicKey,
MTU: n.config.mtu,
DNSAddresses: nameservers,
LocalAddresses: []netip.Addr{ip},
ListenPort: &n.destination.wgPort,
PersistentKeepalive: &n.destination.wgKeepAlive,
AllowedIP: buildAllowedIP(n.destination.sourceIP),
VerboseLogging: n.destination.wgVerboseLogging,
}, nil
}
func (n *Node) createDestinationVirtualNetwork() error {
wgConfig, err := n.createDestinationWireGuardConfig()
if err != nil {
return fmt.Errorf("failed to create wireguard config: %v", err)
}
vNet, err := wireguard.CreateVirtualNetwork(wgConfig)
if err != nil {
return fmt.Errorf("failed to create virtual network: %v", err)
}
n.virtualNetwork = vNet
return nil
}
type keyxServer struct {
keyx.UnimplementedKeyExchangeServer
publicKey string
keyExchange chan string
}
func (s *keyxServer) ExchangeKeys(ctx context.Context, req *keyx.PublicKey) (*keyx.PublicKey, error) {
s.keyExchange <- req.Key
return &keyx.PublicKey{Key: s.publicKey}, nil
}