diff --git a/kglobe.py b/kglobe.py index 09639ad..8a0f5b1 100644 --- a/kglobe.py +++ b/kglobe.py @@ -10,6 +10,8 @@ import re import requests import argparse import asyncio +import os +import IP2Location from terminalplotter import TerminalPlotter @@ -21,14 +23,15 @@ from terminalplotter import TerminalPlotter # DONE: Async rendering? EXAMPLE_ROUTE = [ - (47.996, 7.849), # freiburg - (50.110, 8.682), # ffm - (52.231, 21.006), # warsaw - (12.988, 77.622), # bangalore - (22.350, 114.184), # hong kong - (-33.869, 151.208) # sydney + (47.996, 7.849), # freiburg + (50.110, 8.682), # ffm + (52.231, 21.006), # warsaw + (12.988, 77.622), # bangalore + (22.350, 114.184), # hong kong + (-33.869, 151.208), # sydney ] + # Convert lat/lon to Cartesian coordinates def latlon_to_xyz(lat: float, lon: float, radius=1.0): lat_rad = np.radians(lat) @@ -38,6 +41,7 @@ def latlon_to_xyz(lat: float, lon: float, radius=1.0): z = radius * np.sin(lat_rad) return np.array([x, y, z]) + # Create an arch between two 3D points def generate_arch(p1, p2, height_factor=0.2, n_points=100): # Normalize input points to lie on the unit sphere @@ -66,6 +70,8 @@ def generate_arch(p1, p2, height_factor=0.2, n_points=100): def traceroute(target: str) -> list[tuple[int, int]]: + database = IP2Location.IP2Location("IP2LOCATION-LITE-DB5.BIN", "SHARED_MEMORY") + # Run traceroute command result = subprocess.run( ["traceroute", "-n", target, "-q", "1", "-w", "1,3,10"], @@ -76,12 +82,34 @@ def traceroute(target: str) -> list[tuple[int, int]]: coords: list[tuple[int, int]] = [] for ip in hops: - try: - response: dict = requests.get(f"http://ip-api.com/json/{ip}").json() - if response["status"] == "success": - coords.append((response["lat"], response["lon"])) - except Exception: + if ip.startswith( + ( + "10.", + "127.", + "169.254.", + "172.16.", + "172.17.", + "172.18.", + "172.19.", + "172.20.", + "172.21.", + "172.22.", + "172.23.", + "172.24.", + "172.25.", + "172.26.", + "172.27.", + "172.28.", + "172.29.", + "172.30.", + "172.30.", + "192.168.", + ) + ): + # exclude common local network addreses continue + info = database.get_all(ip) + coords.append((float(info.latitude), float(info.longitude))) return coords @@ -129,14 +157,16 @@ async def main(): else: height, width = await kitty.get_terminal_size_pixel() plotter = TerminalPlotter(width, height) - plotter.add_mesh(globe, color="tan", smooth_shading=False, texture=tex, show_edges=False) + plotter.add_mesh( + globe, color="tan", smooth_shading=False, texture=tex, show_edges=False + ) for point in points_3d: city_marker = pv.Sphere(center=point, radius=0.02) plotter.add_mesh(city_marker, color="blue") labels = [str(_) for _ in range(len(points_3d))] - raised_points = [ point * 1.1 for point in points_3d] + raised_points = [point * 1.1 for point in points_3d] if raised_points: plotter.add_point_labels(raised_points, labels, point_size=0, font_size=14) @@ -145,10 +175,8 @@ async def main(): line = pv.lines_from_points(arch, close=False) plotter.add_mesh(line, color="red", line_width=2) - kitty.hide_cursor() try: - #asyncio.run(plotter.run()) await plotter.run() finally: kitty.show_cursor()