Skip to content

Commit

Permalink
handle udp resolve of endpoints in addPeer
Browse files Browse the repository at this point in the history
- improve logging
- remove continue on device failures to always execute ipv6 code as well
  • Loading branch information
andreek committed Nov 7, 2021
1 parent 9ab7488 commit 3ced711
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
9 changes: 7 additions & 2 deletions backend/wireguard/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,12 @@ func (dev *wgDevice) ConfigureV6(devIP *ip.IP6, flannelnet ip.IP6Net) error {
return nil
}

func (dev *wgDevice) addPeer(udpEndpoint *net.UDPAddr, peerPublicKeyRaw string, peerSubnet *net.IPNet) error {
func (dev *wgDevice) addPeer(publicEndpoint string, peerPublicKeyRaw string, peerSubnet *net.IPNet) error {
udpEndpoint, err := net.ResolveUDPAddr("udp", publicEndpoint)
if err != nil {
return fmt.Errorf("failed to resolve UDP address: %v", err)
}

peerPublicKey, err := wgtypes.ParseKey(peerPublicKeyRaw)
if err != nil {
return fmt.Errorf("failed to parse publicKey: %v", err)
Expand Down Expand Up @@ -305,7 +310,7 @@ func (dev *wgDevice) addPeer(udpEndpoint *net.UDPAddr, peerPublicKeyRaw string,

err = client.ConfigureDevice(dev.attrs.name, wgcfg)
if err != nil {
return fmt.Errorf("failed to add peer %v", err)
return fmt.Errorf("failed to configure device %v", err)
}

client.Close()
Expand Down
33 changes: 8 additions & 25 deletions backend/wireguard/wireguard_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package wireguard
import (
"encoding/json"
"fmt"
"net"
"sync"

"github.com/flannel-io/flannel/backend"
Expand Down Expand Up @@ -47,15 +46,15 @@ type network struct {
}

func newNetwork(sm subnet.Manager, extIface *backend.ExternalInterface, dev, v6Dev *wgDevice, lease *subnet.Lease) (*network, error) {
nw := &network{
n := &network{
dev: dev,
v6Dev: v6Dev,
extIface: extIface,
lease: lease,
sm: sm,
}

return nw, nil
return n, nil
}

func (n *network) Lease() *subnet.Lease {
Expand Down Expand Up @@ -116,18 +115,11 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
}

publicEndpoint := fmt.Sprintf("%s:%d", event.Lease.Attrs.PublicIP.String(), n.dev.attrs.listenPort)
udpEndpoint, err := net.ResolveUDPAddr("udp", publicEndpoint)
if err != nil {
log.Errorf("failed to resolve UDP address: %v", err)
continue
}

if err := n.dev.addPeer(
udpEndpoint,
publicEndpoint,
wireguardAttrs.PublicKey,
event.Lease.Subnet.ToIPNet()); err != nil {
log.Errorf("failed to setup peer %v", err)
continue
log.Errorf("failed to setup ipv4 peer (%s): %v", wireguardAttrs.PublicKey, err)
}
}

Expand All @@ -142,18 +134,11 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
}

publicEndpoint := fmt.Sprintf("[%s]:%d", event.Lease.Attrs.PublicIPv6.String(), n.v6Dev.attrs.listenPort)
udpEndpoint, err := net.ResolveUDPAddr("udp", publicEndpoint)
if err != nil {
log.Errorf("failed to resolve UDP address: %v", err)
continue
}

if err := n.v6Dev.addPeer(
udpEndpoint,
publicEndpoint,
wireguardAttrs.PublicKey,
event.Lease.IPv6Subnet.ToIPNet()); err != nil {
log.Errorf("failed to setup peer %v", err)
continue
log.Errorf("failed to setup ipv6 peer (%s): %v", wireguardAttrs.PublicKey, err)
}
}

Expand All @@ -177,8 +162,7 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
if err := n.dev.removePeer(
wireguardAttrs.PublicKey,
); err != nil {
log.Errorf("failed to remove peer %v", err)
continue
log.Errorf("failed to remove ipv4 peer (%s): %v", wireguardAttrs.PublicKey, err)
}
}

Expand All @@ -194,8 +178,7 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
if err := n.v6Dev.removePeer(
wireguardAttrs.PublicKey,
); err != nil {
log.Errorf("failed to remove peer %v", err)
continue
log.Errorf("failed to remove ipv6 peer (%s): %v", wireguardAttrs.PublicKey, err)
}
}

Expand Down

0 comments on commit 3ced711

Please sign in to comment.