diff --git a/internal/app/configfile/manager.go b/internal/app/configfile/manager.go index 0f695315..3ec1a961 100644 --- a/internal/app/configfile/manager.go +++ b/internal/app/configfile/manager.go @@ -5,15 +5,16 @@ 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 { @@ -21,7 +22,7 @@ type Manager struct { bus evbus.MessageBus tplHandler *TemplateHandler - fsRepo FileSystemRepo // can be nil if storing the configuration is disabled + fsRepo FileSystemRepo users UserDatabaseRepo wg WireguardDatabaseRepo } @@ -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", @@ -64,21 +65,17 @@ 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) @@ -86,12 +83,6 @@ func (m Manager) handleInterfaceUpdatedEvent(iface *domain.Interface) { } 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) @@ -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) @@ -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) diff --git a/internal/app/configfile/tpl_files/wg_interface.tpl b/internal/app/configfile/tpl_files/wg_interface.tpl index c72f64a6..ed641119 100644 --- a/internal/app/configfile/tpl_files/wg_interface.tpl +++ b/internal/app/configfile/tpl_files/wg_interface.tpl @@ -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}} @@ -86,4 +88,4 @@ Endpoint = {{ .Endpoint.GetValue }} PersistentKeepalive = {{ .PersistentKeepalive.GetValue }} {{- end}} {{- end}} -{{end}} \ No newline at end of file +{{end}} diff --git a/internal/app/wireguard/wireguard_peers.go b/internal/app/wireguard/wireguard_peers.go index 6549b2db..8feb60fc 100644 --- a/internal/app/wireguard/wireguard_peers.go +++ b/internal/app/wireguard/wireguard_peers.go @@ -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 { @@ -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 }