Show the server's public IP with click to copy, and VPS network graphs

Cards show the VPS's public IP (PUBLIC_IP, else the default route's source address) instead of the
page's hostname; clicking the address copies it. VPS resources gets stacked download/upload graphs
of the last 5 minutes, kept server side.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
neil 2026-10-26 10:32:31 -07:00
parent cb9a6d1970
commit 3ae03160e8
No known key found for this signature in database
4 changed files with 93 additions and 5 deletions

View file

@ -2,6 +2,7 @@
import asyncio
import hmac
import ipaddress
import json
import io
import os
@ -12,6 +13,7 @@ import socket
import threading
import time
import zipfile
from collections import deque
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
@ -129,6 +131,24 @@ def _read_meminfo():
return info
def _public_ip():
"""The address players connect to: PUBLIC_IP if set, else the source address of the default route
(gsm runs on the host network). None behind NAT; the UI then falls back to the page's hostname."""
if os.environ.get("PUBLIC_IP"):
return os.environ["PUBLIC_IP"]
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("1.1.1.1", 80)) # UDP: picks a route, sends nothing
ip = s.getsockname()[0]
return ip if ipaddress.ip_address(ip).is_global else None
except OSError:
return None
PUBLIC_IP = _public_ip()
NET_HISTORY = 100 # samples (one per 3 s) kept for the VPS network graphs
def _read_net():
rx = tx = 0
for line in Path("/proc/net/dev").read_text().splitlines()[2:]:
@ -323,6 +343,7 @@ def stats_loop():
prev_cpu = _read_cpu()
prev_net = _read_net()
prev_t = time.time()
net_history = deque(maxlen=NET_HISTORY)
pool = ThreadPoolExecutor(max_workers=8)
while True:
time.sleep(3)
@ -336,6 +357,7 @@ def stats_loop():
rx, tx = _read_net()
now = time.time()
dt = now - prev_t
net_history.append([round((rx - prev_net[0]) / dt), round((tx - prev_net[1]) / dt)])
host = {
"cpu_percent": round(cpu_pct, 1),
"cpu_count": os.cpu_count(),
@ -347,6 +369,8 @@ def stats_loop():
"disk_used": du.used,
"net_rx_rate": (rx - prev_net[0]) / dt,
"net_tx_rate": (tx - prev_net[1]) / dt,
"net_history": list(net_history), # [[rx, tx] bytes/s], oldest first
"public_ip": PUBLIC_IP,
}
prev_net, prev_t = (rx, tx), now