mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-11-22 16:57:56 +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>
110 lines
3.8 KiB
Go
110 lines
3.8 KiB
Go
package main
|
|
|
|
// Per-client token bucket for public endpoints whose requests cost terrain
|
|
// tile downloads or heavy compute (/api/elevation/profile, /api/planner/*).
|
|
// Clients are told apart by wsLimiter.clientIPRightmost: X-Forwarded-For is
|
|
// believed only from webSocket.trustedProxies, and is read from the right, so
|
|
// the client is the last hop no trusted proxy vouches for and a forged
|
|
// leftmost entry buys nothing (these limits protect an upstream quota).
|
|
// Operators behind a CDN must list the CDN's ranges in
|
|
// webSocket.trustedProxies, or every client behind it looks like the CDN.
|
|
// When clients cannot be told apart (a local reverse proxy that is not in
|
|
// webSocket.trustedProxies, the shipped default), the per-client limit is
|
|
// skipped, as /ws and the login limiter skip theirs: one bucket for the whole
|
|
// site would refuse real visitors. The global budgets still apply (terrain
|
|
// downloads: srtmBudgetFor; planner jobs: plannerGlobalJobsPerMin), and a
|
|
// one-time warning tells the operator what to configure.
|
|
|
|
import (
|
|
"log"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// clientLimiter charges tokens to the caller's bucket. A nil limiter, ipr or
|
|
// rl disables the limit (tests).
|
|
type clientLimiter struct {
|
|
ipr *wsLimiter
|
|
rl *rateLimiter
|
|
warn sync.Once
|
|
// tag is the log prefix ("los"); what names the limit in the warning
|
|
// ("terrain tile").
|
|
tag, what string
|
|
}
|
|
|
|
// clientSharedKey names, in logs, the clients that cannot be told apart. It
|
|
// is never a bucket: those clients are not charged.
|
|
const clientSharedKey = "shared"
|
|
|
|
// clientKey is r's bucket, the client IP, and whether it identifies a
|
|
// client. When it does not, the key is clientSharedKey, nothing is charged
|
|
// to it and the one-time warning is logged.
|
|
func (c *clientLimiter) clientKey(r *http.Request) (key string, distinct bool) {
|
|
if c.ipr == nil {
|
|
return clientSharedKey, false
|
|
}
|
|
ip, distinct := c.ipr.clientIPRightmost(r)
|
|
if distinct && ip != nil {
|
|
return ip.String(), true
|
|
}
|
|
c.warn.Do(func() {
|
|
log.Printf("[%s] WARNING: clients cannot be told apart (requests arrive from %v), "+
|
|
"so the per-client %s rate limit is off and only the global budget applies. "+
|
|
"Set webSocket.trustedProxies to your reverse proxy's address for per-client limits.", c.tag, ip, c.what)
|
|
})
|
|
return clientSharedKey, false
|
|
}
|
|
|
|
// take consumes one token from r's bucket. When none is left it reports how
|
|
// long until the next one. It writes nothing and takes no lock but the
|
|
// bucket map's, so callers may hold their own locks around it.
|
|
func (c *clientLimiter) take(r *http.Request) (bool, time.Duration) {
|
|
return c.takeN(r, 1)
|
|
}
|
|
|
|
// takeN consumes n tokens from r's bucket, all or nothing; on refusal it
|
|
// reports how long until n are available and takes none. n < 1 always
|
|
// succeeds, and leaves no bucket behind, as does a client that cannot be
|
|
// told apart from the others (clientKey).
|
|
func (c *clientLimiter) takeN(r *http.Request, n int) (bool, time.Duration) {
|
|
if c.off() || n < 1 {
|
|
return true, 0
|
|
}
|
|
key, distinct := c.clientKey(r)
|
|
if !distinct {
|
|
return true, 0
|
|
}
|
|
return c.rl.takeN(key, n)
|
|
}
|
|
|
|
// refundN gives back n tokens takeN just took from r's bucket.
|
|
func (c *clientLimiter) refundN(r *http.Request, n int) {
|
|
if c.off() || n < 1 {
|
|
return
|
|
}
|
|
if key, distinct := c.clientKey(r); distinct {
|
|
c.rl.refundN(key, n)
|
|
}
|
|
}
|
|
|
|
func (c *clientLimiter) off() bool { return c == nil || c.ipr == nil || c.rl == nil }
|
|
|
|
// retryAfterSecs rounds wait up to whole seconds, at least 1.
|
|
func retryAfterSecs(wait time.Duration) int {
|
|
secs := int(math.Ceil(wait.Seconds()))
|
|
if secs < 1 {
|
|
secs = 1
|
|
}
|
|
return secs
|
|
}
|
|
|
|
// writeRateLimited answers 429 "rate limited" with a Retry-After of at least
|
|
// 1 s.
|
|
func writeRateLimited(w http.ResponseWriter, wait time.Duration) {
|
|
w.Header().Set("Retry-After", strconv.Itoa(retryAfterSecs(wait)))
|
|
writeError(w, http.StatusTooManyRequests, "rate limited")
|
|
}
|