new privacy features

This commit is contained in:
Racks 2026-03-26 18:06:23 +01:00
parent ddf7817d5c
commit c90b6c5c39
4 changed files with 1248 additions and 24 deletions

View file

@ -18,7 +18,7 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/address"
)
//go:embed dashboard.html
//go:embed dashboard.html dashboard_public.html
var dashboardHTML embed.FS
type dashboardServer struct {
@ -91,6 +91,7 @@ func startDashboard(n *node, logger *log.Logger, listenAddr, username, password
mux := http.NewServeMux()
mux.HandleFunc("/", d.handleIndex)
mux.HandleFunc("/api/status", d.handleStatus)
mux.HandleFunc("/api/status/admin", d.handleStatusAdmin)
mux.HandleFunc("/api/peer/traffic", d.handlePeerTraffic)
mux.HandleFunc("/api/peer/ban", d.handlePeerBan)
@ -144,7 +145,11 @@ func (d *dashboardServer) withAuth(next http.Handler) http.Handler {
}
func (d *dashboardServer) handleIndex(w http.ResponseWriter, _ *http.Request) {
bs, err := dashboardHTML.ReadFile("dashboard.html")
page := "dashboard.html"
if d.readOnly {
page = "dashboard_public.html"
}
bs, err := dashboardHTML.ReadFile(page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -154,6 +159,22 @@ func (d *dashboardServer) handleIndex(w http.ResponseWriter, _ *http.Request) {
}
func (d *dashboardServer) handleStatus(w http.ResponseWriter, _ *http.Request) {
status := d.buildStatus(!d.readOnly)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(status)
}
func (d *dashboardServer) handleStatusAdmin(w http.ResponseWriter, _ *http.Request) {
if d.readOnly {
http.Error(w, "admin status is unavailable on the public dashboard", http.StatusForbidden)
return
}
status := d.buildStatus(true)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(status)
}
func (d *dashboardServer) buildStatus(showAdditional bool) dashboardStatus {
status := dashboardStatus{Now: time.Now()}
self := d.node.core.GetSelf()
subnet := d.node.core.Subnet()
@ -184,23 +205,24 @@ func (d *dashboardServer) handleStatus(w http.ResponseWriter, _ *http.Request) {
})
}
sort.Slice(status.Peers, func(i, j int) bool { return status.Peers[i].URI < status.Peers[j].URI })
for _, s := range d.node.core.GetSessions() {
status.Sessions = append(status.Sessions, dashboardFlow{
IP: keyToIP(s.Key),
RX: s.RXBytes,
TX: s.TXBytes,
Uptime: s.Uptime.String(),
})
}
for _, p := range d.node.core.GetPaths() {
status.Paths = append(status.Paths, dashboardPath{IP: keyToIP(p.Key), Path: p.Path})
}
for _, t := range d.node.core.GetTree() {
status.Tree = append(status.Tree, dashboardTree{IP: keyToIP(t.Key), Parent: keyToIP(t.Parent)})
if showAdditional {
for _, s := range d.node.core.GetSessions() {
status.Sessions = append(status.Sessions, dashboardFlow{
IP: keyToIP(s.Key),
RX: s.RXBytes,
TX: s.TXBytes,
Uptime: s.Uptime.String(),
})
}
for _, p := range d.node.core.GetPaths() {
status.Paths = append(status.Paths, dashboardPath{IP: keyToIP(p.Key), Path: p.Path})
}
for _, t := range d.node.core.GetTree() {
status.Tree = append(status.Tree, dashboardTree{IP: keyToIP(t.Key), Parent: keyToIP(t.Parent)})
}
}
status.Banned = d.bannedList()
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(status)
return status
}
func (d *dashboardServer) handlePeerTraffic(w http.ResponseWriter, r *http.Request) {