From 2b2067482d30109c26644631215d61a5ca9e6430 Mon Sep 17 00:00:00 2001 From: Hirviturkki Date: Tue, 29 Apr 2025 14:23:15 +0200 Subject: [PATCH] added app --- server.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/server.py b/server.py index b7f5da1..00064dd 100644 --- a/server.py +++ b/server.py @@ -5,6 +5,7 @@ Signalling relay + periodic label aggregation broadcaster with stale‑timeout. import asyncio, json, logging, time import websockets +import requests logging.basicConfig(level=logging.INFO) @@ -24,6 +25,20 @@ warning = False info = False debug = False +def lookup_location(ip): + try: + response = requests.get(f"https://ipinfo.io/{ip}/json") + if response.status_code == 200: + data = response.json() + city = data.get("city") + region = data.get("region") + country = data.get("country") + return f"{city}, {region}, {country}" + else: + return "Unknown location" + except Exception as e: + return f"Lookup failed: {e}" + async def handler(ws: websockets.WebSocketServerProtocol): peer_id = None try: @@ -41,6 +56,10 @@ async def handler(ws: websockets.WebSocketServerProtocol): if not peer_id: await ws.close(code=4000, reason="Missing id") return + ip, port = ws.remote_address + logging.info(f"New connection from {ip}:{port}") + location = lookup_location(ip) + logging.info(f"Approximate location: {location}") CLIENTS[peer_id] = ws CLIENT_LABELS[peer_id] = [] CLIENT_LABELS_TS[peer_id] = time.monotonic()