From e5dcbd8e35c55f5660b96ac4e4edd59c1930c3e0 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Wed, 4 Mar 2020 20:30:05 +0000 Subject: [PATCH] successful adding of peers! --- configtypes.go | 28 ++++++++++++++++++++++++++++ up.go | 6 ++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/configtypes.go b/configtypes.go index 94df0df..93a8fab 100644 --- a/configtypes.go +++ b/configtypes.go @@ -5,7 +5,9 @@ import ( "io/ioutil" "net" "os" + "time" + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "github.com/go-playground/validator/v10" ) @@ -143,3 +145,29 @@ func (conf DsnetConfig) MustAllocateIP() net.IP { return net.IP{} } + +func (conf DsnetConfig) GetWgPeerConfigs() []wgtypes.PeerConfig { + wgPeers := make([]wgtypes.PeerConfig, 0, len(conf.Peers)) + + interval := time.Second * KEEPALIVE_SECONDS; + + for _, peer := range conf.Peers { + wgPeers = append(wgPeers, wgtypes.PeerConfig{ + PublicKey: peer.PublicKey.Key, + Remove: false, + UpdateOnly: false, + PresharedKey: &peer.PresharedKey.Key, + Endpoint: nil, + PersistentKeepaliveInterval: &interval, + ReplaceAllowedIPs: true, + AllowedIPs: []net.IPNet{ + net.IPNet{ + IP: peer.IP, + Mask: conf.Network.IPNet.Mask, + }, + }, + }) + } + + return wgPeers +} diff --git a/up.go b/up.go index 07f2b72..2fc45e6 100644 --- a/up.go +++ b/up.go @@ -39,15 +39,17 @@ func CreateInterface(conf *DsnetConfig) { ExitFail("Could not add addr %s to interface %s", addr.IP, err) } - deviceConfig := wgtypes.Config{ + wgConfig := wgtypes.Config{ PrivateKey: &conf.PrivateKey.Key, ListenPort: &conf.ListenPort, + ReplacePeers: true, + Peers: conf.GetWgPeerConfigs(), } wg, err := wgctrl.New() check(err) - err = wg.ConfigureDevice(linkAttrs.Name, deviceConfig) + err = wg.ConfigureDevice(linkAttrs.Name, wgConfig) if err != nil { ExitFail("Could not configure device '%s' (%v)", linkAttrs.Name, err)