2020-11-10 09:31:02 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-04-03 23:54:35 +02:00
|
|
|
"fmt"
|
2020-11-10 09:31:02 +01:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/h44z/wg-portal/internal/common"
|
2021-03-22 22:51:37 +01:00
|
|
|
"github.com/h44z/wg-portal/internal/wireguard"
|
|
|
|
csrf "github.com/utrack/gin-csrf"
|
2020-11-10 09:31:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) GetAdminEditInterface(c *gin.Context) {
|
2021-03-21 12:36:11 +01:00
|
|
|
currentSession := GetSessionData(c)
|
|
|
|
device := s.peers.GetDevice(currentSession.DeviceName)
|
2020-11-10 09:31:02 +01:00
|
|
|
currentSession, err := s.setFormInSession(c, device)
|
|
|
|
if err != nil {
|
|
|
|
s.GetHandleError(c, http.StatusInternalServerError, "Session error", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-21 12:36:11 +01:00
|
|
|
c.HTML(http.StatusOK, "admin_edit_interface.html", gin.H{
|
|
|
|
"Route": c.Request.URL.Path,
|
|
|
|
"Alerts": GetFlashes(c),
|
|
|
|
"Session": currentSession,
|
|
|
|
"Static": s.getStaticData(),
|
|
|
|
"Device": currentSession.FormData.(wireguard.Device),
|
|
|
|
"EditableKeys": s.config.Core.EditableKeys,
|
2021-04-05 19:12:27 +02:00
|
|
|
"DeviceNames": s.GetDeviceNames(),
|
2021-03-22 22:51:37 +01:00
|
|
|
"Csrf": csrf.GetToken(c),
|
2020-11-10 09:31:02 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) PostAdminEditInterface(c *gin.Context) {
|
2021-02-24 21:24:45 +01:00
|
|
|
currentSession := GetSessionData(c)
|
2021-03-21 12:36:11 +01:00
|
|
|
var formDevice wireguard.Device
|
2020-11-10 09:31:02 +01:00
|
|
|
if currentSession.FormData != nil {
|
2021-03-21 12:36:11 +01:00
|
|
|
formDevice = currentSession.FormData.(wireguard.Device)
|
2020-11-10 09:31:02 +01:00
|
|
|
}
|
|
|
|
if err := c.ShouldBind(&formDevice); err != nil {
|
|
|
|
_ = s.updateFormInSession(c, formDevice)
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, err.Error(), "danger")
|
2020-11-10 09:31:02 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=bind")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Clean list input
|
2021-04-03 19:11:05 +02:00
|
|
|
formDevice.IPsStr = common.ListToString(common.ParseStringList(formDevice.IPsStr))
|
|
|
|
formDevice.DefaultAllowedIPsStr = common.ListToString(common.ParseStringList(formDevice.DefaultAllowedIPsStr))
|
|
|
|
formDevice.DNSStr = common.ListToString(common.ParseStringList(formDevice.DNSStr))
|
2020-11-10 09:31:02 +01:00
|
|
|
|
2021-04-03 23:54:35 +02:00
|
|
|
// Clean interface parameters based on interface type
|
|
|
|
switch formDevice.Type {
|
|
|
|
case wireguard.DeviceTypeClient:
|
|
|
|
formDevice.ListenPort = 0
|
|
|
|
formDevice.DefaultEndpoint = ""
|
|
|
|
formDevice.DefaultAllowedIPsStr = ""
|
|
|
|
formDevice.DefaultPersistentKeepalive = 0
|
|
|
|
formDevice.SaveConfig = false
|
|
|
|
case wireguard.DeviceTypeServer:
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:31:02 +01:00
|
|
|
// Update WireGuard device
|
2021-02-21 23:23:58 +01:00
|
|
|
err := s.wg.UpdateDevice(formDevice.DeviceName, formDevice.GetConfig())
|
2020-11-10 09:31:02 +01:00
|
|
|
if err != nil {
|
|
|
|
_ = s.updateFormInSession(c, formDevice)
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, "Failed to update device in WireGuard: "+err.Error(), "danger")
|
2020-11-10 09:31:02 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=wg")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update in database
|
2021-02-24 21:24:45 +01:00
|
|
|
err = s.peers.UpdateDevice(formDevice)
|
2020-11-10 09:31:02 +01:00
|
|
|
if err != nil {
|
|
|
|
_ = s.updateFormInSession(c, formDevice)
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, "Failed to update device in database: "+err.Error(), "danger")
|
2020-11-10 09:31:02 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=update")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 16:10:05 +01:00
|
|
|
// Update WireGuard config file
|
2021-03-21 12:36:11 +01:00
|
|
|
err = s.WriteWireGuardConfigFile(currentSession.DeviceName)
|
2020-12-17 16:10:05 +01:00
|
|
|
if err != nil {
|
|
|
|
_ = s.updateFormInSession(c, formDevice)
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, "Failed to update WireGuard config-file: "+err.Error(), "danger")
|
2020-12-17 16:10:05 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=update")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-18 21:54:57 +01:00
|
|
|
// Update interface IP address
|
|
|
|
if s.config.WG.ManageIPAddresses {
|
2021-04-03 19:11:05 +02:00
|
|
|
if err := s.wg.SetIPAddress(currentSession.DeviceName, formDevice.GetIPAddresses()); err != nil {
|
2020-12-18 21:54:57 +01:00
|
|
|
_ = s.updateFormInSession(c, formDevice)
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, "Failed to update ip address: "+err.Error(), "danger")
|
2020-12-18 21:54:57 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=update")
|
|
|
|
}
|
2021-03-21 12:36:11 +01:00
|
|
|
if err := s.wg.SetMTU(currentSession.DeviceName, formDevice.Mtu); err != nil {
|
2020-12-18 21:54:57 +01:00
|
|
|
_ = s.updateFormInSession(c, formDevice)
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, "Failed to update MTU: "+err.Error(), "danger")
|
2020-12-18 21:54:57 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, "Changes applied successfully!", "success")
|
2020-12-18 21:54:57 +01:00
|
|
|
if !s.config.WG.ManageIPAddresses {
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, "WireGuard must be restarted to apply ip changes.", "warning")
|
2020-12-18 21:54:57 +01:00
|
|
|
}
|
2020-11-10 09:31:02 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetInterfaceConfig(c *gin.Context) {
|
2021-03-21 12:36:11 +01:00
|
|
|
currentSession := GetSessionData(c)
|
|
|
|
device := s.peers.GetDevice(currentSession.DeviceName)
|
|
|
|
peers := s.peers.GetActivePeers(device.DeviceName)
|
|
|
|
cfg, err := device.GetConfigFile(peers)
|
2020-11-10 09:31:02 +01:00
|
|
|
if err != nil {
|
|
|
|
s.GetHandleError(c, http.StatusInternalServerError, "ConfigFile error", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := strings.ToLower(device.DeviceName) + ".conf"
|
|
|
|
|
|
|
|
c.Header("Content-Disposition", "attachment; filename="+filename)
|
|
|
|
c.Data(http.StatusOK, "application/config", cfg)
|
|
|
|
return
|
|
|
|
}
|
2020-11-10 22:23:05 +01:00
|
|
|
|
2021-04-05 18:38:38 +02:00
|
|
|
func (s *Server) GetSaveConfig(c *gin.Context) {
|
|
|
|
currentSession := GetSessionData(c)
|
|
|
|
|
|
|
|
err := s.WriteWireGuardConfigFile(currentSession.DeviceName)
|
|
|
|
if err != nil {
|
|
|
|
SetFlashMessage(c, "Failed to save WireGuard config-file: "+err.Error(), "danger")
|
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFlashMessage(c, "Updated WireGuard config-file", "success")
|
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-10 22:23:05 +01:00
|
|
|
func (s *Server) GetApplyGlobalConfig(c *gin.Context) {
|
2021-03-21 12:36:11 +01:00
|
|
|
currentSession := GetSessionData(c)
|
|
|
|
device := s.peers.GetDevice(currentSession.DeviceName)
|
|
|
|
peers := s.peers.GetAllPeers(device.DeviceName)
|
2020-11-10 22:23:05 +01:00
|
|
|
|
2021-04-03 23:54:35 +02:00
|
|
|
if device.Type == wireguard.DeviceTypeClient {
|
|
|
|
SetFlashMessage(c, "Cannot apply global configuration while interface is in client mode.", "danger")
|
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCounter := 0
|
2021-03-21 12:36:11 +01:00
|
|
|
for _, peer := range peers {
|
2021-04-03 23:54:35 +02:00
|
|
|
if peer.IgnoreGlobalSettings {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-02 23:48:30 +02:00
|
|
|
peer.AllowedIPsStr = device.DefaultAllowedIPsStr
|
2021-04-03 23:54:35 +02:00
|
|
|
peer.Endpoint = device.DefaultEndpoint
|
|
|
|
peer.PersistentKeepalive = device.DefaultPersistentKeepalive
|
|
|
|
peer.DNSStr = device.DNSStr
|
|
|
|
peer.Mtu = device.Mtu
|
|
|
|
|
2021-03-21 12:36:11 +01:00
|
|
|
if err := s.peers.UpdatePeer(peer); err != nil {
|
2021-02-24 21:24:45 +01:00
|
|
|
SetFlashMessage(c, err.Error(), "danger")
|
2020-11-10 22:23:05 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit")
|
2021-04-03 23:54:35 +02:00
|
|
|
return
|
2020-11-10 22:23:05 +01:00
|
|
|
}
|
2021-04-03 23:54:35 +02:00
|
|
|
updateCounter++
|
2020-11-10 22:23:05 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 23:54:35 +02:00
|
|
|
SetFlashMessage(c, fmt.Sprintf("Global configuration updated for %d clients.", updateCounter), "success")
|
2020-11-10 22:23:05 +01:00
|
|
|
c.Redirect(http.StatusSeeOther, "/admin/device/edit")
|
|
|
|
return
|
|
|
|
}
|