The machine's own interface IPs. Used so the host always reaches the app even via its LAN address (e.g. 192.168.x.x), not just 127.0.0.1 — turning network access off must never cut the host off from its own server.
()
| 310 | |
| 311 | @functools.lru_cache(maxsize=1) |
| 312 | def _local_ips() -> frozenset[str]: |
| 313 | """The machine's own interface IPs. Used so the host always reaches the app |
| 314 | even via its LAN address (e.g. 192.168.x.x), not just 127.0.0.1 — turning |
| 315 | network access off must never cut the host off from its own server.""" |
| 316 | ips: set[str] = set() |
| 317 | try: |
| 318 | hostname = socket.gethostname() |
| 319 | for info in socket.getaddrinfo(hostname, None): |
| 320 | ips.add(info[4][0]) |
| 321 | except Exception: |
| 322 | # Best-effort: name resolution can fail on odd hostnames/configs; we |
| 323 | # still try the outbound-socket probe below and fall back to loopback. |
| 324 | _log.debug("hostname IP enumeration failed", exc_info=True) |
| 325 | try: # primary outbound IP, robust when the hostname doesn't resolve them all |
| 326 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 327 | s.connect(("8.8.8.8", 80)) |
| 328 | ips.add(s.getsockname()[0]) |
| 329 | s.close() |
| 330 | except Exception: |
| 331 | # Best-effort: no default route / offline — just return what we have. |
| 332 | _log.debug("outbound IP probe failed", exc_info=True) |
| 333 | return frozenset(ips) |
| 334 | |
| 335 | |
| 336 | def _is_host_request(host: str | None) -> bool: |
no test coverage detected