Files
Lucas RayanandClaude Opus 5.5 8546d14cef
CI / Format and vet (pull_request) Failing after 53s
CI / Dependency audit (pull_request) Successful in 33s
CI / Tests (Linux, race detector) (pull_request) Successful in 2m1s
CI / Docker images (build and smoke test) (pull_request) Failing after 47s
feat!: Urutau 2.0.0, ultralight probes and a single analysis hub
Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that
tails the logs, ships raw lines to the hub over HTTPS with an on-disk
spool, and applies enforce decisions locally. The hub keeps one node per
probe (primary node = the 1.x data dir, unchanged), does all analysis and
serves the UI. Peer federation is removed. 'urutau import' brings 1.x
data dirs in with hash and count verification. UI: probes page, host
qualified incidents, IPs never wrap, egress heatmap labels, live and
replay globes no longer auto-rotate, replay repeat, live traffic metrics.

BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone;
log path variables move from the hub to the probe. See docs/migration-2.0.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:14:50 -03:00

111 lines
3.6 KiB
Go

package app
import (
"context"
"log/slog"
"net/http"
"net/http/httptest"
"net/netip"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel"
)
func TestParseTorLists(t *testing.T) {
got := ParseTorExitList(strings.NewReader("1.2.3.4\n# c\nlixo\n2001:db8::1 # x\n999.1.1.1\n::ffff:5.6.7.8\n"))
if len(got) != 3 || got[2].String() != "5.6.7.8" {
t.Fatalf("exits: %v", got)
}
js := `{"relays":[{"or_addresses":["10.1.1.1:9001","[2001:db8::2]:443","junk"],"exit_addresses":["10.1.1.2"]},{"or_addresses":[]}]}`
a, err := ParseOnionoo(strings.NewReader(js))
if err != nil || len(a) != 3 || a[1].String() != "2001:db8::2" {
t.Fatalf("onionoo: %v %v", a, err)
}
if _, err := ParseOnionoo(strings.NewReader("<html>")); err == nil {
t.Fatal("garbage must fail")
}
// the size limit truncates the input: broken JSON, an error
if _, err := ParseOnionoo(strings.NewReader(`{"relays":[{"or_addresses":["1.1.1.1:1"]}` + strings.Repeat(" ", maxTorList+10) + `]}`)); err == nil {
t.Fatal("oversize must fail")
}
}
func TestRefreshTorListsCache(t *testing.T) {
hits := 0
fail := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits++
if fail || r.Header.Get("User-Agent") != "urutau" {
http.Error(w, "x", 500)
return
}
if r.URL.Path == "/exit" {
w.Write([]byte("198.51.100.1\n"))
return
}
w.Write([]byte(`{"relays":[{"or_addresses":["198.51.100.2:9001"]}]}`))
}))
defer srv.Close()
defer torLists.setManual(nil)
defer torLists.setAuto([]netip.Addr{}, []netip.Addr{})
dir := filepath.Join(t.TempDir(), "tor")
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
ctx := context.Background()
refreshTorLists(ctx, dir, srv.URL+"/exit", srv.URL+"/relays", time.Hour, log)
if !intel.IsTorExit(netip.MustParseAddr("198.51.100.1")) || !intel.IsTorRelay(netip.MustParseAddr("198.51.100.2")) || hits != 2 {
t.Fatalf("lists not loaded, hits=%d", hits)
}
refreshTorLists(ctx, dir, srv.URL+"/exit", srv.URL+"/relays", time.Hour, log) // fresh cache: no download
if hits != 2 {
t.Fatalf("cache ignored: %d", hits)
}
torLists.setAuto([]netip.Addr{}, []netip.Addr{})
fail = true
refreshTorLists(ctx, dir, srv.URL+"/exit", srv.URL+"/relays", 0, log) // stale + failing: the cache serves
if !intel.IsTorExit(netip.MustParseAddr("198.51.100.1")) || !intel.IsTorRelay(netip.MustParseAddr("198.51.100.2")) {
t.Fatal("fallback to cache failed")
}
// the manual list coexists
torLists.setManual([]netip.Addr{netip.MustParseAddr("203.0.113.9")})
if !intel.IsTorExit(netip.MustParseAddr("203.0.113.9")) || !intel.IsTorExit(netip.MustParseAddr("198.51.100.1")) {
t.Fatal("manual + auto merge failed")
}
}
func TestNetTypeTorEntry(t *testing.T) {
a := netip.MustParseAddr("198.51.100.5")
intel.SetTorExits([]netip.Addr{a})
defer intel.SetTorExits(nil)
if typ, _ := intel.NetTypeIP(a, 0, "", ""); typ != "tor" {
t.Fatal(typ)
}
}
func TestEgressOwnAllowAndProvider(t *testing.T) {
if got := providerASNs("31898, AS47583,x", nil); len(got) != 2 || got[0] != "AS31898" || got[1] != "AS47583" {
t.Fatalf("%v", got)
}
ps := ownPublicPrefixes()
for _, p := range ps {
if p.Addr().IsPrivate() || p.Addr().IsLoopback() {
t.Fatal(p)
}
}
// the peer has no URUTAU_PEERS: URUTAU_TRUSTED carries both servers, and a
// bad item does not discard the others
al := egressAllow(Config{Trusted: "136.248.78.83,lixo,195.200.0.252/32"}, "")
for _, ipS := range []string{"136.248.78.83", "195.200.0.252"} {
ok := false
for _, p := range al {
ok = ok || p.Contains(netip.MustParseAddr(ipS))
}
if !ok {
t.Fatal("not allowed: ", ipS)
}
}
}