-
-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathwireguard.go
47 lines (38 loc) · 1.48 KB
/
wireguard.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
package utils
import (
"net/netip"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/wireguard"
)
func BuildWireguardSettings(connection models.Connection,
userSettings settings.Wireguard, ipv6Supported bool,
) (settings wireguard.Settings) {
settings.PrivateKey = *userSettings.PrivateKey
settings.PublicKey = connection.PubKey
settings.PreSharedKey = *userSettings.PreSharedKey
settings.InterfaceName = userSettings.Interface
settings.Implementation = userSettings.Implementation
settings.MTU = userSettings.MTU
settings.IPv6 = &ipv6Supported
const rulePriority = 101 // 100 is to receive external connections
settings.RulePriority = rulePriority
settings.Endpoint = netip.AddrPortFrom(connection.IP, connection.Port)
settings.Addresses = make([]netip.Prefix, 0, len(userSettings.Addresses))
for _, address := range userSettings.Addresses {
if !ipv6Supported && address.Addr().Is6() {
continue
}
addressCopy := netip.PrefixFrom(address.Addr(), address.Bits())
settings.Addresses = append(settings.Addresses, addressCopy)
}
settings.AllowedIPs = make([]netip.Prefix, 0, len(userSettings.AllowedIPs))
for _, allowedIP := range userSettings.AllowedIPs {
if !ipv6Supported && allowedIP.Addr().Is6() {
continue
}
settings.AllowedIPs = append(settings.AllowedIPs, allowedIP)
}
settings.PersistentKeepaliveInterval = *userSettings.PersistentKeepaliveInterval
return settings
}