simple_repeater: filter hash throttle + opt-in message age limit (tth #3)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-02 17:40:09 +01:00
1 parent e3029de6e4
commit 548b96d15c
8 files changed
+370 -29

No files matched your search

+24 -3
View File
@@ -76,6 +76,7 @@ void MyMesh::channelsInit() {
memset(known_channel_flags, 0, sizeof(known_channel_flags));
#ifdef FILTER_CHANNEL_PSKS
filter_dropped = 0;
filter_age_dropped = 0;
{
// comma-separated hex PSKs
const char* seg = FILTER_CHANNEL_PSKS;
@@ -110,7 +111,6 @@ void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Gro
uint8_t* data, size_t len) {
if (type != PAYLOAD_TYPE_GRP_TXT) return; // GRP_DATA (telemetry etc.) is not ours
if (len < 5) return;
if ((data[4] >> 2) != 0) return; // only plain text, per BaseChatMesh.cpp:374
// which registry entry decrypted this? (the caller passes a COPY, not our slot)
int chan_idx = -1;
@@ -122,6 +122,25 @@ void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Gro
}
if (chan_idx < 0) return;
#ifdef FILTER_CHANNEL_PSKS
// `filter age` (ChannelFilter.h): any group text, not only plain ones -- the
// timestamp leads every GRP_TXT. Same veto as the deny list below.
if (known_channel_flags[chan_idx] & CHAN_FLAG_FILTER) {
uint32_t sender_ts;
memcpy(&sender_ts, data, 4);
if (msgAgeCheck(sender_ts, getRTCClock()->getCurrentTime(), _prefs.filter_age_min) == MSG_AGE_OLD) {
filter_age_dropped++;
if (!_prefs.filter_dry_run) {
packet->markDoNotRetransmit();
MESH_DEBUG_PRINTLN("filter: drop fwd, zprava starsi nez %u min", _prefs.filter_age_min);
return;
}
}
}
#endif
if ((data[4] >> 2) != 0) return; // only plain text, per BaseChatMesh.cpp:374
// Work on a bounded copy. Upstream NUL-terminates the caller's buffer in place
// (data[len] = 0), which is fine there but relies on headroom this function
// cannot verify -- and every other parse in this fork is bounds-checked.
@@ -152,9 +171,11 @@ void MyMesh::onChannelText(mesh::Packet* packet, int chan_idx, const char* sende
// (Mesh.cpp:357). Local processing already happened (rxlog/pktfeed capture
// on receive), so the observer evidence trail is intact; the node just
// refuses to re-air it. And the bot must not answer a denied sender either.
packet->markDoNotRetransmit();
filter_dropped++;
MESH_DEBUG_PRINTLN("filter: drop fwd, sender na deny listu");
if (!_prefs.filter_dry_run) { // dry-run: counted, but forwarded
packet->markDoNotRetransmit();
MESH_DEBUG_PRINTLN("filter: drop fwd, sender na deny listu");
}
return;
}
#endif
+126 -15
View File
@@ -82,7 +82,7 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max
return true;
}
strcpy(reply, "Err - filter [add|del <name>|list|clear]");
strcpy(reply, "Err - filter [add|del <name>|list|clear|hash|age|dryrun]");
return true;
}
@@ -107,8 +107,17 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max
// stays intact.
// * Runtime-editable over RF admin and persisted in prefs (f_dhash/f_dhash_n),
// so an incident response is `filter hash add 43`, not a mast reflash.
//
// THROTTLE (tth #3, 2. 11. 2026): an entry may carry an interval --
// `filter hash add 43 60` lets one packet of that byte through per 60 s and
// drops the rest, so a chatty channel is tamed instead of cut off. The window
// state is RAM only (first match after boot passes) and only a pass moves it;
// see HashThrottle in src/helpers/ChannelGate.h. Interval 0 = block, as before.
//
// `filter dryrun on` counts every verdict of `filter` (hash, age, sender deny)
// but forwards anyway -- the way to try a new rule on a live mast.
bool MyMesh::filterHashDenied(const mesh::Packet* packet) const {
bool MyMesh::filterHashDenied(const mesh::Packet* packet) {
uint8_t n = _prefs.deny_hash_n;
if (n == 0) return false;
uint8_t type = packet->getPayloadType();
@@ -116,13 +125,25 @@ bool MyMesh::filterHashDenied(const mesh::Packet* packet) const {
if (packet->payload_len < 1) return false;
if (n > sizeof(_prefs.deny_hash)) n = sizeof(_prefs.deny_hash);
for (int i = 0; i < n; i++) {
if (_prefs.deny_hash[i] == packet->payload[0]) return true;
if (_prefs.deny_hash[i] != packet->payload[0]) continue;
uint16_t secs = _prefs.deny_hash_secs[i];
if (secs != 0 && hash_throttle.pass(i, secs, millis())) {
filter_hash_passed++;
return false;
}
filter_hash_dropped++;
return !_prefs.filter_dry_run;
}
return false;
}
// `filter hash` -- show list + drop counter; `filter hash add|del <hex>` --
// edit (1 byte, e.g. `43` or `0x43`); `filter hash clear` -- empty the list.
// `filter hash` -- show list + counters (passed = let through by a throttle);
// `filter hash add <hex> [secs]` -- block (no secs / 0) or throttle to one
// packet per secs; re-adding a listed byte changes its interval;
// `filter hash del <hex>`; `filter hash clear`.
// Listing: `43` = blocked, `43/60` = throttled to one per 60 s. The Ansible
// module (tth.meshcore.meshcore_filter_hash) parses this line -- keep the
// "filter hash: dropped N, passed M, deny:" shape stable.
bool MyMesh::filterHashHandleCommand(const char* command, char* reply, int reply_max) {
if (memcmp(command, "filter hash", 11) != 0 || (command[11] != 0 && command[11] != ' ')) return false;
@@ -133,6 +154,8 @@ bool MyMesh::filterHashHandleCommand(const char* command, char* reply, int reply
if (strcmp(arg, "clear") == 0) {
_prefs.deny_hash_n = 0;
memset(_prefs.deny_hash, 0, sizeof(_prefs.deny_hash));
memset(_prefs.deny_hash_secs, 0, sizeof(_prefs.deny_hash_secs));
hash_throttle.reset();
savePrefs();
strcpy(reply, "OK - hash deny list cleared");
return true;
@@ -145,35 +168,123 @@ bool MyMesh::filterHashHandleCommand(const char* command, char* reply, int reply
if (hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X')) hex += 2;
char* end;
long v = strtol(hex, &end, 16);
if (end == hex || *end != 0 || end - hex > 2 || v < 0 || v > 0xFF) {
if (end == hex || (*end != 0 && *end != ' ') || end - hex > 2 || v < 0 || v > 0xFF) {
strcpy(reply, "Err - 1 byte hex, e.g. 43");
return true;
}
long secs = 0;
if (*end == ' ') {
if (is_del) { strcpy(reply, "Err - filter hash del <hex>"); return true; }
const char* s = end + 1;
secs = strtol(s, &end, 10);
if (end == s || *end != 0 || secs < 0 || secs > 65535) {
strcpy(reply, "Err - secs 0..65535 (0 = block)");
return true;
}
}
int at = -1;
for (int i = 0; i < n; i++) {
if (_prefs.deny_hash[i] == (uint8_t)v) { at = i; break; }
}
if (is_add) {
if (at >= 0) { strcpy(reply, "Err - already listed"); return true; }
if (n >= sizeof(_prefs.deny_hash)) { strcpy(reply, "Err - list full"); return true; }
_prefs.deny_hash[n++] = (uint8_t)v;
if (at >= 0) {
if (_prefs.deny_hash_secs[at] == (uint16_t)secs) { strcpy(reply, "Err - already listed"); return true; }
_prefs.deny_hash_secs[at] = (uint16_t)secs; // change of interval
hash_throttle.reset(at);
} else {
if (n >= sizeof(_prefs.deny_hash)) { strcpy(reply, "Err - list full"); return true; }
_prefs.deny_hash[n] = (uint8_t)v;
_prefs.deny_hash_secs[n] = (uint16_t)secs;
hash_throttle.reset(n);
n++;
}
} else {
if (at < 0) { strcpy(reply, "Err - not listed"); return true; }
for (int i = at; i + 1 < n; i++) _prefs.deny_hash[i] = _prefs.deny_hash[i + 1];
_prefs.deny_hash[--n] = 0;
for (int i = at; i + 1 < n; i++) {
_prefs.deny_hash[i] = _prefs.deny_hash[i + 1];
_prefs.deny_hash_secs[i] = _prefs.deny_hash_secs[i + 1];
}
hash_throttle.remove(at);
--n;
_prefs.deny_hash[n] = 0;
_prefs.deny_hash_secs[n] = 0;
}
_prefs.deny_hash_n = n;
savePrefs();
// fall through to the listing as the reply
} else if (arg[0] != 0 && strcmp(arg, "list") != 0) {
strcpy(reply, "Err - filter hash [add|del <hex>|list|clear]");
strcpy(reply, "Err - filter hash [add <hex> [secs]|del <hex>|list|clear]");
return true;
}
int len = snprintf(reply, reply_max, "filter hash: dropped %u, deny:", filter_hash_dropped);
if (n == 0 && len < reply_max) snprintf(&reply[len], reply_max - len, " (nic)");
int len = snprintf(reply, reply_max, "filter hash: dropped %u, passed %u, deny:",
filter_hash_dropped, filter_hash_passed);
if (n == 0 && len < reply_max) len += snprintf(&reply[len], reply_max - len, " (nic)");
for (int i = 0; i < n && len < reply_max; i++) {
len += snprintf(&reply[len], reply_max - len, " %02X", _prefs.deny_hash[i]);
if (_prefs.deny_hash_secs[i]) {
len += snprintf(&reply[len], reply_max - len, " %02X/%u", _prefs.deny_hash[i], _prefs.deny_hash_secs[i]);
} else {
len += snprintf(&reply[len], reply_max - len, " %02X", _prefs.deny_hash[i]);
}
}
return true;
}
// `filter dryrun [on|off]` -- verdicts of `filter` (hash, age, sender deny)
// are counted but the packet is forwarded anyway. Persisted (f_dry).
// `filter age [<min>|off]` -- refuse to forward a group text the node can
// DECRYPT (channels of FILTER_CHANNEL_PSKS) whose sender timestamp is older
// than <min> minutes by THIS node's clock (1..10080). Off by default.
// * It judges the sender's clock: a companion that never got a time sync
// stamps 2024 and is dropped. On the CZ mesh 2. 11. 2026 every one of the
// 5/2026 messages older than 10 min was such a clock, none a reflood --
// hence opt-in, and days rather than minutes.
// * Inactive while our own clock reads before 2026 (RTC-less boot). A clock
// running AHEAD is not detected and makes everything look old -- check the
// drift (tools/fw_status.py) before turning it on.
bool MyMesh::filterMiscHandleCommand(const char* command, char* reply, int reply_max) {
if (strcmp(command, "filter dryrun") == 0 || strcmp(command, "filter dryrun on") == 0
|| strcmp(command, "filter dryrun off") == 0) {
if (command[13] == ' ') {
_prefs.filter_dry_run = (command[15] == 'n') ? 1 : 0;
savePrefs();
snprintf(reply, reply_max, "OK - filter dry-run %s", _prefs.filter_dry_run ? "on" : "off");
} else {
snprintf(reply, reply_max, "filter dry-run %s", _prefs.filter_dry_run ? "on" : "off");
}
return true;
}
if (memcmp(command, "filter age", 10) != 0 || (command[10] != 0 && command[10] != ' ')) return false;
#ifndef FILTER_CHANNEL_PSKS
strcpy(reply, "Err - no channel keys in this build (FILTER_CHANNEL_PSKS)");
return true;
#else
const char* arg = (command[10] == ' ') ? &command[11] : "";
bool changed = false;
if (strcmp(arg, "off") == 0) {
_prefs.filter_age_min = 0;
changed = true;
} else if (arg[0] != 0) {
char* end;
long v = strtol(arg, &end, 10);
if (end == arg || *end != 0 || v < 0 || v > MSG_AGE_MAX_MINUTES) {
snprintf(reply, reply_max, "Err - filter age <1..%d min>|off", MSG_AGE_MAX_MINUTES);
return true;
}
_prefs.filter_age_min = (uint16_t)v;
changed = true;
}
if (changed) savePrefs();
if (_prefs.filter_age_min == 0) {
snprintf(reply, reply_max, "%sfilter age off, dropped %u", changed ? "OK - " : "", filter_age_dropped);
} else {
uint32_t now = getRTCClock()->getCurrentTime();
snprintf(reply, reply_max, "%sfilter age %um%s%s, dropped %u", changed ? "OK - " : "",
_prefs.filter_age_min, _prefs.filter_dry_run ? " DRY-RUN" : "",
now < MSG_AGE_CLOCK_VALID_FROM ? " (clock not set, inactive)" : "", filter_age_dropped);
}
return true;
#endif
}
+12 -8
View File
@@ -699,14 +699,6 @@ void MyMesh::sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, ui
bool MyMesh::allowPacketForward(const mesh::Packet *packet) {
if (_prefs.disable_fwd) return false;
// CUSTOM (TeTeHacko): keyless channel-hash deny list (`filter hash`). Before
// takeForwardingRateLimit() in routeRecvPacket(), so a vetoed packet does not
// eat FairnessLimiter tokens either.
if (filterHashDenied(packet)) {
filter_hash_dropped++;
MESH_DEBUG_PRINTLN("allowPacketForward: channel hash %02X on deny list", packet->payload[0]);
return false;
}
if (packet->isRouteFlood()
&& mesh::isFloodHopLimitExceeded(packet, _prefs.flood_max, _prefs.flood_max_unscoped, _prefs.flood_max_advert)) {
return false;
@@ -729,6 +721,14 @@ bool MyMesh::allowPacketForward(const mesh::Packet *packet) {
return false;
}
}
// CUSTOM (TeTeHacko): keyless channel-hash deny list / throttle (`filter hash`).
// LAST of the checks here, so a throttle pass is only spent on a packet that
// would otherwise go out; and before takeForwardingRateLimit() in
// routeRecvPacket(), so a vetoed packet does not eat FairnessLimiter tokens.
if (filterHashDenied(packet)) {
MESH_DEBUG_PRINTLN("allowPacketForward: channel hash %02X on deny list", packet->payload[0]);
return false;
}
return true;
}
@@ -1405,6 +1405,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
#endif
_prefs.fairness_enabled = FAIRNESS_ENABLED_DEFAULT;
filter_hash_dropped = 0;
filter_hash_passed = 0;
_prefs.interference_threshold = 0; // disabled
_prefs.cad_enabled = 0; // hardware CAD before TX (off by default; 'set cad on')
_prefs.loop_detect = LOOP_DETECT_MINIMAL;
@@ -1845,6 +1846,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
// CUSTOM (TeTeHacko): `filter hash [add|del <hex>|clear]` — keyless channel-hash
// deny list. Before filterHandleCommand(), which would claim any `filter ...`.
if (filterHashHandleCommand(command, reply, reply_max)) return;
// CUSTOM (TeTeHacko): `filter dryrun [on|off]`, `filter age [<min>|off]`. Also
// before filterHandleCommand(), which would take `dryrun`/`age` for names.
if (filterMiscHandleCommand(command, reply, reply_max)) return;
#ifdef FILTER_CHANNEL_PSKS
// CUSTOM (TeTeHacko): `filter add|del|list|clear` — runtime deny list for
// channel messages this repeater refuses to forward. See ChannelFilter.h.
+7 -1
View File
@@ -34,6 +34,7 @@
#include <helpers/StatsFormatHelper.h>
#include <helpers/TxtDataHelpers.h>
#include <helpers/FairnessLimiter.h>
#include <helpers/ChannelGate.h>
#include <helpers/RegionMap.h>
#include <helpers/RoutingPolicy.h>
#include "RateLimiter.h"
@@ -210,6 +211,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
// upstream proposal #1502). Kill switch: _prefs.fairness_enabled.
FairnessLimiter fairness_limiter;
uint32_t filter_hash_dropped; // forwards vetoed by the channel-hash deny list
uint32_t filter_hash_passed; // throttled entries: forwards let through within budget
HashThrottle hash_throttle; // per-entry windows of `filter hash add <hex> <secs>`
unsigned long next_fair_sender_normal_refill, next_fair_sender_low_refill, next_fair_group_refill;
uint32_t pending_discover_tag;
unsigned long pending_discover_until;
@@ -249,6 +252,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
#endif
#ifdef FILTER_CHANNEL_PSKS
uint32_t filter_dropped; // forwards vetoed by the deny list
uint32_t filter_age_dropped; // forwards vetoed by `filter age`
#endif
#ifdef BOT_CHANNEL_PSK
// Default zde, ne az v ChannelBot.h -- ten se includuje na konci MyMesh.cpp,
@@ -323,8 +327,10 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
// CUSTOM (TeTeHacko): `fairness [on|off|grid|cap ...|refill ...]` CLI. Defined in MyMesh.cpp.
bool fairnessHandleCommand(const char* command, char* reply, int reply_max);
// CUSTOM (TeTeHacko): keyless deny-forward by channel hash. Definitions in ChannelFilter.h.
bool filterHashDenied(const mesh::Packet* packet) const;
bool filterHashDenied(const mesh::Packet* packet);
bool filterHashHandleCommand(const char* command, char* reply, int reply_max);
// `filter dryrun [on|off]` and `filter age [<min>|off]`. Definitions in ChannelFilter.h.
bool filterMiscHandleCommand(const char* command, char* reply, int reply_max);
// Refill interval in ms: persisted pref (seconds) if set, else the build default.
uint32_t fairGroupRefillMs() const {
return _prefs.fair_group_refill_s ? (uint32_t)_prefs.fair_group_refill_s * 1000 : FAIRNESS_GROUP_REFILL_MS;