-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
250 lines (217 loc) · 5.12 KB
/
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
package vpnconfig
import (
"encoding/json"
"net"
"reflect"
log "github.com/sirupsen/logrus"
)
// Device is a VPN device configuration in Config
type Device struct {
Name string
MTU int
}
// Copy returns a copy of device
func (d *Device) Copy() Device {
return Device{
Name: d.Name,
MTU: d.MTU,
}
}
// Address is a IPv4/IPv6 address configuration in Config
type Address struct {
Address net.IP
Netmask net.IPMask
}
// Copy returns a copy of address
func (a *Address) Copy() Address {
return Address{
Address: append(a.Address[:0:0], a.Address...),
Netmask: append(a.Netmask[:0:0], a.Netmask...),
}
}
// DNS is a DNS configuration in Config
type DNS struct {
DefaultDomain string
ServersIPv4 []net.IP
ServersIPv6 []net.IP
}
// Copy returns a copy of DNS
func (d *DNS) Copy() DNS {
serversIPv4 := []net.IP{}
if d.ServersIPv4 == nil {
serversIPv4 = nil
}
for _, s := range d.ServersIPv4 {
ip := append(s[:0:0], s...)
serversIPv4 = append(serversIPv4, ip)
}
serversIPv6 := []net.IP{}
if d.ServersIPv6 == nil {
serversIPv6 = nil
}
for _, s := range d.ServersIPv6 {
ip := append(s[:0:0], s...)
serversIPv6 = append(serversIPv6, ip)
}
return DNS{
DefaultDomain: d.DefaultDomain,
ServersIPv4: serversIPv4,
ServersIPv6: serversIPv6,
}
}
// Remotes returns a map of DNS remotes from the DNS configuration that maps
// domain "." to the IPv4 and IPv6 DNS servers in the configuration including
// port number 53
func (d *DNS) Remotes() map[string][]string {
remotes := map[string][]string{}
for _, s := range d.ServersIPv4 {
server := s.String() + ":53"
remotes["."] = append(remotes["."], server)
}
for _, s := range d.ServersIPv6 {
server := "[" + s.String() + "]:53"
remotes["."] = append(remotes["."], server)
}
return remotes
}
// Split is a split routing configuration in Config
type Split struct {
ExcludeIPv4 []*net.IPNet
ExcludeIPv6 []*net.IPNet
ExcludeDNS []string
ExcludeVirtualSubnetsOnlyIPv4 bool
}
// Copy returns a copy of split
func (s *Split) Copy() Split {
excludeIPv4 := []*net.IPNet{}
if s.ExcludeIPv4 == nil {
excludeIPv4 = nil
}
for _, e := range s.ExcludeIPv4 {
ipnet := &net.IPNet{
IP: append(e.IP[:0:0], e.IP...),
Mask: append(e.Mask[:0:0], e.Mask...),
}
excludeIPv4 = append(excludeIPv4, ipnet)
}
excludeIPv6 := []*net.IPNet{}
if s.ExcludeIPv6 == nil {
excludeIPv6 = nil
}
for _, e := range s.ExcludeIPv6 {
ipnet := &net.IPNet{
IP: append(e.IP[:0:0], e.IP...),
Mask: append(e.Mask[:0:0], e.Mask...),
}
excludeIPv6 = append(excludeIPv6, ipnet)
}
return Split{
ExcludeIPv4: excludeIPv4,
ExcludeIPv6: excludeIPv6,
ExcludeDNS: append(s.ExcludeDNS[:0:0], s.ExcludeDNS...),
ExcludeVirtualSubnetsOnlyIPv4: s.ExcludeVirtualSubnetsOnlyIPv4,
}
}
// DNSExcludes returns a list of DNS-based split excludes from the
// split routing configuration. The list contains domain names including the
// trailing "."
func (s *Split) DNSExcludes() []string {
excludes := make([]string, len(s.ExcludeDNS))
for i, e := range s.ExcludeDNS {
excludes[i] = e + "."
}
return excludes
}
// Flags are other configuration settings in Config
type Flags struct {
DisableAlwaysOnVPN bool
}
// Copy returns a copy of flags
func (f *Flags) Copy() Flags {
return Flags{
DisableAlwaysOnVPN: f.DisableAlwaysOnVPN,
}
}
// Config is a VPN configuration
type Config struct {
Gateway net.IP
PID int
Timeout int
Device Device
IPv4 Address
IPv6 Address
DNS DNS
Split Split
Flags Flags
}
// Copy returns a new copy of config
func (c *Config) Copy() *Config {
if c == nil {
return nil
}
return &Config{
Gateway: append([]byte{}, c.Gateway...),
PID: c.PID,
Timeout: c.Timeout,
Device: c.Device.Copy(),
IPv4: c.IPv4.Copy(),
IPv6: c.IPv6.Copy(),
DNS: c.DNS.Copy(),
Split: c.Split.Copy(),
Flags: c.Flags.Copy(),
}
}
// Empty returns if the config is empty
func (c *Config) Empty() bool {
empty := New()
return c.Equal(empty)
}
// Equal returns if the config and other are equal
func (c *Config) Equal(other *Config) bool {
return reflect.DeepEqual(c, other)
}
// Valid returns if the config is valid
func (c *Config) Valid() bool {
// an empty config is valid
if c.Empty() {
return true
}
// check config entries
for i, invalid := range []bool{
c.Gateway == nil,
c.Device.Name == "",
len(c.Device.Name) > 15,
c.Device.MTU < 68,
c.Device.MTU > 16384,
len(c.IPv4.Address) == 0 && len(c.IPv6.Address) == 0,
len(c.IPv4.Netmask) == 0 && len(c.IPv6.Netmask) == 0,
len(c.DNS.ServersIPv4) == 0 && len(c.DNS.ServersIPv6) == 0,
} {
if invalid {
log.WithField("check", i).Error("VPNConfig is invalid config")
return false
}
}
// TODO: check more?
return true
}
// JSON returns the configuration as JSON
func (c *Config) JSON() ([]byte, error) {
b, err := json.Marshal(c)
if err != nil {
return nil, err
}
return b, nil
}
// New returns a new Config
func New() *Config {
return &Config{}
}
// NewFromJSON returns a new config parsed from the json in b
func NewFromJSON(b []byte) (*Config, error) {
c := New()
if err := json.Unmarshal(b, c); err != nil {
return nil, err
}
return c, nil
}