mirror of
https://github.com/h44z/wg-portal.git
synced 2025-08-25 14:31:14 +00:00
WIP: support different interface types: server, client and custom. Show different UI for each type.
This commit is contained in:
@@ -85,6 +85,7 @@ func NewConfig() *Config {
|
||||
cfg.Core.AdminUser = "admin@wgportal.local"
|
||||
cfg.Core.AdminPassword = "wgportal"
|
||||
cfg.Core.LdapEnabled = false
|
||||
cfg.Core.EditableKeys = true
|
||||
cfg.Core.SessionSecret = "secret"
|
||||
|
||||
cfg.Database.Typ = "sqlite"
|
||||
|
@@ -44,12 +44,9 @@ func (s *Server) PostAdminEditInterface(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
// Clean list input
|
||||
formDevice.IPs = common.ParseStringList(formDevice.IPsStr)
|
||||
formDevice.DefaultAllowedIPs = common.ParseStringList(formDevice.DefaultAllowedIPsStr)
|
||||
formDevice.DNS = common.ParseStringList(formDevice.DNSStr)
|
||||
formDevice.IPsStr = common.ListToString(formDevice.IPs)
|
||||
formDevice.DefaultAllowedIPsStr = common.ListToString(formDevice.DefaultAllowedIPs)
|
||||
formDevice.DNSStr = common.ListToString(formDevice.DNS)
|
||||
formDevice.IPsStr = common.ListToString(common.ParseStringList(formDevice.IPsStr))
|
||||
formDevice.DefaultAllowedIPsStr = common.ListToString(common.ParseStringList(formDevice.DefaultAllowedIPsStr))
|
||||
formDevice.DNSStr = common.ListToString(common.ParseStringList(formDevice.DNSStr))
|
||||
|
||||
// Update WireGuard device
|
||||
err := s.wg.UpdateDevice(formDevice.DeviceName, formDevice.GetConfig())
|
||||
@@ -80,7 +77,7 @@ func (s *Server) PostAdminEditInterface(c *gin.Context) {
|
||||
|
||||
// Update interface IP address
|
||||
if s.config.WG.ManageIPAddresses {
|
||||
if err := s.wg.SetIPAddress(currentSession.DeviceName, formDevice.IPs); err != nil {
|
||||
if err := s.wg.SetIPAddress(currentSession.DeviceName, formDevice.GetIPAddresses()); err != nil {
|
||||
_ = s.updateFormInSession(c, formDevice)
|
||||
SetFlashMessage(c, "Failed to update ip address: "+err.Error(), "danger")
|
||||
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=update")
|
||||
@@ -122,7 +119,6 @@ func (s *Server) GetApplyGlobalConfig(c *gin.Context) {
|
||||
peers := s.peers.GetAllPeers(device.DeviceName)
|
||||
|
||||
for _, peer := range peers {
|
||||
peer.AllowedIPs = device.DefaultAllowedIPs
|
||||
peer.AllowedIPsStr = device.DefaultAllowedIPsStr
|
||||
if err := s.peers.UpdatePeer(peer); err != nil {
|
||||
SetFlashMessage(c, err.Error(), "danger")
|
||||
|
@@ -40,6 +40,7 @@ func (s *Server) GetAdminEditPeer(c *gin.Context) {
|
||||
"EditableKeys": s.config.Core.EditableKeys,
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"AdminEmail": s.config.Core.AdminUser,
|
||||
"Csrf": csrf.GetToken(c),
|
||||
})
|
||||
}
|
||||
@@ -61,10 +62,8 @@ func (s *Server) PostAdminEditPeer(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Clean list input
|
||||
formPeer.IPs = common.ParseStringList(formPeer.IPsStr)
|
||||
formPeer.AllowedIPs = common.ParseStringList(formPeer.AllowedIPsStr)
|
||||
formPeer.IPsStr = common.ListToString(formPeer.IPs)
|
||||
formPeer.AllowedIPsStr = common.ListToString(formPeer.AllowedIPs)
|
||||
formPeer.IPsStr = common.ListToString(common.ParseStringList(formPeer.IPsStr))
|
||||
formPeer.AllowedIPsStr = common.ListToString(common.ParseStringList(formPeer.AllowedIPsStr))
|
||||
|
||||
disabled := c.PostForm("isdisabled") != ""
|
||||
now := time.Now()
|
||||
@@ -101,6 +100,7 @@ func (s *Server) GetAdminCreatePeer(c *gin.Context) {
|
||||
"EditableKeys": s.config.Core.EditableKeys,
|
||||
"Device": s.peers.GetDevice(currentSession.DeviceName),
|
||||
"DeviceNames": s.wg.Cfg.DeviceNames,
|
||||
"AdminEmail": s.config.Core.AdminUser,
|
||||
"Csrf": csrf.GetToken(c),
|
||||
})
|
||||
}
|
||||
@@ -119,10 +119,8 @@ func (s *Server) PostAdminCreatePeer(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Clean list input
|
||||
formPeer.IPs = common.ParseStringList(formPeer.IPsStr)
|
||||
formPeer.AllowedIPs = common.ParseStringList(formPeer.AllowedIPsStr)
|
||||
formPeer.IPsStr = common.ListToString(formPeer.IPs)
|
||||
formPeer.AllowedIPsStr = common.ListToString(formPeer.AllowedIPs)
|
||||
formPeer.IPsStr = common.ListToString(common.ParseStringList(formPeer.IPsStr))
|
||||
formPeer.AllowedIPsStr = common.ListToString(common.ParseStringList(formPeer.AllowedIPsStr))
|
||||
|
||||
disabled := c.PostForm("isdisabled") != ""
|
||||
now := time.Now()
|
||||
@@ -328,7 +326,7 @@ func (s *Server) GetPeerStatus(c *gin.Context) {
|
||||
isOnline := false
|
||||
ping := make(chan bool)
|
||||
defer close(ping)
|
||||
for _, cidr := range peer.IPs {
|
||||
for _, cidr := range peer.GetIPAddresses() {
|
||||
ip, _, _ := net.ParseCIDR(cidr)
|
||||
var ra *net.IPAddr
|
||||
if common.IsIPv6(ip.String()) {
|
||||
|
@@ -8,7 +8,6 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/common"
|
||||
"github.com/h44z/wg-portal/internal/users"
|
||||
"github.com/h44z/wg-portal/internal/wireguard"
|
||||
"github.com/pkg/errors"
|
||||
@@ -20,19 +19,20 @@ import (
|
||||
// PrepareNewPeer initiates a new peer for the given WireGuard device.
|
||||
func (s *Server) PrepareNewPeer(device string) (wireguard.Peer, error) {
|
||||
dev := s.peers.GetDevice(device)
|
||||
deviceIPs := dev.GetIPAddresses()
|
||||
|
||||
peer := wireguard.Peer{}
|
||||
peer.IsNew = true
|
||||
peer.AllowedIPsStr = dev.DefaultAllowedIPsStr
|
||||
peer.IPs = make([]string, len(dev.IPs))
|
||||
for i := range dev.IPs {
|
||||
freeIP, err := s.peers.GetAvailableIp(device, dev.IPs[i])
|
||||
peerIPs := make([]string, len(deviceIPs))
|
||||
for i := range deviceIPs {
|
||||
freeIP, err := s.peers.GetAvailableIp(device, deviceIPs[i])
|
||||
if err != nil {
|
||||
return wireguard.Peer{}, errors.WithMessage(err, "failed to get available IP addresses")
|
||||
}
|
||||
peer.IPs[i] = freeIP
|
||||
peerIPs[i] = freeIP
|
||||
}
|
||||
peer.IPsStr = common.ListToString(peer.IPs)
|
||||
peer.SetIPAddresses(peerIPs...)
|
||||
psk, err := wgtypes.GenerateKey()
|
||||
if err != nil {
|
||||
return wireguard.Peer{}, errors.Wrap(err, "failed to generate key")
|
||||
@@ -77,17 +77,20 @@ func (s *Server) CreatePeerByEmail(device, email, identifierSuffix string, disab
|
||||
// This function also configures the new peer on the physical WireGuard interface if the peer is not deactivated.
|
||||
func (s *Server) CreatePeer(device string, peer wireguard.Peer) error {
|
||||
dev := s.peers.GetDevice(device)
|
||||
deviceIPs := dev.GetIPAddresses()
|
||||
peerIPs := peer.GetIPAddresses()
|
||||
|
||||
peer.AllowedIPsStr = dev.DefaultAllowedIPsStr
|
||||
if peer.IPs == nil || len(peer.IPs) == 0 {
|
||||
peer.IPs = make([]string, len(dev.IPs))
|
||||
for i := range dev.IPs {
|
||||
freeIP, err := s.peers.GetAvailableIp(device, dev.IPs[i])
|
||||
if len(peerIPs) == 0 {
|
||||
peerIPs = make([]string, len(deviceIPs))
|
||||
for i := range deviceIPs {
|
||||
freeIP, err := s.peers.GetAvailableIp(device, deviceIPs[i])
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed to get available IP addresses")
|
||||
}
|
||||
peer.IPs[i] = freeIP
|
||||
peerIPs[i] = freeIP
|
||||
}
|
||||
peer.IPsStr = common.ListToString(peer.IPs)
|
||||
peer.SetIPAddresses(peerIPs...)
|
||||
}
|
||||
if peer.PrivateKey == "" { // if private key is empty create a new one
|
||||
psk, err := wgtypes.GenerateKey()
|
||||
|
Reference in New Issue
Block a user