/
device_config.go
349 lines (285 loc) · 7.84 KB
/
device_config.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package wg
import (
"fmt"
"io"
"net"
"strings"
"time"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"gopkg.in/ini.v1"
)
type Config struct {
wgtypes.Config
PeerEndpoints []string
PeerNames []string
Address []net.IPNet
DNS []net.IPAddr
MTU *int
Table *string
PreUp []string
PreDown []string
PostUp []string
PostDown []string
SaveConfig *bool
}
type config struct {
Interface interfaceConfig
Peers []peerConfig `ini:"Peer,nonunique"`
}
type interfaceConfig struct {
PrivateKey *string `ini:",omitempty"`
ListenPort *int `ini:",omitempty"`
FwMark *int `ini:",omitempty"`
// Settings for wg-quick
Address []string `ini:",omitempty" delim:", "`
DNS []string `ini:",omitempty" delim:", "`
MTU *int `ini:",omitempty"`
Table *string `ini:",omitempty"`
PreUp []string `ini:",omitempty,allowshadow"`
PreDown []string `ini:",omitempty,allowshadow"`
PostUp []string `ini:",omitempty,allowshadow"`
PostDown []string `ini:",omitempty,allowshadow"`
SaveConfig *bool `ini:",omitempty"`
}
type peerConfig struct {
PublicKey string `ini:",omitempty"`
PresharedKey *string `ini:",omitempty"`
AllowedIPs []string `ini:",omitempty" delim:", "`
Endpoint *string `ini:",omitempty"`
PersistentKeepalive *int `ini:",omitempty"`
}
func parseCIDRs(nets []string, ip bool) ([]net.IPNet, error) {
pn := []net.IPNet{}
for _, ne := range nets {
i, n, err := net.ParseCIDR(ne)
if err != nil {
// Try parsing URL without CIDR suffix
i := net.ParseIP(ne)
if i == nil {
return nil, fmt.Errorf("failed to parse network %s: %w", ne, err)
}
n = &net.IPNet{
IP: i,
}
if isV4 := i.To4() != nil; isV4 {
n.Mask = net.CIDRMask(32, 32)
} else {
n.Mask = net.CIDRMask(128, 128)
}
} else if ip {
n.IP = i
}
pn = append(pn, *n)
}
return pn, nil
}
func parseIPs(ips []string) ([]net.IPAddr, error) {
pips := []net.IPAddr{}
for _, ip := range ips {
i, err := net.ResolveIPAddr("ip", ip)
if err != nil {
return nil, fmt.Errorf("failed to parse ip %s: %w", ip, err)
}
pips = append(pips, *i)
}
return pips, nil
}
func (cfg *Config) Dump(wr io.Writer) error {
iniCfg := &config{
Interface: interfaceConfig{
ListenPort: cfg.ListenPort,
FwMark: cfg.FirewallMark,
},
}
if cfg.PrivateKey != nil {
pk := cfg.PrivateKey.String()
iniCfg.Interface.PrivateKey = &pk
}
// wg-quick settings
iniCfg.Interface.MTU = cfg.MTU
iniCfg.Interface.SaveConfig = cfg.SaveConfig
iniCfg.Interface.Table = cfg.Table
iniCfg.Interface.PreUp = cfg.PreUp
iniCfg.Interface.PreDown = cfg.PreDown
iniCfg.Interface.PostUp = cfg.PostUp
iniCfg.Interface.PostDown = cfg.PostDown
iniCfg.Interface.SaveConfig = cfg.SaveConfig
if len(cfg.Address) > 0 {
iniCfg.Interface.Address = []string{}
for _, addr := range cfg.Address {
iniCfg.Interface.Address = append(iniCfg.Interface.Address, addr.String())
}
}
if len(cfg.DNS) > 0 {
iniCfg.Interface.DNS = []string{}
for _, addr := range cfg.DNS {
iniCfg.Interface.DNS = append(iniCfg.Interface.DNS, addr.String())
}
}
for i, peer := range cfg.Peers {
iniPeer := peerConfig{
PublicKey: peer.PublicKey.String(),
}
if peer.PresharedKey != nil {
psk := peer.PresharedKey.String()
iniPeer.PresharedKey = &psk
}
if cfg.PeerEndpoints != nil && cfg.PeerEndpoints[i] != "" {
iniPeer.Endpoint = &cfg.PeerEndpoints[i]
} else if peer.Endpoint != nil {
ep := peer.Endpoint.String()
iniPeer.Endpoint = &ep
}
if peer.PersistentKeepaliveInterval != nil {
ka := int(*peer.PersistentKeepaliveInterval / time.Second)
iniPeer.PersistentKeepalive = &ka
}
if len(peer.AllowedIPs) > 0 {
iniPeer.AllowedIPs = []string{}
for _, aip := range peer.AllowedIPs {
iniPeer.AllowedIPs = append(iniPeer.AllowedIPs, aip.String())
}
}
iniCfg.Peers = append(iniCfg.Peers, iniPeer)
}
iniFile := ini.Empty(ini.LoadOptions{
AllowNonUniqueSections: true,
AllowShadows: true,
})
if err := iniFile.ReflectFrom(iniCfg); err != nil {
return err
}
if cfg.PeerNames != nil {
if peerSections, err := iniFile.SectionsByName("Peer"); err == nil {
for i, peerSection := range peerSections {
peerSection.Comment = fmt.Sprintf("# %s", cfg.PeerNames[i])
}
}
}
_, err := iniFile.WriteTo(wr)
return err
}
func ParseConfig(data []byte) (*Config, error) {
var err error
iniFile, err := ini.LoadSources(ini.LoadOptions{
AllowNonUniqueSections: true,
AllowShadows: true,
}, data)
if err != nil {
return nil, err
}
// We add a pseudo peer section just allow mapping via StrictMapTo if there are no peers configured
fakePeer := !iniFile.HasSection("Peer")
if fakePeer {
if _, err := iniFile.NewSection("Peer"); err != nil {
return nil, fmt.Errorf("failed to create new peer section: %w", err)
}
}
iniCfg := &config{}
if err := iniFile.StrictMapTo(iniCfg); err != nil {
return nil, fmt.Errorf("failed to parse Interface section: %w", err)
}
// Remove fake peer section again
if fakePeer {
iniCfg.Peers = nil
}
peerSects, err := iniFile.SectionsByName("Peer")
if err != nil {
return nil, err
}
cfg, err := iniCfg.Config()
if err != nil {
return nil, err
}
for _, peerSect := range peerSects {
peerName := strings.TrimPrefix(peerSect.Comment, "#")
peerName = strings.TrimSpace(peerName)
cfg.PeerNames = append(cfg.PeerNames, peerName)
}
return cfg, nil
}
func (c *config) Config() (*Config, error) {
var err error
cfg := &Config{
Config: wgtypes.Config{
Peers: []wgtypes.PeerConfig{},
ListenPort: c.Interface.ListenPort,
FirewallMark: c.Interface.FwMark,
},
}
if c.Interface.Address != nil {
if cfg.Address, err = parseCIDRs(c.Interface.Address, true); err != nil {
return nil, err
}
}
if c.Interface.DNS != nil {
if cfg.DNS, err = parseIPs(c.Interface.DNS); err != nil {
return nil, err
}
}
if c.Interface.PrivateKey != nil {
pk, err := wgtypes.ParseKey(*c.Interface.PrivateKey)
if err != nil {
return nil, fmt.Errorf("failed to parse key: %w", err)
}
cfg.PrivateKey = &pk
}
// wg-quick settings
cfg.MTU = c.Interface.MTU
cfg.Table = c.Interface.Table
cfg.PreUp = c.Interface.PreUp
cfg.PreDown = c.Interface.PreDown
cfg.PostUp = c.Interface.PostUp
cfg.PostDown = c.Interface.PostDown
cfg.SaveConfig = c.Interface.SaveConfig
for _, pSection := range c.Peers {
peerCfg, err := pSection.Config()
if err != nil {
return nil, fmt.Errorf("failed to parse peer config: %w", err)
}
cfg.Peers = append(cfg.Peers, *peerCfg)
if pSection.Endpoint != nil {
cfg.PeerEndpoints = append(cfg.PeerEndpoints, *pSection.Endpoint)
} else {
cfg.PeerEndpoints = append(cfg.PeerEndpoints, "")
}
}
return cfg, nil
}
func (p *peerConfig) Config() (*wgtypes.PeerConfig, error) {
var err error
cfg := &wgtypes.PeerConfig{
AllowedIPs: []net.IPNet{},
}
cfg.PublicKey, err = wgtypes.ParseKey(p.PublicKey)
if err != nil {
return nil, fmt.Errorf("failed to parse key: %w", err)
}
if p.PresharedKey != nil {
psk, err := wgtypes.ParseKey(*p.PresharedKey)
if err != nil {
return nil, fmt.Errorf("failed to parse key: %w", err)
}
cfg.PresharedKey = &psk
}
if p.PersistentKeepalive != nil {
ka := time.Duration(*p.PersistentKeepalive) * time.Second
cfg.PersistentKeepaliveInterval = &ka
}
if p.Endpoint != nil {
addr, err := net.ResolveUDPAddr("udp", *p.Endpoint)
if err != nil {
return nil, fmt.Errorf("failed to resolve endpoint %s: %w", *p.Endpoint, err)
}
cfg.Endpoint = addr
}
if p.AllowedIPs != nil {
if cfg.AllowedIPs, err = parseCIDRs(p.AllowedIPs, false); err != nil {
return nil, fmt.Errorf("failed to parse allowed ips: %w", err)
}
cfg.ReplaceAllowedIPs = true
}
return cfg, nil
}