-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker_backend.py
More file actions
57 lines (48 loc) · 1.6 KB
/
tracker_backend.py
File metadata and controls
57 lines (48 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from flask import Flask, jsonify
import psutil
import threading
import time
from datetime import datetime
app = Flask(__name__)
# Shared dictionary to store network stats
network_status = {
"timestamp": str(datetime.now()),
"incoming_mbps": 0.0,
"threshold_mbps": 50,
"alert": False
}
def monitor_ddos(threshold_mbps=50, interval=2):
"""Background thread to monitor incoming network traffic."""
old_bytes = psutil.net_io_counters().bytes_recv
while True:
time.sleep(interval)
new_bytes = psutil.net_io_counters().bytes_recv
diff = (new_bytes - old_bytes) * 8 / (interval * 1024 * 1024) # Mbps
old_bytes = new_bytes
alert = diff > threshold_mbps
network_status.update({
"timestamp": str(datetime.now()),
"incoming_mbps": round(diff, 2),
"threshold_mbps": threshold_mbps,
"alert": alert
})
if alert:
print(f"⚠️ Potential DDoS Detected! Incoming = {diff:.2f} Mbps")
else:
print(f"[{datetime.now()}] Normal traffic: {diff:.2f} Mbps")
@app.route("/")
def home():
return """
<h2>DDoS Tracker Dashboard</h2>
<p>Use <a href='/status'>/status</a> to view current traffic metrics (JSON).</p>
"""
@app.route("/status")
def status():
"""Returns current network traffic information as JSON."""
return jsonify(network_status)
if __name__ == "__main__":
# Start background thread
monitor_thread = threading.Thread(target=monitor_ddos, daemon=True)
monitor_thread.start()
# Run the Flask API
app.run(host="0.0.0.0", port=5000)