ConnectionGuard.java 986 B

12345678910111213141516171819202122232425262728293031
  1. package de.tudarmstadt.informatik.hostage;
  2. import de.tudarmstadt.informatik.hostage.logging.Logger;
  3. public class ConnectionGuard {
  4. private final static ConnectionGuard INSTANCE = new ConnectionGuard();
  5. private ConnectionGuard() {
  6. }
  7. private final static long ONE_SECOND_IN_NANOSECONDS = 1000000000;
  8. private static long lastTimestamp = 0;
  9. private static String lastIP = "";
  10. private static String lastProtocol = "";
  11. public static void registerConnection(String protocol, String ip) {
  12. long timestamp = System.nanoTime();
  13. boolean firstConnection = (lastTimestamp == 0);
  14. boolean belowThreshold = ((timestamp - lastTimestamp) < ONE_SECOND_IN_NANOSECONDS);
  15. boolean sameIP = (lastIP == ip);
  16. boolean sameProtocol = (lastProtocol == protocol);
  17. if (!firstConnection && sameIP && belowThreshold && !sameProtocol) {
  18. Logger.logPortscan(Hostage.getContext(), System.currentTimeMillis(), ip);
  19. }
  20. lastTimestamp = timestamp;
  21. lastIP = ip;
  22. lastProtocol = protocol;
  23. }
  24. }