2
0
Fork 0
forked from zynk/zynk

0.41.2 Implement comprehensive flood protection for IRC bot.

Phase 1: Incoming Protocol-Level Protection
- Added MAX_LINES_PER_POLL = 50 constant to limit lines processed per
  poll iteration in irc_feed(), preventing burst flooding.
- Added poll_lines_left counter to Session struct, reset each poll cycle.
- Changed RateLimitEntry to track by user@host instead of nick, preventing
  trivial bypass via nick cycling.
- Parse userhost from IRC prefix (nick!user@host) in irc_handle().
- Added violation tracking to RateLimitEntry for escalating penalties.

Phase 2: Outgoing Message Throttling
- Added OUTGOING_DELAY_MS = 100 constant for minimum delay between sends.
- Modified net_send() with timestamp-based throttling using clock_gettime().
- Added 200ms delay between lines in !gitlog output (cmd_gitlog.h).
- Added 150ms delay between AI response chunks (ai.h).

Phase 3: Rate Limit Improvements
- Increased RATE_LIMIT_MAX from 8 to 32 for larger tracking buffer.
- Added TELL_DELIVER_MAX = 5 to limit messages per nick delivery.
- Added AI_COOLDOWN_SECS = 5 to prevent rapid AI query re-submission.

Phase 4: Command-Specific Fixes
- Added rate_limit_check() to cmd_greeting_or_chat.h for casual chat.
- Limited tell_deliver() to 5 messages per nick, excess stays queued.
- Added 5-second cooldown after AI query completion before next query.

Files modified: zynk.h, zynk.c, irc.h, net.h, ai.h, db.h, and all
19 command header files to pass current_userhost to rate_limit_check().
This commit is contained in:
Johannes Findeisen 2026-07-25 20:37:07 +02:00
commit 3b97b4d902
25 changed files with 136 additions and 42 deletions

15
ai.h
View file

@ -81,6 +81,7 @@ void strip_opencode_header(char *s) {
volatile pid_t pending_ai_pid;
volatile long long pending_ai_id;
const char *g_opencode_bin = OPENCODE_BIN;
time_t last_ai_completion;
/*
* ai_child_task - Child process entry point for running an AI query.
@ -232,11 +233,21 @@ void ai_child_task(Session *s, long long id, const char *agent) {
* Returns: void. Sends error messages to IRC on failure.
*/
void ai_ask(Session *s, const char *question, const char *reply_target, const char *src_nick, const char *agent) {
if (rate_limit_check(src_nick, reply_target) < 0) return;
if (rate_limit_check(src_nick, reply_target, current_userhost) < 0) return;
if (pending_ai_pid > 0) {
irc_reply(s, reply_target, src_nick, "Already processing an AI query, please wait.");
return;
}
if (last_ai_completion > 0) {
time_t now = time(NULL);
if (now - last_ai_completion < AI_COOLDOWN_SECS) {
char msg[128];
snprintf(msg, sizeof msg, "AI cooldown, please wait %lld seconds.",
(long long)(AI_COOLDOWN_SECS - (now - last_ai_completion)));
irc_reply(s, reply_target, src_nick, msg);
return;
}
}
db_update_lastq(question);
long long id = 0;
if (ai_start(question, reply_target, src_nick, &id) != 0) {
@ -275,6 +286,7 @@ void ai_check_completion(Session *s) {
long long done_id = pending_ai_id;
if (waitpid(pending_ai_pid, &wstatus, WNOHANG) <= 0) return;
pending_ai_pid = 0;
last_ai_completion = time(NULL);
char *nick = ai_get_nick(done_id);
char *result = ai_get_answer(done_id);
int is_code_change = 0;
@ -306,6 +318,7 @@ void ai_check_completion(Session *s) {
answer[end] = save;
pos = end + 1;
chunks++;
if (pos < alen) { struct timespec ts = { .tv_sec = 0, .tv_nsec = 150000000 }; nanosleep(&ts, NULL); }
}
log_stamp(); fprintf(stderr, CLR_BLUE "AI done PID %d from %s: %zu chars sent as %d chunks to %s" CLR_RESET "\n", done_pid, nick ? nick : "?", alen, chunks, target);
if (is_code_change) try_compile_and_restart(s, target);