From fcf9d6e8268239c8047959c21f564967868e1c1a Mon Sep 17 00:00:00 2001 From: Andrevich <47223721+1andrevich@users.noreply.github.com> Date: Sun, 8 Dec 2024 01:21:45 +0400 Subject: [PATCH] Update ipsum_bgp.py Replaced BGPView for ip.guide --- src/step 5 ooni list/ipsum_bgp.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/step 5 ooni list/ipsum_bgp.py b/src/step 5 ooni list/ipsum_bgp.py index 5112e8b..560fe7e 100644 --- a/src/step 5 ooni list/ipsum_bgp.py +++ b/src/step 5 ooni list/ipsum_bgp.py @@ -5,6 +5,8 @@ import requests import ipaddress import time import os +import subprocess +import json from collections import defaultdict from idna import encode as idna_encode @@ -100,25 +102,17 @@ def handle_rate_limit(): logging.warning(f'Rate limit hit. Waiting for {wait_time} seconds.') time.sleep(wait_time) -# Function to get CIDRs for a domain from ASN using GeoLite2 +# Function to get CIDRs for a domain from ASN using ip.guide def get_cidr_for_asn(asn): try: - url = f'https://api.bgpview.io/asn/{asn}/prefixes' - response = requests.get(url) - - if response.status_code == 200: - data = response.json() - return [prefix['prefix'] for prefix in data['data']['ipv4_prefixes']] - - elif response.status_code == 429: - handle_rate_limit() - return get_cidr_for_asn(asn) # Retry after waiting - - elif response.status_code == 403: - logging.error(f'Access forbidden for ASN {asn}, skipping.') + command = f'curl -sL https://ip.guide/as{asn}' + result = subprocess.run(command, shell=True, capture_output=True, text=True) + if result.returncode == 0: + data = json.loads(result.stdout) + return data.get('routes', {}).get('v4', []) + else: + logging.error(f'Error executing curl command: {result.stderr}') return [] - - return [] except Exception as e: logging.error(f'Error retrieving CIDRs for ASN {asn}: {e}') return []