Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.bdevgw.app.boerse;
- import java.util.HashMap;
- import java.util.Timer;
- import java.util.TimerTask;
- public class SpamManager
- {
- private HashMap<String, Integer> requests = new HashMap<String, Integer>();
- private HashMap<String, Integer> logins = new HashMap<String, Integer>();
- private int maxRequestsPerSecond;
- private int maxLoginsPerIPPerMinute;
- public SpamManager(int maxRequestsPerSecond, int maxLoginsPerIPPerMinute)
- {
- this.maxRequestsPerSecond = maxRequestsPerSecond;
- this.maxLoginsPerIPPerMinute = maxLoginsPerIPPerMinute;
- Timer clearReq = new Timer();
- clearReq.schedule(new TimerTask()
- {
- @Override
- public void run()
- {
- requests.clear();
- }
- }, 0, 1 * 1000);
- Timer clearLogin = new Timer();
- clearLogin.schedule(new TimerTask()
- {
- @Override
- public void run()
- {
- logins.clear();
- }
- }, 0, 60 * 1000);
- }
- public boolean Request(String ip, int port)
- {
- if(requests.containsKey(ip + ":" + port))
- {
- requests.put(ip + ":" + port, requests.get(ip + ":" + port) + 1);
- }
- else
- {
- requests.put(ip + ":" + port, 1);
- }
- return requests.get(ip + ":" + port) > maxRequestsPerSecond;
- }
- public boolean Login(String ip)
- {
- if(logins.containsKey(ip))
- {
- logins.put(ip, logins.get(ip) + 1);
- }
- else
- {
- logins.put(ip, 1);
- }
- return logins.get(ip) > maxLoginsPerIPPerMinute;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement