Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ guessed from current settings.

The PWA publishes everything the companion hears (iteration 1) — or all zero-hop receptions
(iteration 2 after ratification). The local map filter (direct/all toggle) affects only the local
view, not what is published upstream. The ingestor deduplicates by `(origin_id, rx_at, sender_key)`.
view, not what is published upstream. The ingestor deduplicates by `(hunter_pubkey, rx_at, raw)` (`server/internal/store/store.go`, `enforceReceptionIdentity`).
Receptions without a GPS fix are dropped at the PWA before publishing (no row, no publish), and a
fix too inaccurate to place one is refused there too (#274).

Expand Down Expand Up @@ -792,6 +792,23 @@ A `config.json` broker can list `streams` instead of one `url` (#704): each stre
token. The site configures DutchMeshCore this way: collector 1 under `hunter`, collector 2 under
`wardriver`.

### Reading wardrive brokers (#735)

`extraBrokers` in the ingestor's `config.json` lists other brokers (`id`, `url`, `username`,
`password`, `topics`, `format`) whose `.../wardriver/obs` messages are read into `hunter_receptions`
next to our own. Each entry gets its own MQTT client (`core-hunter-ingestor-<id>`), and a broker that
is down at startup does not stop the ingestor. `format` can only be `"wardrive"`, and `id` must be
unique and not `"site"`: `site` is the source of a reception from our own broker.

- The sender is read off `raw` alone by `meshpacket.Classify`. Its five rules need no channel keys
and no app state, so `channel_name` and the `trace_reply`, `telemetry_reply` and `anon_reply`
senders, which the app resolves from its own requests, are not set for these rows.
- The `track` message is not read, only `obs`.
- The `source` column holds the broker `id` (`site` for our own broker).
- `origin_id` and `raw` are folded to lower case, so the same reception through two brokers is one key.
- Insert rule: a row from our own broker replaces an extra-broker copy of the same reception, never
the reverse (`server/internal/store/store.go`).

---

## 10. Resilience invariants (do not break)
Expand Down
7 changes: 7 additions & 0 deletions app/changelog.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
{
"id": "2026-11-25-dutchmeshcore-wardrive-on-map",
"date": "2026-11-25",
"where": "map",
"title": "DutchMeshCore wardrives on the map",
"body": "Receptions that others publish to DutchMeshCore's wardrive topics now show up on the map, with the new hunters they bring."
},
{
"id": "2026-10-29-bar-labels-one-line",
"date": "2026-10-29",
Expand Down
7 changes: 7 additions & 0 deletions changelog.d/2026-11-25-01-dutchmeshcore-wardrive-on-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "2026-11-25-dutchmeshcore-wardrive-on-map",
"date": "2026-11-25",
"where": "map",
"title": "DutchMeshCore wardrives on the map",
"body": "Receptions that others publish to DutchMeshCore's wardrive topics now show up on the map, with the new hunters they bring."
}
56 changes: 56 additions & 0 deletions server/cmd/ingestor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,72 @@ func main() {
log.Fatalf("mqtt connect: %v", t.Error())
}

// An extra broker that is down must not stop the ingestor: each one is
// connected, and retried, on its own goroutine.
var extras []mqtt.Client
for _, b := range cfg.ExtraBrokers {
extras = append(extras, connectExtraBroker(b, st))
}

log.Printf("ingestor listening on %s, topic %s", cfg.HTTPAddr, cfg.MQTTTopic)

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
log.Printf("shutting down")
client.Disconnect(250)
for _, c := range extras {
c.Disconnect(250)
}
_ = st.Close()
}

// connectExtraBroker starts a client for one extra broker and returns at once.
// The first connection is retried every 30 s on a goroutine, and each failure
// is logged here: paho's own ConnectRetry logs a refused or unreachable broker
// only at DEBUG, so a wrong password would be silent. Once connected,
// AutoReconnect takes over.
func connectExtraBroker(b config.Broker, st *store.Store) mqtt.Client {
opts := mqtt.NewClientOptions().
AddBroker(b.URL).
SetUsername(b.Username).
SetPassword(b.Password).
SetClientID("core-hunter-ingestor-" + b.ID).
SetAutoReconnect(true).
SetCleanSession(false)
opts.SetConnectionLostHandler(func(_ mqtt.Client, err error) {
log.Printf("mqtt connection lost on %s: %v", b.ID, err)
})
opts.SetOnConnectHandler(func(c mqtt.Client) {
for _, topic := range b.Topics {
if tok := c.Subscribe(topic, 1, func(_ mqtt.Client, m mqtt.Message) {
if err := ingest.HandleObs(st, b.ID, m.Topic(), m.Payload(), func() string {
return time.Now().UTC().Format(time.RFC3339)
}); err != nil {
log.Printf("ingest error on %s: %v", b.ID, err)
}
}); tok.Wait() && tok.Error() != nil {
log.Printf("subscribe error on %s: %s: %v", b.ID, topic, tok.Error())
} else {
log.Printf("subscribed to %s on %s", topic, b.ID)
}
}
})
c := mqtt.NewClient(opts)
go func() {
for {
t := c.Connect()
t.Wait()
if t.Error() == nil {
return
}
log.Printf("mqtt connect %s: %v", b.ID, t.Error())
time.Sleep(30 * time.Second)
}
}()
return c
}

