mirror of
https://github.com/h44z/wg-portal.git
synced 2025-08-25 14:31:14 +00:00
fix some bugs in client mode, improve login ui
This commit is contained in:
@@ -23,7 +23,7 @@ func (s *Server) GetHandleError(c *gin.Context, code int, message, details strin
|
||||
"Session": GetSessionData(c),
|
||||
"Static": s.getStaticData(),
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func (s *Server) GetIndex(c *gin.Context) {
|
||||
"Session": currentSession,
|
||||
"Static": s.getStaticData(),
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ func (s *Server) GetAdminIndex(c *gin.Context) {
|
||||
"TotalPeers": len(s.peers.GetAllPeers(currentSession.DeviceName)),
|
||||
"Users": s.users.GetUsers(),
|
||||
"Device": device,
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ func (s *Server) GetUserIndex(c *gin.Context) {
|
||||
"TotalPeers": len(peers),
|
||||
"Users": []users.User{*s.users.GetUser(currentSession.Email)},
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
})
|
||||
}
|
||||
|
||||
|
@@ -27,7 +27,7 @@ func (s *Server) GetAdminEditInterface(c *gin.Context) {
|
||||
"Static": s.getStaticData(),
|
||||
"Device": currentSession.FormData.(wireguard.Device),
|
||||
"EditableKeys": s.config.Core.EditableKeys,
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
"Csrf": csrf.GetToken(c),
|
||||
})
|
||||
}
|
||||
|
@@ -39,7 +39,7 @@ func (s *Server) GetAdminEditPeer(c *gin.Context) {
|
||||
"Peer": currentSession.FormData.(wireguard.Peer),
|
||||
"EditableKeys": s.config.Core.EditableKeys,
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
"AdminEmail": s.config.Core.AdminUser,
|
||||
"Csrf": csrf.GetToken(c),
|
||||
})
|
||||
@@ -99,7 +99,7 @@ func (s *Server) GetAdminCreatePeer(c *gin.Context) {
|
||||
"Peer": currentSession.FormData.(wireguard.Peer),
|
||||
"EditableKeys": s.config.Core.EditableKeys,
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
"AdminEmail": s.config.Core.AdminUser,
|
||||
"Csrf": csrf.GetToken(c),
|
||||
})
|
||||
@@ -154,7 +154,7 @@ func (s *Server) GetAdminCreateLdapPeers(c *gin.Context) {
|
||||
"Users": s.users.GetFilteredAndSortedUsers("lastname", "asc", ""),
|
||||
"FormData": currentSession.FormData.(LdapCreateForm),
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
"Csrf": csrf.GetToken(c),
|
||||
})
|
||||
}
|
||||
|
@@ -58,7 +58,7 @@ func (s *Server) GetAdminUsersIndex(c *gin.Context) {
|
||||
"Users": dbUsers,
|
||||
"TotalUsers": len(s.users.GetUsers()),
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func (s *Server) GetAdminUsersEdit(c *gin.Context) {
|
||||
"Static": s.getStaticData(),
|
||||
"User": currentSession.FormData.(users.User),
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
"Epoch": time.Time{},
|
||||
"Csrf": csrf.GetToken(c),
|
||||
})
|
||||
@@ -156,7 +156,7 @@ func (s *Server) GetAdminUsersCreate(c *gin.Context) {
|
||||
"Static": s.getStaticData(),
|
||||
"User": currentSession.FormData.(users.User),
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"DeviceNames": s.GetDeviceNames(),
|
||||
"Epoch": time.Time{},
|
||||
"Csrf": csrf.GetToken(c),
|
||||
})
|
||||
|
@@ -140,8 +140,13 @@ func (s *Server) UpdatePeer(peer wireguard.Peer, updateTime time.Time) error {
|
||||
// Update WireGuard device
|
||||
var err error
|
||||
switch {
|
||||
case peer.DeactivatedAt == &updateTime:
|
||||
err = s.wg.RemovePeer(peer.DeviceName, peer.PublicKey)
|
||||
case peer.DeactivatedAt != nil && *peer.DeactivatedAt == updateTime:
|
||||
switch dev.Type {
|
||||
case wireguard.DeviceTypeServer:
|
||||
err = s.wg.RemovePeer(peer.DeviceName, peer.PublicKey)
|
||||
case wireguard.DeviceTypeClient:
|
||||
err = s.wg.RemovePeer(peer.DeviceName, peer.EndpointPublicKey)
|
||||
}
|
||||
case peer.DeactivatedAt == nil && currentPeer.Peer != nil:
|
||||
err = s.wg.UpdatePeer(peer.DeviceName, peer.GetConfig(&dev))
|
||||
case peer.DeactivatedAt == nil && currentPeer.Peer == nil:
|
||||
@@ -161,8 +166,18 @@ func (s *Server) UpdatePeer(peer wireguard.Peer, updateTime time.Time) error {
|
||||
|
||||
// DeletePeer removes the peer from the physical WireGuard interface and the database.
|
||||
func (s *Server) DeletePeer(peer wireguard.Peer) error {
|
||||
dev := s.peers.GetDevice(peer.DeviceName)
|
||||
|
||||
var publicKey string
|
||||
switch dev.Type {
|
||||
case wireguard.DeviceTypeServer:
|
||||
publicKey = peer.PublicKey
|
||||
case wireguard.DeviceTypeClient:
|
||||
publicKey = peer.EndpointPublicKey
|
||||
}
|
||||
|
||||
// Delete WireGuard peer
|
||||
if err := s.wg.RemovePeer(peer.DeviceName, peer.PublicKey); err != nil {
|
||||
if err := s.wg.RemovePeer(peer.DeviceName, publicKey); err != nil {
|
||||
return errors.WithMessage(err, "failed to remove WireGuard peer")
|
||||
}
|
||||
|
||||
@@ -308,3 +323,14 @@ func (s *Server) CreateUserDefaultPeer(email, device string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) GetDeviceNames() map[string]string {
|
||||
devNames := make(map[string]string, len(s.wg.Cfg.DeviceNames))
|
||||
|
||||
for _, devName := range s.wg.Cfg.DeviceNames {
|
||||
dev := s.peers.GetDevice(devName)
|
||||
devNames[devName] = dev.DisplayName
|
||||
}
|
||||
|
||||
return devNames
|
||||
}
|
||||
|
Reference in New Issue
Block a user