Files
OpsLog/internal/integrations/udp/forwarder_test.go
T
rouggy 4352b9aec5 fix(udp): read WSJT-X packets that arrive through a relay
A forwarder (W&P, seen in the field in front of MSHV) prepends the origin as
plain text before re-broadcasting:

    "127.0.0.1:2237|" + <the original, untouched WSJT-X packet>

That puts the magic 15 bytes in, so every datagram failed on "bad magic
0x3132372e" — those four bytes being ASCII "127." — and an operator running
MSHV behind the relay saw no decodes, no callsigns and no auto-logged QSOs.

No new service type: what follows the header IS a WSJT-X packet, so the parser
and everything downstream apply unchanged, and a separate type would duplicate
decode, status and logged-ADIF handling to strip 15 bytes. ParseWSJT skips the
header instead, which also covers any other relay that wraps traffic this way.

The match is deliberately narrow — the magic must fall within the first 64
bytes AND every byte before it must be printable ASCII. A corrupt or truncated
packet that merely contains those four bytes somewhere is not resurrected into
a QSO; it fails exactly as it did before.

Test data is the real captured datagram, header included.
2026-08-09 08:06:21 +02:00

64 lines
2.4 KiB
Go

package udp
import (
"bytes"
"testing"
)
// Bytes captured from a real W&P relay in front of MSHV: the origin as text,
// a '|', then the untouched WSJT-X packet. This exact datagram produced
// "bad magic 0x3132372e" — 0x3132372e being ASCII "127.".
var wpDecode = []byte{
// "127.0.0.1:2237|"
0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x3a, 0x32, 0x32, 0x33, 0x37, 0x7c,
// magic, schema 3, type 2 (Decode), id "MSHV"
0xad, 0xbc, 0xcb, 0xda, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x04, 0x4d, 0x53, 0x48, 0x56,
0x01, 0x01, 0x4b, 0x31, 0x28, 0x00, 0x00, 0x00, 0x16,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8a,
0x00, 0x00, 0x00, 0x03, 0x46, 0x54, 0x38, // "FT8"
0x00, 0x00, 0x00, 0x0e, 0x54, 0x4e, 0x38, 0x47, 0x44, 0x20, 0x39, 0x41, 0x31, 0x4d, 0x4d, 0x20, 0x37, 0x33, // "TN8GD 9A1MM 73"
0x00, 0x00,
}
func TestParseWSJTBehindAForwarder(t *testing.T) {
if _, _, err := ParseWSJT(wpDecode); err != nil {
t.Fatalf("relayed packet still fails: %v", err)
}
// And the header is what was in the way: without it the same bytes parse.
if _, _, err := ParseWSJT(wpDecode[15:]); err != nil {
t.Fatalf("bare packet fails: %v", err)
}
}
func TestStripForwarderHeader(t *testing.T) {
bare := wpDecode[15:]
if got := stripForwarderHeader(bare); !bytes.Equal(got, bare) {
t.Error("a packet with no header must be returned untouched")
}
if got := stripForwarderHeader(wpDecode); !bytes.Equal(got, bare) {
t.Error("the text header was not stripped")
}
// A BINARY prefix is not a forwarder header — refusing it is what stops a
// corrupt packet that merely contains the magic from becoming a QSO.
binPrefix := append([]byte{0x00, 0x01, 0x02}, bare...)
if got := stripForwarderHeader(binPrefix); !bytes.Equal(got, binPrefix) {
t.Error("a non-printable prefix must not be treated as a header")
}
// Beyond the window, the magic is ignored however printable the prefix is.
far := append(bytes.Repeat([]byte("A"), maxFwdHeader+1), bare...)
if got := stripForwarderHeader(far); !bytes.Equal(got, far) {
t.Error("magic past maxFwdHeader must not be trusted")
}
// No magic anywhere, and runt packets, must not panic or invent anything.
for _, junk := range [][]byte{[]byte("127.0.0.1:2237|hello"), {}, {0xad}, {0xad, 0xbc, 0xcb}} {
if got := stripForwarderHeader(junk); !bytes.Equal(got, junk) {
t.Errorf("junk %q was altered", junk)
}
}
}