diff --git a/.github/workflows/update-relay-data.yml b/.github/workflows/update-relay-data.yml index 99bf7d4..d6c29e1 100644 --- a/.github/workflows/update-relay-data.yml +++ b/.github/workflows/update-relay-data.yml @@ -47,6 +47,8 @@ jobs: - name: Update relay discovery results run: | python3 nostr_relay_discovery.py wss://relay.damus.io --output relay_discovery_results.json --batch-size 20 + env: + BLOCKED_RELAYS: wss://nostr.2b9t.xyz,wss://nostr-relay.zimage.com - name: Update relay geolocation data run: | diff --git a/nostr_relay_discovery.py b/nostr_relay_discovery.py index 0fd6499..1d73300 100644 --- a/nostr_relay_discovery.py +++ b/nostr_relay_discovery.py @@ -14,6 +14,7 @@ import argparse import secrets import base64 +import os from collections import deque from dataclasses import dataclass, field from typing import Set, List, Dict, Optional, Tuple @@ -203,11 +204,39 @@ async def test_relays_connections(self, relay_urls: List[str]) -> Dict[str, bool if not relay_urls: return {} - logger.info(f"Testing {len(relay_urls)} relays concurrently") + # Filter out blocked relays + blocked_relays_env = os.getenv('BLOCKED_RELAYS', '') + blocked_relays = set() + if blocked_relays_env: + raw_relays = [relay.strip() for relay in blocked_relays_env.split(',')] + for relay in raw_relays: + if relay and self.is_valid_relay_url(relay): + normalized_relay = self.normalize_relay_url(relay) + if normalized_relay: + blocked_relays.add(normalized_relay) + + filtered_relays = [] + blocked_count = 0 + for relay_url in relay_urls: + normalized_url = self.normalize_relay_url(relay_url) + if normalized_url in blocked_relays: + logger.info(f"Skipping blocked relay: {relay_url}") + blocked_count += 1 + else: + filtered_relays.append(relay_url) + + if blocked_count > 0: + logger.info(f"Filtered out {blocked_count} blocked relays") + + if not filtered_relays: + # Return blocked relays as non-functioning + return {relay_url: False for relay_url in relay_urls} + + logger.info(f"Testing {len(filtered_relays)} relays concurrently") # Create tasks for concurrent testing tasks = [] - for relay_url in relay_urls: + for relay_url in filtered_relays: task = asyncio.create_task(self.test_relay_connection(relay_url)) tasks.append((relay_url, task)) @@ -225,6 +254,12 @@ async def test_relays_connections(self, relay_urls: List[str]) -> Dict[str, bool logger.error(f"Error testing relay {relay_url}: {e}") results[relay_url] = False + # Add blocked relays as non-functioning to the results + for relay_url in relay_urls: + normalized_url = self.normalize_relay_url(relay_url) + if normalized_url in blocked_relays: + results[relay_url] = False + functioning_count = sum(1 for status in results.values() if status) logger.info(f"Batch test completed: {functioning_count}/{len(relay_urls)} relays functioning") @@ -324,6 +359,17 @@ def extract_relays_from_events(self, events: List[Dict]) -> Set[str]: """Extract relay URLs from follow list events""" relay_urls = set() + # Get blocked relays + blocked_relays_env = os.getenv('BLOCKED_RELAYS', '') + blocked_relays = set() + if blocked_relays_env: + raw_relays = [relay.strip() for relay in blocked_relays_env.split(',')] + for relay in raw_relays: + if relay and self.is_valid_relay_url(relay): + normalized_relay = self.normalize_relay_url(relay) + if normalized_relay: + blocked_relays.add(normalized_relay) + for event in events: self.stats.events_processed += 1 @@ -339,14 +385,20 @@ def extract_relays_from_events(self, events: List[Dict]) -> Set[str]: potential_relay = tag[1] if self.is_valid_relay_url(potential_relay): normalized_url = self.normalize_relay_url(potential_relay) - relay_urls.add(normalized_url) + if normalized_url not in blocked_relays: + relay_urls.add(normalized_url) + else: + logger.debug(f"Skipping blocked relay from events: {normalized_url}") # Also check 'p' tags for potential relay info in some implementations elif tag[0] == 'p' and len(tag) >= 3: # Some implementations put relay info in the 3rd element of p tags if len(tag) > 2 and self.is_valid_relay_url(tag[2]): normalized_url = self.normalize_relay_url(tag[2]) - relay_urls.add(normalized_url) + if normalized_url not in blocked_relays: + relay_urls.add(normalized_url) + else: + logger.debug(f"Skipping blocked relay from events: {normalized_url}") return relay_urls @@ -482,7 +534,8 @@ def save_results(self, output_file: str = None): "max_depth": self.max_depth, "connection_timeout": self.connection_timeout, "save_point": self.save_point, - "batch_size": self.batch_size + "batch_size": self.batch_size, + "blocked_relays_env": os.getenv('BLOCKED_RELAYS', '') }, "progress_info": { "relays_processed": len(self.visited_relays),