wg-portal/internal/server/handlers_interface.go

141 lines
4.4 KiB
Go
Raw Normal View History

2020-11-10 09:31:02 +01:00
package server
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/h44z/wg-portal/internal/common"
)
func (s *Server) GetAdminEditInterface(c *gin.Context) {
device := s.users.GetDevice()
users := s.users.GetAllUsers()
currentSession, err := s.setFormInSession(c, device)
if err != nil {
s.GetHandleError(c, http.StatusInternalServerError, "Session error", err.Error())
return
}
c.HTML(http.StatusOK, "admin_edit_interface.html", struct {
Route string
Alerts []FlashData
Session SessionData
Static StaticData
2021-02-21 23:23:58 +01:00
Peers []Peer
Device Device
EditableKeys bool
2020-11-10 09:31:02 +01:00
}{
Route: c.Request.URL.Path,
Alerts: s.getFlashes(c),
Session: currentSession,
Static: s.getStaticData(),
Peers: users,
Device: currentSession.FormData.(Device),
EditableKeys: s.config.Core.EditableKeys,
2020-11-10 09:31:02 +01:00
})
}
func (s *Server) PostAdminEditInterface(c *gin.Context) {
currentSession := s.getSessionData(c)
var formDevice Device
if currentSession.FormData != nil {
formDevice = currentSession.FormData.(Device)
}
if err := c.ShouldBind(&formDevice); err != nil {
_ = s.updateFormInSession(c, formDevice)
2020-11-10 22:23:05 +01:00
s.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
formDevice.IPs = common.ParseStringList(formDevice.IPsStr)
formDevice.AllowedIPs = common.ParseStringList(formDevice.AllowedIPsStr)
formDevice.DNS = common.ParseStringList(formDevice.DNSStr)
formDevice.IPsStr = common.ListToString(formDevice.IPs)
formDevice.AllowedIPsStr = common.ListToString(formDevice.AllowedIPs)
formDevice.DNSStr = common.ListToString(formDevice.DNS)
// 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)
2020-11-10 22:23:05 +01:00
s.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
err = s.users.UpdateDevice(formDevice)
if err != nil {
_ = s.updateFormInSession(c, formDevice)
2020-11-10 22:23:05 +01:00
s.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
}
// Update WireGuard config file
err = s.WriteWireGuardConfigFile()
if err != nil {
_ = s.updateFormInSession(c, formDevice)
2021-02-08 22:56:02 +01:00
s.setFlashMessage(c, "Failed to update WireGuard config-file: "+err.Error(), "danger")
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 {
if err := s.wg.SetIPAddress(formDevice.IPs); err != nil {
_ = s.updateFormInSession(c, formDevice)
s.setFlashMessage(c, "Failed to update ip address: "+err.Error(), "danger")
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=update")
}
if err := s.wg.SetMTU(formDevice.Mtu); err != nil {
_ = s.updateFormInSession(c, formDevice)
s.setFlashMessage(c, "Failed to update MTU: "+err.Error(), "danger")
c.Redirect(http.StatusSeeOther, "/admin/device/edit?formerr=update")
}
}
2020-11-10 22:23:05 +01:00
s.setFlashMessage(c, "Changes applied successfully!", "success")
2020-12-18 21:54:57 +01:00
if !s.config.WG.ManageIPAddresses {
s.setFlashMessage(c, "WireGuard must be restarted to apply ip changes.", "warning")
}
2020-11-10 09:31:02 +01:00
c.Redirect(http.StatusSeeOther, "/admin/device/edit")
}
func (s *Server) GetInterfaceConfig(c *gin.Context) {
device := s.users.GetDevice()
users := s.users.GetActiveUsers()
2021-02-21 23:23:58 +01:00
cfg, err := device.GetConfigFile(users)
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
func (s *Server) GetApplyGlobalConfig(c *gin.Context) {
device := s.users.GetDevice()
users := s.users.GetAllUsers()
for _, user := range users {
user.AllowedIPs = device.AllowedIPs
user.AllowedIPsStr = device.AllowedIPsStr
if err := s.users.UpdateUser(user); err != nil {
s.setFlashMessage(c, err.Error(), "danger")
c.Redirect(http.StatusSeeOther, "/admin/device/edit")
}
}
2021-02-08 22:56:02 +01:00
s.setFlashMessage(c, "Allowed IP's updated for all clients.", "success")
2020-11-10 22:23:05 +01:00
c.Redirect(http.StatusSeeOther, "/admin/device/edit")
return
}