From f95c692aedbc58a99d4d4e77f4a6dd69b9317506 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Mon, 22 Mar 2021 13:00:02 +0100 Subject: [PATCH] migrate peer database --- internal/server/handlers_auth.go | 4 ++-- internal/wireguard/config.go | 11 ++++++++++- internal/wireguard/peermanager.go | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/internal/server/handlers_auth.go b/internal/server/handlers_auth.go index 72833cc..88fc435 100644 --- a/internal/server/handlers_auth.go +++ b/internal/server/handlers_auth.go @@ -98,7 +98,7 @@ func (s *Server) PostLogin(c *gin.Context) { Firstname: userData.Firstname, Lastname: userData.Lastname, Phone: userData.Phone, - }, s.wg.Cfg.DefaultDeviceName); err != nil { + }, s.wg.Cfg.GetDefaultDeviceName()); err != nil { s.GetHandleError(c, http.StatusInternalServerError, "login error", "failed to update user data") return } @@ -124,7 +124,7 @@ func (s *Server) PostLogin(c *gin.Context) { sessionData.DeviceName = s.wg.Cfg.DeviceNames[0] // Check if user already has a peer setup, if not create one - if err := s.CreateUserDefaultPeer(user.Email, s.wg.Cfg.DefaultDeviceName); err != nil { + if err := s.CreateUserDefaultPeer(user.Email, s.wg.Cfg.GetDefaultDeviceName()); err != nil { // Not a fatal error, just log it... logrus.Errorf("failed to automatically create vpn peer for %s: %v", sessionData.Email, err) } diff --git a/internal/wireguard/config.go b/internal/wireguard/config.go index 027d928..ee84fe4 100644 --- a/internal/wireguard/config.go +++ b/internal/wireguard/config.go @@ -1,8 +1,17 @@ package wireguard +import "github.com/h44z/wg-portal/internal/common" + type Config struct { DeviceNames []string `yaml:"devices" envconfig:"WG_DEVICES"` // managed devices - DefaultDeviceName string `yaml:"devices" envconfig:"WG_DEFAULT_DEVICE"` // this device is used for auto-created peers + DefaultDeviceName string `yaml:"devices" envconfig:"WG_DEFAULT_DEVICE"` // this device is used for auto-created peers, use GetDefaultDeviceName() to access this field ConfigDirectoryPath string `yaml:"configDirectory" envconfig:"WG_CONFIG_PATH"` // optional, if set, updates will be written to this path, filename: .conf ManageIPAddresses bool `yaml:"manageIPAddresses" envconfig:"MANAGE_IPS"` // handle ip-address setup of interface } + +func (c Config) GetDefaultDeviceName() string { + if c.DefaultDeviceName == "" || !common.ListContains(c.DeviceNames, c.DefaultDeviceName) { + return c.DeviceNames[0] + } + return c.DefaultDeviceName +} diff --git a/internal/wireguard/peermanager.go b/internal/wireguard/peermanager.go index 099b491..11217dc 100644 --- a/internal/wireguard/peermanager.go +++ b/internal/wireguard/peermanager.go @@ -274,13 +274,23 @@ type PeerManager struct { } func NewPeerManager(db *gorm.DB, wg *Manager) (*PeerManager, error) { - um := &PeerManager{db: db, wg: wg} + pm := &PeerManager{db: db, wg: wg} - if err := um.db.AutoMigrate(&Peer{}, &Device{}); err != nil { + if err := pm.db.AutoMigrate(&Peer{}, &Device{}); err != nil { return nil, errors.WithMessage(err, "failed to migrate peer database") } - return um, nil + // check if peers without device name exist (from version <= 1.0.3), if so assign them to the default device. + peers := make([]Peer, 0) + pm.db.Find(&peers) + for i := range peers { + if peers[i].DeviceName == "" { + peers[i].DeviceName = wg.Cfg.GetDefaultDeviceName() + pm.db.Save(&peers[i]) + } + } + + return pm, nil } // InitFromPhysicalInterface read all WireGuard peers from the WireGuard interface configuration. If a peer does not