Skip to content

nasnet-community/prefix-fetcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prefix Fetcher

Prefix Fetcher is a Python-based tool for fetching country-specific IP prefixes from BGP data. Dynamically fetches ASN lists from Regional Internet Registry (RIR) delegated files and filters BGP prefixes accordingly.

Features

  • Dynamic ASN Fetching: Automatically downloads the latest ASN allocations from RIR delegated files
    • Iran (IR): Uses RIPE NCC delegated data
    • China (CN): Uses APNIC delegated data
    • Russia (RU): Uses RIPE NCC delegated data
  • Comprehensive Coverage: Fetches from all RIRs for complete ASN discovery
  • BGP Data: Fetches IP prefixes from bgp.tools
  • Multiple Output Formats:
    • Regular form: Original BGP prefixes as-is
    • Collapsed form: Shortest aggregated prefixes (automatically generated)
    • Split form: IPv4 as /24 blocks, IPv6 as /48 blocks (optional with --split flag)
  • Clean & Simple: No caching, fresh data on every run
  • Automatic retry logic with exponential backoff
  • Duplicate removal: Automatically deduplicates ASNs found across multiple RIRs

Prerequisites

  • Python (>=3.14)
  • uv (recommended) or pip for package management

Installation

  1. Clone the repository:

    git clone https://github.com/compassvpn/prefix-fetcher.git
    cd prefix-fetcher
  2. Install dependencies:

    Using uv (recommended):

    uv sync

    Using pip:

    pip install -e .

Usage

Using uv:

uv run python main.py [OPTIONS]

Or after installation:

python main.py [OPTIONS]

Available Options

Option Description
--fetch-ir Fetch Iranian IP prefixes
--fetch-cn Fetch Chinese IP prefixes
--fetch-ru Fetch Russian IP prefixes
--split Also generate split form files (IPv4 as /24, IPv6 as /48)
-h, --help Show help information

Examples

  • Fetch Iranian IP prefixes (regular and collapsed forms):

    uv run python main.py --fetch-ir
  • Fetch Chinese IP prefixes with split form:

    uv run python main.py --fetch-cn --split
  • Fetch Russian IP prefixes:

    uv run python main.py --fetch-ru
  • Fetch multiple countries with split form:

    uv run python main.py --fetch-ir --fetch-cn --fetch-ru --split
  • Show help:

    uv run python main.py --help

Output Files

The tool generates country-specific prefix files in multiple formats:

Always Generated

Regular form (original BGP prefixes):

  • ir_prefixes_v4.txt / ir_prefixes_v6.txt
  • cn_prefixes_v4.txt / cn_prefixes_v6.txt
  • ru_prefixes_v4.txt / ru_prefixes_v6.txt

Collapsed form (shortest aggregated prefixes):

  • ir_prefixes_v4_collapsed.txt / ir_prefixes_v6_collapsed.txt
  • cn_prefixes_v4_collapsed.txt / cn_prefixes_v6_collapsed.txt
  • ru_prefixes_v4_collapsed.txt / ru_prefixes_v6_collapsed.txt

Optional (with --split flag)

Split form (IPv4 as /24 blocks, IPv6 as /48 blocks):

  • ir_prefixes_v4_split.txt / ir_prefixes_v6_split.txt
  • cn_prefixes_v4_split.txt / cn_prefixes_v6_split.txt
  • ru_prefixes_v4_split.txt / ru_prefixes_v6_split.txt

Format Descriptions

  • Regular: Original BGP prefixes as announced (varying prefix lengths)
  • Collapsed: Optimized/aggregated prefixes merged into shortest possible form
  • Split: Normalized to fixed-size blocks (/24 for IPv4, /48 for IPv6)

How It Works

  1. Fetches ASN Lists: Downloads the latest ASN allocations from all RIRs for comprehensive coverage:
    • Iran: Primary coverage from RIPE NCC, with additional checks across all RIRs
    • China: Primary coverage from APNIC, with additional checks across all RIRs
    • Russia: Primary coverage from RIPE NCC, with additional checks across all RIRs
  2. Downloads BGP Data: Fetches the complete BGP routing table from bgp.tools
  3. Filters by ASN: Keeps only prefixes from the dynamically fetched ASN lists
  4. Generates Multiple Formats:
    • Regular: Saves original BGP prefixes
    • Collapsed: Aggregates overlapping/adjacent prefixes into shortest form using Python's collapse_addresses()
    • Split (optional): Normalizes IPv4 to /24 blocks and IPv6 to /48 blocks using subnets() method
  5. Sorts and Saves: Outputs clean, sorted prefix lists to text files

Data Sources

License

This project is licensed under the MIT License.

Contributions

Contributions are welcome! Feel free to fork the repository and submit a pull request.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages