dsnet/sync.go

63 lines
1.6 KiB
Go
Raw Normal View History

2020-03-04 23:06:15 +01:00
package dsnet
2020-03-04 23:50:44 +01:00
import (
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func Sync() {
2020-03-04 23:06:15 +01:00
// TODO check device settings first
conf := MustLoadDsnetConfig()
ConfigureDevice(conf)
}
2020-03-04 23:50:44 +01:00
func ConfigureDevice(conf *DsnetConfig) {
wg, err := wgctrl.New()
check(err)
defer wg.Close()
dev, err := wg.Device(conf.InterfaceName)
if err != nil {
ExitFail("Could not retrieve device '%s' (%v)", conf.InterfaceName, err)
}
peers := conf.GetWgPeerConfigs()
// compare peers to see if any exist on the device and not the config. If
// so, they should be removed by appending a dummy peer with Remove:true + pubkey.
knownKeys := make(map[wgtypes.Key]bool)
for _, peer := range peers {
knownKeys[peer.PublicKey] = true
}
// find deleted peers, and append dummy "remove" peers
for _, peer := range dev.Peers {
if !knownKeys[peer.PublicKey] {
peers = append(peers, wgtypes.PeerConfig{
PublicKey: peer.PublicKey,
2020-03-07 22:57:00 +01:00
Remove: true,
})
}
}
2020-03-04 23:50:44 +01:00
wgConfig := wgtypes.Config{
2020-03-07 22:57:00 +01:00
PrivateKey: &conf.PrivateKey.Key,
ListenPort: &conf.ListenPort,
// ReplacePeers with the same peers results in those peers losing
// connection, so it's not possible to do declarative configuration
// idempotently with ReplacePeers like I had assumed. Instead, peers
// must be removed imperatively with Remove:true. Peers can still be
// added/updated with ConfigureDevice declaratively.
ReplacePeers: false,
Peers: peers,
2020-03-04 23:50:44 +01:00
}
err = wg.ConfigureDevice(conf.InterfaceName, wgConfig)
if err != nil {
ExitFail("Could not configure device '%s' (%v)", conf.InterfaceName, err)
}
}