diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 8755712f..402cacd6 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -15,7 +15,6 @@ import ( "github.com/stv0g/cunicu/pkg/wg" "go.uber.org/zap" "golang.zx2c4.com/wireguard/wgctrl" - "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "github.com/stv0g/cunicu/pkg/signaling" ) @@ -250,11 +249,3 @@ func (d *Daemon) ForEachInterface(cb func(i *Interface) error) error { return nil } - -func (d *Daemon) ConfigureDevice(name string, cfg wgtypes.Config) error { - if err := d.client.ConfigureDevice(name, cfg); err != nil { - return err - } - - return d.watcher.Sync() -} diff --git a/pkg/daemon/interface.go b/pkg/daemon/interface.go index 32814087..c22af50c 100644 --- a/pkg/daemon/interface.go +++ b/pkg/daemon/interface.go @@ -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) +} diff --git a/pkg/watcher/watcher_linux_test.go b/pkg/watcher/watcher_linux_test.go index 508fc003..171f0d01 100644 --- a/pkg/watcher/watcher_linux_test.go +++ b/pkg/watcher/watcher_linux_test.go @@ -10,9 +10,9 @@ import ( . "github.com/onsi/gomega" "github.com/stv0g/cunicu/pkg/core" "github.com/stv0g/cunicu/pkg/crypto" + "github.com/stv0g/cunicu/pkg/daemon" "github.com/stv0g/cunicu/pkg/device" "github.com/stv0g/cunicu/pkg/watcher" - "github.com/stv0g/cunicu/pkg/wg" "github.com/stv0g/cunicu/test" g "github.com/stv0g/gont/pkg" "golang.zx2c4.com/wireguard/wgctrl" @@ -77,7 +77,7 @@ var _ = Describe("watcher", func() { } TestSync := func() { - var i *core.Interface + var i *daemon.Interface var p *core.Peer var d device.Device @@ -91,7 +91,9 @@ var _ = Describe("watcher", func() { var ie core.InterfaceAddedEvent Expect(h.Events).To(test.ReceiveEvent(&ie)) - i = ie.Interface + i = &daemon.Interface{ + Interface: ie.Interface, + } Expect(ie.Interface).NotTo(BeNil()) Expect(ie.Interface.Name()).To(Equal(devName)) @@ -101,10 +103,8 @@ var _ = Describe("watcher", func() { oldListenPort := i.ListenPort newListenPort := oldListenPort + 1 - err = i.Configure(&wg.Config{ - Config: wgtypes.Config{ - ListenPort: &newListenPort, - }, + err = i.ConfigureDevice(wgtypes.Config{ + ListenPort: &newListenPort, }) Expect(err).To(Succeed())