|
|
@@ -35,22 +35,18 @@ def evaluate_loesing (fingerprint):
|
|
|
|
|
|
return None
|
|
|
|
|
|
-# If abs is True, we need to have observed more than 32 connections on
|
|
|
-# some day to be considered blocked.
|
|
|
-def is_blocked (harshness, bridge_ips_max, bridge_ips_today, abs):
|
|
|
+# Consider a bridge blocked if it receives <= t connections in one day,
|
|
|
+# when it has previously received >= m connections. Note that t=32,
|
|
|
+# m=100 is NOT the same as Loesing's algorithm, as we consider m only
|
|
|
+# for previous days, while Loesing's algorithm considers all days within
|
|
|
+# the period. t must be less than m.
|
|
|
+def is_blocked_abs (t, m, bridge_ips_max, bridge_ips_today):
|
|
|
if bridge_ips_max is None:
|
|
|
return False
|
|
|
|
|
|
- threshold = 8 * harshness
|
|
|
+ return bridge_ips_max >= m and bridge_ips_today <= t
|
|
|
|
|
|
- if abs:
|
|
|
- # absolute threshold
|
|
|
- return bridge_ips_today <= threshold and bridge_ips_max > 32
|
|
|
- else:
|
|
|
- # relative threshold based on prior connection counts
|
|
|
- return bridge_ips_today <= threshold and bridge_ips_today <= bridge_ips_max - 32 + threshold
|
|
|
-
|
|
|
-def evaluate (harshness, fingerprint, abs=True):
|
|
|
+def evaluate_abs (t, m, fingerprint):
|
|
|
fingerprint = fingerprint.upper()
|
|
|
bridge_ips_max = None
|
|
|
|
|
|
@@ -61,7 +57,7 @@ def evaluate (harshness, fingerprint, abs=True):
|
|
|
bridge_ips_today = int(row[1])
|
|
|
|
|
|
if not bridge_ips_max is None:
|
|
|
- if is_blocked (harshness, bridge_ips_max, bridge_ips_today, abs):
|
|
|
+ if is_blocked_abs (t, m, bridge_ips_max, bridge_ips_today):
|
|
|
return row[0]
|
|
|
|
|
|
if bridge_ips_today > bridge_ips_max:
|
|
|
@@ -75,35 +71,64 @@ def evaluate (harshness, fingerprint, abs=True):
|
|
|
# If we got here, the bridge is not blocked
|
|
|
return None
|
|
|
|
|
|
+# Consider a bridge blocked if we observe a drop of at least d from the
|
|
|
+# previous day's count. d must be at least 8.
|
|
|
+def is_blocked_rel (d, bridge_ips_yesterday, bridge_ips_today):
|
|
|
+ if bridge_ips_yesterday is None:
|
|
|
+ return False
|
|
|
+
|
|
|
+ return bridge_ips_today <= bridge_ips_yesterday - d
|
|
|
+
|
|
|
+def evaluate_rel (d, fingerprint):
|
|
|
+ fingerprint = fingerprint.upper()
|
|
|
+ bridge_ips_yesterday = None
|
|
|
+ bridge_ips_today = None
|
|
|
+
|
|
|
+ with open (f"data/bridge_data_cleaned/{fingerprint}", 'r') as file:
|
|
|
+ bridge_data = csv.reader(file, delimiter=',')
|
|
|
+
|
|
|
+ for row in bridge_data:
|
|
|
+ bridge_ips_today = int(row[1])
|
|
|
+
|
|
|
+ if is_blocked_rel (d, bridge_ips_yesterday, bridge_ips_today):
|
|
|
+ return row[0]
|
|
|
+
|
|
|
+ bridge_ips_yesterday = bridge_ips_today
|
|
|
+
|
|
|
# Remove any previous blocked_* files, start over
|
|
|
-for i in range(5):
|
|
|
- if os.path.exists (f"data/blocked_{i}"):
|
|
|
- os.remove (f"data/blocked_{i}")
|
|
|
- if os.path.exists (f"data/blocked_{i}_abs"):
|
|
|
- os.remove (f"data/blocked_{i}_abs")
|
|
|
if os.path.exists (f"data/blocked_loesing"):
|
|
|
os.remove (f"data/blocked_loesing")
|
|
|
|
|
|
+for t in range (8, 40, 8):
|
|
|
+ for m in range (t+8, 112, 8):
|
|
|
+ if os.path.exists (f"data/blocked_abs_{t}_{m}"):
|
|
|
+ os.remove (f"data/blocked_abs_{t}_{m}")
|
|
|
+
|
|
|
+for d in range (8, 112, 8):
|
|
|
+ if os.path.exists (f"data/blocked_rel_{d}"):
|
|
|
+ os.remove (f"data/blocked_rel_{d}")
|
|
|
+
|
|
|
with open ("data/all-bridges", 'r') as all_bridges:
|
|
|
for fingerprint in all_bridges:
|
|
|
fingerprint = fingerprint.strip()
|
|
|
if fingerprint:
|
|
|
- # Go through all harshness values
|
|
|
- for harshness in range(5):
|
|
|
- blocked = evaluate (harshness, fingerprint)
|
|
|
- # If the bridge is blocked add its fingerprint and
|
|
|
- # blocked date to the list for that harshness level
|
|
|
- if not blocked is None:
|
|
|
- with open (f"data/blocked_{harshness}", 'a') as f:
|
|
|
- f.write(f"{fingerprint},{blocked}\n")
|
|
|
-
|
|
|
- blocked = evaluate (harshness, fingerprint)
|
|
|
- if not blocked is None:
|
|
|
- with open (f"data/blocked_{harshness}_abs", 'a') as f:
|
|
|
- f.write(f"{fingerprint},{blocked}\n")
|
|
|
-
|
|
|
# Evaluate with Loesing's algorithm
|
|
|
blocked = evaluate_loesing (fingerprint)
|
|
|
if not blocked is None:
|
|
|
with open (f"data/blocked_loesing", 'a') as f:
|
|
|
f.write(f"{fingerprint},{blocked}\n")
|
|
|
+
|
|
|
+ # Absolute threshold with t and m
|
|
|
+ for t in range (8, 40, 8):
|
|
|
+ for m in range (t+8, 112, 8):
|
|
|
+ blocked = evaluate_abs (t, m, fingerprint)
|
|
|
+ if not blocked is None:
|
|
|
+ with open (f"data/blocked_abs_{t}_{m}", 'a') as f:
|
|
|
+ f.write (f"{fingerprint},{blocked}\n")
|
|
|
+
|
|
|
+ # Relative threshold with d
|
|
|
+ for d in range (8, 112, 8):
|
|
|
+ blocked = evaluate_rel (d, fingerprint)
|
|
|
+ if not blocked is None:
|
|
|
+ with open (f"data/blocked_rel_{d}", 'a') as f:
|
|
|
+ f.write (f"{fingerprint},{blocked}\n")
|