### Build MMDB from Source using Go Source: https://context7.com/nextdns/icloud-private-relay-mmdb/llms.txt This bash script demonstrates how to build the `egress-ip-ranges.mmdb` file from source. It involves cloning the repository, downloading Go dependencies, and running the `main.go` program. It also explains how to force regeneration by removing the ETag file. ```bash # Clone the repository git clone https://github.com/nextdns/icloud-private-relay-mmdb.git cd icloud-private-relay-mmdb # Install Go dependencies go mod download # Run the generator to fetch and convert the latest IP ranges go run main.go # If the egress-ip-ranges.etag matches the current feed, you'll see: # "No changes in the egress IP ranges" # Force regeneration by removing the etag file rm egress-ip-ranges.etag go run main.go # This will download the CSV and generate a fresh egress-ip-ranges.mmdb ``` -------------------------------- ### Download MMDB Database using cURL Source: https://context7.com/nextdns/icloud-private-relay-mmdb/llms.txt This snippet demonstrates how to download the pre-built `egress-ip-ranges.mmdb` file directly from the GitHub repository using `curl`. It also shows how to verify the downloaded file using the `file` command. ```bash # Download the latest MMDB file directly from GitHub curl -L -o egress-ip-ranges.mmdb \ "https://github.com/nextdns/icloud-private-relay-mmdb/raw/main/egress-ip-ranges.mmdb" # Verify the file was downloaded correctly file egress-ip-ranges.mmdb # Output: egress-ip-ranges.mmdb: data ``` -------------------------------- ### Query MMDB Database with Python Source: https://context7.com/nextdns/icloud-private-relay-mmdb/llms.txt This Python code snippet uses the `maxminddb` library to query the `egress-ip-ranges.mmdb` file. It shows how to open the database, perform single IP lookups, and also a batch lookup for multiple IP addresses, indicating whether each IP is part of the iCloud Private Relay network. ```python import maxminddb # Open the iCloud Private Relay MMDB database with maxminddb.open_database('egress-ip-ranges.mmdb') as reader: # Look up an IP address ip_address = '172.224.224.1' result = reader.get(ip_address) if result: # IP found - this is an iCloud Private Relay egress IP print(f"IP {ip_address} is an iCloud Private Relay egress IP") print(f"Country: {result['country']['iso_code']}") else: # IP not found - not an iCloud Private Relay IP print(f"IP {ip_address} is not an iCloud Private Relay egress IP") # Example batch lookup for multiple IPs ips_to_check = ['172.224.224.1', '8.8.8.8', '1.1.1.1'] with maxminddb.open_database('egress-ip-ranges.mmdb') as reader: for ip in ips_to_check: result = reader.get(ip) status = "iCloud Private Relay" if result else "Regular IP" print(f"{ip}: {status}") ``` -------------------------------- ### Query MMDB Database with Go Source: https://context7.com/nextdns/icloud-private-relay-mmdb/llms.txt This Go code snippet utilizes the `geoip2-golang` library to query the `egress-ip-ranges.mmdb` file. It demonstrates how to open the database, look up an IP address, and determine if it belongs to iCloud Private Relay's egress network, returning the country ISO code if found. ```go package main import ( "fmt" "log" "net" "github.com/oschwald/geoip2-golang" ) func main() { // Open the iCloud Private Relay MMDB database db, err := geoip2.Open("egress-ip-ranges.mmdb") if err != nil { log.Fatal(err) } defer db.Close() // Look up an IP address ip := net.ParseIP("172.224.224.1") record, err := db.Country(ip) if err != nil { // IP not found in database - not an iCloud Private Relay IP fmt.Printf("IP %s is not an iCloud Private Relay egress IP\n", ip) return } // IP found - this is an iCloud Private Relay egress IP fmt.Printf("IP %s is an iCloud Private Relay egress IP\n", ip) fmt.Printf("Country: %s\n", record.Country.IsoCode) } ``` -------------------------------- ### View Apple's Source CSV Feed Source: https://context7.com/nextdns/icloud-private-relay-mmdb/llms.txt This bash snippet shows how to fetch and view the raw CSV data from Apple's official iCloud Private Relay egress IP ranges endpoint using `curl`. It also demonstrates how to check the `ETag` header to detect updates to the feed. ```bash # View the raw source data from Apple curl -s "https://mask-api.icloud.com/egress-ip-ranges.csv" | head -10 # Example output format (CIDR,CountryCode): # 172.224.224.0/19,US # 172.225.0.0/17,US # 172.225.128.0/18,US # 2607:fb90::/32,US # ... # Check the ETag header to detect updates curl -I "https://mask-api.icloud.com/egress-ip-ranges.csv" 2>/dev/null | grep -i etag ``` -------------------------------- ### Configure nginx GeoIP2 for iCloud Private Relay Detection Source: https://context7.com/nextdns/icloud-private-relay-mmdb/llms.txt This configuration demonstrates how to load the iCloud Private Relay MMDB into the nginx GeoIP2 module. It sets a variable based on the lookup and adds a custom HTTP header to requests identified as originating from the relay service. ```nginx http { geoip2 /path/to/egress-ip-ranges.mmdb { $is_icloud_relay country iso_code; } server { listen 80; log_format relay '$remote_addr - $is_icloud_relay - $request'; access_log /var/log/nginx/access.log relay; location / { if ($is_icloud_relay) { add_header X-Private-Relay "true"; } proxy_pass http://backend; } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.