Libraryless. Click here for Pure Java version (2676L/17K).
scope simpleSpamClientDetect2. static int #interval = 60000; static double #perSecondThreshold = 2.0; // per IP + URI static int #spamMax; static Set<S> #whiteList = syncSet(myIPs()); static Lock #myLock = lock(); // ip + uri -> time, count static Map<PairS, Pair<Long, Int>> #ipsAndUris = synchroMap(); // returns pair(count, bad) static Pair<Int, Bool> simpleSpamClientDetect2(S clientIP, S uri) { lock myLock; // TODO: clean up map sometimes Pair<S> pair = pair(clientIP, uri); Pair<Long, Int> value = ipsAndUris.get(pair); if (value == null || now() > value.a+interval) ipsAndUris.put(pair, value = pair(now(), 1)); else value.b++; spamMax = iround(perSecondThreshold*interval/1000); ret pair(value.b, !contains(whiteList, clientIP) && value.b >= spamMax); } // returns new count static int #markNoSpam(S clientIP, S uri) { lock myLock; PairS pair = pair(clientIP, uri); Pair<Long, Int> value = ipsAndUris.get(pair); if (value != null && value.b > 0) ret --value.b; ret -1; } end scope
Began life as a copy of #1014077
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1014225 |
Snippet name: | simpleSpamClientDetect2 |
Eternal ID of this version: | #1014225/8 |
Text MD5: | 6a6c27960c29d6f3eebf34d6977e1dbf |
Transpilation MD5: | e6daaba8c32f66f0ca587065ac6e94be |
Author: | stefan |
Category: | javax / http |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-10-27 13:08:06 |
Source code size: | 1062 bytes / 35 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 462 / 575 |
Version history: | 7 change(s) |
Referenced in: | [show references] |