From f4edc55851b9fdf7c2d6848e6a21209ee99e21fd Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Mon, 22 Mar 2021 12:39:50 +0100 Subject: [PATCH] fix mail template (#3) and rename some variables, also change default ordering (latest handshake first) --- assets/tpl/email.html | 10 +++---- internal/server/handlers_peer.go | 50 +++++++++++++++++--------------- internal/server/server.go | 7 ++--- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/assets/tpl/email.html b/assets/tpl/email.html index b67a683..3bc2238 100644 --- a/assets/tpl/email.html +++ b/assets/tpl/email.html @@ -92,7 +92,7 @@ - +
@@ -100,14 +100,14 @@ - {{if .Client.LdapUser}} - + {{if $.User}} + {{else}} {{end}} - +
Hello {{.Client.LdapUser.Firstname}} {{.Client.LdapUser.Lastname}}Hello {{$.User.Firstname}} {{$.User.Lastname}}Hello
You or your administrator probably requested this VPN configuration. Scan the Qrcode or open the attached configuration file ({{.Client.GetConfigFileName}}) in the WireGuard VPN client to establish a secure VPN connection.You or your administrator probably requested this VPN configuration. Scan the Qrcode or open the attached configuration file ({{$.Peer.GetConfigFileName}}) in the WireGuard VPN client to establish a secure VPN connection.
@@ -170,7 +170,7 @@ This mail was generated using WireGuard Portal. - Visit WireGuard Portal + Visit WireGuard Portal diff --git a/internal/server/handlers_peer.go b/internal/server/handlers_peer.go index 9f2de38..b51ea57 100644 --- a/internal/server/handlers_peer.go +++ b/internal/server/handlers_peer.go @@ -8,10 +8,10 @@ import ( "strings" "time" - "github.com/h44z/wg-portal/internal/wireguard" - "github.com/gin-gonic/gin" "github.com/h44z/wg-portal/internal/common" + "github.com/h44z/wg-portal/internal/users" + "github.com/h44z/wg-portal/internal/wireguard" "github.com/sirupsen/logrus" "github.com/tatsushid/go-fastping" ) @@ -197,8 +197,8 @@ func (s *Server) PostAdminCreateLdapPeers(c *gin.Context) { } func (s *Server) GetAdminDeletePeer(c *gin.Context) { - currentUser := s.peers.GetPeerByKey(c.Query("pkey")) - if err := s.DeletePeer(currentUser); err != nil { + currentPeer := s.peers.GetPeerByKey(c.Query("pkey")) + if err := s.DeletePeer(currentPeer); err != nil { s.GetHandleError(c, http.StatusInternalServerError, "Deletion error", err.Error()) return } @@ -207,14 +207,14 @@ func (s *Server) GetAdminDeletePeer(c *gin.Context) { } func (s *Server) GetPeerQRCode(c *gin.Context) { - user := s.peers.GetPeerByKey(c.Query("pkey")) + peer := s.peers.GetPeerByKey(c.Query("pkey")) currentSession := GetSessionData(c) - if !currentSession.IsAdmin && user.Email != currentSession.Email { + if !currentSession.IsAdmin && peer.Email != currentSession.Email { s.GetHandleError(c, http.StatusUnauthorized, "No permissions", "You don't have permissions to view this resource!") return } - png, err := user.GetQRCode() + png, err := peer.GetQRCode() if err != nil { s.GetHandleError(c, http.StatusInternalServerError, "QRCode error", err.Error()) return @@ -224,38 +224,40 @@ func (s *Server) GetPeerQRCode(c *gin.Context) { } func (s *Server) GetPeerConfig(c *gin.Context) { - user := s.peers.GetPeerByKey(c.Query("pkey")) + peer := s.peers.GetPeerByKey(c.Query("pkey")) currentSession := GetSessionData(c) - if !currentSession.IsAdmin && user.Email != currentSession.Email { + if !currentSession.IsAdmin && peer.Email != currentSession.Email { s.GetHandleError(c, http.StatusUnauthorized, "No permissions", "You don't have permissions to view this resource!") return } - cfg, err := user.GetConfigFile(s.peers.GetDevice(currentSession.DeviceName)) + cfg, err := peer.GetConfigFile(s.peers.GetDevice(currentSession.DeviceName)) if err != nil { s.GetHandleError(c, http.StatusInternalServerError, "ConfigFile error", err.Error()) return } - c.Header("Content-Disposition", "attachment; filename="+user.GetConfigFileName()) + c.Header("Content-Disposition", "attachment; filename="+peer.GetConfigFileName()) c.Data(http.StatusOK, "application/config", cfg) return } func (s *Server) GetPeerConfigMail(c *gin.Context) { - user := s.peers.GetPeerByKey(c.Query("pkey")) + peer := s.peers.GetPeerByKey(c.Query("pkey")) currentSession := GetSessionData(c) - if !currentSession.IsAdmin && user.Email != currentSession.Email { + if !currentSession.IsAdmin && peer.Email != currentSession.Email { s.GetHandleError(c, http.StatusUnauthorized, "No permissions", "You don't have permissions to view this resource!") return } - cfg, err := user.GetConfigFile(s.peers.GetDevice(currentSession.DeviceName)) + user := s.users.GetUser(peer.Email) + + cfg, err := peer.GetConfigFile(s.peers.GetDevice(currentSession.DeviceName)) if err != nil { s.GetHandleError(c, http.StatusInternalServerError, "ConfigFile error", err.Error()) return } - png, err := user.GetQRCode() + png, err := peer.GetQRCode() if err != nil { s.GetHandleError(c, http.StatusInternalServerError, "QRCode error", err.Error()) return @@ -263,11 +265,13 @@ func (s *Server) GetPeerConfigMail(c *gin.Context) { // Apply mail template var tplBuff bytes.Buffer if err := s.mailTpl.Execute(&tplBuff, struct { - Client wireguard.Peer + Peer wireguard.Peer + User *users.User QrcodePngName string PortalUrl string }{ - Client: user, + Peer: peer, + User: user, QrcodePngName: "wireguard-config.png", PortalUrl: s.config.Core.ExternalUrl, }); err != nil { @@ -278,7 +282,7 @@ func (s *Server) GetPeerConfigMail(c *gin.Context) { // Send mail attachments := []common.MailAttachment{ { - Name: user.GetConfigFileName(), + Name: peer.GetConfigFileName(), ContentType: "application/config", Data: bytes.NewReader(cfg), }, @@ -291,7 +295,7 @@ func (s *Server) GetPeerConfigMail(c *gin.Context) { if err := common.SendEmailWithAttachments(s.config.Email, s.config.Core.MailFrom, "", "WireGuard VPN Configuration", "Your mail client does not support HTML. Please find the configuration attached to this mail.", tplBuff.String(), - []string{user.Email}, attachments); err != nil { + []string{peer.Email}, attachments); err != nil { s.GetHandleError(c, http.StatusInternalServerError, "Email error", err.Error()) return } @@ -301,14 +305,14 @@ func (s *Server) GetPeerConfigMail(c *gin.Context) { } func (s *Server) GetPeerStatus(c *gin.Context) { - user := s.peers.GetPeerByKey(c.Query("pkey")) + peer := s.peers.GetPeerByKey(c.Query("pkey")) currentSession := GetSessionData(c) - if !currentSession.IsAdmin && user.Email != currentSession.Email { + if !currentSession.IsAdmin && peer.Email != currentSession.Email { s.GetHandleError(c, http.StatusUnauthorized, "No permissions", "You don't have permissions to view this resource!") return } - if user.Peer == nil { // no peer means disabled + if peer.Peer == nil { // no peer means disabled c.JSON(http.StatusOK, false) return } @@ -316,7 +320,7 @@ func (s *Server) GetPeerStatus(c *gin.Context) { isOnline := false ping := make(chan bool) defer close(ping) - for _, cidr := range user.IPs { + for _, cidr := range peer.IPs { ip, _, _ := net.ParseCIDR(cidr) var ra *net.IPAddr if common.IsIPv6(ip.String()) { diff --git a/internal/server/server.go b/internal/server/server.go index 6b35a71..54b14f3 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -14,8 +14,6 @@ import ( "strings" "time" - "gorm.io/gorm" - "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/memstore" "github.com/gin-gonic/gin" @@ -28,6 +26,7 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" ginlogrus "github.com/toorop/gin-logrus" + "gorm.io/gorm" ) const SessionIdentifier = "wgPortalSession" @@ -258,8 +257,8 @@ func GetSessionData(c *gin.Context) SessionData { } else { sessionData = SessionData{ Search: map[string]string{"peers": "", "userpeers": "", "users": ""}, - SortedBy: map[string]string{"peers": "mail", "userpeers": "mail", "users": "email"}, - SortDirection: map[string]string{"peers": "asc", "userpeers": "asc", "users": "asc"}, + SortedBy: map[string]string{"peers": "handshake", "userpeers": "id", "users": "email"}, + SortDirection: map[string]string{"peers": "desc", "userpeers": "asc", "users": "asc"}, Email: "", Firstname: "", Lastname: "",