1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package de.tudarmstadt.informatik.hostage;
- import android.util.Log;
- public class ConnectionGuard {
- private final static ConnectionGuard INSTANCE = new ConnectionGuard();
- private ConnectionGuard() {
- }
- public final static long ONE_SECOND_IN_NANOSECONDS = 1000000000;
- private static long lastTimestamp = 0;
- private static String lastIP = "";
- private static String lastProtocol = "";
- public synchronized static boolean registerConnection(String protocol, String ip) {
- long timestamp = System.nanoTime();
- boolean result = detectedPortscan(protocol, ip, timestamp);
- lastTimestamp = timestamp;
- lastIP = ip;
- lastProtocol = protocol;
- return result;
- }
-
- public synchronized static boolean detectedPortscan(String protocol, String ip){
- return detectedPortscan(protocol, ip, System.nanoTime());
- }
-
- public synchronized static boolean detectedPortscan(String protocol, String ip, long timestamp) {
- boolean result = false;
- boolean firstConnection = (lastTimestamp == 0);
- boolean belowThreshold = ((timestamp - lastTimestamp) < ONE_SECOND_IN_NANOSECONDS);
- boolean sameIP = (lastIP.equals(ip));
- boolean sameProtocol = (lastProtocol.equals(protocol));
- if (!firstConnection && sameIP && belowThreshold && !sameProtocol) {
- result = true;
- }
-
- return result;
- }
- }
|