ConnectionGuard.java 666 B

12345678910111213141516171819202122232425
  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. private final static long ONE_SECOND_IN_NANOSECONDS = 1000000000;
  8. private static long lastTimestamp = 0;
  9. public static void registerConnection() {
  10. long timestamp = System.nanoTime();
  11. boolean firstConnection = (lastTimestamp == 0);
  12. boolean belowThreshold = ((timestamp - lastTimestamp) < ONE_SECOND_IN_NANOSECONDS);
  13. if (!firstConnection && belowThreshold) {
  14. Log.d("ConnectionGuard", "PORTSCAN DETECTED");
  15. }
  16. lastTimestamp = timestamp;
  17. }
  18. }