Skip to content

Commit

Permalink
wireguard: create interface before adding it
Browse files Browse the repository at this point in the history
  • Loading branch information
USA-RedDragon committed Feb 12, 2024
1 parent 305adfc commit 87f34bb
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/wireguard/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/USA-RedDragon/aredn-manager/internal/db/models"
"github.com/phayes/freeport"
"github.com/vishvananda/netlink"
"golang.org/x/sync/errgroup"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
Expand Down Expand Up @@ -123,13 +124,41 @@ func GenerateWireguardInterfaceName(peer models.Tunnel) string {
return fmt.Sprintf("wgc%d", peer.ID)
}

type WG struct {
netlink.LinkAttrs
}

func (wglink *WG) Attrs() *netlink.LinkAttrs {
return &wglink.LinkAttrs
}

func (wglink *WG) Type() string {
return "wireguard"
}

func (m *Manager) addPeer(peer models.Tunnel) {
// Create a new wireguard interface listening on the port from the peer tunnel
// If the peer is a client, then the password is the public key of the client
// If the peer is a server, then the password is the private key of the server
log.Println("adding peer", peer)

iface := GenerateWireguardInterfaceName(peer)

_, err := netlink.LinkByName(iface)
if err == nil {
log.Println("wireguard interface already exists", iface)
} else {
la := netlink.NewLinkAttrs()
la.Name = iface
la.MTU = 1420
wgdev := &WG{LinkAttrs: la}
err := netlink.LinkAdd(wgdev)
if err != nil {
log.Println("failed to add wireguard device", err)
return
}
}

var privkey wgtypes.Key
portInt := int(peer.WireguardPort)
var peers []wgtypes.PeerConfig
Expand Down

0 comments on commit 87f34bb

Please sign in to comment.