Skip to content

Commit 766597b

Browse files
committed
Add exception to see any pcap save failures
1 parent 86bca52 commit 766597b

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/libinspector/server/packet_collector.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55
import sys
66
import datetime
7-
import common
7+
import re
88
from flask import Flask, request, jsonify
99
from pymongo import MongoClient
1010
from scapy.all import wrpcap, Ether, IP
@@ -51,6 +51,31 @@
5151
# If running with 'python app.py', this will stop the application.
5252

5353

54+
def is_prolific_id_valid(prolific_id: str) -> bool:
55+
"""
56+
Performs sanity checks on the Prolific ID:
57+
1. Not empty.
58+
2. Length between 1 and 50 characters (inclusive).
59+
3. Contains only alphanumeric characters (A-Z, a-z, 0-9).
60+
Args:
61+
prolific_id (str): The Prolific ID to validate.
62+
63+
Returns:
64+
bool: True if the ID is non-empty, 1-50 characters long, and alphanumeric; False otherwise.
65+
"""
66+
if not prolific_id or not isinstance(prolific_id, str):
67+
return False
68+
69+
# 2. Length check
70+
if not 1 <= len(prolific_id) <= 50:
71+
return False
72+
73+
# 3. Alphanumeric check using regex (ensures no special characters)
74+
if not re.fullmatch(r'^[a-zA-Z0-9]+$', prolific_id):
75+
return False
76+
77+
return True
78+
5479
@app.route('/label_packets', methods=['GET'])
5580
def check_status():
5681
"""
@@ -87,7 +112,7 @@ def label_packets():
87112
print(f"Packet decoding occurred for collection '{data['prolific_id']}': {e}")
88113
return jsonify({"error": "Packet decoding failed"}), 400
89114

90-
if not common.is_prolific_id_valid(data["prolific_id"]):
115+
if not is_prolific_id_valid(data["prolific_id"]):
91116
return jsonify({"error": "Prolific ID is invalid"}), 500
92117
folder_path: str = os.path.join(str(data["prolific_id"]), str(data["device_name"]), str(data["activity_label"]))
93118
fullpath = os.path.normpath(os.path.join(packet_root_dir, folder_path))
@@ -100,8 +125,8 @@ def label_packets():
100125
"mac_address": data["mac_address"],
101126
"device_name": data["device_name"],
102127
"activity_label": data["activity_label"],
103-
"start_time": data["start_time"],
104-
"end_time": data["end_time"],
128+
"start_time": int(data["start_time"]),
129+
"end_time": int(data["end_time"]),
105130
"raw_packets": raw_packets
106131
}
107132
try:
@@ -110,10 +135,19 @@ def label_packets():
110135
print(f"MongoDB Insert FAILED for collection '{data['prolific_id']}': {e}")
111136
return jsonify({"error": "Database insert failed"}), 500
112137

113-
os.makedirs(fullpath, exist_ok=True)
114-
pcap_file_name: str = make_pcap_filename(int(data["start_time"]), int(data["end_time"]))
115-
pcap_name: str = os.path.join(fullpath, pcap_file_name)
116-
save_packets_to_pcap(raw_packets, pcap_name)
138+
try:
139+
os.makedirs(fullpath, exist_ok=True)
140+
pcap_file_name: str = make_pcap_filename(int(data["start_time"]), int(data["end_time"]))
141+
pcap_name: str = os.path.join(fullpath, pcap_file_name)
142+
save_packets_to_pcap(raw_packets, pcap_name)
143+
except Exception as e:
144+
# If file saving fails, return a 500 but note that the DB save succeeded
145+
print(f"PCAP File Save FAILED for ID: {e}")
146+
return jsonify({
147+
"status": "partial_success",
148+
"inserted": 1,
149+
"warning": "PCAP file creation failed. Data successfully saved to MongoDB.",
150+
}), 200
117151
return jsonify({"status": "success", "inserted": 1}), 200
118152

119153

0 commit comments

Comments
 (0)