From 75863ad581ce96c468a36c8d2680a0b07ad2ae56 Mon Sep 17 00:00:00 2001 From: martbost Date: Fri, 21 Aug 2026 07:47:38 -0500 Subject: [PATCH] Exempt container-internal calls from the translate rate limit (cache warmer) --- server.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 3859ebd..30777c9 100644 --- a/server.js +++ b/server.js @@ -368,7 +368,9 @@ const trIpHits=new Map(); function trLimited(ip){ const now=Date.now(); const h=trIpHits.get(ip)||{n:0,ts:now}; if(now-h.ts>600000){h.n=0;h.ts=now} h.n++; trIpHits.set(ip,h); if(trIpHits.size>2000)trIpHits.clear(); return h.n>60; } async function handleTranslate(req,res){ const ip=String(req.headers['x-forwarded-for']||req.socket.remoteAddress||'').split(',')[0].trim(); - if(trLimited(ip))return json(res,429,{error:'Too many translation requests — give it a minute.'}); + // direct container-internal calls (no proxy header) skip the limiter — used by the cache warmer + const internal=!req.headers['x-forwarded-for']&&/^(127\.|10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.|::1|::ffff:(127\.|10\.|172\.|192\.168\.))/.test(String(req.socket.remoteAddress||'')); + if(!internal&&trLimited(ip))return json(res,429,{error:'Too many translation requests — give it a minute.'}); const b=await bodyJson(req).catch(()=>null); const tl=b?String(b.tl||''):''; const texts=b&&Array.isArray(b.texts)?b.texts.slice(0,25).map(t=>String(t).slice(0,300)):null;