271 lines
10 KiB
Go
271 lines
10 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
|
|
)
|
|
|
|
func (s *Server) query(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{Form: map[string]string{"name": r.URL.Query().Get("name"), "type": r.URL.Query().Get("type")}}
|
|
if d.Form["type"] == "" {
|
|
d.Form["type"] = "A"
|
|
}
|
|
if d.Form["name"] != "" {
|
|
q := url.Values{"name": {strings.TrimSpace(d.Form["name"])}, "type": {d.Form["type"]}}
|
|
var result easydns.QueryResult
|
|
if e := s.get(r.Context(), "/api/v1/query?"+q.Encode(), &result); e != nil {
|
|
d.Error = e.Error()
|
|
} else {
|
|
d.Query = &result
|
|
}
|
|
}
|
|
s.render(w, r, "query", "DNS lookup", "Resolve a name through the running EasyDNS server. No query history is stored.", "query", "DNS / LOOKUP", d)
|
|
}
|
|
func (s *Server) upstreams(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
var a struct {
|
|
Items []easydns.UpstreamGroup `json:"items"`
|
|
}
|
|
if e := s.get(r.Context(), "/api/v1/upstreams", &a); e != nil {
|
|
d.Error = e.Error()
|
|
} else {
|
|
d.Upstreams = a.Items
|
|
}
|
|
s.render(w, r, "upstreams", "Upstreams", "Live resolver groups and upstream health.", "upstreams", "RESOLUTION / UPSTREAMS", d)
|
|
}
|
|
func (s *Server) upstreamForm(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{Form: map[string]string{}}
|
|
s.render(w, r, "upstream-form", "Add upstream", "Add one resolver address to the API overlay.", "upstreams", "RESOLUTION / UPSTREAMS / ADD", d)
|
|
}
|
|
func (s *Server) upstreamCreate(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{Form: formValues(r, "address")}
|
|
address := strings.TrimSpace(d.Form["address"])
|
|
if address == "" {
|
|
d.Error = "Upstream address is required."
|
|
} else {
|
|
var out any
|
|
err := s.api.Write(r.Context(), "POST", "/api/v1/upstreams", map[string]string{"address": address}, "", &out)
|
|
if err == nil {
|
|
redirect(w, r, "/upstreams?notice=upstream-added")
|
|
return
|
|
}
|
|
d.Error = err.Error()
|
|
}
|
|
s.render(w, r, "upstream-form", "Add upstream", "Add one resolver address.", "upstreams", "RESOLUTION / UPSTREAMS / ADD", d)
|
|
}
|
|
func (s *Server) policies(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
if e := s.get(r.Context(), "/api/v1/policies", &d.Policies); e != nil {
|
|
d.Error = e.Error()
|
|
} else {
|
|
d.Form = map[string]string{"allowlist": joinStrings(d.Policies.Policy["allowlist"]), "block_action": fmt.Sprint(d.Policies.Policy["block_action"])}
|
|
}
|
|
s.render(w, r, "policies", "Policies", "Inspect DNS policy and edit only the two REST-writable fields.", "policies", "RESOLUTION / POLICIES", d)
|
|
}
|
|
func joinStrings(v any) string {
|
|
a, ok := v.([]any)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
parts := make([]string, 0, len(a))
|
|
for _, x := range a {
|
|
parts = append(parts, fmt.Sprint(x))
|
|
}
|
|
return strings.Join(parts, "\n")
|
|
}
|
|
func (s *Server) policiesSave(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{Form: formValues(r, "allowlist", "block_action")}
|
|
action := strings.ToLower(strings.TrimSpace(d.Form["block_action"]))
|
|
allowed := map[string]bool{"nxdomain": true, "refused": true, "null": true, "address": true, "nodata": true}
|
|
if !allowed[action] {
|
|
d.Error = "Unsupported block action."
|
|
} else {
|
|
body := map[string]any{"allowlist": splitLines(d.Form["allowlist"]), "block_action": action}
|
|
var result easydns.Policies
|
|
if e := s.api.Write(r.Context(), "PUT", "/api/v1/policies", body, "", &result); e != nil {
|
|
d.Error = e.Error()
|
|
} else {
|
|
redirect(w, r, "/policies?notice=policy-updated")
|
|
return
|
|
}
|
|
}
|
|
if e := s.get(r.Context(), "/api/v1/policies", &d.Policies); e != nil && d.Error == "" {
|
|
d.Error = e.Error()
|
|
}
|
|
s.render(w, r, "policies", "Policies", "Edit supported policy fields.", "policies", "RESOLUTION / POLICIES", d)
|
|
}
|
|
func (s *Server) policiesReadOnly(w http.ResponseWriter, r *http.Request, which string) {
|
|
d := PageData{}
|
|
if e := s.get(r.Context(), "/api/v1/policies", &d.Policies); e != nil {
|
|
d.Error = e.Error()
|
|
}
|
|
title, subtitle := map[string]string{"views": "Views", "acl": "Access control lists", "rate": "Rate limiting"}[which], ""
|
|
switch which {
|
|
case "views":
|
|
subtitle = "Split-horizon view configuration (read-only REST)."
|
|
case "acl":
|
|
subtitle = "Named ACLs and allowed clients (read-only REST)."
|
|
case "rate":
|
|
subtitle = "DNS response rate limiting configuration (read-only REST)."
|
|
}
|
|
d.Tab = which
|
|
if which == "rate" {
|
|
var stats easydns.Stats
|
|
if e := s.get(r.Context(), "/api/v1/stats", &stats); e == nil {
|
|
d.Stats = stats
|
|
}
|
|
}
|
|
s.render(w, r, "policy-readonly", title, subtitle, map[string]string{"views": "views", "acl": "acl", "rate": "rate"}[which], "RESOLUTION / "+strings.ToUpper(title), d)
|
|
}
|
|
func (s *Server) blocklists(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
if e := s.get(r.Context(), "/api/v1/blocklists", &d.Blocklists); e != nil {
|
|
d.Error = e.Error()
|
|
}
|
|
s.render(w, r, "blocklists", "Blocklists", "Configured feeds, load state and aggregate rule count.", "blocklists", "RESOLUTION / BLOCKLISTS", d)
|
|
}
|
|
func (s *Server) blockForm(w http.ResponseWriter, r *http.Request) {
|
|
s.render(w, r, "block-form", "Add block rule", "Append one rule to the EasyDNS API overlay.", "blocklists", "RESOLUTION / BLOCKLISTS / NEW", PageData{Form: map[string]string{}})
|
|
}
|
|
func (s *Server) blockCreate(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{Form: formValues(r, "rule")}
|
|
rule := strings.TrimSpace(d.Form["rule"])
|
|
if rule == "" || strings.ContainsAny(rule, "\r\n") {
|
|
d.Error = "Enter exactly one non-empty rule on one line."
|
|
} else {
|
|
if e := s.api.Write(r.Context(), "POST", "/api/v1/blocklists", map[string]string{"rule": rule}, "", nil); e != nil {
|
|
d.Error = e.Error()
|
|
} else {
|
|
redirect(w, r, "/blocklists?notice=rule-added")
|
|
return
|
|
}
|
|
}
|
|
s.render(w, r, "block-form", "Add block rule", "Append one rule to the EasyDNS API overlay.", "blocklists", "RESOLUTION / BLOCKLISTS / NEW", d)
|
|
}
|
|
func (s *Server) cache(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
if e := s.get(r.Context(), "/api/v1/cache", &d.Cache); e != nil {
|
|
d.Error = e.Error()
|
|
}
|
|
s.render(w, r, "cache", "Cache", "Resolver cache counters and precise invalidation.", "cache", "MONITORING / CACHE", d)
|
|
}
|
|
func (s *Server) cacheFlush(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
scope := r.FormValue("scope")
|
|
domain := strings.TrimSpace(r.FormValue("domain"))
|
|
path := "/api/v1/cache"
|
|
if scope == "domain" {
|
|
if domain == "" {
|
|
d.Error = "A domain is required for targeted invalidation."
|
|
} else {
|
|
path += "?domain=" + url.QueryEscape(domain)
|
|
}
|
|
} else if scope == "all" {
|
|
if r.FormValue("confirm") != "FLUSH ALL" {
|
|
d.Error = "Type FLUSH ALL to confirm full cache invalidation."
|
|
}
|
|
} else {
|
|
d.Error = "Unknown cache scope."
|
|
}
|
|
if d.Error == "" {
|
|
var out struct {
|
|
Removed uint64 `json:"removed"`
|
|
}
|
|
if e := s.api.Write(r.Context(), "DELETE", path, nil, "", &out); e != nil {
|
|
d.Error = e.Error()
|
|
} else {
|
|
redirect(w, r, "/cache?notice=cache-flushed")
|
|
return
|
|
}
|
|
}
|
|
if e := s.get(r.Context(), "/api/v1/cache", &d.Cache); e != nil && d.Error == "" {
|
|
d.Error = e.Error()
|
|
}
|
|
s.render(w, r, "cache", "Cache", "Resolver cache counters and invalidation.", "cache", "MONITORING / CACHE", d)
|
|
}
|
|
func (s *Server) metrics(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{MetricsMode: s.metricsMode(w, r)}
|
|
if d.MetricsMode == "single" {
|
|
if e := s.get(r.Context(), "/api/v1/stats", &d.Stats); e != nil {
|
|
d.Error = e.Error()
|
|
}
|
|
} else {
|
|
d.MetricNodes, d.MetricTotals, d.MetricOnline, d.MetricHasCache = s.fetchFleetMetrics(r.Context())
|
|
d.MetricTotal = len(d.MetricNodes)
|
|
}
|
|
sm, e, last := s.telemetry.Snapshot()
|
|
d.SamplingCount = len(sm)
|
|
d.SamplingTime = last
|
|
d.SamplingError = e
|
|
subtitle := "Real counters and live samples collected by this panel."
|
|
if d.MetricsMode != "single" {
|
|
subtitle = "Compare independent EasyDNS installations using actual per-node counters and samples."
|
|
}
|
|
s.render(w, r, "metrics", "Metrics", subtitle, "metrics", "MONITORING / METRICS", d)
|
|
}
|
|
func (s *Server) audit(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
n := getInt(r.URL.Query().Get("n"), 100, 1, 10000)
|
|
d.Filter = map[string]string{"n": strconv.Itoa(n)}
|
|
var res struct {
|
|
Items []easydns.AuditItem `json:"items"`
|
|
}
|
|
if e := s.get(r.Context(), "/api/v1/audit?n="+strconv.Itoa(n), &res); e != nil {
|
|
d.Error = e.Error()
|
|
} else {
|
|
d.Audit = res.Items
|
|
}
|
|
s.render(w, r, "audit", "Audit log", "Management actions recorded by EasyDNS. Not DNS query history.", "audit", "MONITORING / AUDIT", d)
|
|
}
|
|
func (s *Server) status(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
if e := s.get(r.Context(), "/api/v1/status", &d.Status); e != nil {
|
|
d.Error = e.Error()
|
|
}
|
|
if e := s.get(r.Context(), "/api/v1/health", &d.Health); e != nil && d.Error == "" {
|
|
d.Error = e.Error()
|
|
}
|
|
// /readyz is plaintext and may return 503; record only the returned readiness result.
|
|
if _, e := s.api.Get(r.Context(), "/readyz", nil); e != nil {
|
|
d.Filter = map[string]string{"ready": "Not ready / unavailable"}
|
|
} else {
|
|
d.Filter = map[string]string{"ready": "Ready"}
|
|
}
|
|
s.render(w, r, "status", "Server status", "Service roles, listeners, health and readiness.", "status", "MONITORING / SERVER STATUS", d)
|
|
}
|
|
func (s *Server) configuration(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
if e := s.get(r.Context(), "/api/v1/status", &d.Status); e != nil {
|
|
d.Error = e.Error()
|
|
}
|
|
if e := s.get(r.Context(), "/api/v1/policies", &d.Policies); e != nil && d.Error == "" {
|
|
d.Error = e.Error()
|
|
}
|
|
s.render(w, r, "configuration", "Configuration", "Read visible runtime settings and request hot reload. File editing is not exposed by REST.", "configuration", "MONITORING / CONFIGURATION", d)
|
|
}
|
|
func (s *Server) reload(w http.ResponseWriter, r *http.Request) {
|
|
d := PageData{}
|
|
if r.FormValue("confirm") != "RELOAD" {
|
|
d.Error = "Type RELOAD to confirm."
|
|
} else {
|
|
var res any
|
|
if e := s.api.Write(r.Context(), "POST", "/api/v1/config/reload", nil, "", &res); e != nil {
|
|
d.Error = e.Error()
|
|
} else {
|
|
redirect(w, r, "/configuration?notice=reloaded")
|
|
return
|
|
}
|
|
}
|
|
_ = s.get(r.Context(), "/api/v1/status", &d.Status)
|
|
_ = s.get(r.Context(), "/api/v1/policies", &d.Policies)
|
|
s.render(w, r, "configuration", "Configuration", "Hot reload with rollback on failure.", "configuration", "MONITORING / CONFIGURATION", d)
|
|
}
|
|
|
|
var _ = time.Now
|