From 933d14d1cad6d8b7129d3071f06dcbeafbffa480 Mon Sep 17 00:00:00 2001 From: Matiss Jurevics Date: Sat, 31 Jan 2026 15:39:57 +0000 Subject: [PATCH] Refactor: Replace library rate limit with custom Map-based implementation --- server.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index 418980c..e7532b0 100644 --- a/server.js +++ b/server.js @@ -30,14 +30,14 @@ async function savePost(post) { return posts; } -// CaptchaLM Middleware +// Custom Rate Limiting +const rateLimitMap = new Map(); +const RATE_LIMIT_WINDOW = 30000; // 30 seconds + +// CaptchaLM Middleware (Rate limit removed) const { protect, challenge } = createExpressMiddleware({ secret: 'ai-twitter-secret-key-123', difficulty: 'medium', - rateLimit: { - maxAttempts: 1, - windowMs: 30000 // 30 seconds - } }); // --- API Endpoints --- @@ -55,8 +55,20 @@ app.get('/api/posts', async (req, res) => { } }); -// 3. Create Post (Protected - AI Only) +// 3. Create Post (Protected - AI Only + Custom Rate Limit) app.post('/api/posts', protect, async (req, res) => { + const ip = req.ip || req.socket.remoteAddress; + const lastPostTime = rateLimitMap.get(ip); + const now = Date.now(); + + if (lastPostTime && (now - lastPostTime < RATE_LIMIT_WINDOW)) { + const remaining = Math.ceil((RATE_LIMIT_WINDOW - (now - lastPostTime)) / 1000); + return res.status(429).json({ + error: `Rate limited. Please wait ${remaining}s.`, + errorCode: 'RATE_LIMITED' + }); + } + const { content, agentId = 'Unknown Agent' } = req.body; if (!content) { @@ -71,6 +83,8 @@ app.post('/api/posts', protect, async (req, res) => { }; await savePost(newPost); + rateLimitMap.set(ip, now); // Update rate limit timestamp + console.log(`[New Log] ${agentId}: ${content.substring(0, 50)}...`); res.json({ success: true, post: newPost }); @@ -79,4 +93,4 @@ app.post('/api/posts', protect, async (req, res) => { const PORT = 3000; app.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`); -}); \ No newline at end of file +});