Automatically configure LAN networking without installer prompts
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
"""Find a physical RFC1918 LAN IPv4 address without contacting another server."""
|
||||
import ipaddress
|
||||
import json
|
||||
import re
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def private_lan(address):
|
||||
try:
|
||||
ip = ipaddress.IPv4Address(address)
|
||||
return any(ip in network for network in (
|
||||
ipaddress.IPv4Network("10.0.0.0/8"),
|
||||
ipaddress.IPv4Network("172.16.0.0/12"),
|
||||
ipaddress.IPv4Network("192.168.0.0/16"),
|
||||
))
|
||||
except ipaddress.AddressValueError:
|
||||
return False
|
||||
|
||||
|
||||
def physical(interface):
|
||||
return not interface.startswith(("lo", "docker", "veth", "br-", "virbr", "tun", "tap",
|
||||
"utun", "wg", "tailscale", "zt", "vpn", "bridge"))
|
||||
|
||||
|
||||
def detect_lan(runner=subprocess.run, platform=sys.platform, route_probe=None):
|
||||
def output(*command):
|
||||
try:
|
||||
result = runner(list(command), capture_output=True, text=True, timeout=5)
|
||||
return result.stdout if result.returncode == 0 else ""
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
return ""
|
||||
|
||||
candidates, preferred = [], []
|
||||
if platform.startswith("linux"):
|
||||
try:
|
||||
routes = json.loads(output("ip", "-j", "-4", "route", "show", "default") or "[]")
|
||||
preferred = [r["dev"] for r in sorted(routes, key=lambda r: r.get("metric", 0))
|
||||
if "dev" in r and physical(r["dev"])]
|
||||
interfaces = json.loads(output("ip", "-j", "-4", "address", "show", "up") or "[]")
|
||||
for interface in interfaces:
|
||||
name = interface.get("ifname", "")
|
||||
if not physical(name):
|
||||
continue
|
||||
for addr in interface.get("addr_info", []):
|
||||
address = addr.get("local", "")
|
||||
if addr.get("scope") == "global" and private_lan(address):
|
||||
candidates.append((name, address))
|
||||
except (ValueError, TypeError, KeyError):
|
||||
pass
|
||||
elif platform == "darwin":
|
||||
route = output("/sbin/route", "-n", "get", "default")
|
||||
match = re.search(r"interface:\s*(\S+)", route)
|
||||
if match and physical(match[1]):
|
||||
preferred.append(match[1])
|
||||
name, up = "", False
|
||||
for line in output("/sbin/ifconfig").splitlines():
|
||||
if line and not line[0].isspace():
|
||||
name = line.split(":", 1)[0]
|
||||
up = "UP" in line and "RUNNING" in line
|
||||
match = re.search(r"\binet\s+(\d+\.\d+\.\d+\.\d+)", line)
|
||||
if up and physical(name) and match and private_lan(match[1]):
|
||||
candidates.append((name, match[1]))
|
||||
if candidates:
|
||||
return min(candidates, key=lambda pair: (
|
||||
preferred.index(pair[0]) if pair[0] in preferred else len(preferred), pair[0], pair[1]))[1]
|
||||
|
||||
# UDP connect selects a local route but sends no packets (no DNS involved).
|
||||
def probe():
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
||||
sock.connect(("192.168.1.1", 9))
|
||||
return sock.getsockname()[0]
|
||||
|
||||
try:
|
||||
address = (route_probe or probe)()
|
||||
if private_lan(address):
|
||||
return address
|
||||
except OSError:
|
||||
pass
|
||||
raise ValueError("No LAN IPv4 address detected. Connect this computer to your LAN and rerun ./install.sh, or override with --ntfy-url and --bind-address.")
|
||||
Reference in New Issue
Block a user