daemon: move more code from core.Interface to daemon.Interface

Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
Steffen Vogel
2022-10-04 11:22:09 +02:00
parent eb57b4c357
commit b10c7f37ce
3 changed files with 43 additions and 16 deletions

View File

@@ -3,6 +3,8 @@ package daemon
import (
"github.com/stv0g/cunicu/pkg/config"
"github.com/stv0g/cunicu/pkg/core"
"github.com/stv0g/cunicu/pkg/crypto"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
type Interface struct {
@@ -67,3 +69,37 @@ func (i *Interface) Close() error {
return nil
}
func (i *Interface) ConfigureDevice(cfg wgtypes.Config) error {
if err := i.Daemon.client.ConfigureDevice(i.Name(), cfg); err != nil {
return err
}
return i.Daemon.watcher.Sync()
}
func (i *Interface) AddPeer(pcfg *wgtypes.PeerConfig) error {
return i.ConfigureDevice(wgtypes.Config{
Peers: []wgtypes.PeerConfig{*pcfg},
})
}
func (i *Interface) UpdatePeer(pcfg *wgtypes.PeerConfig) error {
pcfg2 := *pcfg
pcfg2.UpdateOnly = true
return i.AddPeer(&pcfg2)
}
func (i *Interface) RemovePeer(pk crypto.Key) error {
cfg := wgtypes.Config{
Peers: []wgtypes.PeerConfig{
{
PublicKey: wgtypes.Key(pk),
Remove: true,
},
},
}
return i.ConfigureDevice(cfg)
}