Skip to content

Commit

Permalink
fix: autosave wireguard conf files (#303)
Browse files Browse the repository at this point in the history
* fix: autosave wireguard conf files

- Fix subscription to Interface and Peer updates topics
- Remove admin permissions validation
- Update file on peer deletion
- Change save condition to configured storage path only, as initialized interface is not nil

* Added  comment to peer config for prometheus exporter
  • Loading branch information
bonddim authored Sep 22, 2024
1 parent 605841f commit 2428ded
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
49 changes: 17 additions & 32 deletions internal/app/configfile/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ import (
"bytes"
"context"
"fmt"
"io"
"os"
"strings"

"github.com/h44z/wg-portal/internal/app"
"github.com/h44z/wg-portal/internal/config"
"github.com/h44z/wg-portal/internal/domain"
"github.com/sirupsen/logrus"
evbus "github.com/vardius/message-bus"
"github.com/yeqown/go-qrcode/v2"
"io"
"os"
"strings"
)

type Manager struct {
cfg *config.Config
bus evbus.MessageBus
tplHandler *TemplateHandler

fsRepo FileSystemRepo // can be nil if storing the configuration is disabled
fsRepo FileSystemRepo
users UserDatabaseRepo
wg WireguardDatabaseRepo
}
Expand All @@ -42,18 +43,18 @@ func NewConfigFileManager(cfg *config.Config, bus evbus.MessageBus, users UserDa
wg: wg,
}

if err := m.createStorageDirectory(); err != nil {
return nil, err
if m.cfg.Advanced.ConfigStoragePath != "" {
if err := m.createStorageDirectory(); err != nil {
return nil, err
}

m.connectToMessageBus()
}

return m, nil
}

func (m Manager) createStorageDirectory() error {
if m.cfg.Advanced.ConfigStoragePath == "" {
return nil // no storage path configured, skip initialization step
}

err := os.MkdirAll(m.cfg.Advanced.ConfigStoragePath, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create configuration storage path %s: %w",
Expand All @@ -64,34 +65,24 @@ func (m Manager) createStorageDirectory() error {
}

func (m Manager) connectToMessageBus() {
if m.fsRepo == nil {
return // skip subscription
}

_ = m.bus.Subscribe(app.TopicInterfaceUpdated, m.handleInterfaceUpdatedEvent)
_ = m.bus.Subscribe(app.TopicPeerInterfaceUpdated, m.handleInterfaceUpdatedEvent)
_ = m.bus.Subscribe(app.TopicPeerInterfaceUpdated, m.handlePeerInterfaceUpdatedEvent)
}

func (m Manager) handleInterfaceUpdatedEvent(iface *domain.Interface) {
logrus.Errorf("handling interface updated event for %s", iface.Identifier)

if !iface.SaveConfig || m.fsRepo == nil {
if !iface.SaveConfig {
return
}

logrus.Debugf("handling interface updated event for %s", iface.Identifier)

err := m.PersistInterfaceConfig(context.Background(), iface.Identifier)
if err != nil {
logrus.Errorf("failed to automatically persist interface config for %s: %v", iface.Identifier, err)
}
}

func (m Manager) handlePeerInterfaceUpdatedEvent(id domain.InterfaceIdentifier) {
logrus.Errorf("handling interface updated event for %s", id)

if m.fsRepo == nil {
return
}

peerInterface, err := m.wg.GetInterface(context.Background(), id)
if err != nil {
logrus.Errorf("failed to load interface %s: %v", id, err)
Expand All @@ -102,6 +93,8 @@ func (m Manager) handlePeerInterfaceUpdatedEvent(id domain.InterfaceIdentifier)
return
}

logrus.Debugf("handling peer interface updated event for %s", id)

err = m.PersistInterfaceConfig(context.Background(), peerInterface.Identifier)
if err != nil {
logrus.Errorf("failed to automatically persist interface config for %s: %v", peerInterface.Identifier, err)
Expand Down Expand Up @@ -184,14 +177,6 @@ func (m Manager) GetPeerConfigQrCode(ctx context.Context, id domain.PeerIdentifi
}

func (m Manager) PersistInterfaceConfig(ctx context.Context, id domain.InterfaceIdentifier) error {
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
return err
}

if m.fsRepo == nil {
return fmt.Errorf("peristing configuration is not supported")
}

iface, peers, err := m.wg.GetInterfaceAndPeers(ctx, id)
if err != nil {
return fmt.Errorf("failed to fetch interface %s: %w", id, err)
Expand Down
4 changes: 3 additions & 1 deletion internal/app/configfile/tpl_files/wg_interface.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ PostDown = {{ .Interface.PostDown }}
{{range .Peers}}
{{- if not .IsDisabled}}
[Peer]
{{/* `friendly_name` used by https://github.com/MindFlavor/prometheus_wireguard_exporter */ -}}
# friendly_name = {{ .DisplayName }}
# -WGP- Peer: {{.Identifier}}
# -WGP- Created: {{.CreatedAt}}
# -WGP- Updated: {{.UpdatedAt}}
Expand All @@ -86,4 +88,4 @@ Endpoint = {{ .Endpoint.GetValue }}
PersistentKeepalive = {{ .PersistentKeepalive.GetValue }}
{{- end}}
{{- end}}
{{end}}
{{end}}
8 changes: 7 additions & 1 deletion internal/app/wireguard/wireguard_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/h44z/wg-portal/internal"
"github.com/h44z/wg-portal/internal/app"
"github.com/h44z/wg-portal/internal/domain"
"github.com/sirupsen/logrus"
"time"
)

func (m Manager) CreateDefaultPeer(ctx context.Context, userId domain.UserIdentifier) error {
Expand Down Expand Up @@ -253,6 +254,11 @@ func (m Manager) DeletePeer(ctx context.Context, id domain.PeerIdentifier) error
return fmt.Errorf("failed to delete peer %s: %w", id, err)
}

// Update routes after peers have changed
m.bus.Publish(app.TopicRouteUpdate, "peers updated")
// Update interface after peers have changed
m.bus.Publish(app.TopicPeerInterfaceUpdated, peer.InterfaceIdentifier)

return nil
}

Expand Down

0 comments on commit 2428ded

Please sign in to comment.