~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Network utility functions for IP address classification.
This module provides utilities for classifying IP addresses and hostnames
as private/local vs public. Used for URL normalization and security validation.
"""
import ipaddress
#: IPv6 transition ranges whose addresses stand for a host beyond a tunnel
#: relay or translator, never for a peer on this host or LAN. CPython's
#: ``is_private`` may nonetheless report addresses in 6to4 (``2002::/16``),
#: Teredo (``2001::/32``, inside the private ``2001::/23`` block) and the
#: RFC 8215 NAT64 local-use prefix (``64:ff9b:1::/48``) as private on some
#: Python versions -- including ones wrapping a public IPv4 such as
#: ``2002:808:808::1`` -- since which ranges ``ipaddress`` treats as
#: private has changed between releases. The well-known NAT64 prefix
#: (``64:ff9b::/96``) reads as public. LDR excludes all four ranges on
#: every version, so the rule does not depend on either behaviour.
#:
#: The single definition shared by every PEER-classification decision (who
#: is talking to this server). It deliberately does not feed
#: ``is_private_ip``, whose outbound callers (URL normalisation, egress
#: classification) answer a different question.
IPV6_TRANSITION_NETWORKS = tuple(
ipaddress.ip_network(net)
for net in ("2002::/16", "2001::/32", "64:ff9b::/96", "64:ff9b:1::/48")
)
def is_ipv6_transition_address(
ip: ipaddress.IPv4Address | ipaddress.IPv6Address,
) -> bool:
"""Whether ``ip`` lies in one of ``IPV6_TRANSITION_NETWORKS``."""
return ip.version == 6 and any(
ip in net for net in IPV6_TRANSITION_NETWORKS
)
def is_private_peer_ip(host: str) -> bool:
"""Whether a direct peer address is private/loopback/link-local.
``is_private_ip`` for an inbound peer (``request.client.host``), used
where the answer confers trust -- e.g. whether that peer may supply
``X-Forwarded-For``. It differs from ``is_private_ip`` only for IP
addresses:
* 6to4, Teredo and NAT64 addresses (``IPV6_TRANSITION_NETWORKS``) are
NOT private: such a peer is a remote host arriving through a relay
or translator, whatever ``ipaddress`` says.
* An IPv4-mapped IPv6 address (``::ffff:a.b.c.d``, how an IPv4 client
appears on a dual-stack listener) is judged by the IPv4 address it
wraps. CPython's ``ipaddress`` does the same from 3.12.4, but 3.12.0
to 3.12.3 answer from their IPv6 table, which lists
``::ffff:0:0/96`` as private, so the unwrap here keeps this trust
decision from depending on the interpreter.
Non-IP strings fall through to ``is_private_ip`` unchanged.
"""
candidate = host
if candidate.startswith("[") and candidate.endswith("]"):
candidate = candidate[1:-1]
try:
ip = ipaddress.ip_address(candidate)
except ValueError:
return is_private_ip(host)
if ip.version == 6 and ip.ipv4_mapped is not None:
ip = ip.ipv4_mapped
if is_ipv6_transition_address(ip):
return False
return ip.is_private or ip.is_loopback or ip.is_link_local
def is_private_ip(hostname: str) -> bool:
"""Check if hostname is a private/local IP address.
Recognizes:
- Localhost values (127.0.0.1, localhost, [::1], 0.0.0.0)
- RFC 1918 private IPv4 ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16)
- IPv6 private addresses (fc00::/7, fe80::/10)
- mDNS .local domains
Args:
hostname: The hostname or IP address to check
Returns:
True if the hostname is a private/local address, False otherwise
Examples:
>>> is_private_ip("192.168.1.100")
True
>>> is_private_ip("172.16.0.50")
True
>>> is_private_ip("10.0.0.1")
True
>>> is_private_ip("8.8.8.8")
False
>>> is_private_ip("api.openai.com")
False
"""
# Known localhost values
# Recognize a localhost spelling; no socket is bound here.
if hostname in ("localhost", "127.0.0.1", "[::1]", "0.0.0.0"): # nosec B104
return True
# Handle bracketed IPv6
if hostname.startswith("[") and hostname.endswith("]"):
hostname = hostname[1:-1]
try:
ip = ipaddress.ip_address(hostname)
# Check if private (includes 10.x, 172.16-31.x, 192.168.x, fc00::/7, etc.)
# Also check loopback and link-local
return ip.is_private or ip.is_loopback or ip.is_link_local
except ValueError:
# Not a valid IP address, check for .local domain (mDNS)
return hostname.endswith(".local")