Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

35
LINES

< > BotCompany Repo | #1014225 // simpleSpamClientDetect2

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2676L/17K).

1  
scope simpleSpamClientDetect2.
2  
3  
static int #interval = 60000;
4  
static double #perSecondThreshold = 2.0; // per IP + URI
5  
static int #spamMax;
6  
static Set<S> #whiteList = syncSet(myIPs());
7  
static Lock #myLock = lock();
8  
9  
// ip + uri -> time, count
10  
static Map<PairS, Pair<Long, Int>> #ipsAndUris = synchroMap();
11  
12  
// returns pair(count, bad)
13  
static Pair<Int, Bool> simpleSpamClientDetect2(S clientIP, S uri) {
14  
  lock myLock;
15  
  // TODO: clean up map sometimes
16  
  Pair<S> pair = pair(clientIP, uri);
17  
  Pair<Long, Int> value = ipsAndUris.get(pair);
18  
  if (value == null || now() > value.a+interval)
19  
    ipsAndUris.put(pair, value = pair(now(), 1));
20  
  else
21  
    value.b++;
22  
  spamMax = iround(perSecondThreshold*interval/1000);
23  
  ret pair(value.b, !contains(whiteList, clientIP) && value.b >= spamMax);
24  
}
25  
26  
// returns new count
27  
static int #markNoSpam(S clientIP, S uri) {
28  
  lock myLock;
29  
  PairS pair = pair(clientIP, uri);
30  
  Pair<Long, Int> value = ipsAndUris.get(pair);
31  
  if (value != null && value.b > 0) ret --value.b;
32  
  ret -1;
33  
}
34  
35  
end scope

Author comment

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: 464 / 578
Version history: 7 change(s)
Referenced in: [show references]