Skip to main content

Resolve Google IP CIDR ranges from published DNS TXT records

Project description

google-dns-ips

Python library that resolves Google-published IP ranges from DNS TXT records.

It parses SPF-style TXT records (including nested include: records) and extracts all ip4: and ip6: CIDRs.

Why DNS TXT?

Google publishes network ranges in DNS TXT records such as:

  • _netblocks.google.com
  • _netblocks2.google.com
  • _netblocks3.google.com
  • _spf.google.com
  • _cloud-netblocks.googleusercontent.com

This package also resolves host records (A/AAAA), by default dns.google, and includes those addresses as /32 or /128 networks.

This library recursively walks those records and returns normalized ranges.

Install

From the project root:

pip install -e .

Library Usage

from google_dns_ips import GoogleIPQuery, get_google_ip_cidrs

result = get_google_ip_cidrs()
query = GoogleIPQuery.from_cidrs(result.cidrs)

print(query.contains("8.8.8.8"))
print(query.collides("8.8.8.0/24"))
print(query.contains("8.8.0.0-8.8.255.255"))

result is a ResolutionResult with:

  • cidrs: sorted tuple of unique CIDR strings
  • visited_records: sorted tuple of DNS names visited during recursion

GoogleIPQuery supports:

  • contains(value): full containment for IP, CIDR, or range string (start-end)
  • collides(value): any overlap for IP, CIDR, or range string (start-end)
  • Specific methods: contains_ip, contains_cidr, contains_range, collides_ip, collides_cidr, collides_range

Example Script

Run:

python examples/print_google_ips.py

Flask JSON Server Example

Run:

python examples/flask_server.py

All endpoints return JSON.

Examples:

curl "http://127.0.0.1:5001/health"
curl "http://127.0.0.1:5001/metadata"
curl "http://127.0.0.1:5001/cidrs"
curl "http://127.0.0.1:5001/networks"
curl "http://127.0.0.1:5001/check?value=8.8.8.8"
curl "http://127.0.0.1:5001/check?value=8.8.8.0/24"
curl "http://127.0.0.1:5001/check?value=8.8.0.0-8.8.255.255"
curl "http://127.0.0.1:5001/check/range?start=8.8.8.0&end=8.8.8.255"

Full Endpoint List (127.0.0.1)

Base URL:

http://127.0.0.1:5001
  1. GET /health
curl "http://127.0.0.1:5001/health"
  1. GET /metadata
curl "http://127.0.0.1:5001/metadata"
  1. GET /cidrs
curl "http://127.0.0.1:5001/cidrs"
  1. GET /networks
curl "http://127.0.0.1:5001/networks"
  1. GET /check with required query param value

IP examples:

curl "http://127.0.0.1:5001/check?value=8.8.8.8"
curl "http://127.0.0.1:5001/check?value=2001:4860:4860::8888"

CIDR examples:

curl "http://127.0.0.1:5001/check?value=8.8.8.0/24"
curl "http://127.0.0.1:5001/check?value=2001:4860:4860::/48"

Range examples (start-end):

curl "http://127.0.0.1:5001/check?value=8.8.0.0-8.8.255.255"
curl "http://127.0.0.1:5001/check?value=2001:4860:4860::8888-2001:4860:4860::8899"

Error examples:

curl "http://127.0.0.1:5001/check"
curl "http://127.0.0.1:5001/check?value=not-an-ip"
curl "http://127.0.0.1:5001/check?value=1.1.1.1-2001:4860:4860::8888"
  1. GET /check/range with required query params start and end

IPv4 range examples:

curl "http://127.0.0.1:5001/check/range?start=8.8.8.0&end=8.8.8.255"
curl "http://127.0.0.1:5001/check/range?start=8.8.8.255&end=8.8.8.0"

IPv6 range example:

curl "http://127.0.0.1:5001/check/range?start=2001:4860:4860::8888&end=2001:4860:4860::8899"

Error examples:

curl "http://127.0.0.1:5001/check/range"
curl "http://127.0.0.1:5001/check/range?start=8.8.8.8"
curl "http://127.0.0.1:5001/check/range?start=8.8.8.8&end=2001:4860:4860::8888"
curl "http://127.0.0.1:5001/check/range?start=bad&end=input"
  1. GET unknown route (JSON 404)
curl "http://127.0.0.1:5001/not-found"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

google_dns_ips-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

google_dns_ips-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file google_dns_ips-0.1.0.tar.gz.

File metadata

  • Download URL: google_dns_ips-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for google_dns_ips-0.1.0.tar.gz
Algorithm Hash digest
SHA256 06f8ab45196647564d093414fcaa0928f9d0b9c196c30ff5ac78e69b1e0af7bb
MD5 e3d01e2091c1dbb01b33317be665f8ef
BLAKE2b-256 f5a3dc0fdd8c56200e19fe0f803edcbb6ba7dfc42b27f534493bdea255bc558d

See more details on using hashes here.

File details

Details for the file google_dns_ips-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: google_dns_ips-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for google_dns_ips-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ce14c624680de594302e1ee25c8fc9ef14dbd02d6c764ccddbf4b766021e05b
MD5 501454c3c55a94f54191c5ec3814d59f
BLAKE2b-256 29617681fb9d9a9caeab1148e913dd7a66067ed51130b380cb7f558f88275a8b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page