2024-05-31 23:13:38 +03:00
|
|
|
import json
|
|
|
|
import uuid
|
|
|
|
import hashlib
|
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
from datetime import datetime
|
|
|
|
|
2024-05-31 23:13:38 +03:00
|
|
|
from .db import Database
|
|
|
|
|
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
def parse_traceroute_output(data: str, origin: str):
|
|
|
|
lines = data.strip().split("\n")
|
|
|
|
target = lines[0].split()[2]
|
|
|
|
|
|
|
|
created = datetime.now().isoformat()
|
2024-05-31 23:13:38 +03:00
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
trace = {"target": target, "created": created, "origin": origin, "hops": []}
|
|
|
|
|
|
|
|
prev_latency = 0
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
for line in lines[1:]:
|
|
|
|
hop_info = line.split()
|
2024-06-01 16:14:29 +03:00
|
|
|
print("LINE:", hop_info)
|
|
|
|
try:
|
|
|
|
number, name, ip, latency, _ = hop_info
|
|
|
|
hop = {
|
|
|
|
"created": created,
|
|
|
|
"number": number,
|
|
|
|
"name": name,
|
|
|
|
"ip": ip.strip("()"),
|
|
|
|
"latency": float(latency),
|
|
|
|
}
|
|
|
|
except ValueError:
|
|
|
|
number, name = hop_info
|
|
|
|
hop = {
|
|
|
|
"created": created,
|
|
|
|
"number": number,
|
|
|
|
"name": name,
|
|
|
|
"ip": None,
|
|
|
|
"latency": None,
|
|
|
|
}
|
|
|
|
|
|
|
|
trace["hops"].append(hop)
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
return trace
|
|
|
|
|
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
def store_traceroute(trace):
|
2024-05-31 23:13:38 +03:00
|
|
|
db = Database()
|
|
|
|
|
2024-06-01 16:14:29 +03:00
|
|
|
# hops_json = json.dumps(trace['hops'])
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
path_ids = {}
|
|
|
|
|
|
|
|
previous_hop_ip = None
|
|
|
|
previous_hop_latency = None
|
2024-06-01 16:14:29 +03:00
|
|
|
for hop in trace["hops"]:
|
|
|
|
hop_number = hop["number"]
|
|
|
|
hop_name = hop.get("name")
|
|
|
|
hop_ip = hop.get("ip")
|
|
|
|
hop_latency = hop.get("latency")
|
2024-05-31 23:13:38 +03:00
|
|
|
link_id = None
|
|
|
|
|
|
|
|
# insert links and get their id's
|
|
|
|
if previous_hop_ip:
|
|
|
|
link_id = db.create_link(previous_hop_ip, hop_ip)
|
|
|
|
path_ids[hop_number] = link_id
|
|
|
|
|
|
|
|
previous_hop_ip = hop_ip
|
|
|
|
|
|
|
|
# Save hop details
|
|
|
|
db.create_hop(hop_name, hop_ip, hop_latency)
|
|
|
|
|
|
|
|
# calculate link latency if possible and store it
|
|
|
|
if link_id and previous_hop_latency:
|
|
|
|
link_latency = hop_latency - previous_hop_latency
|
2024-06-01 16:14:29 +03:00
|
|
|
db.create_latency(link_id, trace["created"], link_latency)
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
# make entry to "Paths" table
|
|
|
|
if path_ids:
|
|
|
|
json_path_ids = json.dumps(path_ids)
|
2024-06-01 16:14:29 +03:00
|
|
|
db.create_path(node, trace["target"], json_path_ids)
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
db.end()
|
|
|
|
|
|
|
|
|
|
|
|
def generate_node_id():
|
|
|
|
mac = uuid.getnode()
|
2024-06-01 16:14:29 +03:00
|
|
|
mac_str = ":".join(
|
|
|
|
["{:02x}".format((mac >> ele) & 0xFF) for ele in range(0, 8 * 6, 8)][::-1]
|
|
|
|
)
|
2024-05-31 23:13:38 +03:00
|
|
|
|
|
|
|
# Hash the MAC address using SHA-256 to generate a unique ID
|
|
|
|
unique_id = hashlib.sha256(mac_str.encode()).hexdigest()
|
|
|
|
|
|
|
|
return unique_id
|