-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo_router.py
More file actions
551 lines (457 loc) · 18.7 KB
/
geo_router.py
File metadata and controls
551 lines (457 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
"""
Geographic Routing System for Load Balancer
PURPOSE:
This module provides intelligent geographic routing for load balancers across multiple
datacenters. It resolves client IP addresses to geographic locations and routes requests
to the nearest datacenter based on distance calculations. The system supports:
- Lower latency through proximity-based routing
- Data residency compliance (GDPR, CCPA, etc.)
- Disaster recovery and failover capabilities
- Reduced bandwidth costs through optimized routing
- Multi-provider GeoIP resolution with automatic fallback
- Caching for improved performance
- Thread-safe operations for concurrent requests
ARCHITECTURE:
- Datacenter: Represents physical datacenter locations with coordinates and backends
- GeoIPResolver: Handles IP geolocation using multiple providers with fallback
- GeoRouter: Main routing engine that matches clients to optimal datacenters
SECURITY CONSIDERATIONS:
- Input validation for IP addresses to prevent injection attacks
- HTTPS enforcement for external API calls
- Rate limiting support for external API providers
- Secure error handling without exposing sensitive information
- Cache poisoning prevention through validation
"""
import requests
import time
import re
import logging
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass
import math
from functools import lru_cache
from threading import Lock
from urllib.parse import quote
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class Datacenter:
name: str
region: str
country: str
latitude: float
longitude: float
backends: List[str]
health_status: str = "healthy"
def __post_init__(self):
if not -90 <= self.latitude <= 90:
raise ValueError(f"Invalid latitude: {self.latitude}")
if not -180 <= self.longitude <= 180:
raise ValueError(f"Invalid longitude: {self.longitude}")
if not self.backends:
raise ValueError("Datacenter must have at least one backend")
for backend in self.backends:
if not backend.startswith(('http://', 'https://')):
raise ValueError(f"Invalid backend URL: {backend}")
def distance_to(self, lat: float, lon: float) -> float:
R = 6371
lat1, lon1 = math.radians(self.latitude), math.radians(self.longitude)
lat2, lon2 = math.radians(lat), math.radians(lon)
dlat = lat2 - lat1
dlon = lon2 - lon1
a = (math.sin(dlat/2)**2 +
math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2)
c = 2 * math.asin(math.sqrt(a))
return R * c
class GeoIPResolver:
IP_PATTERN = re.compile(
r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}'
r'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
)
IPV6_PATTERN = re.compile(
r'^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|'
r'([0-9a-fA-F]{1,4}:){1,7}:|'
r'([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|'
r'([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|'
r'([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|'
r'([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|'
r'([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|'
r'[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|'
r':((:[0-9a-fA-F]{1,4}){1,7}|:)|'
r'fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|'
r'::(ffff(:0{1,4}){0,1}:){0,1}'
r'((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}'
r'(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|'
r'([0-9a-fA-F]{1,4}:){1,4}:'
r'((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}'
r'(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$'
)
def __init__(self, cache_ttl=3600, max_cache_size=10000):
self.cache = {}
self.cache_ttl = cache_ttl
self.max_cache_size = max_cache_size
self.cache_lock = Lock()
self.request_timeout = 3
self.max_retries = 2
self.providers = [
self._resolve_ipapi,
self._resolve_ipinfo,
]
def _is_valid_ip(self, ip: str) -> bool:
if not ip or not isinstance(ip, str):
return False
return bool(self.IP_PATTERN.match(ip) or self.IPV6_PATTERN.match(ip))
def _is_private_ip(self, ip: str) -> bool:
if ip in ('127.0.0.1', 'localhost', '::1', '0.0.0.0'):
return True
private_ranges = [
'10.', '192.168.', '172.16.', '172.17.', '172.18.', '172.19.',
'172.20.', '172.21.', '172.22.', '172.23.', '172.24.', '172.25.',
'172.26.', '172.27.', '172.28.', '172.29.', '172.30.', '172.31.',
'169.254.', 'fc00:', 'fd00:', 'fe80:'
]
return any(ip.startswith(prefix) for prefix in private_ranges)
def resolve(self, ip: str) -> Optional[Dict]:
if not self._is_valid_ip(ip):
logger.warning(f"Invalid IP address format: {ip}")
return self._get_default_location()
if self._is_private_ip(ip):
return self._get_default_location()
cache_key = f"geo:{ip}"
with self.cache_lock:
if cache_key in self.cache:
cached_data, timestamp = self.cache[cache_key]
if time.time() - timestamp < self.cache_ttl:
return cached_data
for provider in self.providers:
for attempt in range(self.max_retries):
try:
result = provider(ip)
if result and self._validate_geo_data(result):
with self.cache_lock:
if len(self.cache) >= self.max_cache_size:
oldest_key = min(self.cache.keys(),
key=lambda k: self.cache[k][1])
del self.cache[oldest_key]
self.cache[cache_key] = (result, time.time())
return result
except requests.exceptions.Timeout:
logger.warning(f"Timeout for provider {provider.__name__} "
f"(attempt {attempt + 1}/{self.max_retries})")
continue
except requests.exceptions.RequestException as e:
logger.error(f"Request error for provider {provider.__name__}: {e}")
break
except Exception as e:
logger.error(f"Unexpected error in provider {provider.__name__}: {e}")
break
return self._get_default_location()
def _validate_geo_data(self, data: Dict) -> bool:
required_fields = ['ip', 'country', 'latitude', 'longitude', 'region']
if not all(field in data for field in required_fields):
return False
try:
lat = float(data['latitude'])
lon = float(data['longitude'])
if not (-90 <= lat <= 90 and -180 <= lon <= 180):
return False
except (ValueError, TypeError):
return False
return True
def _resolve_ipapi(self, ip: str) -> Optional[Dict]:
url = f"http://ip-api.com/json/{quote(ip)}"
try:
response = requests.get(
url,
timeout=self.request_timeout,
headers={'User-Agent': 'GeoRouter/1.0'}
)
if response.status_code != 200:
logger.warning(f"ip-api returned status {response.status_code}")
return None
data = response.json()
if data.get('status') != 'success':
logger.warning(f"ip-api failed: {data.get('message', 'Unknown error')}")
return None
country = data.get('countryCode')
lat = data.get('lat')
lon = data.get('lon')
if not all([country, lat is not None, lon is not None]):
return None
return {
'ip': ip,
'country': country,
'city': data.get('city', 'Unknown'),
'latitude': float(lat),
'longitude': float(lon),
'region': self._determine_region(country, float(lat), float(lon))
}
except requests.exceptions.RequestException as e:
logger.error(f"Request error in _resolve_ipapi: {e}")
raise
except (ValueError, KeyError) as e:
logger.error(f"Data parsing error in _resolve_ipapi: {e}")
return None
def _resolve_ipinfo(self, ip: str) -> Optional[Dict]:
url = f"https://ipinfo.io/{quote(ip)}/json"
try:
response = requests.get(
url,
timeout=self.request_timeout,
headers={'User-Agent': 'GeoRouter/1.0'}
)
if response.status_code != 200:
logger.warning(f"ipinfo.io returned status {response.status_code}")
return None
data = response.json()
if 'bogon' in data:
logger.warning(f"ipinfo.io identified {ip} as bogon IP")
return None
loc = data.get('loc', '0,0')
if ',' not in loc:
return None
loc_parts = loc.split(',')
if len(loc_parts) != 2:
return None
lat, lon = float(loc_parts[0]), float(loc_parts[1])
country = data.get('country')
if not country:
return None
return {
'ip': ip,
'country': country,
'city': data.get('city', 'Unknown'),
'latitude': lat,
'longitude': lon,
'region': self._determine_region(country, lat, lon)
}
except requests.exceptions.RequestException as e:
logger.error(f"Request error in _resolve_ipinfo: {e}")
raise
except (ValueError, KeyError, IndexError) as e:
logger.error(f"Data parsing error in _resolve_ipinfo: {e}")
return None
def _determine_region(self, country: str, lat: float, lon: float) -> str:
if not country:
return 'us-east'
region_map = {
'US': self._determine_us_region(lon),
'CA': 'us-east',
'MX': 'us-central',
'GB': 'eu-west',
'FR': 'eu-west',
'DE': 'eu-central',
'IT': 'eu-central',
'ES': 'eu-west',
'NL': 'eu-west',
'SE': 'eu-north',
'NO': 'eu-north',
'FI': 'eu-north',
'PL': 'eu-central',
'IN': 'ap-south',
'CN': 'ap-northeast',
'JP': 'ap-northeast',
'KR': 'ap-northeast',
'SG': 'ap-southeast',
'AU': 'ap-southeast',
'NZ': 'ap-southeast',
'ID': 'ap-southeast',
'TH': 'ap-southeast',
'MY': 'ap-southeast',
'BR': 'sa-east',
'AR': 'sa-east',
'CL': 'sa-east',
'ZA': 'af-south',
'NG': 'af-south',
'EG': 'af-north',
'AE': 'me-central',
'SA': 'me-central',
'IL': 'me-central',
}
return region_map.get(country, 'us-east')
def _determine_us_region(self, lon: float) -> str:
if lon > -95:
return 'us-east'
elif lon < -115:
return 'us-west'
else:
return 'us-central'
def _get_default_location(self) -> Dict:
return {
'ip': 'unknown',
'country': 'US',
'city': 'Unknown',
'latitude': 40.7128,
'longitude': -74.0060,
'region': 'us-east'
}
class GeoRouter:
def __init__(self):
self.datacenters: List[Datacenter] = []
self.geoip = GeoIPResolver()
self.fallback_datacenter = None
self.router_lock = Lock()
self.stats = {
'total_routes': 0,
'routes_by_region': {},
'avg_distance': 0.0,
'fallback_count': 0,
'errors': 0
}
def add_datacenter(self,
name: str,
region: str,
country: str,
latitude: float,
longitude: float,
backends: List[str]) -> bool:
try:
dc = Datacenter(
name=name,
region=region,
country=country,
latitude=latitude,
longitude=longitude,
backends=backends
)
with self.router_lock:
self.datacenters.append(dc)
if not self.fallback_datacenter:
self.fallback_datacenter = dc
logger.info(f"Added datacenter: {name} ({region}) - {len(backends)} backends")
return True
except (ValueError, TypeError) as e:
logger.error(f"Failed to add datacenter {name}: {e}")
return False
def route_request(self, client_ip: str) -> Tuple[Optional[Datacenter], float]:
try:
location = self.geoip.resolve(client_ip)
if not location:
logger.warning(f"Could not resolve location for IP: {client_ip}")
with self.router_lock:
self.stats['fallback_count'] += 1
return self.fallback_datacenter, 0.0
if not self.datacenters:
logger.error("No datacenters available for routing")
with self.router_lock:
self.stats['errors'] += 1
return self.fallback_datacenter, 0.0
lat, lon = location['latitude'], location['longitude']
nearest = None
min_distance = float('inf')
for dc in self.datacenters:
if dc.health_status != "healthy":
continue
distance = dc.distance_to(lat, lon)
if distance < min_distance:
min_distance = distance
nearest = dc
if not nearest:
logger.warning("No healthy datacenters available")
nearest = self.fallback_datacenter
min_distance = 0.0
with self.router_lock:
self.stats['total_routes'] += 1
region = nearest.region if nearest else 'unknown'
self.stats['routes_by_region'][region] = \
self.stats['routes_by_region'].get(region, 0) + 1
total = self.stats['total_routes']
self.stats['avg_distance'] = (
(self.stats['avg_distance'] * (total - 1) + min_distance) / total
)
return nearest, min_distance
except Exception as e:
logger.error(f"Error routing request for IP {client_ip}: {e}")
with self.router_lock:
self.stats['errors'] += 1
return self.fallback_datacenter, 0.0
def get_stats(self) -> Dict:
with self.router_lock:
return {
'total_routes': self.stats['total_routes'],
'routes_by_region': dict(self.stats['routes_by_region']),
'avg_distance_km': round(self.stats['avg_distance'], 2),
'fallback_count': self.stats['fallback_count'],
'errors': self.stats['errors'],
'datacenters': [
{
'name': dc.name,
'region': dc.region,
'country': dc.country,
'backends': len(dc.backends),
'health_status': dc.health_status
}
for dc in self.datacenters
]
}
def set_datacenter_health(self, datacenter_name: str, status: str) -> bool:
if status not in ('healthy', 'unhealthy', 'maintenance'):
logger.error(f"Invalid health status: {status}")
return False
with self.router_lock:
for dc in self.datacenters:
if dc.name == datacenter_name:
dc.health_status = status
logger.info(f"Set {datacenter_name} health to {status}")
return True
logger.warning(f"Datacenter not found: {datacenter_name}")
return False
def setup_demo_geo_routing():
router = GeoRouter()
router.add_datacenter(
name="US-East-Demo",
region="us-east",
country="US",
latitude=37.4316,
longitude=-78.6569,
backends=["http://localhost:5001"]
)
router.add_datacenter(
name="EU-West-Demo",
region="eu-west",
country="IE",
latitude=53.3498,
longitude=-6.2603,
backends=["http://localhost:5002"]
)
router.add_datacenter(
name="AP-South-Demo",
region="ap-south",
country="IN",
latitude=19.0760,
longitude=72.8777,
backends=["http://localhost:5003"]
)
return router
if __name__ == '__main__':
router = setup_demo_geo_routing()
test_ips = {
'US': '8.8.8.8',
'EU': '193.0.6.139',
'Asia': '1.1.1.1',
'India': '103.21.244.0',
}
print("\n" + "="*60)
print("🌍 Geographic Routing Test")
print("="*60)
for region, ip in test_ips.items():
dc, distance = router.route_request(ip)
if dc:
print(f"\n{region} IP ({ip}):")
print(f" → Routed to: {dc.name}")
print(f" → Distance: {distance:.0f} km")
print(f" → Backends: {dc.backends}")
else:
print(f"\n{region} IP ({ip}): Routing failed")
print("\n" + "="*60)
print("📊 Routing Statistics")
print("="*60)
stats = router.get_stats()
print(f"Total routes: {stats['total_routes']}")
print(f"Avg distance: {stats['avg_distance_km']} km")
print(f"Fallback count: {stats['fallback_count']}")
print(f"Errors: {stats['errors']}")
print(f"\nRoutes by region:")
for region, count in stats['routes_by_region'].items():
print(f" {region}: {count}")