ConnectionGuard.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package de.tudarmstadt.informatik.hostage;
  2. import android.util.Log;
  3. public class ConnectionGuard {
  4. private final static ConnectionGuard INSTANCE = new ConnectionGuard();
  5. private ConnectionGuard() {
  6. }
  7. public 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 synchronized static boolean registerConnection(String protocol, String ip) {
  12. long timestamp = System.nanoTime();
  13. boolean result = detectedPortscan(protocol, ip, timestamp);
  14. lastTimestamp = timestamp;
  15. lastIP = ip;
  16. lastProtocol = protocol;
  17. return result;
  18. }
  19. public synchronized static boolean detectedPortscan(String protocol, String ip){
  20. return detectedPortscan(protocol, ip, System.nanoTime());
  21. }
  22. public synchronized static boolean detectedPortscan(String protocol, String ip, long timestamp) {
  23. boolean result = false;
  24. boolean firstConnection = (lastTimestamp == 0);
  25. boolean belowThreshold = ((timestamp - lastTimestamp) < ONE_SECOND_IN_NANOSECONDS);
  26. boolean sameIP = (lastIP.equals(ip));
  27. boolean sameProtocol = (lastProtocol.equals(protocol));
  28. if (!firstConnection && sameIP && belowThreshold && !sameProtocol) {
  29. result = true;
  30. }
  31. return result;
  32. }
  33. }