added app
All checks were successful
Build and Push Multi-Platform Docker Image / build-and-push (push) Successful in 1m10s
All checks were successful
Build and Push Multi-Platform Docker Image / build-and-push (push) Successful in 1m10s
This commit is contained in:
parent
58d90f4229
commit
2b2067482d
19
server.py
19
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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user