/
provider_wg.go
173 lines (132 loc) · 3.38 KB
/
provider_wg.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package config
import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"cunicu.li/cunicu/pkg/crypto"
"cunicu.li/cunicu/pkg/link"
"cunicu.li/cunicu/pkg/log"
"cunicu.li/cunicu/pkg/wg"
)
var errNotSupported = errors.New("not supported on this platform")
type WireGuardProvider struct {
path string
order []string
logger *log.Logger
}
func NewWireGuardProvider() *WireGuardProvider {
path := os.Getenv("WG_CONFIG_PATH")
if path == "" {
path = wg.ConfigPath
}
return &WireGuardProvider{
path: path,
logger: log.Global.Named("config.wg"),
}
}
func (p *WireGuardProvider) Read() (map[string]interface{}, error) {
m := map[string]any{}
des, err := os.ReadDir(p.path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return m, nil
}
return nil, fmt.Errorf("failed to list config files in '%s': %w", p.path, err)
}
p.order = []string{}
for _, de := range des {
cfg := filepath.Join(p.path, de.Name())
filename := filepath.Base(cfg)
extension := filepath.Ext(filename)
name := strings.TrimSuffix(filename, extension)
if extension != ".conf" {
continue
}
cfgData, err := os.ReadFile(cfg)
if err != nil {
return nil, fmt.Errorf("failed to open config file %s: %w", cfg, err)
}
var pa wgParser
m[name], err = pa.Unmarshal(cfgData)
if err != nil {
return nil, fmt.Errorf("failed to parse config file %s: %w", cfg, err)
}
p.order = append(p.order, name)
}
return map[string]any{
"interfaces": m,
}, nil
}
func (p *WireGuardProvider) ReadBytes() ([]byte, error) {
return nil, errNotImplemented
}
func (p *WireGuardProvider) Order() []string {
slices.Sort(p.order)
return p.order
}
type wgParser struct{}
func (p *wgParser) Unmarshal(data []byte) (map[string]interface{}, error) {
c, err := wg.ParseConfig(data)
if err != nil {
return nil, fmt.Errorf("failed to parse configuration: %w", err)
}
s, err := NewInterfaceSettingsFromConfig(c)
if err != nil {
return nil, err
}
return Map(s, "koanf"), nil
}
func (p *wgParser) Marshal(map[string]interface{}) ([]byte, error) {
return nil, errNotSupported
}
func NewInterfaceSettingsFromConfig(c *wg.Config) (*InterfaceSettings, error) {
s := &InterfaceSettings{
ListenPort: c.ListenPort,
Peers: map[string]PeerSettings{},
Addresses: c.Address,
DNS: c.DNS,
}
if c.PrivateKey != nil {
s.PrivateKey = crypto.Key(*c.PrivateKey)
}
if c.FirewallMark != nil {
s.FirewallMark = *c.FirewallMark
}
if c.MTU != nil {
s.MTU = *c.MTU
}
if c.Table != nil && *c.Table != "off" {
var err error
s.RoutingTable, err = link.Table(*c.Table)
if err != nil {
return nil, fmt.Errorf("failed to parse routing table '%s': %w", *c.Table, err)
}
}
// TODO: Add exec hooks for wg-quick PreUp, PostUp, PreDown, PostDown hooks
for i, p := range c.Peers {
wgps := PeerSettings{
PublicKey: crypto.Key(p.PublicKey),
AllowedIPs: p.AllowedIPs,
}
if p.PresharedKey != nil {
wgps.PresharedKey = crypto.Key(*p.PresharedKey)
}
if c.PeerEndpoints[i] != "" {
wgps.Endpoint = c.PeerEndpoints[i]
}
if p.PersistentKeepaliveInterval != nil {
wgps.PersistentKeepaliveInterval = *p.PersistentKeepaliveInterval
}
name := c.PeerNames[i]
if name == "" {
name = p.PublicKey.String()[:8]
}
s.Peers[name] = wgps
}
return s, nil
}