diff --git a/handler/routes.go b/handler/routes.go index cc8ae7d..261ac71 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -592,6 +592,43 @@ func GetServerClients(db store.IStore) echo.HandlerFunc { } } +// GetServerSettings handler returns a single server's per-server settings +// (endpoint address, table, firewall mark, config file path) as JSON. +func GetServerSettings(db store.IStore) echo.HandlerFunc { + return func(c echo.Context) error { + serverID := c.Param("id") + settings, err := db.GetServerSettings(serverID) + if err != nil { + return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Server settings not found"}) + } + return c.JSON(http.StatusOK, settings) + } +} + +// SaveServerSettingsHandler updates a single server's per-server settings. +// Admin-only (registered with handler.NeedsAdmin). +func SaveServerSettingsHandler(db store.IStore) echo.HandlerFunc { + return func(c echo.Context) error { + serverID := c.Param("id") + if _, err := db.GetServerByID(serverID); err != nil { + return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Server not found"}) + } + + var settings model.ServerSetting + if err := c.Bind(&settings); err != nil { + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"}) + } + settings.UpdatedAt = time.Now().UTC() + + if err := db.SaveServerSettings(serverID, settings); err != nil { + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, fmt.Sprintf("Cannot save server settings: %v", err)}) + } + log.Infof("Updated settings for server %s", serverID) + + return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated server settings successfully"}) + } +} + // CreateServer handler creates a new WireGuard server (step 3 of the // multi-server extension). Admin-only. Generates a fresh key pair, // validates the ID/interface name/subnet, and stores the server plus its @@ -658,6 +695,8 @@ func CreateServer(db store.IStore) echo.HandlerFunc { settings := model.ServerSetting{ ConfigFilePath: fmt.Sprintf("/etc/wireguard/%s.conf", req.Interface), + FirewallMark: util.DefaultFirewallMark, + Table: util.DefaultTable, UpdatedAt: time.Now().UTC(), } if err := db.SaveServerSettings(req.ID, settings); err != nil { diff --git a/main.go b/main.go index 15ec91e..1bfcdcf 100644 --- a/main.go +++ b/main.go @@ -263,6 +263,8 @@ func main() { app.POST(util.BasePath+"/servers/:id/remove-client", handler.RemoveClient(db), handler.ValidSession, handler.ContentTypeJson, handler.RequireServerAccess(db)) app.GET(util.BasePath+"/servers/:id/download", handler.DownloadClient(db), handler.ValidSession, handler.RequireServerAccess(db)) app.POST(util.BasePath+"/servers/:id/api/apply-wg-config", handler.ApplyServerConfig(db, tmplDir), handler.ValidSession, handler.ContentTypeJson, handler.RequireServerAccess(db)) + app.GET(util.BasePath+"/servers/:id/settings", handler.GetServerSettings(db), handler.ValidSession, handler.RequireServerAccess(db)) + app.POST(util.BasePath+"/servers/:id/settings", handler.SaveServerSettingsHandler(db), handler.ValidSession, handler.ContentTypeJson, handler.NeedsAdmin) app.GET(util.BasePath+"/api/clients", handler.GetClients(db), handler.ValidSession) app.GET(util.BasePath+"/api/client/:id", handler.GetClient(db), handler.ValidSession) app.GET(util.BasePath+"/api/machine-ips", handler.MachineIPAddresses(), handler.ValidSession) diff --git a/templates/servers.html b/templates/servers.html index debeb13..8c5baff 100644 --- a/templates/servers.html +++ b/templates/servers.html @@ -70,6 +70,50 @@ Servers + +