v0.1.1
Go quality gates / test (push) Waiting to run

This commit is contained in:
Mateusz Gruszczyński committed 2026-11-30 14:44:28 +01:00
1 parent 8c4d463f1e
commit de13d851a8
75 files changed
+4656 -442

No files matched your search

+209
View File
@@ -0,0 +1,209 @@
package server
import (
"net/http"
"net/url"
"strings"
)
const activeNodeCookie = "easydns_ui_node"
func clearNodeCookie(w http.ResponseWriter, secure bool) {
http.SetCookie(w, &http.Cookie{Name: activeNodeCookie, Path: "/", Value: "", MaxAge: -1, HttpOnly: true, Secure: secure, SameSite: http.SameSiteStrictMode})
}
func (s *Server) multiServerPreference(w http.ResponseWriter, r *http.Request) {
if s.cfg.MultiServerExplicit {
http.Error(w, "Multi-server mode is configured in TOML or environment; change the configuration and restart the panel.", http.StatusForbidden)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 4096)
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form", http.StatusBadRequest)
return
}
value := r.PostForm.Get("enabled")
if value != "" && value != "on" {
http.Error(w, "Invalid preference value", http.StatusBadRequest)
return
}
enabled := value == "on"
if err := s.nodes.SetEnabled(enabled); err != nil {
http.Error(w, "Could not save panel preferences: "+err.Error(), http.StatusInternalServerError)
return
}
if enabled {
redirect(w, r, "/servers?notice=servers-enabled")
return
}
clearNodeCookie(w, s.cfg.CookieSecure)
redirect(w, r, "/configuration?notice=servers-disabled")
}
func (s *Server) serversOverview(w http.ResponseWriter, r *http.Request) {
d := PageData{ServerProbes: s.nodes.ProbeAll(r.Context())}
for _, p := range d.ServerProbes {
if p.State == "healthy" {
d.ServerHealthy++
}
}
s.render(w, r, "servers", "Servers", "Monitor and manage independent EasyDNS installations from one panel.", "servers", "MULTI-SERVER / SERVERS", d)
}
func (s *Server) serverNewForm(w http.ResponseWriter, r *http.Request) {
s.render(w, r, "server-form", "Add server", "Connect another EasyDNS REST API using its own generated control token.", "servers", "MULTI-SERVER / ADD SERVER", PageData{Form: map[string]string{"role": "secondary"}})
}
func (s *Server) serverCreate(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 12<<10)
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form", http.StatusBadRequest)
return
}
d := PageData{Form: formValues(r, "name", "role", "url")}
token := strings.TrimSpace(r.PostForm.Get("token")) // never add this to PageData or logs
_, err := s.nodes.Add(d.Form["name"], d.Form["role"], d.Form["url"], token)
if err != nil {
d.Error = err.Error()
s.render(w, r, "server-form", "Add server", "Connect another EasyDNS REST API using its own generated control token.", "servers", "MULTI-SERVER / ADD SERVER", d)
return
}
redirect(w, r, "/servers?notice=server-added")
}
func (s *Server) serverSelect(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 4096)
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form", http.StatusBadRequest)
return
}
id := r.PostForm.Get("server_id")
if _, _, _, ok := s.nodes.Get(id); !ok {
http.Error(w, "Unknown server", http.StatusBadRequest)
return
}
http.SetCookie(w, &http.Cookie{Name: activeNodeCookie, Value: id, Path: "/", HttpOnly: true, Secure: s.cfg.CookieSecure, SameSite: http.SameSiteStrictMode, MaxAge: 12 * 60 * 60})
next := nodeSwitchDestination(r.PostForm.Get("next"))
redirect(w, r, next)
}
// nodeSwitchDestination preserves list/diagnostic pages when switching nodes.
// Per-zone forms and detail URLs are reset to the list because the new server
// may not contain the same zone, record, ETag, or other editable resource.
func nodeSwitchDestination(raw string) string {
if raw == "" || !strings.HasPrefix(raw, "/") || strings.HasPrefix(raw, "//") || strings.ContainsAny(raw, "\\\r\n") {
return "/"
}
u, err := url.ParseRequestURI(raw)
if err != nil || u.IsAbs() || u.Host != "" || u.Fragment != "" {
return "/"
}
path := u.Path
switch {
case strings.HasPrefix(path, "/zones/"):
return "/zones"
case strings.HasPrefix(path, "/upstreams/"):
return "/upstreams"
case strings.HasPrefix(path, "/blocklists/"):
return "/blocklists"
case strings.HasPrefix(path, "/servers/") || path == "/servers":
return "/"
case path == "/", path == "/zones", path == "/query", path == "/upstreams", path == "/policies", path == "/views", path == "/acl", path == "/rate-limiting", path == "/blocklists", path == "/cache", path == "/metrics", path == "/audit", path == "/status", path == "/configuration":
return raw
default:
return "/"
}
}
func (s *Server) serverCompare(w http.ResponseWriter, r *http.Request) {
d := PageData{CompareZone: strings.TrimSpace(r.URL.Query().Get("zone"))}
if d.CompareZone != "" {
c, err := s.nodes.Compare(r.Context(), d.CompareZone)
if err != nil {
d.Error = err.Error()
} else {
d.Comparisons = c
for _, item := range c {
if item.Node.Role == "primary" && item.Present && item.Zone.Serial != nil {
d.PrimarySerial = item.Serial
break
}
}
}
}
s.render(w, r, "server-compare", "Zone comparison", "Compare a zone's presence and SOA serial across configured servers.", "servers", "MULTI-SERVER / ZONE COMPARISON", d)
}
func (s *Server) serverAction(w http.ResponseWriter, r *http.Request) {
raw := strings.TrimPrefix(r.URL.Path, "/servers/")
parts := strings.Split(raw, "/")
if len(parts) != 2 || parts[0] == "" {
http.NotFound(w, r)
return
}
id, action := parts[0], parts[1]
node, _, _, ok := s.nodes.Get(id)
if !ok {
http.NotFound(w, r)
return
}
switch {
case action == "remove" && r.Method == "GET":
if node.Local {
http.Error(w, "Local server cannot be removed", http.StatusBadRequest)
return
}
if node.Managed {
http.Error(w, "Configuration-managed servers cannot be removed from this panel.", http.StatusForbidden)
return
}
s.render(w, r, "server-remove", "Remove server connection", "Remove a managed connection without deleting or changing EasyDNS itself.", "servers", "MULTI-SERVER / REMOVE SERVER", PageData{ManagedNode: node})
case action == "remove" && r.Method == "POST":
if node.Local {
http.Error(w, "Local server cannot be removed", http.StatusBadRequest)
return
}
if node.Managed {
http.Error(w, "Configuration-managed servers cannot be removed from this panel.", http.StatusForbidden)
return
}
if r.FormValue("confirm_name") != node.Name {
s.render(w, r, "server-remove", "Remove server connection", "The confirmation name must match.", "servers", "MULTI-SERVER / REMOVE SERVER", PageData{ManagedNode: node, Error: "Type the exact server name to remove this panel connection."})
return
}
if err := s.nodes.Remove(id); err != nil {
http.Error(w, "Unable to remove server: "+err.Error(), http.StatusInternalServerError)
return
}
if s.selectedNode.ID == id {
clearNodeCookie(w, s.cfg.CookieSecure)
}
redirect(w, r, "/servers?notice=server-removed")
case action == "role" && r.Method == "POST":
if node.Managed {
http.Error(w, "Configuration-managed role labels cannot be changed from this panel.", http.StatusForbidden)
return
}
if node.Local && s.cfg.LocalRoleExplicit {
http.Error(w, "The local server label is configured in TOML or environment; change the configuration and restart the panel.", http.StatusForbidden)
return
}
r.Body = http.MaxBytesReader(w, r.Body, 2048)
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form", http.StatusBadRequest)
return
}
if err := s.nodes.SetRole(id, r.PostForm.Get("role")); err != nil {
// Role changes affect panel labels only; preserve a user-readable error.
d := PageData{ServerProbes: s.nodes.ProbeAll(r.Context()), Error: err.Error()}
for _, p := range d.ServerProbes {
if p.State == "healthy" {
d.ServerHealthy++
}
}
s.render(w, r, "servers", "Servers", "Monitor and manage independent EasyDNS installations from one panel.", "servers", "MULTI-SERVER / SERVERS", d)
return
}
redirect(w, r, "/servers?notice=server-role")
default:
http.NotFound(w, r)
}
}
// All native EasyDNS operations are carried out through the selected client's
// documented routes. The panel never creates server-side cluster endpoints.