// mailerAdapter implements httpapi.Mailer over mail.Sender, building the
// account emails from the configured base URL.
type mailerAdapter struct {
Expand Down
12 changes: 11 additions & 1 deletion server/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@
"dbPath": "/app/data/hunter.db",
"httpAddr": ":8090",
"resolveUpstreams": ["https://<resolver-host>/sf7/api/nodes/resolve", "https://<resolver-host>/sf8/api/nodes/resolve"],
"nodePositionUpstreams": ["https://<resolver-host>/sf7/api/nodes/positions", "https://<corescope-host>/api/nodes?limit=2000"]
"nodePositionUpstreams": ["https://<resolver-host>/sf7/api/nodes/positions", "https://<corescope-host>/api/nodes?limit=2000"],
"extraBrokers": [
{
"id": "dmc",
"url": "tcp://<dutchmeshcore-host>:1883",
"username": "<read account>",
"password": "<set me>",
"topics": ["meshcore/hunter/+/wardriver/obs", "meshcore/wardriver/+/wardriver/obs"],
"format": "wardrive"
}
]
}
47 changes: 47 additions & 0 deletions server/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package config

import (
"encoding/json"
"fmt"
"os"
"slices"

"github.com/efiten/core-hunter/server/internal/store"
)

type Config struct {
Expand Down Expand Up @@ -39,6 +43,21 @@ type Config struct {
// must carry the ?limit= it should ask for per page — that is what opts it
// into paging, and it caps a page at 2000 rows.
NodePositionUpstreams []string `json:"nodePositionUpstreams"`

// ExtraBrokers are other MQTT brokers whose wardrive obs messages are read
// into hunter_receptions next to our own (#735).
ExtraBrokers []Broker `json:"extraBrokers"`
}

// Broker is one extra MQTT broker. ID names it in logs and is stored as the
// source of every reception read from it.
type Broker struct {
ID string `json:"id"`
URL string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
Topics []string `json:"topics"`
Format string `json:"format"`
}

func Load(path string) (Config, error) {
Expand All @@ -59,5 +78,33 @@ func Load(path string) (Config, error) {
if c.HTTPAddr == "" {
c.HTTPAddr = ":8090"
}
if err := checkExtraBrokers(c.ExtraBrokers); err != nil {
return c, err
}
return c, nil
}

func checkExtraBrokers(bs []Broker) error {
seen := map[string]bool{}
for i, b := range bs {
bad := func(reason string) error {
return fmt.Errorf("extraBrokers[%d] (%s): %s", i, b.ID, reason)
}
switch {
case b.ID == "":
return bad("id is empty")
case b.ID == store.SourceSite:
return bad(`id "site" is reserved for our own broker`)
case seen[b.ID]:
return bad("id is used twice")
case b.Format != "wardrive":
return bad(`format must be "wardrive"`)
case b.URL == "":
return bad("url is empty")
case len(b.Topics) == 0 || slices.Contains(b.Topics, ""):
return bad("topics must list at least one topic")
}
seen[b.ID] = true
}
return nil
}
60 changes: 60 additions & 0 deletions server/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -54,3 +55,62 @@ func TestLoadAuthExplicit(t *testing.T) {
t.Fatalf("fields not parsed: %+v", c)
}
}

func writeCfg(t *testing.T, body string) string {
t.Helper()
path := t.TempDir() + "/c.json"
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
return path
}

func TestLoadReadsExtraBrokers(t *testing.T) {
c, err := Load(writeCfg(t, `{"mqttUrl":"x","extraBrokers":[{"id":"dmc","url":"tcp://h:1883","username":"u","password":"p","topics":["a/+/obs","b/+/obs"],"format":"wardrive"}]}`))
if err != nil {
t.Fatalf("Load: %v", err)
}
if len(c.ExtraBrokers) != 1 {
t.Fatalf("want 1 broker, got %+v", c.ExtraBrokers)
}
b := c.ExtraBrokers[0]
if b.ID != "dmc" || b.URL != "tcp://h:1883" || b.Username != "u" || b.Password != "p" ||
b.Format != "wardrive" || len(b.Topics) != 2 || b.Topics[1] != "b/+/obs" {
t.Fatalf("broker not read back as given: %+v", b)
}
}

func TestLoadRefusesABadExtraBroker(t *testing.T) {
ok := func(id string) string {
return `{"id":"` + id + `","url":"tcp://h:1883","topics":["a"],"format":"wardrive"}`
}
cases := []struct{ name, brokers, want string }{
{"empty id", `[{"id":"","url":"tcp://h","topics":["a"],"format":"wardrive"}]`, "extraBrokers[0] ()"},
{"id site", `[` + ok("site") + `]`, "extraBrokers[0] (site)"},
{"duplicate id", `[` + ok("dmc") + `,` + ok("dmc") + `]`, "extraBrokers[1] (dmc)"},
{"bad format", `[{"id":"dmc","url":"tcp://h","topics":["a"],"format":"hunter"}]`, "extraBrokers[0] (dmc)"},
{"empty url", `[{"id":"dmc","url":"","topics":["a"],"format":"wardrive"}]`, "extraBrokers[0] (dmc)"},
{"empty topics", `[{"id":"dmc","url":"tcp://h","topics":[],"format":"wardrive"}]`, "extraBrokers[0] (dmc)"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := Load(writeCfg(t, `{"mqttUrl":"x","extraBrokers":`+tc.brokers+`}`))
if err == nil {
t.Fatalf("want an error")
}
if !strings.Contains(err.Error(), tc.want) {
t.Fatalf("error %q does not contain %q", err, tc.want)
}
})
}
}

