Define WireGuard PersistentKeepAlive via Annotation (#31)
* Add WireGuardPersistentKeepAlive to mesh.Node * Connect to configuration * Shorten keepalive key * Fix casing on keepalive * Add annotated keepalive value to peer functions
This commit is contained in:
@@ -79,11 +79,12 @@ type Node struct {
|
||||
LastSeen int64
|
||||
// Leader is a suggestion to Kilo that
|
||||
// the node wants to lead its segment.
|
||||
Leader bool
|
||||
Location string
|
||||
Name string
|
||||
Subnet *net.IPNet
|
||||
WireGuardIP *net.IPNet
|
||||
Leader bool
|
||||
Location string
|
||||
Name string
|
||||
PersistentKeepalive int
|
||||
Subnet *net.IPNet
|
||||
WireGuardIP *net.IPNet
|
||||
}
|
||||
|
||||
// Ready indicates whether or not the node is ready.
|
||||
|
@@ -64,6 +64,9 @@ type segment struct {
|
||||
hostnames []string
|
||||
// leader is the index of the leader of the segment.
|
||||
leader int
|
||||
// persistentKeepalive is the interval in seconds of the emission
|
||||
// of keepalive packets to the peer.
|
||||
persistentKeepalive 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
|
||||
@@ -117,14 +120,15 @@ func NewTopology(nodes map[string]*Node, peers map[string]*Peer, granularity Gra
|
||||
privateIPs = append(privateIPs, node.InternalIP.IP)
|
||||
}
|
||||
t.segments = append(t.segments, &segment{
|
||||
allowedIPs: allowedIPs,
|
||||
endpoint: topoMap[location][leader].ExternalIP.IP,
|
||||
key: topoMap[location][leader].Key,
|
||||
location: location,
|
||||
cidrs: cidrs,
|
||||
hostnames: hostnames,
|
||||
leader: leader,
|
||||
privateIPs: privateIPs,
|
||||
allowedIPs: allowedIPs,
|
||||
endpoint: topoMap[location][leader].ExternalIP.IP,
|
||||
key: topoMap[location][leader].Key,
|
||||
location: location,
|
||||
cidrs: cidrs,
|
||||
hostnames: hostnames,
|
||||
leader: leader,
|
||||
privateIPs: privateIPs,
|
||||
persistentKeepalive: topoMap[location][leader].PersistentKeepalive,
|
||||
})
|
||||
}
|
||||
// Sort the Topology segments so the result is stable.
|
||||
@@ -334,7 +338,8 @@ func (t *Topology) Conf() *wireguard.Conf {
|
||||
IP: s.endpoint,
|
||||
Port: uint32(t.port),
|
||||
},
|
||||
PublicKey: s.key,
|
||||
PublicKey: s.key,
|
||||
PersistentKeepalive: s.persistentKeepalive,
|
||||
}
|
||||
c.Peers = append(c.Peers, peer)
|
||||
}
|
||||
@@ -363,7 +368,8 @@ func (t *Topology) AsPeer() *wireguard.Peer {
|
||||
IP: s.endpoint,
|
||||
Port: uint32(t.port),
|
||||
},
|
||||
PublicKey: s.key,
|
||||
PersistentKeepalive: s.persistentKeepalive,
|
||||
PublicKey: s.key,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -379,7 +385,8 @@ func (t *Topology) PeerConf(name string) *wireguard.Conf {
|
||||
IP: s.endpoint,
|
||||
Port: uint32(t.port),
|
||||
},
|
||||
PublicKey: s.key,
|
||||
PersistentKeepalive: s.persistentKeepalive,
|
||||
PublicKey: s.key,
|
||||
}
|
||||
c.Peers = append(c.Peers, peer)
|
||||
}
|
||||
|
@@ -39,12 +39,13 @@ func setup(t *testing.T) (map[string]*Node, map[string]*Peer, []byte, uint32) {
|
||||
i2 := &net.IPNet{IP: net.ParseIP("192.168.0.2").To4(), Mask: net.CIDRMask(32, 32)}
|
||||
nodes := map[string]*Node{
|
||||
"a": {
|
||||
Name: "a",
|
||||
ExternalIP: e1,
|
||||
InternalIP: i1,
|
||||
Location: "1",
|
||||
Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
|
||||
Key: []byte("key1"),
|
||||
Name: "a",
|
||||
ExternalIP: e1,
|
||||
InternalIP: i1,
|
||||
Location: "1",
|
||||
Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
|
||||
Key: []byte("key1"),
|
||||
PersistentKeepalive: 25,
|
||||
},
|
||||
"b": {
|
||||
Name: "b",
|
||||
@@ -117,14 +118,15 @@ func TestNewTopology(t *testing.T) {
|
||||
wireGuardCIDR: &net.IPNet{IP: w1, Mask: net.CIDRMask(16, 32)},
|
||||
segments: []*segment{
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Location,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
wireGuardIP: w1,
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Location,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
persistentKeepalive: nodes["a"].PersistentKeepalive,
|
||||
wireGuardIP: w1,
|
||||
},
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
|
||||
@@ -153,14 +155,15 @@ func TestNewTopology(t *testing.T) {
|
||||
wireGuardCIDR: &net.IPNet{IP: w2, Mask: net.CIDRMask(16, 32)},
|
||||
segments: []*segment{
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Location,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
wireGuardIP: w1,
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Location,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
persistentKeepalive: nodes["a"].PersistentKeepalive,
|
||||
wireGuardIP: w1,
|
||||
},
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
|
||||
@@ -189,14 +192,15 @@ func TestNewTopology(t *testing.T) {
|
||||
wireGuardCIDR: nil,
|
||||
segments: []*segment{
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Location,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
wireGuardIP: w1,
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Location,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
persistentKeepalive: nodes["a"].PersistentKeepalive,
|
||||
wireGuardIP: w1,
|
||||
},
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
|
||||
@@ -225,14 +229,15 @@ func TestNewTopology(t *testing.T) {
|
||||
wireGuardCIDR: &net.IPNet{IP: w1, Mask: net.CIDRMask(16, 32)},
|
||||
segments: []*segment{
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Name,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
wireGuardIP: w1,
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Name,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
persistentKeepalive: nodes["a"].PersistentKeepalive,
|
||||
wireGuardIP: w1,
|
||||
},
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
|
||||
@@ -271,14 +276,15 @@ func TestNewTopology(t *testing.T) {
|
||||
wireGuardCIDR: &net.IPNet{IP: w2, Mask: net.CIDRMask(16, 32)},
|
||||
segments: []*segment{
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Name,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
wireGuardIP: w1,
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Name,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
persistentKeepalive: nodes["a"].PersistentKeepalive,
|
||||
wireGuardIP: w1,
|
||||
},
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
|
||||
@@ -317,14 +323,15 @@ func TestNewTopology(t *testing.T) {
|
||||
wireGuardCIDR: &net.IPNet{IP: w3, Mask: net.CIDRMask(16, 32)},
|
||||
segments: []*segment{
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Name,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
wireGuardIP: w1,
|
||||
allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
|
||||
endpoint: nodes["a"].ExternalIP.IP,
|
||||
key: nodes["a"].Key,
|
||||
location: nodes["a"].Name,
|
||||
cidrs: []*net.IPNet{nodes["a"].Subnet},
|
||||
hostnames: []string{"a"},
|
||||
privateIPs: []net.IP{nodes["a"].InternalIP.IP},
|
||||
persistentKeepalive: nodes["a"].PersistentKeepalive,
|
||||
wireGuardIP: w1,
|
||||
},
|
||||
{
|
||||
allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
|
||||
@@ -1027,6 +1034,7 @@ AllowedIPs = 10.5.0.3/24
|
||||
[Peer]
|
||||
PublicKey = key1
|
||||
Endpoint = 10.1.0.1:51820
|
||||
PersistentKeepalive = 25
|
||||
AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
|
||||
|
||||
[Peer]
|
||||
@@ -1051,6 +1059,7 @@ AllowedIPs = 10.5.0.3/24
|
||||
[Peer]
|
||||
PublicKey = key1
|
||||
Endpoint = 10.1.0.1:51820
|
||||
PersistentKeepalive = 25
|
||||
AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
|
||||
|
||||
[Peer]
|
||||
@@ -1104,6 +1113,7 @@ AllowedIPs = 10.5.0.3/24
|
||||
[Peer]
|
||||
PublicKey = key1
|
||||
Endpoint = 10.1.0.1:51820
|
||||
PersistentKeepalive = 25
|
||||
AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
|
||||
|
||||
[Peer]
|
||||
@@ -1133,6 +1143,7 @@ AllowedIPs = 10.5.0.3/24
|
||||
[Peer]
|
||||
PublicKey = key1
|
||||
Endpoint = 10.1.0.1:51820
|
||||
PersistentKeepalive = 25
|
||||
AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
|
||||
|
||||
[Peer]
|
||||
|
Reference in New Issue
Block a user