successful adding of peers!

This commit is contained in:
Callan Bryant 2020-03-04 20:30:05 +00:00
parent deef2574e7
commit e5dcbd8e35
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
2 changed files with 32 additions and 2 deletions

View File

@ -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
}

6
up.go
View File

@ -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)