-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
842 lines (682 loc) · 28.3 KB
/
Copy pathapp.py
File metadata and controls
842 lines (682 loc) · 28.3 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, Form, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, EmailStr
from scapy.all import sniff, IP, get_if_list
from datetime import datetime, timedelta
import sqlite3
import asyncio
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from typing import List
import json
from html import escape
import bcrypt
import secrets
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
active_connections: List[WebSocket] = []
"""
conn = sqlite3.connect('network_monitoring.db')
cursor = conn.cursor()
try:
cursor.execute("PRAGMA foreign_keys = OFF;")
cursor.execute("DELETE FROM AttackLogs;")
cursor.execute("DELETE FROM TrafficLogs;")
cursor.execute("DELETE FROM Alerts;")
cursor.execute("DELETE FROM Reports;")
cursor.execute("DELETE FROM sqlite_sequence WHERE name='AttackLogs';")
cursor.execute("DELETE FROM sqlite_sequence WHERE name='TrafficLogs';")
cursor.execute("DELETE FROM sqlite_sequence WHERE name='Alerts';")
cursor.execute("DELETE FROM sqlite_sequence WHERE name='Reports';")
cursor.execute("PRAGMA foreign_keys = ON;")
conn.commit()
print("Đã xóa dữ liệu và reset ID thành công.")
except Exception as e:
print("Lỗi khi xóa dữ liệu:", e)
conn.rollback()
finally:
conn.close()
"""
conn = sqlite3.connect("network_monitoring.db", check_same_thread=False)
cursor = conn.cursor()
reset_tokens = {}
reset_tokens_expiry = {}
def hash_password(password: str) -> str:
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
def check_password(password: str, hashed: str) -> bool:
return bcrypt.checkpw(password.encode(), hashed.encode())
@app.get("/update-password-to-hashed")
def update_password_to_hashed():
cursor.execute("SELECT password FROM Admin WHERE username = ?", ("admin",))
row = cursor.fetchone()
if row is None:
return {"message": "Không tìm thấy user admin"}
current_password = row[2]
if current_password.startswith("$2b$"):
return {"message": "Mật khẩu đã được mã hóa rồi"}
hashed = hash_password(current_password)
cursor.execute("UPDATE Admin SET password = ? WHERE username = ?", (hashed, "admin"))
conn.commit()
return {"message": "Cập nhật mật khẩu đã mã hóa thành công!"}
def send_reset_password(email: str, reset_link: str):
subject = "Yêu cầu đặt lại mật khẩu"
body = f"""\
Xin chào,
Bạn đã yêu cầu đặt lại mật khẩu. Hãy nhấp vào liên kết dưới đây để đặt lại mật khẩu (có hiệu lực trong 15 phút):
{reset_link}
Nếu bạn không yêu cầu, hãy bỏ qua email này.
"""
sender_email = "quangloanthanhchien4@gmail.com"
sender_password = "nvtwvjpwnenkzrhj"
try:
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = sender_email
msg["To"] = email
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, [email], msg.as_string())
return {"message": "Mật khẩu đã được gửi đến email của bạn"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi khi gửi email: {str(e)}")
class ForgotPasswordRequest(BaseModel):
admin_email: str
@app.post("/forgot-password")
async def forgot_password(request_data: ForgotPasswordRequest):
email = request_data.admin_email.strip()
cursor.execute("SELECT * FROM Admin WHERE email = ?", (email,))
if not cursor.fetchone():
return JSONResponse(content={"message": "Email không tồn tại!"}, status_code=404)
token = secrets.token_urlsafe(16)
reset_tokens[token] = email
reset_tokens_expiry[token] = datetime.now() + timedelta(minutes=15)
reset_link = f"http://localhost:8000/reset-password?token={token}"
send_reset_password(email, reset_link)
return {"message": "Đã gửi link đặt lại mật khẩu đến email."}
@app.get("/reset-password", response_class=HTMLResponse)
async def reset_password_form(token: str):
if token not in reset_tokens or datetime.now() > reset_tokens_expiry[token]:
return HTMLResponse("Token không hợp lệ hoặc đã hết hạn!", status_code=400)
escaped_token = escape(token)
return f"""
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Đặt lại mật khẩu</title>
<style>
/* CSS giữ nguyên như bạn đã viết */
* {{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}}
body {{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, #833ab4, #4a90e2);
}}
.login-container {{
width: 350px;
padding: 20px;
background: white;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
text-align: center;
}}
.login-container h2 {{
margin-bottom: 20px;
font-size: 24px;
color: #333;
font-weight: bold;
}}
.form-group {{
position: relative;
margin-bottom: 20px;
}}
.form-group input {{
width: 100%;
padding: 10px;
border: none;
border-bottom: 2px solid #ccc;
font-size: 16px;
outline: none;
background: transparent;
}}
.form-group input:focus {{
border-bottom: 2px solid #4a90e2;
}}
.submit {{
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background: linear-gradient(to right, #833ab4, #4a90e2);
color: white;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
}}
.submit:hover {{
background: linear-gradient(to right, #4a90e2, #833ab4);
}}
.error {{
color: red;
font-size: 14px;
margin-top: 5px;
}}
</style>
</head>
<body>
<div class="login-container">
<h2>Đặt lại mật khẩu</h2>
<form id="resetForm" action="/reset-password?token={escaped_token}" method="post">
<div class="form-group">
<input type="password" name="new_password" id="new_password" placeholder="Mật khẩu mới" required/>
</div>
<div class="form-group">
<input type="password" name="confirm_password" id="confirm_password" placeholder="Nhập lại mật khẩu" required/>
</div>
<button class="submit" type="submit">Cập nhật</button>
<p id="error-message" class="error"></p>
</form>
</div>
<script>
document.getElementById('resetForm').onsubmit = function(event) {{
var newPassword = document.getElementById('new_password').value;
var confirmPassword = document.getElementById('confirm_password').value;
var errorMessage = document.getElementById('error-message');
if (newPassword.trim() === "" || confirmPassword.trim() === "") {{
errorMessage.textContent = "Mật khẩu không được để trống.";
event.preventDefault();
return false;
}}
if (newPassword.includes(" ") || confirmPassword.includes(" ")) {{
errorMessage.textContent = "Mật khẩu không được chứa khoảng trắng.";
event.preventDefault();
return false;
}}
if (newPassword !== confirmPassword) {{
errorMessage.textContent = "Mật khẩu và mật khẩu xác nhận không khớp.";
event.preventDefault();
return false;
}}
return true;
}};
</script>
</body>
</html>
"""
@app.post("/reset-password")
async def reset_password(request: Request, new_password: str = Form(...)):
token = request.query_params.get("token")
if token not in reset_tokens or datetime.now() > reset_tokens_expiry[token]:
return JSONResponse({"message": "Token không hợp lệ hoặc đã hết hạn!"}, status_code=400)
email = reset_tokens[token]
hashed_new_pw = hash_password(new_password)
cursor.execute("UPDATE Admin SET password = ? WHERE email = ?", (hashed_new_pw, email))
conn.commit()
del reset_tokens[token]
del reset_tokens_expiry[token]
return {"message": "Đặt lại mật khẩu thành công!"}
from fastapi import Request
from fastapi import FastAPI
from email.mime.text import MIMEText
import smtplib
def send_alert_email(alert_data, admin_name, admin_email):
subject = "[CẢNH BÁO] Tấn công DDOS được phát hiện"
body = f"""
Xin chào {admin_name},
Bạn có một cảnh báo mới từ hệ thống giám sát mạng:
- Nội dung: {alert_data.get('alert_message')}
- Thời gian: {alert_data.get('timestamp')}
- Loại tấn công: {alert_data.get('attack_type')}
- Tổng số gói tin: {alert_data.get('packet_count')}
- Băng thông: {alert_data.get('bandwidth_usage')}
- Mức độ: {alert_data.get('level')}
- IP nguồn: {alert_data.get('source_ip')}
Vui lòng kiểm tra hệ thống để xử lý kịp thời.
Trân trọng,
Hệ thống Giám sát Mạng
"""
sender_email = "quangloanthanhchien4@gmail.com"
sender_password = "nvtwvjpwnenkzrhj"
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = sender_email
msg["To"] = admin_email
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, [admin_email], msg.as_string())
print("✅ Gửi email cảnh báo thành công")
except Exception as e:
print("❌ Lỗi gửi email:", e)
def get_admin_info():
conn = sqlite3.connect("network_monitoring.db")
cursor = conn.cursor()
cursor.execute("SELECT username, email FROM Admin LIMIT 1")
result = cursor.fetchone()
conn.close()
if result:
return {"username": result[0], "email": result[1]}
return None
class ThresholdRequest(BaseModel):
value: float
@app.post("/change-threshold")
async def change_threshold(data: ThresholdRequest):
global threshold
try:
threshold = data.value
return {"status": "success", "message": f"Threshold đã được cập nhật thành {threshold}"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi khi cập nhật threshold: {str(e)}")
class ChangePasswordRequest(BaseModel):
admin_id: int
old_password: str
new_password: str
@app.post("/change-password")
async def change_password(data: ChangePasswordRequest):
try:
if not data.new_password.strip():
raise HTTPException(status_code=400, detail="Mật khẩu mới không được để trống")
if ' ' in data.new_password:
raise HTTPException(status_code=400, detail="Mật khẩu mới không được chứa khoảng trắng")
with sqlite3.connect("network_monitoring.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT password FROM Admin WHERE id = ?", (data.admin_id,))
result = cursor.fetchone()
if not result:
raise HTTPException(status_code=404, detail="Không tìm thấy admin")
stored_hashed_password = result[0]
if not check_password(data.old_password, stored_hashed_password):
raise HTTPException(status_code=400, detail="Mật khẩu cũ không đúng")
hashed_new_password = hash_password(data.new_password)
cursor.execute("UPDATE Admin SET password = ? WHERE id = ?", (hashed_new_password, data.admin_id))
conn.commit()
return {"status": "success", "message": "Mật khẩu đã được thay đổi thành công"}
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi khi thay đổi mật khẩu: {str(e)}")
class ChangeNameRequest(BaseModel):
admin_id: int
new_name: str
@app.post("/change-name")
async def change_name(data: ChangeNameRequest):
try:
with sqlite3.connect("network_monitoring.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM Admin WHERE id = ?", (data.admin_id,))
user = cursor.fetchone()
if user is None:
raise HTTPException(status_code=404, detail="Admin không tồn tại")
cursor.execute("UPDATE Admin SET username = ? WHERE id = ?", (data.new_name, data.admin_id))
conn.commit()
return {"status": "success", "message": "Tên đã được thay đổi thành công"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi khi đổi tên: {str(e)}")
class ChangeEmailRequest(BaseModel):
admin_id: int
new_email: EmailStr
@app.post("/change-email")
async def change_email(data: ChangeEmailRequest):
try:
with sqlite3.connect("network_monitoring.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM Admin WHERE id = ?", (data.admin_id,))
user = cursor.fetchone()
if user is None:
raise HTTPException(status_code=404, detail="Admin không tồn tại")
cursor.execute("UPDATE Admin SET email = ? WHERE id = ?", (data.new_email, data.admin_id))
conn.commit()
return {"status": "success", "message": "Email đã được cập nhật thành công"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Lỗi khi thay đổi email: {str(e)}")
@app.websocket("/ws/admin")
async def websocket_admins(websocket: WebSocket):
await websocket.accept()
try:
while True:
with sqlite3.connect("network_monitoring.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM Admin ORDER BY username DESC")
rows = cursor.fetchall()
admins = []
for row in rows:
admin = {
"id": row[0],
"username": row[1],
"password": row[2],
"email": row[3]
}
admins.append(admin)
await websocket.send_json(admins)
await asyncio.sleep(1)
except WebSocketDisconnect:
print(" Client đã ngắt kết nối WebSocket.")
except Exception as e:
print(f" Lỗi khi xử lý WebSocket: {e}")
class LoginRequest(BaseModel):
username: str
password: str
@app.post("/login")
def login(data: LoginRequest):
conn = sqlite3.connect("network_monitoring.db")
cursor = conn.cursor()
cursor.execute("SELECT password FROM Admin WHERE username = ?", (data.username,))
result = cursor.fetchone()
conn.close()
if result:
hashed_password = result[0]
if check_password(data.password, hashed_password):
return {"status": "success", "message": "Đăng nhập thành công"}
else:
raise HTTPException(status_code=401, detail="Sai mật khẩu")
else:
raise HTTPException(status_code=404, detail="Không tìm thấy tài khoản")
@app.websocket("/ws/alerts")
async def websocket_alerts(websocket: WebSocket):
await websocket.accept()
last_count = -1
try:
while True:
with sqlite3.connect("network_monitoring.db") as conn:
cursor = conn.cursor()
# Kiểm tra số lượng cảnh báo hiện tại
cursor.execute("SELECT COUNT(*) FROM Alerts")
current_count = cursor.fetchone()[0]
if current_count != last_count:
cursor.execute("""
SELECT
a.id, a.alert_message,
al.attack_type, al.level,
t.source_ip, t.bandwidth_usage, t.packet_count, t.timestamp,
ad.username, ad.email
FROM Alerts a
JOIN AttackLogs al ON a.attack_log_id = al.id
JOIN TrafficLogs t ON al.traffic_log_id = t.id
LEFT JOIN Admin ad ON a.admin_id = ad.id
ORDER BY t.timestamp DESC
""")
rows = cursor.fetchall()
alerts = []
for row in rows:
alert = {
"id": row[0],
"alert_message": row[1],
"attack_type": row[2],
"level": row[3],
"source_ip": row[4],
"bandwidth_usage": row[5],
"packet_count": row[6],
"timestamp": row[7],
"admin_name": row[8],
"admin_email": row[9],
}
alerts.append(alert)
await websocket.send_json(alerts)
last_count = current_count
await asyncio.sleep(1)
except WebSocketDisconnect:
print("❌ WebSocket client đã ngắt kết nối.")
except Exception as e:
print(f"❌ Lỗi WebSocket Alerts: {e}")
@app.websocket("/ws/reports")
async def websocket_reports(websocket: WebSocket):
await websocket.accept()
last_count = -1
try:
while True:
with sqlite3.connect("network_monitoring.db") as conn:
cursor = conn.cursor()
# Đếm số lượng bản ghi Reports thay vì AttackLogs
cursor.execute("SELECT COUNT(*) FROM Reports")
current_count = cursor.fetchone()[0]
if current_count != last_count:
cursor.execute("""
SELECT
r.id, t.timestamp, t.source_ip, t.packet_count,
t.bandwidth_usage, al.attack_type, al.level,
r.admin_id, r.attack_log_id
FROM Reports r
JOIN AttackLogs al ON r.attack_log_id = al.id
JOIN TrafficLogs t ON al.traffic_log_id = t.id
ORDER BY t.timestamp DESC
""")
rows = cursor.fetchall()
reports = []
for row in rows:
report = {
"id": row[0],
"timestamp": row[1],
"source_ip": row[2],
"packet_count": row[3],
"bandwidth_usage": row[4],
"attack_type": row[5],
"level": row[6],
"admin_id": row[7],
"attack_log_id": row[8]
}
reports.append(report)
await websocket.send_json(reports)
last_count = current_count
await asyncio.sleep(1)
except WebSocketDisconnect:
print("❌ Client đã ngắt kết nối WebSocket.")
except Exception as e:
print(f"❌ Lỗi khi xử lý WebSocket Reports: {e}")
import aiosqlite
@app.delete("/api/alerts/{alert_id}")
async def delete_alert(alert_id: int):
async with aiosqlite.connect("network_monitoring.db") as db:
cursor = await db.execute("SELECT * FROM Alerts WHERE id = ?", (alert_id,))
row = await cursor.fetchone()
if not row:
raise HTTPException(status_code=404, detail="Alert not found")
await db.execute("DELETE FROM Alerts WHERE id = ?", (alert_id,))
await db.commit()
print("DELETE_SUCCESS")
return {"message": "Alert deleted successfully"}
threshold = 70
active_connections = []
@app.websocket("/ws/traffic")
async def websocket_traffic(websocket: WebSocket):
await websocket.accept()
active_connections.append(websocket)
print(f"✅ Client connected: {websocket.client}")
try:
while True:
try:
message = await websocket.receive_text()
if message.strip().lower() == "ping":
await websocket.send_text("pong")
continue
try:
data = json.loads(message)
except json.JSONDecodeError:
print(f"❌ Dữ liệu không hợp lệ từ client: {message}")
continue
source_ip = data.get("source_ip")
if isinstance(source_ip, list):
source_ip = " ".join(source_ip)
bandwidth = data.get("bandwidth_usage", 0)
if bandwidth > threshold:
with sqlite3.connect("network_monitoring.db") as conn:
cursor = conn.cursor()
# 1. Thêm vào TrafficLogs
cursor.execute("""
INSERT INTO TrafficLogs (timestamp, source_ip, destination_ip, packet_count, bandwidth_usage)
VALUES (?, ?, ?, ?, ?)
""", (
data.get("timestamp"),
source_ip,
data.get("destination_ip"),
data.get("packet_count"),
bandwidth
))
traffic_log_id = cursor.lastrowid
# 2. Xác định mức độ tấn công
if bandwidth > threshold + 60:
level = "High"
elif bandwidth > threshold + 30:
level = "Medium"
else:
level = "Low"
# 3. Thêm vào AttackLogs
cursor.execute("""
INSERT INTO AttackLogs (
attack_type, level, traffic_log_id
)
VALUES (?, ?, ?)
""", (
data.get("attack_type"),
level,
traffic_log_id
))
attack_log_id = cursor.lastrowid
# 4. Thêm vào Reports (bỏ timestamp)
cursor.execute("""
INSERT INTO Reports (
attack_log_id, admin_id
)
VALUES (?, ?)
""", (
attack_log_id,
1
))
# 5. Thêm vào Alerts (bỏ timestamp)
cursor.execute("""
INSERT INTO Alerts (
attack_log_id, admin_id, alert_message
)
VALUES (?, ?, ?)
""", (
attack_log_id,
1,
"Abnormal traffic increase"
))
# 6. Gửi email nếu cần
admin = get_admin_info()
if admin:
alert_data = {
"alert_message": "Abnormal traffic increase",
"timestamp": data.get("timestamp"),
"attack_type": data.get("attack_type"),
"packet_count": data.get("packet_count"),
"bandwidth_usage": bandwidth,
"level": level,
"source_ip": source_ip,
}
send_alert_email(alert_data, admin["username"], admin["email"])
conn.commit()
# Gửi dữ liệu về tất cả client đang kết nối
disconnected_clients = []
for client in active_connections:
try:
await client.send_json(data)
except:
disconnected_clients.append(client)
for dc in disconnected_clients:
if dc in active_connections:
active_connections.remove(dc)
except WebSocketDisconnect:
break
finally:
if websocket in active_connections:
active_connections.remove(websocket)
print(f"❌ Client disconnected: {websocket.client}")
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/", include_in_schema=False)
async def root():
return FileResponse("static/login.html", media_type="text/html")
html_routes = {
"/main.html": "main.html",
"/traffic.html": "traffic.html",
"/login.html": "login.html",
"/alerts.html": "alerts.html",
"/reports.html": "reports.html",
"/settings.html": "Settings.html",
"/attacklog.html": "attackLog.html",
"/test.html": "test.html",
"/pass.html": "pass.html"
}
def create_html_route(path, filename):
@app.get(path)
async def serve():
return FileResponse(f"static/{filename}", media_type="text/html")
for path, filename in html_routes.items():
create_html_route(path, filename)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class TrafficLog(BaseModel):
timestamp: str
source_ip: str
destination_ip: str
packet_count: int
bandwidth_usage: float
sniffing_status = {"status": "stopped"}
def create_tables():
conn = sqlite3.connect("network_monitoring.db")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS Admin (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username VARCHAR(100),
password VARCHAR(255),
email VARCHAR(255)
)""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS TrafficLogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
source_ip TEXT,
destination_ip TEXT,
packet_count INTEGER,
bandwidth_usage REAL
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS AttackLogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
attack_type TEXT,
level TEXT,
traffic_log_id INTEGER
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS Alerts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
attack_log_id INTEGER,
admin_id INTEGER,
alert_message VARCHAR(255),
FOREIGN KEY (attack_log_id) REFERENCES AttackLogs(id)
)""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS Reports (
id INTEGER PRIMARY KEY AUTOINCREMENT,
attack_log_id INTEGER,
admin_id INTEGER,
FOREIGN KEY (attack_log_id) REFERENCES AttackLogs(id)
)""")
conn.commit()
conn.close()
create_tables()