feat: report preview page, sharp report logo, time to ban by blocker
The Reports page now previews the HTML report itself (same document as the download and the print), with only Download and Print. The report logo is the repository logo at 800 px. Survival splits fail2ban and the Urutau second firewall. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5d3374ee11
commit
9bdfdb491b
10 files changed
+245
-500
No files matched your search
@@ -2,17 +2,14 @@ package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress"
|
||||
@@ -52,61 +49,16 @@ type reportInput struct {
|
||||
Peers bool // other hosts included: health and egress cover only this server
|
||||
}
|
||||
|
||||
var (
|
||||
logoOnce sync.Once
|
||||
logoURI string
|
||||
)
|
||||
// reportLogo is the repository logo.png (1254 px) scaled to 800 px WebP, sharp
|
||||
// on the cover even when printed. Regenerate it from ./logo.png when the logo changes.
|
||||
//
|
||||
//go:embed report_logo.webp
|
||||
var reportLogo []byte
|
||||
|
||||
// logoDataURI returns web/logo.png scaled to 200px as a data URI (computed once).
|
||||
func logoDataURI() template.URL {
|
||||
logoOnce.Do(func() {
|
||||
raw, err := webFS.ReadFile("web/logo.png")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
src, err := png.Decode(bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dst := scaleRGBA(src, 200)
|
||||
var buf bytes.Buffer
|
||||
if png.Encode(&buf, dst) == nil {
|
||||
logoURI = "data:image/png;base64," + base64.StdEncoding.EncodeToString(buf.Bytes())
|
||||
}
|
||||
})
|
||||
return template.URL(logoURI)
|
||||
}
|
||||
var logoURI = "data:image/webp;base64," + base64.StdEncoding.EncodeToString(reportLogo)
|
||||
|
||||
// scaleRGBA shrinks to a square of side n by averaging premultiplied pixels.
|
||||
func scaleRGBA(src image.Image, n int) *image.NRGBA {
|
||||
b := src.Bounds()
|
||||
w, h := b.Dx(), b.Dy()
|
||||
dst := image.NewNRGBA(image.Rect(0, 0, n, n))
|
||||
for y := 0; y < n; y++ {
|
||||
y0, y1 := y*h/n, (y+1)*h/n
|
||||
if y1 <= y0 {
|
||||
y1 = y0 + 1
|
||||
}
|
||||
for x := 0; x < n; x++ {
|
||||
x0, x1 := x*w/n, (x+1)*w/n
|
||||
if x1 <= x0 {
|
||||
x1 = x0 + 1
|
||||
}
|
||||
var r, g, bl, a, c uint64
|
||||
for yy := y0; yy < y1 && yy < h; yy++ {
|
||||
for xx := x0; xx < x1 && xx < w; xx++ {
|
||||
pr, pg, pb, pa := src.At(b.Min.X+xx, b.Min.Y+yy).RGBA()
|
||||
r, g, bl, a, c = r+uint64(pr), g+uint64(pg), bl+uint64(pb), a+uint64(pa), c+1
|
||||
}
|
||||
}
|
||||
if c == 0 || a == 0 {
|
||||
continue
|
||||
}
|
||||
dst.SetNRGBA(x, y, color.NRGBA{R: uint8(r * 255 / a), G: uint8(g * 255 / a), B: uint8(bl * 255 / a), A: uint8(a / c >> 8)})
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
// logoDataURI returns the report logo as a data URI.
|
||||
func logoDataURI() template.URL { return template.URL(logoURI) }
|
||||
|
||||
// ---- view model -------------------------------------------------------------
|
||||
|
||||
@@ -763,7 +715,8 @@ func (s *Server) writeReportHTML(w http.ResponseWriter, r *http.Request, rc *rep
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
// the document is self-contained: inline CSS and data: images only, no script
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; img-src data:; base-uri 'none'; form-action 'none'; frame-ancestors 'none'")
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; img-src data:; base-uri 'none'; form-action 'none'; frame-ancestors 'self'")
|
||||
w.Header().Set("X-Frame-Options", "SAMEORIGIN") // previewed in an iframe by the Reports page only
|
||||
disp := "attachment"
|
||||
if r.URL.Query().Get("inline") == "1" {
|
||||
disp = "inline"
|
||||
|
||||
Reference in new issue
Block a user