func TestLoadWithoutExtraBrokers(t *testing.T) {
c, err := Load("testdata/min.json")
if err != nil {
t.Fatalf("Load: %v", err)
}
if c.ExtraBrokers != nil {
t.Fatalf("want nil, got %+v", c.ExtraBrokers)
}
}
15 changes: 14 additions & 1 deletion server/internal/ingest/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ type Store interface {
}

func Handle(s Store, topic string, body []byte, now func() string) error {
return handle(s, topic, body, now, store.ParsePayload)
}

// HandleObs ingests a wardrive obs read from an extra broker; source is the
// broker's id and is stored with the reception (#735).
func HandleObs(s Store, source, topic string, body []byte, now func() string) error {
return handle(s, topic, body, now, func(topic string, body []byte, ts string) (store.Reception, error) {
return store.ParseObs(topic, body, ts, source)
})
}

func handle(s Store, topic string, body []byte, now func() string,
parse func(topic string, body []byte, ts string) (store.Reception, error)) error {
ts := now()
r, err := store.ParsePayload(topic, body, ts)
r, err := parse(topic, body, ts)
if err != nil {
return deadLetter(s, topic, body, ts, "parse: "+err.Error())
}
Expand Down
35 changes: 35 additions & 0 deletions server/internal/ingest/ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,38 @@ func TestHandleLogsNothingForAGoodPayload(t *testing.T) {
t.Fatalf("want no log output, got %q", buf.String())
}
}

const obsTopic = "meshcore/hunter/AA/wardriver/obs"

const obsNow = "2026-11-25T10:00:30Z"

func TestHandleObsInsertsAReception(t *testing.T) {
f := &fake{}
body := []byte(`{"origin_id":"AA","rx_at":"2026-11-25T10:00:00Z","raw":"00","RSSI":-90,"SNR":5.5,"pos":{"lat":51.1,"lon":4.2,"accuracy":8}}`)
if err := HandleObs(f, "dmc", obsTopic, body, func() string { return obsNow }); err != nil {
t.Fatalf("HandleObs: %v", err)
}
if len(f.got) != 1 || len(f.raws) != 0 {
t.Fatalf("want one insert and no dead letter, got %+v / %+v", f.got, f.raws)
}
if f.got[0].Source != "dmc" || f.got[0].IngestedAt != obsNow {
t.Fatalf("source/ingested_at = %q / %q", f.got[0].Source, f.got[0].IngestedAt)
}
}

func TestHandleObsDeadLettersAnUnusableObs(t *testing.T) {
f := &fake{}
noSignal := []byte(`{"origin_id":"AA","rx_at":"2026-11-25T10:00:00Z","raw":"00","SNR":5,"pos":{"lat":51.1,"lon":4.2}}`)
noRaw := []byte(`{"origin_id":"AA","rx_at":"2026-11-25T10:00:00Z","raw":"","RSSI":-90,"SNR":5,"pos":{"lat":51.1,"lon":4.2}}`)
for _, b := range [][]byte{noSignal, noRaw} {
if err := HandleObs(f, "dmc", obsTopic, b, func() string { return obsNow }); err != nil {
t.Fatalf("HandleObs: %v", err)
}
}
if len(f.got) != 0 {
t.Fatalf("Insert should not be called: %+v", f.got)
}
if len(f.raws) != 2 || !strings.HasPrefix(f.raws[0].err, "parse: ") || f.raws[1].err != "missing raw or rx_at" {
t.Fatalf("want parse and missing-raw dead letters, got %+v", f.raws)
}
}
Loading
Loading