Files
urutau/internal/server/report_html_svg.go
T
Lucas RayanandClaude Opus 5.5 c33cca22e3
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 16s
CI / Tests (Linux, race detector) (push) Successful in 1m3s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Skipped
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Successful in 3s
feat: 1.4.0 second firewall, egress analysis and HTML report
Optional second firewall (URUTAU_ENFORCE off/observe/on) through the host
fail2ban, outbound traffic analysis (beaconing, mining, spam, DNS bypass,
unauthorised scans) with authorised scanner workloads, host kit in
deploy/host and a printable HTML report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 23:35:34 -03:00

185 lines
5.7 KiB
Go

package server
import (
"fmt"
"html"
"math"
"strings"
"time"
)
// Palette of the printed report (light theme, Ciberfauna identity).
var reportPalette = []string{"#f97316", "#087f68", "#f0b35b", "#45d6b0", "#c2410c", "#4b6b61", "#9aa9a2"}
type chartItem struct {
Label string
N float64
}
func clip(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[:n-1]) + "…"
}
// niceMax rounds a maximum up to 1, 2, 5 x 10^k so the axis reads well.
func niceMax(v float64) float64 {
if v <= 0 {
return 1
}
p := math.Pow(10, math.Floor(math.Log10(v)))
for _, m := range []float64{1, 2, 5, 10} {
if v <= m*p {
return m * p
}
}
return 10 * p
}
// svgArea draws a filled time series. All text is escaped; numbers are formatted here.
func svgArea(vals []uint32, start, step int64, fi func(float64) string, title string) string {
const w, h, ml, mr, mt, mb = 680.0, 150.0, 40.0, 8.0, 8.0, 20.0
var sb strings.Builder
fmt.Fprintf(&sb, `<svg class="chart" viewBox="0 0 %g %g" role="img" aria-label="%s" preserveAspectRatio="xMidYMid meet">`, w, h, html.EscapeString(title))
var mx float64
for _, v := range vals {
if float64(v) > mx {
mx = float64(v)
}
}
top := niceMax(mx)
pw, ph := w-ml-mr, h-mt-mb
for i := 0; i <= 4; i++ {
y := mt + ph - ph*float64(i)/4
fmt.Fprintf(&sb, `<line x1="%g" y1="%.1f" x2="%g" y2="%.1f" stroke="#d7e2dd" stroke-width="0.6"/>`, ml, y, w-mr, y)
fmt.Fprintf(&sb, `<text x="%g" y="%.1f" font-size="9" text-anchor="end" fill="#55665f">%s</text>`, ml-4, y+3, fi(top*float64(i)/4))
}
n := len(vals)
if n > 1 {
xs := func(i int) float64 { return ml + pw*float64(i)/float64(n-1) }
ys := func(v uint32) float64 { return mt + ph - ph*float64(v)/top }
var line strings.Builder
for i, v := range vals {
if i == 0 {
fmt.Fprintf(&line, "M%.1f %.1f", xs(i), ys(v))
} else {
fmt.Fprintf(&line, "L%.1f %.1f", xs(i), ys(v))
}
}
fmt.Fprintf(&sb, `<path d="%sL%.1f %.1f L%.1f %.1f Z" fill="#f97316" fill-opacity="0.18"/>`, line.String(), xs(n-1), mt+ph, xs(0), mt+ph)
fmt.Fprintf(&sb, `<path d="%s" fill="none" stroke="#f97316" stroke-width="1.4" stroke-linejoin="round"/>`, line.String())
if step > 0 {
for i := 0; i < 5; i++ {
idx := (n - 1) * i / 4
ts := start + int64(idx)*step
lbl := time.Unix(ts, 0).UTC().Format("02/01")
if step < 86400 && int64(n)*step <= 3*86400 {
lbl = time.Unix(ts, 0).UTC().Format("02/01 15h")
}
anchor := "middle"
if i == 0 {
anchor = "start"
} else if i == 4 {
anchor = "end"
}
fmt.Fprintf(&sb, `<text x="%.1f" y="%g" font-size="9" text-anchor="%s" fill="#55665f">%s</text>`, xs(idx), h-6, anchor, lbl)
}
}
} else if n == 1 {
fmt.Fprintf(&sb, `<circle cx="%g" cy="%.1f" r="2" fill="#f97316"/>`, ml+pw/2, mt+ph-ph*float64(vals[0])/top)
}
sb.WriteString(`</svg>`)
return sb.String()
}
// svgBars draws horizontal bars with the label on the left and the value on the right.
func svgBars(items []chartItem, color string, fi func(float64) string, title string) string {
const w, lw, rw, rh = 300.0, 112.0, 40.0, 19.0
if len(items) == 0 {
return ""
}
var mx float64
for _, it := range items {
if it.N > mx {
mx = it.N
}
}
if mx <= 0 {
mx = 1
}
h := rh*float64(len(items)) + 2
var sb strings.Builder
fmt.Fprintf(&sb, `<svg class="chart" viewBox="0 0 %g %g" role="img" aria-label="%s">`, w, h, html.EscapeString(title))
bw := w - lw - rw
for i, it := range items {
y := rh * float64(i)
l := it.N / mx * bw
if it.N > 0 && l < 1.5 {
l = 1.5
}
fmt.Fprintf(&sb, `<text x="%g" y="%.1f" font-size="10" text-anchor="end" fill="#22352f">%s</text>`, lw-6, y+13, html.EscapeString(clip(it.Label, 20)))
fmt.Fprintf(&sb, `<rect x="%g" y="%.1f" width="%.1f" height="11" rx="2" fill="%s"/>`, lw, y+3.5, l, color)
fmt.Fprintf(&sb, `<text x="%.1f" y="%.1f" font-size="10" fill="#55665f">%s</text>`, lw+l+4, y+13, fi(it.N))
}
sb.WriteString(`</svg>`)
return sb.String()
}
// svgDonut draws a ring; the legend is HTML next to it (see donutLegend).
func svgDonut(items []chartItem, title string) string {
var total float64
for _, it := range items {
total += it.N
}
var sb strings.Builder
fmt.Fprintf(&sb, `<svg class="donut" viewBox="0 0 120 120" role="img" aria-label="%s">`, html.EscapeString(title))
const r = 44.0
circ := 2 * math.Pi * r
sb.WriteString(`<circle cx="60" cy="60" r="44" fill="none" stroke="#eaf2ef" stroke-width="20"/>`)
if total > 0 {
off := 0.0
for i, it := range items {
if it.N <= 0 {
continue
}
l := it.N / total * circ
fmt.Fprintf(&sb, `<circle cx="60" cy="60" r="44" fill="none" stroke="%s" stroke-width="20" stroke-dasharray="%.2f %.2f" stroke-dashoffset="%.2f" transform="rotate(-90 60 60)"/>`,
reportPalette[i%len(reportPalette)], l, circ-l, -off)
off += l
}
}
sb.WriteString(`</svg>`)
return sb.String()
}
// svgMotif is the faint decoration of the cover, built from the attack series.
func svgMotif(vals []uint32) string {
var sb strings.Builder
sb.WriteString(`<svg class="motif" viewBox="0 0 210 120" preserveAspectRatio="none" aria-hidden="true">`)
for i := 1; i <= 7; i++ {
fmt.Fprintf(&sb, `<line x1="0" y1="%d" x2="210" y2="%d" stroke="#45d6b0" stroke-opacity="0.12" stroke-width="0.25"/>`, i*15, i*15)
}
n := len(vals)
if n > 1 {
var mx uint32 = 1
for _, v := range vals {
if v > mx {
mx = v
}
}
step := 1
if n > 120 {
step = n / 120
}
for i := 0; i < n; i += step {
x := 210 * float64(i) / float64(n-1)
hgt := 4 + 38*float64(vals[i])/float64(mx)
fmt.Fprintf(&sb, `<line x1="%.1f" y1="%.1f" x2="%.1f" y2="%.1f" stroke="#f0b35b" stroke-opacity="0.55" stroke-width="0.7" stroke-linecap="round"/>`, x, 60-hgt/2, x, 60+hgt/2)
}
}
sb.WriteString(`</svg>`)
return sb.String()
}