2022-01-30 16:38:45 +00:00
|
|
|
// Copyright 2021 the Kilo authors
|
2019-01-18 01:50:10 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package mesh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"sort"
|
2022-01-30 16:38:45 +00:00
|
|
|
"time"
|
2019-01-18 01:50:10 +00:00
|
|
|
|
2021-05-27 07:01:22 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2022-01-30 16:38:45 +00:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2021-05-27 07:01:22 +00:00
|
|
|
|
2019-05-03 10:53:40 +00:00
|
|
|
"github.com/squat/kilo/pkg/wireguard"
|
2019-01-18 01:50:10 +00:00
|
|
|
)
|
|
|
|
|
2021-01-24 13:19:01 +00:00
|
|
|
const (
|
|
|
|
logicalLocationPrefix = "location:"
|
|
|
|
nodeLocationPrefix = "node:"
|
|
|
|
)
|
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
// Topology represents the logical structure of the overlay network.
|
|
|
|
type Topology struct {
|
2019-05-03 10:53:40 +00:00
|
|
|
// key is the private key of the node creating the topology.
|
2022-01-30 16:38:45 +00:00
|
|
|
key wgtypes.Key
|
|
|
|
port int
|
2019-01-18 01:50:10 +00:00
|
|
|
// Location is the logical location of the local host.
|
2019-05-03 10:53:40 +00:00
|
|
|
location string
|
|
|
|
segments []*segment
|
|
|
|
peers []*Peer
|
2019-01-18 01:50:10 +00:00
|
|
|
|
|
|
|
// hostname is the hostname of the local host.
|
|
|
|
hostname string
|
|
|
|
// leader represents whether or not the local host
|
|
|
|
// is the segment leader.
|
|
|
|
leader bool
|
2020-03-03 19:10:20 +00:00
|
|
|
// persistentKeepalive is the interval in seconds of the emission
|
|
|
|
// of keepalive packets by the local node to its peers.
|
2022-01-30 16:38:45 +00:00
|
|
|
persistentKeepalive time.Duration
|
2020-03-03 19:10:20 +00:00
|
|
|
// privateIP is the private IP address of the local node.
|
|
|
|
privateIP *net.IPNet
|
2020-02-20 20:27:50 +00:00
|
|
|
// subnet is the Pod subnet of the local node.
|
2019-01-18 01:50:10 +00:00
|
|
|
subnet *net.IPNet
|
|
|
|
// wireGuardCIDR is the allocated CIDR of the WireGuard
|
2021-03-25 01:59:54 +00:00
|
|
|
// interface of the local node within the Kilo subnet.
|
|
|
|
// If the local node is not the leader of a location, then
|
|
|
|
// the IP is the 0th address in the subnet, i.e. the CIDR
|
|
|
|
// is equal to the Kilo subnet.
|
2019-01-18 01:50:10 +00:00
|
|
|
wireGuardCIDR *net.IPNet
|
2021-04-21 17:47:29 +00:00
|
|
|
// discoveredEndpoints is the updated map of valid discovered Endpoints
|
2022-01-30 16:38:45 +00:00
|
|
|
discoveredEndpoints map[string]*net.UDPAddr
|
2021-05-27 07:01:22 +00:00
|
|
|
logger log.Logger
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 11:42:13 +00:00
|
|
|
// segment represents one logical unit in the topology that is united by one common WireGuard IP.
|
2019-01-18 01:50:10 +00:00
|
|
|
type segment struct {
|
2022-01-30 16:38:45 +00:00
|
|
|
allowedIPs []net.IPNet
|
2021-04-21 17:47:29 +00:00
|
|
|
endpoint *wireguard.Endpoint
|
2022-01-30 16:38:45 +00:00
|
|
|
key wgtypes.Key
|
|
|
|
persistentKeepalive time.Duration
|
2019-01-18 01:50:10 +00:00
|
|
|
// Location is the logical location of this segment.
|
2019-05-03 10:53:40 +00:00
|
|
|
location string
|
2019-01-18 01:50:10 +00:00
|
|
|
|
|
|
|
// cidrs is a slice of subnets of all peers in the segment.
|
|
|
|
cidrs []*net.IPNet
|
|
|
|
// hostnames is a slice of the hostnames of the peers in the segment.
|
|
|
|
hostnames []string
|
|
|
|
// leader is the index of the leader of the segment.
|
|
|
|
leader int
|
|
|
|
// privateIPs is a slice of private IPs of all peers in the segment.
|
|
|
|
privateIPs []net.IP
|
|
|
|
// wireGuardIP is the allocated IP address of the WireGuard
|
|
|
|
// interface on the leader of the segment.
|
|
|
|
wireGuardIP net.IP
|
2021-05-27 07:01:22 +00:00
|
|
|
// allowedLocationIPs are not part of the cluster and are not peers.
|
|
|
|
// They are directly routable from nodes within the segment.
|
|
|
|
// A classic example is a printer that ought to be routable from other locations.
|
2022-01-30 16:38:45 +00:00
|
|
|
allowedLocationIPs []net.IPNet
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 10:53:40 +00:00
|
|
|
// NewTopology creates a new Topology struct from a given set of nodes and peers.
|
2022-01-30 16:38:45 +00:00
|
|
|
func NewTopology(nodes map[string]*Node, peers map[string]*Peer, granularity Granularity, hostname string, port int, key wgtypes.Key, subnet *net.IPNet, persistentKeepalive time.Duration, logger log.Logger) (*Topology, error) {
|
2021-05-27 07:01:22 +00:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
topoMap := make(map[string][]*Node)
|
|
|
|
for _, node := range nodes {
|
|
|
|
var location string
|
|
|
|
switch granularity {
|
2019-05-07 14:34:34 +00:00
|
|
|
case LogicalGranularity:
|
2021-01-24 13:19:01 +00:00
|
|
|
location = logicalLocationPrefix + node.Location
|
2021-02-22 19:28:16 +00:00
|
|
|
// Put node in a different location, if no private
|
|
|
|
// IP was found.
|
2021-01-24 13:19:01 +00:00
|
|
|
if node.InternalIP == nil {
|
|
|
|
location = nodeLocationPrefix + node.Name
|
|
|
|
}
|
2019-05-07 14:34:34 +00:00
|
|
|
case FullGranularity:
|
2021-01-24 13:19:01 +00:00
|
|
|
location = nodeLocationPrefix + node.Name
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
topoMap[location] = append(topoMap[location], node)
|
|
|
|
}
|
|
|
|
var localLocation string
|
|
|
|
switch granularity {
|
2019-05-07 14:34:34 +00:00
|
|
|
case LogicalGranularity:
|
2021-01-24 13:19:01 +00:00
|
|
|
localLocation = logicalLocationPrefix + nodes[hostname].Location
|
|
|
|
if nodes[hostname].InternalIP == nil {
|
|
|
|
localLocation = nodeLocationPrefix + hostname
|
|
|
|
}
|
2019-05-07 14:34:34 +00:00
|
|
|
case FullGranularity:
|
2021-01-24 13:19:01 +00:00
|
|
|
localLocation = nodeLocationPrefix + hostname
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 16:38:45 +00:00
|
|
|
t := Topology{
|
|
|
|
key: key,
|
|
|
|
port: port,
|
|
|
|
hostname: hostname,
|
|
|
|
location: localLocation,
|
|
|
|
persistentKeepalive: persistentKeepalive,
|
|
|
|
privateIP: nodes[hostname].InternalIP,
|
|
|
|
subnet: nodes[hostname].Subnet,
|
|
|
|
wireGuardCIDR: subnet,
|
|
|
|
discoveredEndpoints: make(map[string]*net.UDPAddr),
|
|
|
|
logger: logger,
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
for location := range topoMap {
|
|
|
|
// Sort the location so the result is stable.
|
|
|
|
sort.Slice(topoMap[location], func(i, j int) bool {
|
|
|
|
return topoMap[location][i].Name < topoMap[location][j].Name
|
|
|
|
})
|
|
|
|
leader := findLeader(topoMap[location])
|
|
|
|
if location == localLocation && topoMap[location][leader].Name == hostname {
|
|
|
|
t.leader = true
|
|
|
|
}
|
2022-01-30 16:38:45 +00:00
|
|
|
var allowedIPs []net.IPNet
|
2021-05-27 07:01:22 +00:00
|
|
|
allowedLocationIPsMap := make(map[string]struct{})
|
2022-01-30 16:38:45 +00:00
|
|
|
var allowedLocationIPs []net.IPNet
|
2019-01-18 01:50:10 +00:00
|
|
|
var cidrs []*net.IPNet
|
|
|
|
var hostnames []string
|
|
|
|
var privateIPs []net.IP
|
|
|
|
for _, node := range topoMap[location] {
|
|
|
|
// Allowed IPs should include:
|
|
|
|
// - the node's allocated subnet
|
|
|
|
// - the node's WireGuard IP
|
|
|
|
// - the node's internal IP
|
2021-05-27 07:01:22 +00:00
|
|
|
// - IPs that were specified by the allowed-location-ips annotation
|
2022-01-30 16:38:45 +00:00
|
|
|
if node.Subnet != nil {
|
|
|
|
allowedIPs = append(allowedIPs, *node.Subnet)
|
|
|
|
}
|
2021-05-27 07:01:22 +00:00
|
|
|
for _, ip := range node.AllowedLocationIPs {
|
|
|
|
if _, ok := allowedLocationIPsMap[ip.String()]; !ok {
|
|
|
|
allowedLocationIPs = append(allowedLocationIPs, ip)
|
|
|
|
allowedLocationIPsMap[ip.String()] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2021-01-24 13:19:01 +00:00
|
|
|
if node.InternalIP != nil {
|
2022-01-30 16:38:45 +00:00
|
|
|
allowedIPs = append(allowedIPs, *oneAddressCIDR(node.InternalIP.IP))
|
2021-01-24 13:19:01 +00:00
|
|
|
privateIPs = append(privateIPs, node.InternalIP.IP)
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
cidrs = append(cidrs, node.Subnet)
|
|
|
|
hostnames = append(hostnames, node.Name)
|
|
|
|
}
|
2021-05-27 07:01:22 +00:00
|
|
|
// The sorting has no function, but makes testing easier.
|
|
|
|
sort.Slice(allowedLocationIPs, func(i, j int) bool {
|
|
|
|
return allowedLocationIPs[i].String() < allowedLocationIPs[j].String()
|
|
|
|
})
|
2019-05-03 10:53:40 +00:00
|
|
|
t.segments = append(t.segments, &segment{
|
2021-04-21 17:47:29 +00:00
|
|
|
allowedIPs: allowedIPs,
|
|
|
|
endpoint: topoMap[location][leader].Endpoint,
|
|
|
|
key: topoMap[location][leader].Key,
|
|
|
|
persistentKeepalive: topoMap[location][leader].PersistentKeepalive,
|
|
|
|
location: location,
|
|
|
|
cidrs: cidrs,
|
|
|
|
hostnames: hostnames,
|
|
|
|
leader: leader,
|
|
|
|
privateIPs: privateIPs,
|
2021-05-27 07:01:22 +00:00
|
|
|
allowedLocationIPs: allowedLocationIPs,
|
2019-01-18 01:50:10 +00:00
|
|
|
})
|
2021-10-10 08:44:41 +00:00
|
|
|
level.Debug(t.logger).Log("msg", "generated segment", "location", location, "allowedIPs", allowedIPs, "endpoint", topoMap[location][leader].Endpoint, "cidrs", cidrs, "hostnames", hostnames, "leader", leader, "privateIPs", privateIPs, "allowedLocationIPs", allowedLocationIPs)
|
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
2019-05-03 10:53:40 +00:00
|
|
|
// Sort the Topology segments so the result is stable.
|
|
|
|
sort.Slice(t.segments, func(i, j int) bool {
|
|
|
|
return t.segments[i].location < t.segments[j].location
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, peer := range peers {
|
|
|
|
t.peers = append(t.peers, peer)
|
|
|
|
}
|
|
|
|
// Sort the Topology peers so the result is stable.
|
|
|
|
sort.Slice(t.peers, func(i, j int) bool {
|
|
|
|
return t.peers[i].Name < t.peers[j].Name
|
2019-01-18 01:50:10 +00:00
|
|
|
})
|
2019-05-10 00:07:05 +00:00
|
|
|
// We need to defensively deduplicate peer allowed IPs. If two peers claim the same IP,
|
|
|
|
// the WireGuard configuration could flap, causing the interface to churn.
|
|
|
|
t.peers = deduplicatePeerIPs(t.peers)
|
2021-04-21 17:47:29 +00:00
|
|
|
// Copy the host node DiscoveredEndpoints in the topology as a starting point.
|
|
|
|
for key := range nodes[hostname].DiscoveredEndpoints {
|
|
|
|
t.discoveredEndpoints[key] = nodes[hostname].DiscoveredEndpoints[key]
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
// Allocate IPs to the segment leaders in a stable, coordination-free manner.
|
|
|
|
a := newAllocator(*subnet)
|
2019-05-03 10:53:40 +00:00
|
|
|
for _, segment := range t.segments {
|
2019-01-18 01:50:10 +00:00
|
|
|
ipNet := a.next()
|
|
|
|
if ipNet == nil {
|
|
|
|
return nil, errors.New("failed to allocate an IP address; ran out of IP addresses")
|
|
|
|
}
|
|
|
|
segment.wireGuardIP = ipNet.IP
|
2022-01-30 16:38:45 +00:00
|
|
|
segment.allowedIPs = append(segment.allowedIPs, *oneAddressCIDR(ipNet.IP))
|
2019-05-03 10:53:40 +00:00
|
|
|
if t.leader && segment.location == t.location {
|
2020-02-20 20:27:50 +00:00
|
|
|
t.wireGuardCIDR = &net.IPNet{IP: ipNet.IP, Mask: subnet.Mask}
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
2021-04-21 17:47:29 +00:00
|
|
|
|
|
|
|
// Now that the topology is ordered, update the discoveredEndpoints map
|
|
|
|
// add new ones by going through the ordered topology: segments, nodes
|
|
|
|
for _, node := range topoMap[segment.location] {
|
|
|
|
for key := range node.DiscoveredEndpoints {
|
|
|
|
if _, ok := t.discoveredEndpoints[key]; !ok {
|
|
|
|
t.discoveredEndpoints[key] = node.DiscoveredEndpoints[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 07:01:22 +00:00
|
|
|
// Check for intersecting IPs in allowed location IPs
|
|
|
|
segment.allowedLocationIPs = t.filterAllowedLocationIPs(segment.allowedLocationIPs, segment.location)
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 08:44:41 +00:00
|
|
|
level.Debug(t.logger).Log("msg", "generated topology", "location", t.location, "hostname", t.hostname, "wireGuardIP", t.wireGuardCIDR, "privateIP", t.privateIP, "subnet", t.subnet, "leader", t.leader)
|
2019-01-18 01:50:10 +00:00
|
|
|
return &t, nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 16:38:45 +00:00
|
|
|
func intersect(n1, n2 net.IPNet) bool {
|
2021-05-27 07:01:22 +00:00
|
|
|
return n1.Contains(n2.IP) || n2.Contains(n1.IP)
|
|
|
|
}
|
|
|
|
|
2022-01-30 16:38:45 +00:00
|
|
|
func (t *Topology) filterAllowedLocationIPs(ips []net.IPNet, location string) (ret []net.IPNet) {
|
2021-05-27 07:01:22 +00:00
|
|
|
CheckIPs:
|
|
|
|
for _, ip := range ips {
|
|
|
|
for _, s := range t.segments {
|
|
|
|
// Check if allowed location IPs are also allowed in other locations.
|
|
|
|
if location != s.location {
|
|
|
|
for _, i := range s.allowedLocationIPs {
|
|
|
|
if intersect(ip, i) {
|
|
|
|
level.Warn(t.logger).Log("msg", "overlapping allowed location IPnets", "IP", ip.String(), "IP2", i.String(), "segment-location", s.location)
|
|
|
|
continue CheckIPs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if allowed location IPs intersect with the allowed IPs.
|
|
|
|
for _, i := range s.allowedIPs {
|
|
|
|
if intersect(ip, i) {
|
|
|
|
level.Warn(t.logger).Log("msg", "overlapping allowed location IPnet with allowed IPnets", "IP", ip.String(), "IP2", i.String(), "segment-location", s.location)
|
|
|
|
continue CheckIPs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if allowed location IPs intersect with the private IPs of the segment.
|
|
|
|
for _, i := range s.privateIPs {
|
|
|
|
if ip.Contains(i) {
|
|
|
|
level.Warn(t.logger).Log("msg", "overlapping allowed location IPnet with privateIP", "IP", ip.String(), "IP2", i.String(), "segment-location", s.location)
|
|
|
|
continue CheckIPs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if allowed location IPs intersect with allowed IPs of peers.
|
|
|
|
for _, p := range t.peers {
|
|
|
|
for _, i := range p.AllowedIPs {
|
|
|
|
if intersect(ip, i) {
|
|
|
|
level.Warn(t.logger).Log("msg", "overlapping allowed location IPnet with peer IPnet", "IP", ip.String(), "IP2", i.String(), "peer", p.Name)
|
|
|
|
continue CheckIPs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = append(ret, ip)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-30 16:38:45 +00:00
|
|
|
func (t *Topology) updateEndpoint(endpoint *wireguard.Endpoint, key wgtypes.Key, persistentKeepalive *time.Duration) *wireguard.Endpoint {
|
2021-04-21 17:47:29 +00:00
|
|
|
// Do not update non-nat peers
|
2022-01-30 16:38:45 +00:00
|
|
|
if persistentKeepalive == nil || *persistentKeepalive == time.Duration(0) {
|
2021-04-21 17:47:29 +00:00
|
|
|
return endpoint
|
|
|
|
}
|
2022-01-30 16:38:45 +00:00
|
|
|
e, ok := t.discoveredEndpoints[key.String()]
|
2021-04-21 17:47:29 +00:00
|
|
|
if ok {
|
2022-01-30 16:38:45 +00:00
|
|
|
return wireguard.NewEndpointFromUDPAddr(e)
|
2021-04-21 17:47:29 +00:00
|
|
|
}
|
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
// Conf generates a WireGuard configuration file for a given Topology.
|
2019-05-03 10:53:40 +00:00
|
|
|
func (t *Topology) Conf() *wireguard.Conf {
|
|
|
|
c := &wireguard.Conf{
|
2022-01-30 16:38:45 +00:00
|
|
|
Config: wgtypes.Config{
|
|
|
|
PrivateKey: &t.key,
|
|
|
|
ListenPort: &t.port,
|
|
|
|
ReplacePeers: true,
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, s := range t.segments {
|
|
|
|
if s.location == t.location {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-30 16:38:45 +00:00
|
|
|
peer := wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
AllowedIPs: append(s.allowedIPs, s.allowedLocationIPs...),
|
|
|
|
PersistentKeepaliveInterval: &t.persistentKeepalive,
|
|
|
|
PublicKey: s.key,
|
|
|
|
ReplaceAllowedIPs: true,
|
|
|
|
},
|
|
|
|
Endpoint: t.updateEndpoint(s.endpoint, s.key, &s.persistentKeepalive),
|
2019-05-03 10:53:40 +00:00
|
|
|
}
|
|
|
|
c.Peers = append(c.Peers, peer)
|
|
|
|
}
|
|
|
|
for _, p := range t.peers {
|
2022-01-30 16:38:45 +00:00
|
|
|
peer := wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
AllowedIPs: p.AllowedIPs,
|
|
|
|
PersistentKeepaliveInterval: &t.persistentKeepalive,
|
|
|
|
PresharedKey: p.PresharedKey,
|
|
|
|
PublicKey: p.PublicKey,
|
|
|
|
ReplaceAllowedIPs: true,
|
|
|
|
},
|
|
|
|
Endpoint: t.updateEndpoint(p.Endpoint, p.PublicKey, p.PersistentKeepaliveInterval),
|
2019-05-03 10:53:40 +00:00
|
|
|
}
|
|
|
|
c.Peers = append(c.Peers, peer)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-05-07 23:31:36 +00:00
|
|
|
// AsPeer generates the WireGuard peer configuration for the local location of the given Topology.
|
|
|
|
// This configuration can be used to configure this location as a peer of another WireGuard interface.
|
|
|
|
func (t *Topology) AsPeer() *wireguard.Peer {
|
|
|
|
for _, s := range t.segments {
|
|
|
|
if s.location != t.location {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-30 16:38:45 +00:00
|
|
|
p := &wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
AllowedIPs: s.allowedIPs,
|
|
|
|
PublicKey: s.key,
|
|
|
|
},
|
|
|
|
Endpoint: s.endpoint,
|
2019-05-07 23:31:36 +00:00
|
|
|
}
|
2022-01-30 16:38:45 +00:00
|
|
|
return p
|
2019-05-07 23:31:36 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 10:53:40 +00:00
|
|
|
// PeerConf generates a WireGuard configuration file for a given peer in a Topology.
|
|
|
|
func (t *Topology) PeerConf(name string) *wireguard.Conf {
|
2022-01-30 16:38:45 +00:00
|
|
|
var pka *time.Duration
|
|
|
|
var psk *wgtypes.Key
|
2020-03-03 19:10:20 +00:00
|
|
|
for i := range t.peers {
|
|
|
|
if t.peers[i].Name == name {
|
2022-01-30 16:38:45 +00:00
|
|
|
pka = t.peers[i].PersistentKeepaliveInterval
|
2020-05-05 09:36:39 +00:00
|
|
|
psk = t.peers[i].PresharedKey
|
2020-03-03 19:10:20 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 10:53:40 +00:00
|
|
|
c := &wireguard.Conf{}
|
|
|
|
for _, s := range t.segments {
|
2022-01-30 16:38:45 +00:00
|
|
|
peer := wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
2022-03-31 19:26:53 +00:00
|
|
|
AllowedIPs: append(s.allowedIPs, s.allowedLocationIPs...),
|
2022-01-30 16:38:45 +00:00
|
|
|
PersistentKeepaliveInterval: pka,
|
|
|
|
PresharedKey: psk,
|
|
|
|
PublicKey: s.key,
|
|
|
|
},
|
2022-04-08 11:42:13 +00:00
|
|
|
Endpoint: t.updateEndpoint(s.endpoint, s.key, &s.persistentKeepalive),
|
2019-05-03 10:53:40 +00:00
|
|
|
}
|
|
|
|
c.Peers = append(c.Peers, peer)
|
|
|
|
}
|
2020-03-03 19:10:20 +00:00
|
|
|
for i := range t.peers {
|
|
|
|
if t.peers[i].Name == name {
|
2019-05-03 10:53:40 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-01-30 16:38:45 +00:00
|
|
|
peer := wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
AllowedIPs: t.peers[i].AllowedIPs,
|
|
|
|
PersistentKeepaliveInterval: pka,
|
|
|
|
PublicKey: t.peers[i].PublicKey,
|
|
|
|
},
|
2022-04-08 11:42:13 +00:00
|
|
|
Endpoint: t.updateEndpoint(t.peers[i].Endpoint, t.peers[i].PublicKey, t.peers[i].PersistentKeepaliveInterval),
|
2019-05-03 10:53:40 +00:00
|
|
|
}
|
|
|
|
c.Peers = append(c.Peers, peer)
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
2019-05-03 10:53:40 +00:00
|
|
|
return c
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// oneAddressCIDR takes an IP address and returns a CIDR
|
|
|
|
// that contains only that address.
|
|
|
|
func oneAddressCIDR(ip net.IP) *net.IPNet {
|
|
|
|
return &net.IPNet{IP: ip, Mask: net.CIDRMask(len(ip)*8, len(ip)*8)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// findLeader selects a leader for the nodes in a segment;
|
|
|
|
// it will select the first node that says it should lead
|
|
|
|
// or the first node in the segment if none have volunteered,
|
|
|
|
// always preferring those with a public external IP address,
|
|
|
|
func findLeader(nodes []*Node) int {
|
|
|
|
var leaders, public []int
|
|
|
|
for i := range nodes {
|
|
|
|
if nodes[i].Leader {
|
2022-01-30 16:38:45 +00:00
|
|
|
if isPublic(nodes[i].Endpoint.IP()) {
|
2019-01-18 01:50:10 +00:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
leaders = append(leaders, i)
|
2020-02-22 16:17:13 +00:00
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
2022-01-30 16:38:45 +00:00
|
|
|
if nodes[i].Endpoint.IP() != nil && isPublic(nodes[i].Endpoint.IP()) {
|
2019-01-18 01:50:10 +00:00
|
|
|
public = append(public, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(leaders) != 0 {
|
|
|
|
return leaders[0]
|
|
|
|
}
|
|
|
|
if len(public) != 0 {
|
|
|
|
return public[0]
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2019-05-10 00:07:05 +00:00
|
|
|
|
|
|
|
func deduplicatePeerIPs(peers []*Peer) []*Peer {
|
|
|
|
ps := make([]*Peer, len(peers))
|
|
|
|
ips := make(map[string]struct{})
|
|
|
|
for i, peer := range peers {
|
|
|
|
p := Peer{
|
|
|
|
Name: peer.Name,
|
|
|
|
Peer: wireguard.Peer{
|
2022-01-30 16:38:45 +00:00
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: peer.PersistentKeepaliveInterval,
|
|
|
|
PresharedKey: peer.PresharedKey,
|
|
|
|
PublicKey: peer.PublicKey,
|
|
|
|
},
|
|
|
|
Endpoint: peer.Endpoint,
|
2019-05-10 00:07:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, ip := range peer.AllowedIPs {
|
|
|
|
if _, ok := ips[ip.String()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.AllowedIPs = append(p.AllowedIPs, ip)
|
|
|
|
ips[ip.String()] = struct{}{}
|
|
|
|
}
|
|
|
|
ps[i] = &p
|
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}
|