isPrivateHost static method

bool isPrivateHost(
  1. String host
)

Whether host is loopback, link-local, on a private network (RFC 1918, carrier-grade NAT such as Tailscale, IPv6 unique-local) or a local name, where plain HTTP is acceptable for a local model server.

IPv4 octets must be plain decimal: resolvers read a leading zero as octal, so 010.0.0.1 is really the public 8.0.0.1.

Implementation

static bool isPrivateHost(String host) {
  var name = host.toLowerCase();
  if (name.endsWith('.')) name = name.substring(0, name.length - 1);
  if (name == 'localhost' ||
      name.endsWith('.localhost') ||
      name.endsWith('.local')) {
    return true;
  }
  if (name.contains(':')) return _isPrivateIPv6(name);
  final octets = _ipv4Octets(name);
  return octets != null && _isPrivateIPv4(octets);
}