From 0243af712e025a5c8fba7909343c3b05bed5d313 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Sun, 8 Nov 2026 13:29:31 +0100 Subject: [PATCH] simple_repeater: channel bot caps itself at 10 replies per 5 min Co-Authored-By: Claude Opus 5.5 --- examples/simple_repeater/ChannelBot.h | 30 ++++++++++++++++++++++++++- examples/simple_repeater/MyMesh.h | 5 +++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/examples/simple_repeater/ChannelBot.h b/examples/simple_repeater/ChannelBot.h index 22417e98..932f2898 100644 --- a/examples/simple_repeater/ChannelBot.h +++ b/examples/simple_repeater/ChannelBot.h @@ -62,6 +62,15 @@ #ifndef BOT_MIN_GAP_MS #define BOT_MIN_GAP_MS 2500 #endif +// Cap on the whole bot over a longer window: at most BOT_BURST_MAX replies per +// BOT_BURST_WINDOW_MS (0 = off). The per-sender window is keyed by NAME, which +// is plain text in a channel message, so ten names walk around it; the floor +// alone still lets through 24 replies/min (~18 % of channel airtime). Normal +// use is ~15 replies a DAY, so 10 per 5 min cuts nothing legitimate. +// BOT_BURST_MAX itself defaults in MyMesh.h (it sizes an array there). +#ifndef BOT_BURST_WINDOW_MS + #define BOT_BURST_WINDOW_MS 300000 +#endif // Senders tracked for the per-sender window. Wraps LRU; overflowing just means // the oldest sender's window is forgotten early, i.e. one extra reply. #ifndef BOT_PEER_SLOTS @@ -90,6 +99,8 @@ void MyMesh::botInit() { bot_ignored = 0; bot_throttled = 0; memset(bot_peers, 0, sizeof(bot_peers)); + memset(bot_burst_until, 0, sizeof(bot_burst_until)); + bot_burst_head = 0; bot_last_reply_secs = 0; const char* psk = BOT_CHANNEL_PSK; @@ -328,6 +339,15 @@ void MyMesh::botOnChannelText(mesh::Packet* packet, const char* sender, const ch bot_throttled++; return; } + // Then the cap over the longer window: the slot at head holds the oldest of + // the last BOT_BURST_MAX replies, and while its window runs we are at the cap. + // Checked before botPeerAllowed(), which claims the sender's window. + if (BOT_BURST_MAX > 0 && bot_burst_until[bot_burst_head] != 0 + && !millisHasNowPassed(bot_burst_until[bot_burst_head])) { + MESH_DEBUG_PRINTLN("bot: reply suppressed, burst cap"); + bot_throttled++; + return; + } // Then this sender's own window. Name-based, so spoofable -- fine, this is a // politeness throttle and channel messages carry no identity anyway. if (!botPeerAllowed(sender)) { @@ -337,6 +357,11 @@ void MyMesh::botOnChannelText(mesh::Packet* packet, const char* sender, const ch } bot_next_reply_at = futureMillis(BOT_MIN_GAP_MS); if (bot_next_reply_at == 0) bot_next_reply_at = 1; // 0 means "not armed" + if (BOT_BURST_MAX > 0) { + unsigned long until = futureMillis(BOT_BURST_WINDOW_MS); + bot_burst_until[bot_burst_head] = until ? until : 1; // 0 means "free slot" + bot_burst_head = (bot_burst_head + 1) % (BOT_BURST_MAX > 0 ? BOT_BURST_MAX : 1); + } botSendReply(packet, reply); } @@ -351,11 +376,14 @@ bool MyMesh::botHandleCommand(const char* command, char* reply) { strcpy(reply, "bot: DISABLED (bad BOT_CHANNEL_PSK)"); return true; } - sprintf(reply, "bot: chan %02X, trigger '%s', cmds '%s', ignore '%s', cooldown %d ms/odesilatel + %d ms floor, replies %u, throttled %u, ignored %u, last %u", + // Short keys and snprintf: a remote CLI reply has 161 B (MyMesh.cpp temp[166] + // minus header), and the old long form already overran it with a real cmds list. + snprintf(reply, 161, "bot: ch %02X trig '%s' cmds '%s' ign '%s' cd %dms/odes + %dms, max %d/%ds, odp %u thr %u ign %u last %u", (uint32_t)known_channels[bot_channel_idx].hash[0], BOT_TRIGGER, BOT_COMMANDS[0] ? BOT_COMMANDS : "(vse)", BOT_IGNORE_SENDERS[0] ? BOT_IGNORE_SENDERS : "(nikdo)", (int)BOT_REPLY_COOLDOWN_MS, (int)BOT_MIN_GAP_MS, + (int)BOT_BURST_MAX, (int)(BOT_BURST_WINDOW_MS / 1000), (uint32_t)bot_replies_sent, (uint32_t)bot_throttled, (uint32_t)bot_ignored, (uint32_t)bot_last_reply_secs); return true; diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 598006ac..08cb1675 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -260,6 +260,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #ifndef BOT_PEER_SLOTS #define BOT_PEER_SLOTS 8 #endif + #ifndef BOT_BURST_MAX + #define BOT_BURST_MAX 10 + #endif int8_t bot_channel_idx; // index into known_channels, -1 = PSK malformed, bot silently off unsigned long bot_next_reply_at; // cooldown gate, 0 = not armed uint32_t bot_replies_sent; @@ -268,6 +271,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { struct BotPeer { uint32_t name_hash; unsigned long next_at; }; BotPeer bot_peers[BOT_PEER_SLOTS]; // per-odesilatel okno, LRU uint32_t bot_last_reply_secs; // RTC time of the last reply, 0 = never + unsigned long bot_burst_until[BOT_BURST_MAX > 0 ? BOT_BURST_MAX : 1]; // konce oken poslednich odpovedi, kruh + uint8_t bot_burst_head; // nejstarsi z poslednich BOT_BURST_MAX odpovedi #endif CayenneLPP telemetry; unsigned long set_radio_at, revert_radio_at;