mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-11-22 09:07:06 +00:00
Both features are OPT-IN ("los" and "planner" config blocks, default
off); with them off nothing is fetched and payloads are unchanged.
Route view
- Multi-path picker discoverability: on-map "N hops shown · tap to see
all" badge, picker pulse (reduced-motion safe), one-time tip, row hover
arrow; "no GPS" hops counted apart from "unresolved".
- Line of sight for every leg of a packet route: clear / marginal /
blocked chips (first-Fresnel clearance, k=4/3), obstruction markers on
the map, docked terrain-profile panel with antenna/frequency inputs and
a &los= deep link. Backed by GET /api/elevation/profile, an Open-Meteo
proxy that only accepts known node positions, with weighted budgets,
back-off and a per-IP limit.
Repeater planner (#/tools/planner)
- Chain: LOS on every leg between any number of points or nodes.
- Relay: best single relay for A→B among known repeaters and open
terrain.
- Area: suggested new repeater sites that best join the existing
repeater backbone in a box up to 100×100 km, picked greedily to merge
clusters, then remove single points of failure, then reach unlinked
repeaters.
- Terrain: SRTM 1° tiles from AWS Open Data, in memory only (~90 m,
bounded LRU); the server stays read-only. Exact-length gunzip, fetch
budget with back-off, missing terrain reported rather than read as sea
level, fuzzed pixel maths.
- GET /api/planner/{profile,relay,coverage}: quantised cache keys, 202 +
progress then cached 200, separate compute slots, per-IP and global
limits, failed jobs remembered, unpolled jobs cancelled. Nodes pass the
same blacklist / hidden-name / geo_filter rules as /api/nodes. The Go
LOS maths matches public/los.js on shared test vectors.
Also: the per-IP client limiter is extracted from elevation.go into
client_limit.go (behaviour-preserving) so both features share it.
Perf (real SRTM): coverage cold 1.5-5.6 s (tile download), warm 10 ms;
worst real compute 545 ms; peak RSS 91 MiB; live /api/nodes and
/api/packets p95 at most 1.6x while a job computes (GOMAXPROCS=2).
Tests: Go suites for elevation, terrain (fuzz), LOS vectors, graph
(brute-force property test), search and planner API; frontend unit
suites in test-all.sh; Playwright e2e for route LOS and the planner.
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: efiten <erwin.fiten@gmail.com>
132 lines
6.5 KiB
Go
132 lines
6.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func xffReq(peer string, xff ...string) *http.Request {
|
|
r := httptest.NewRequest("GET", "/api/planner/x", nil)
|
|
r.RemoteAddr = peer
|
|
for _, v := range xff {
|
|
r.Header.Add("X-Forwarded-For", v)
|
|
}
|
|
return r
|
|
}
|
|
|
|
func TestClientIPRightmost(t *testing.T) {
|
|
trusted := []string{"10.0.0.0/8", "2001:db8:ffff::/48"}
|
|
cases := []struct {
|
|
name string
|
|
trusted []string
|
|
peer string
|
|
xff []string
|
|
want string
|
|
wantDistinct bool
|
|
wantLeftmost string // what /ws resolves for the same request
|
|
}{
|
|
{"single proxy", trusted, "10.0.0.2:1", []string{"203.0.113.20"}, "203.0.113.20", true, "203.0.113.20"},
|
|
{"chain of two trusted proxies", trusted, "10.0.0.2:1", []string{"203.0.113.20, 10.0.0.9"}, "203.0.113.20", true, "203.0.113.20"},
|
|
{"spoofed leftmost", trusted, "10.0.0.2:1", []string{"1.2.3.4, 203.0.113.20"}, "203.0.113.20", true, "1.2.3.4"},
|
|
{"spoofed leftmost across header lines", trusted, "10.0.0.2:1", []string{"1.2.3.4", "203.0.113.20"}, "203.0.113.20", true, "1.2.3.4"},
|
|
{"spoofed leftmost behind two proxies", trusted, "10.0.0.2:1", []string{"1.2.3.4, 203.0.113.20, 10.0.0.9"}, "203.0.113.20", true, "1.2.3.4"},
|
|
{"all entries trusted: leftmost", trusted, "10.0.0.2:1", []string{"10.0.0.5, 10.0.0.9"}, "10.0.0.5", true, "10.0.0.5"},
|
|
{"no header", trusted, "10.0.0.2:1", nil, "10.0.0.2", true, "10.0.0.2"},
|
|
{"garbage only", trusted, "10.0.0.2:1", []string{"not-an-ip"}, "10.0.0.2", true, "10.0.0.2"},
|
|
{"garbage rightmost", trusted, "10.0.0.2:1", []string{"203.0.113.20, junk"}, "10.0.0.2", true, "203.0.113.20"},
|
|
{"garbage leftmost is never reached", trusted, "10.0.0.2:1", []string{"junk, 203.0.113.20"}, "203.0.113.20", true, "10.0.0.2"},
|
|
{"ipv6 client", trusted, "10.0.0.2:1", []string{"2001:db8::7"}, "2001:db8::", true, "2001:db8::7"},
|
|
{"ipv6 same /64, other host", trusted, "10.0.0.2:1", []string{"2001:db8:0:0:aaaa::9"}, "2001:db8::", true, "2001:db8::aaaa:0:0:9"},
|
|
{"ipv6 different /64", trusted, "10.0.0.2:1", []string{"2001:db8:0:1::7"}, "2001:db8:0:1::", true, "2001:db8:0:1::7"},
|
|
{"ipv4 with port", trusted, "10.0.0.2:1", []string{"1.2.3.4:5678"}, "1.2.3.4", true, "10.0.0.2"},
|
|
{"ipv6 with port", trusted, "10.0.0.2:1", []string{"[2001:db8::1]:443"}, "2001:db8::", true, "10.0.0.2"},
|
|
{"bracketed bare ipv6", trusted, "10.0.0.2:1", []string{"[2001:db8::1]"}, "2001:db8::", true, "10.0.0.2"},
|
|
{"port on trusted hop still skipped", trusted, "10.0.0.2:1", []string{"203.0.113.20, 10.0.0.9:4000"}, "203.0.113.20", true, "203.0.113.20"},
|
|
{"spoofed leftmost beside ported hop", trusted, "10.0.0.2:1", []string{"1.2.3.4, 203.0.113.20:99"}, "203.0.113.20", true, "1.2.3.4"},
|
|
{"ipv6 proxy chain", trusted, "[2001:db8:ffff::1]:1", []string{"198.51.100.4, 2001:db8:ffff::2"}, "198.51.100.4", true, "198.51.100.4"},
|
|
{"untrusted peer ignores header", trusted, "203.0.113.30:1", []string{"198.51.100.1"}, "203.0.113.30", true, "203.0.113.30"},
|
|
{"private peer, no trusted proxies: not distinct", nil, "10.0.0.2:1", []string{"203.0.113.20"}, "10.0.0.2", false, "10.0.0.2"},
|
|
{"loopback peer, no trusted proxies: not distinct", nil, "127.0.0.1:1", []string{"203.0.113.20"}, "127.0.0.1", false, "127.0.0.1"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
l := &wsLimiter{trustedProxies: parseCIDRList(tc.trusted, "test")}
|
|
ip, distinct := l.clientIPRightmost(xffReq(tc.peer, tc.xff...))
|
|
if ip == nil || ip.String() != tc.want || distinct != tc.wantDistinct {
|
|
t.Errorf("rightmost = %v distinct=%v, want %s distinct=%v", ip, distinct, tc.want, tc.wantDistinct)
|
|
}
|
|
// /ws keeps resolving the leftmost entry.
|
|
ws, _ := l.clientIP(xffReq(tc.peer, tc.xff...))
|
|
if ws == nil || ws.String() != tc.wantLeftmost {
|
|
t.Errorf("/ws clientIP = %v, want %s", ws, tc.wantLeftmost)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClientKeyMasksIPv6To64(t *testing.T) {
|
|
c := &clientLimiter{ipr: &wsLimiter{trustedProxies: parseCIDRList([]string{"10.0.0.0/8"}, "test")}, tag: "t", what: "w"}
|
|
a, _ := c.clientKey(xffReq("10.0.0.2:1", "2001:db8:1:2::1"))
|
|
b, _ := c.clientKey(xffReq("10.0.0.2:1", "2001:db8:1:2:ffff:ffff:ffff:ffff"))
|
|
d, _ := c.clientKey(xffReq("10.0.0.2:1", "2001:db8:1:3::1"))
|
|
if a != b {
|
|
t.Errorf("same /64 gave different keys %q, %q", a, b)
|
|
}
|
|
if a == d {
|
|
t.Errorf("different /64 shared key %q", a)
|
|
}
|
|
// Untrusted IPv6 peer is masked too; IPv4-mapped stays an IPv4 key.
|
|
if k, _ := c.clientKey(xffReq("[2001:db8:9:9::5]:1")); k != "2001:db8:9:9::" {
|
|
t.Errorf("peer key = %q", k)
|
|
}
|
|
if k, _ := c.clientKey(xffReq("10.0.0.2:1", "::ffff:1.2.3.4")); k != "1.2.3.4" {
|
|
t.Errorf("mapped key = %q", k)
|
|
}
|
|
}
|
|
|
|
func TestClientLimiterKeysOnRightmostUntrusted(t *testing.T) {
|
|
c := &clientLimiter{ipr: &wsLimiter{trustedProxies: parseCIDRList([]string{"10.0.0.0/8"}, "test")}, tag: "t", what: "w"}
|
|
// A client rotating its own leftmost entry must stay in one bucket.
|
|
a, _ := c.clientKey(xffReq("10.0.0.2:1", "1.1.1.1, 203.0.113.20"))
|
|
b, _ := c.clientKey(xffReq("10.0.0.2:1", "2.2.2.2, 203.0.113.20"))
|
|
if a != "203.0.113.20" || a != b {
|
|
t.Errorf("keys %q, %q: want both 203.0.113.20", a, b)
|
|
}
|
|
}
|
|
|
|
// Without webSocket.trustedProxies a local reverse proxy hides every client
|
|
// behind one address (the bundled Caddyfile on a default install). One bucket
|
|
// for the whole site would refuse real visitors, so nothing is charged; the
|
|
// global budgets still apply (review #2391).
|
|
func TestClientLimiterSkipsClientsItCannotTellApart(t *testing.T) {
|
|
c := &clientLimiter{ipr: &wsLimiter{}, rl: newRateLimiter(2, time.Hour), tag: "t", what: "w"}
|
|
for i := 0; i < 10; i++ {
|
|
r := xffReq("127.0.0.1:1", fmt.Sprintf("198.51.100.%d", i))
|
|
if key, distinct := c.clientKey(r); distinct || key != clientSharedKey {
|
|
t.Fatalf("clientKey = %q, %v: a loopback peer without trustedProxies is not a client", key, distinct)
|
|
}
|
|
if ok, _ := c.takeN(r, 2); !ok {
|
|
t.Fatalf("request %d limited: every visitor shares one bucket", i)
|
|
}
|
|
c.refundN(r, 2)
|
|
}
|
|
if n := len(c.rl.buckets); n != 0 {
|
|
t.Fatalf("%d buckets for clients that cannot be told apart", n)
|
|
}
|
|
// Listing the proxy turns the per-client limit on.
|
|
c.ipr.trustedProxies = parseCIDRList([]string{"127.0.0.1"}, "test")
|
|
r := xffReq("127.0.0.1:1", "198.51.100.1")
|
|
if ok, _ := c.takeN(r, 2); !ok {
|
|
t.Fatal("first request of a distinct client limited")
|
|
}
|
|
if ok, _ := c.takeN(r, 1); ok {
|
|
t.Fatal("a distinct client over its limit was not limited")
|
|
}
|
|
if ok, _ := c.takeN(xffReq("127.0.0.1:1", "198.51.100.2"), 2); !ok {
|
|
t.Fatal("another client was charged for the first one")
|
|
}
|
|
}
|