Exempt container-internal calls from the translate rate limit (cache warmer)
This commit is contained in:
@@ -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; }
|
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){
|
async function handleTranslate(req,res){
|
||||||
const ip=String(req.headers['x-forwarded-for']||req.socket.remoteAddress||'').split(',')[0].trim();
|
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 b=await bodyJson(req).catch(()=>null);
|
||||||
const tl=b?String(b.tl||''):'';
|
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;
|
const texts=b&&Array.isArray(b.texts)?b.texts.slice(0,25).map(t=>String(t).slice(0,300)):null;
|
||||||
|
|||||||
Reference in New Issue
Block a user