feat: 1.1.0 interface, geography drill-down and live attack map
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 19s
CI / Tests (Linux, race detector) (push) Successful in 35s
CI / Dependency audit (push) Successful in 16s
Release / ci (push) Successful in 35s
Release / Build image (arm64) (push) Successful in 51s
Release / Build image (amd64) (push) Successful in 1m11s
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Skipped
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 19s
CI / Tests (Linux, race detector) (push) Successful in 35s
CI / Dependency audit (push) Successful in 16s
Release / ci (push) Successful in 35s
Release / Build image (arm64) (push) Successful in 51s
Release / Build image (amd64) (push) Successful in 1m11s
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Skipped
A new console interface (side rail, seven pages, opening sentence and active slice on every page, IP drawer, light and dark design tokens); a Geography page that drills from the world to a country's regions and cities; a Live page drawing attacks as arcs on a flat map or a globe. Backend: GET /api/v1/geo, GET /api/v1/live/recent, host/src/dst on live events, --location, a strict static file pattern, and live federation through GET /api/v1/peer/stream (peer token only, bounded and sanitized relay on the hub). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ff143ec4a2
commit
f4b295f582
37 files changed
+5312
-1000
No files matched your search
+135
-10
@@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -29,6 +30,9 @@ var webFS embed.FS
|
||||
// maxSSE bounds the simultaneous live streams.
|
||||
const maxSSE = 20
|
||||
|
||||
// maxPeerSSE bounds the simultaneous hub subscriptions to /api/v1/peer/stream.
|
||||
const maxPeerSSE = 10
|
||||
|
||||
// sseHeartbeat is how often a live stream pings and revalidates its session.
|
||||
var sseHeartbeat = 20 * time.Second
|
||||
|
||||
@@ -73,6 +77,8 @@ type Server struct {
|
||||
lim *limiter
|
||||
mux *http.ServeMux
|
||||
assets fs.FS
|
||||
geo geoCache
|
||||
psse atomic.Int32
|
||||
}
|
||||
|
||||
// New builds the handler.
|
||||
@@ -88,11 +94,11 @@ func New(opt Options) http.Handler {
|
||||
m.HandleFunc("POST /login", s.login)
|
||||
m.HandleFunc("POST /logout", s.logout)
|
||||
m.HandleFunc("GET /{$}", s.index)
|
||||
for _, f := range []string{"/style.css", "/app.js", "/i18n.js", "/login.js", "/logo.png", "/favicon.png", "/world.js"} {
|
||||
name := strings.TrimPrefix(f, "/")
|
||||
m.HandleFunc("GET "+f, func(w http.ResponseWriter, r *http.Request) { s.static(w, r, name) })
|
||||
}
|
||||
m.HandleFunc("GET /{file}", s.asset)
|
||||
m.HandleFunc("GET /api/v1/peer/export", s.peerExport)
|
||||
m.HandleFunc("GET /api/v1/peer/stream", s.peerStream)
|
||||
m.HandleFunc("GET /api/v1/geo", s.api(s.geoAPI))
|
||||
m.HandleFunc("GET /api/v1/live/recent", s.api(s.liveRecent))
|
||||
m.HandleFunc("GET /api/v1/summary", s.api(s.summary))
|
||||
m.HandleFunc("GET /api/v1/traffic", s.api(s.traffic))
|
||||
m.HandleFunc("GET /api/v1/ips", s.api(s.ips))
|
||||
@@ -220,17 +226,18 @@ type exportCache struct {
|
||||
v map[string][]byte
|
||||
}
|
||||
|
||||
// peerExport is the only route the peer token opens: GET /api/v1/peer/export.
|
||||
func (s *Server) peerExport(w http.ResponseWriter, r *http.Request) {
|
||||
// peerGate admits only the federation token (and --peer-allow sources); it
|
||||
// answers the request itself and returns false when it refuses.
|
||||
func (s *Server) peerGate(w http.ResponseWriter, r *http.Request) bool {
|
||||
if len(s.opt.PeerTokens) == 0 && !s.opt.NoAuth {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "federation is not enabled"})
|
||||
return
|
||||
return false
|
||||
}
|
||||
if len(s.opt.PeerAllow) > 0 {
|
||||
a, err := netip.ParseAddr(clientIP(r, s.opt.TrustedProxy))
|
||||
if err != nil || !prefixesContain(s.opt.PeerAllow, a) {
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "source not allowed"})
|
||||
return
|
||||
return false
|
||||
}
|
||||
}
|
||||
if !s.opt.NoAuth && s.authKind(r) != "peer" {
|
||||
@@ -239,6 +246,14 @@ func (s *Server) peerExport(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="urutau-peer"`)
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "unauthorized"})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// peerExport: GET /api/v1/peer/export, opened by the peer token.
|
||||
func (s *Server) peerExport(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.peerGate(w, r) {
|
||||
return
|
||||
}
|
||||
rg := store.ParseRange(r.URL.Query().Get("range")).Name
|
||||
@@ -267,6 +282,22 @@ func prefixesContain(ps []netip.Prefix, a netip.Addr) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
var assetRe = regexp.MustCompile(`^[a-z0-9-]+\.(js|css|png|svg|json)$`)
|
||||
|
||||
// asset serves a top-level file of the embedded web directory by name.
|
||||
func (s *Server) asset(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("file")
|
||||
if !assetRe.MatchString(name) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if st, err := fs.Stat(s.assets, name); err != nil || !st.Mode().IsRegular() {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
s.static(w, r, name)
|
||||
}
|
||||
|
||||
func (s *Server) static(w http.ResponseWriter, r *http.Request, name string) {
|
||||
b, err := fs.ReadFile(s.assets, name)
|
||||
if err != nil {
|
||||
@@ -281,6 +312,10 @@ func (s *Server) static(w http.ResponseWriter, r *http.Request, name string) {
|
||||
ct = "text/javascript; charset=utf-8"
|
||||
case strings.HasSuffix(name, ".png"):
|
||||
ct = "image/png"
|
||||
case strings.HasSuffix(name, ".svg"):
|
||||
ct = "image/svg+xml"
|
||||
case strings.HasSuffix(name, ".json"):
|
||||
ct = "application/json; charset=utf-8"
|
||||
case strings.HasSuffix(name, ".html"):
|
||||
ct = "text/html; charset=utf-8"
|
||||
}
|
||||
@@ -539,10 +574,27 @@ func (s *Server) stream(w http.ResponseWriter, r *http.Request) {
|
||||
h.Set("X-Accel-Buffering", "no")
|
||||
ch, stop := s.opt.Store.Subscribe()
|
||||
defer stop()
|
||||
var hubCh <-chan store.LiveEvent // nil blocks forever: no hub, no peer events
|
||||
if s.opt.Hub != nil {
|
||||
c, stopHub := s.opt.Hub.Subscribe()
|
||||
defer stopHub()
|
||||
hubCh = c
|
||||
}
|
||||
fmt.Fprint(w, "retry: 3000\n: connected\n\n")
|
||||
fl.Flush()
|
||||
tick := time.NewTicker(sseHeartbeat)
|
||||
defer tick.Stop()
|
||||
send := func(e store.LiveEvent) bool {
|
||||
if k := s.authKind(r); k != "session" && k != "bearer" {
|
||||
return false
|
||||
}
|
||||
b, _ := json.Marshal(e)
|
||||
if _, err := fmt.Fprintf(w, "event: ev\ndata: %s\n\n", b); err != nil {
|
||||
return false
|
||||
}
|
||||
fl.Flush()
|
||||
return true
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
@@ -556,10 +608,63 @@ func (s *Server) stream(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
fl.Flush()
|
||||
case e := <-ch:
|
||||
if k := s.authKind(r); k != "session" && k != "bearer" {
|
||||
if !send(s.opt.Store.Live(e)) {
|
||||
return
|
||||
}
|
||||
b, _ := json.Marshal(e)
|
||||
case e := <-hubCh:
|
||||
if !send(e) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// peerStream: GET /api/v1/peer/stream, SSE of this instance's own malicious
|
||||
// events for a hub. Opened only by the peer token, like the export.
|
||||
func (s *Server) peerStream(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.peerGate(w, r) {
|
||||
return
|
||||
}
|
||||
fl, ok := w.(http.Flusher)
|
||||
if !ok {
|
||||
http.Error(w, "streaming unsupported", 500)
|
||||
return
|
||||
}
|
||||
if s.psse.Add(1) > maxPeerSSE {
|
||||
s.psse.Add(-1)
|
||||
w.Header().Set("Retry-After", "10")
|
||||
http.Error(w, "too many peer streams", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
defer s.psse.Add(-1)
|
||||
_ = http.NewResponseController(w).SetWriteDeadline(time.Time{})
|
||||
h := w.Header()
|
||||
h.Set("Content-Type", "text/event-stream")
|
||||
h.Set("Cache-Control", "no-store")
|
||||
h.Set("X-Accel-Buffering", "no")
|
||||
ch, stop := s.opt.Store.Subscribe()
|
||||
defer stop()
|
||||
fmt.Fprint(w, "retry: 3000\n: connected\n\n")
|
||||
fl.Flush()
|
||||
tick := time.NewTicker(sseHeartbeat)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
return
|
||||
case <-tick.C:
|
||||
if !s.opt.NoAuth && s.authKind(r) != "peer" {
|
||||
return
|
||||
}
|
||||
if _, err := fmt.Fprint(w, ": ping\n\n"); err != nil {
|
||||
return
|
||||
}
|
||||
fl.Flush()
|
||||
case e := <-ch:
|
||||
if !core.Category(e.Cat).IsMalicious() {
|
||||
continue
|
||||
}
|
||||
b, _ := json.Marshal(s.opt.Store.Live(e))
|
||||
if _, err := fmt.Fprintf(w, "event: ev\ndata: %s\n\n", b); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -568,6 +673,26 @@ func (s *Server) stream(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// liveRecent: GET /api/v1/live/recent?limit=200 (max 500), newest first, local and peers.
|
||||
func (s *Server) liveRecent(w http.ResponseWriter, r *http.Request) {
|
||||
n, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
if n < 1 {
|
||||
n = 200
|
||||
}
|
||||
if n > 500 {
|
||||
n = 500
|
||||
}
|
||||
list := s.opt.Store.LiveRecent(n)
|
||||
if s.opt.Hub != nil {
|
||||
list = append(list, s.opt.Hub.Recent(n)...)
|
||||
}
|
||||
sort.SliceStable(list, func(i, j int) bool { return list[i].T > list[j].T })
|
||||
if len(list) > n {
|
||||
list = list[:n]
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"events": list})
|
||||
}
|
||||
|
||||
func (s *Server) blocklist(asJSON bool) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
|
||||
Reference in new issue
Block a user