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

@@ -15,7 +15,6 @@ import (
"github.com/stv0g/cunicu/pkg/wg" "github.com/stv0g/cunicu/pkg/wg"
"go.uber.org/zap" "go.uber.org/zap"
"golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"github.com/stv0g/cunicu/pkg/signaling" "github.com/stv0g/cunicu/pkg/signaling"
) )
@@ -250,11 +249,3 @@ func (d *Daemon) ForEachInterface(cb func(i *Interface) error) error {
return nil 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()
}

View File

@@ -3,6 +3,8 @@ package daemon
import ( import (
"github.com/stv0g/cunicu/pkg/config" "github.com/stv0g/cunicu/pkg/config"
"github.com/stv0g/cunicu/pkg/core" "github.com/stv0g/cunicu/pkg/core"
"github.com/stv0g/cunicu/pkg/crypto"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
type Interface struct { type Interface struct {
@@ -67,3 +69,37 @@ func (i *Interface) Close() error {
return nil 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)
}

View File

@@ -10,9 +10,9 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/stv0g/cunicu/pkg/core" "github.com/stv0g/cunicu/pkg/core"
"github.com/stv0g/cunicu/pkg/crypto" "github.com/stv0g/cunicu/pkg/crypto"
"github.com/stv0g/cunicu/pkg/daemon"
"github.com/stv0g/cunicu/pkg/device" "github.com/stv0g/cunicu/pkg/device"
"github.com/stv0g/cunicu/pkg/watcher" "github.com/stv0g/cunicu/pkg/watcher"
"github.com/stv0g/cunicu/pkg/wg"
"github.com/stv0g/cunicu/test" "github.com/stv0g/cunicu/test"
g "github.com/stv0g/gont/pkg" g "github.com/stv0g/gont/pkg"
"golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl"
@@ -77,7 +77,7 @@ var _ = Describe("watcher", func() {
} }
TestSync := func() { TestSync := func() {
var i *core.Interface var i *daemon.Interface
var p *core.Peer var p *core.Peer
var d device.Device var d device.Device
@@ -91,7 +91,9 @@ var _ = Describe("watcher", func() {
var ie core.InterfaceAddedEvent var ie core.InterfaceAddedEvent
Expect(h.Events).To(test.ReceiveEvent(&ie)) Expect(h.Events).To(test.ReceiveEvent(&ie))
i = ie.Interface i = &daemon.Interface{
Interface: ie.Interface,
}
Expect(ie.Interface).NotTo(BeNil()) Expect(ie.Interface).NotTo(BeNil())
Expect(ie.Interface.Name()).To(Equal(devName)) Expect(ie.Interface.Name()).To(Equal(devName))
@@ -101,10 +103,8 @@ var _ = Describe("watcher", func() {
oldListenPort := i.ListenPort oldListenPort := i.ListenPort
newListenPort := oldListenPort + 1 newListenPort := oldListenPort + 1
err = i.Configure(&wg.Config{ err = i.ConfigureDevice(wgtypes.Config{
Config: wgtypes.Config{
ListenPort: &newListenPort, ListenPort: &newListenPort,
},
}) })
Expect(err).To(Succeed()) Expect(err).To(Succeed())