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:
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 asyncio, json, logging, time
|
||||||
import websockets
|
import websockets
|
||||||
|
import requests
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
@@ -24,6 +25,20 @@ warning = False
|
|||||||
info = False
|
info = False
|
||||||
debug = 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):
|
async def handler(ws: websockets.WebSocketServerProtocol):
|
||||||
peer_id = None
|
peer_id = None
|
||||||
try:
|
try:
|
||||||
@@ -41,6 +56,10 @@ async def handler(ws: websockets.WebSocketServerProtocol):
|
|||||||
if not peer_id:
|
if not peer_id:
|
||||||
await ws.close(code=4000, reason="Missing id")
|
await ws.close(code=4000, reason="Missing id")
|
||||||
return
|
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
|
CLIENTS[peer_id] = ws
|
||||||
CLIENT_LABELS[peer_id] = []
|
CLIENT_LABELS[peer_id] = []
|
||||||
CLIENT_LABELS_TS[peer_id] = time.monotonic()
|
CLIENT_LABELS_TS[peer_id] = time.monotonic()
|
||||||
|
|||||||
Reference in New Issue
Block a user